/
jsx.ts
128 lines (107 loc) 路 2.86 KB
/
jsx.ts
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
import type Printer from "../printer";
import type * as t from "@babel/types";
export function JSXAttribute(this: Printer, node: t.JSXAttribute) {
this.print(node.name, node);
if (node.value) {
this.token("=");
this.print(node.value, node);
}
}
export function JSXIdentifier(this: Printer, node: t.JSXIdentifier) {
this.word(node.name);
}
export function JSXNamespacedName(this: Printer, node: t.JSXNamespacedName) {
this.print(node.namespace, node);
this.token(":");
this.print(node.name, node);
}
export function JSXMemberExpression(
this: Printer,
node: t.JSXMemberExpression,
) {
this.print(node.object, node);
this.token(".");
this.print(node.property, node);
}
export function JSXSpreadAttribute(this: Printer, node: t.JSXSpreadAttribute) {
this.token("{");
this.token("...");
this.print(node.argument, node);
this.token("}");
}
export function JSXExpressionContainer(
this: Printer,
node: t.JSXExpressionContainer,
) {
this.token("{");
this.print(node.expression, node);
this.token("}");
}
export function JSXSpreadChild(this: Printer, node: t.JSXSpreadChild) {
this.token("{");
this.token("...");
this.print(node.expression, node);
this.token("}");
}
export function JSXText(this: Printer, node: t.JSXText) {
const raw = this.getPossibleRaw(node);
if (raw !== undefined) {
this.token(raw, true);
} else {
this.token(node.value, true);
}
}
export function JSXElement(this: Printer, node: t.JSXElement) {
const open = node.openingElement;
this.print(open, node);
if (open.selfClosing) return;
this.indent();
for (const child of node.children) {
this.print(child, node);
}
this.dedent();
this.print(node.closingElement, node);
}
function spaceSeparator(this: Printer) {
this.space();
}
export function JSXOpeningElement(this: Printer, node: t.JSXOpeningElement) {
this.token("<");
this.print(node.name, node);
this.print(node.typeParameters, node); // TS
if (node.attributes.length > 0) {
this.space();
this.printJoin(node.attributes, node, { separator: spaceSeparator });
}
if (node.selfClosing) {
this.space();
this.token("/>");
} else {
this.token(">");
}
}
export function JSXClosingElement(this: Printer, node: t.JSXClosingElement) {
this.token("</");
this.print(node.name, node);
this.token(">");
}
export function JSXEmptyExpression(this: Printer, node: t.JSXEmptyExpression) {
this.printInnerComments(node);
}
export function JSXFragment(this: Printer, node: t.JSXFragment) {
this.print(node.openingFragment, node);
this.indent();
for (const child of node.children) {
this.print(child, node);
}
this.dedent();
this.print(node.closingFragment, node);
}
export function JSXOpeningFragment(this: Printer) {
this.token("<");
this.token(">");
}
export function JSXClosingFragment(this: Printer) {
this.token("</");
this.token(">");
}