Repository navigation
Expand file tree
/
Copy pathcomponent.js
More file actions
142 lines (121 loc) · 3.33 KB
/
Copy pathcomponent.js
File metadata and controls
142 lines (121 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import {
group,
hardline,
ifBreak,
indent,
line,
softline,
} from "../../document/builders.js";
import { printDanglingComments } from "../../main/comments/print.js";
import getNextNonSpaceNonCommentCharacter from "../../utils/get-next-non-space-non-comment-character.js";
import { locEnd } from "../loc.js";
import { isNextLineEmpty, shouldPrintComma } from "../utils/index.js";
import { printDeclareToken } from "./misc.js";
/**
* @typedef {import("../../common/ast-path.js").default} AstPath
* @typedef {import("../../document/builders.js").Doc} Doc
*/
/*
- "ComponentDeclaration"
- "DeclareComponent"
- "ComponentTypeAnnotation"
*/
function printComponent(path, options, print) {
const { node } = path;
const parts = [printDeclareToken(path), "component"];
if (node.id) {
parts.push(" ", print("id"));
}
parts.push(print("typeParameters"));
const parametersDoc = printComponentParameters(path, print, options);
if (node.rendersType) {
parts.push(group([parametersDoc, " ", print("rendersType")]));
} else {
parts.push(group([parametersDoc]));
}
if (node.body) {
parts.push(" ", print("body"));
}
if (options.semi && node.type === "DeclareComponent") {
parts.push(";");
}
return parts;
}
function printComponentParameters(path, print, options) {
const { node: componentNode } = path;
let parameters = componentNode.params;
if (componentNode.rest) {
parameters = [...parameters, componentNode.rest];
}
if (parameters.length === 0) {
return [
"(",
printDanglingComments(path, options, {
filter: (comment) =>
getNextNonSpaceNonCommentCharacter(
options.originalText,
locEnd(comment),
) === ")",
}),
")",
];
}
const printed = [];
iterateComponentParametersPath(path, (parameterPath, index) => {
const isLastParameter = index === parameters.length - 1;
if (isLastParameter && componentNode.rest) {
printed.push("...");
}
printed.push(print());
if (isLastParameter) {
return;
}
printed.push(",");
if (isNextLineEmpty(parameters[index], options)) {
printed.push(hardline, hardline);
} else {
printed.push(line);
}
});
return [
"(",
indent([softline, ...printed]),
ifBreak(
shouldPrintComma(options, "all") &&
!hasRestParameter(componentNode, parameters)
? ","
: "",
),
softline,
")",
];
}
function hasRestParameter(componentNode, parameters) {
return componentNode.rest || parameters.at(-1)?.type === "RestElement";
}
function iterateComponentParametersPath(path, iteratee) {
const { node } = path;
let index = 0;
const callback = (childPath) => iteratee(childPath, index++);
path.each(callback, "params");
if (node.rest) {
path.call(callback, "rest");
}
}
function printComponentParameter(path, options, print) {
const { node } = path;
if (node.shorthand) {
return print("local");
}
return [print("name"), " as ", print("local")];
}
function printComponentTypeParameter(path, options, print) {
const { node } = path;
const printed = [];
if (node.name) {
printed.push(print("name"), node.optional ? "?: " : ": ");
}
printed.push(print("typeAnnotation"));
return printed;
}
export { printComponent, printComponentParameter, printComponentTypeParameter };