-
Notifications
You must be signed in to change notification settings - Fork 31
/
babel-plugin.js
247 lines (211 loc) · 6.95 KB
/
babel-plugin.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const dedent = require("dedent-js");
const MODULE_NAME = "djedi-react";
const CLIENT_NAME = "djedi";
const CLIENT_METHOD_NAME = "reportPrefetchableNode";
const COMPONENT_NAME = "Node";
const URI_ATTR_NAME = "uri";
const TEMPLATE_TAG_NAME = "md";
const NODE_URI_KEY = "uri";
const NODE_VALUE_KEY = "value";
const URI_VAR_NAME = "djedi_uri";
const DEFAULT_VAR_NAME = "djedi_default";
module.exports = function djediBabelPlugin({ types: t }) {
return {
name: MODULE_NAME,
pre() {
this.clientId = undefined;
this.last = undefined;
},
post() {
this.clientId = undefined;
this.last = undefined;
},
visitor: {
JSXElement(path) {
// Bail if the element isn't a node.
if (path.node.openingElement.name.name !== COMPONENT_NAME) {
return;
}
const uriAttr = getUriAttr(path, t);
// Bail if the uri prop is missing.
if (uriAttr == null) {
return;
}
const uri = getUri(uriAttr.node.value, t);
// Bail if the uri prop isn't a static string.
if (uri == null) {
return;
}
const child = maybeGetLoneChild(path, t);
const defaultValue =
child == null ? undefined : getDefaultValue(child, t);
// Bail if there is a child but it isn't a static string.
if (child != null && defaultValue == null) {
return;
}
const program = path.scope.getProgramParent();
// Add import for djedi (the first time).
if (this.clientId == null) {
this.clientId = program.generateUidIdentifier(CLIENT_NAME);
[this.last] = program.path.unshiftContainer(
"body",
makeClientImport(this.clientId, t)
);
}
// Move the uri into a variable.
const uriId = path.scope.generateUidIdentifier(URI_VAR_NAME);
program.push({ id: t.cloneNode(uriId), init: t.stringLiteral(uri) });
uriAttr
.get("value")
.replaceWith(t.jSXExpressionContainer(t.cloneNode(uriId)));
let defaultId = undefined;
// Move the default value into a variable (if any).
if (defaultValue != null) {
defaultId = path.scope.generateUidIdentifier(DEFAULT_VAR_NAME);
program.push({
id: t.cloneNode(defaultId),
init: t.stringLiteral(dedent(defaultValue)),
});
child.replaceWith(t.jSXExpressionContainer(t.cloneNode(defaultId)));
for (const otherChild of path.get("children")) {
if (otherChild !== child) {
otherChild.remove();
}
}
}
// Insert a `reportPrefetchableNode` call.
[this.last] = this.last.insertAfter(
makeReportCall(this.clientId, uriId, defaultId, t)
);
},
},
};
};
function getUriAttr(jsxElementPath, t) {
const attrs = jsxElementPath.get("openingElement.attributes");
let result = undefined;
for (const attr of attrs) {
if (t.isJSXIdentifier(attr.node.name, { name: URI_ATTR_NAME })) {
if (result != null) {
throw attr.buildCodeFrameError(
`<${COMPONENT_NAME}> must only specify the \`${URI_ATTR_NAME}\` prop once.`
);
}
result = attr;
} else if (t.isJSXIdentifier(attr.node.name, { name: "children" })) {
throw attr.buildCodeFrameError(
`<${COMPONENT_NAME}> must not have \`children\` as a prop.`
);
}
}
return result;
}
function getUri(value, t) {
if (t.isStringLiteral(value)) {
return value.value;
}
if (t.isJSXExpressionContainer(value)) {
if (t.isStringLiteral(value.expression)) {
return value.expression.value;
}
if (
t.isTemplateLiteral(value.expression) &&
value.expression.expressions.length === 0 &&
value.expression.quasis.length === 1
) {
return value.expression.quasis[0].value.cooked;
}
return undefined;
}
return undefined;
}
function maybeGetLoneChild(jsxElementPath, t) {
const children = jsxElementPath.get("children");
let result = undefined;
for (const child of children) {
// There can be whitespace-only JSXText nodes around an
// JSXExpressionContainer. Ignore those, just like Babel does.
// Also ignore `{/* comments */}`.
if (!(isEmptyJSXText(child.node, t) || isJSXComment(child.node, t))) {
if (t.isJSXElement(child.node)) {
throw child.buildCodeFrameError(
`<${COMPONENT_NAME}> only takes a single string as children. Wrap the default value in the \`md\` template tag to include HTML.`
);
}
if (result != null) {
throw child.buildCodeFrameError(
`<${COMPONENT_NAME}> only takes a single string as children. Did you mean to use \`[foo]\` instead of \`{foo}\`?`
);
}
result = child;
}
}
return result;
}
function isEmptyJSXText(node, t) {
return t.isJSXText(node) && node.value.trim() === "";
}
function isJSXComment(node, t) {
return (
t.isJSXExpressionContainer(node) &&
t.isJSXEmptyExpression(node.expression) &&
node.expression.innerComments != null &&
node.expression.innerComments.length > 0
);
}
function getDefaultValue(valuePath, t) {
if (t.isJSXText(valuePath.node)) {
return valuePath.node.value;
}
if (t.isJSXExpressionContainer(valuePath.node)) {
if (t.isStringLiteral(valuePath.node.expression)) {
return valuePath.node.expression.value;
}
const templateLiteral = t.isTemplateLiteral(valuePath.node.expression)
? valuePath.get("expression")
: t.isTaggedTemplateExpression(valuePath.node.expression) &&
t.isIdentifier(valuePath.node.expression.tag, {
name: TEMPLATE_TAG_NAME,
})
? valuePath.get("expression.quasi")
: undefined;
if (templateLiteral != null) {
const expressions = templateLiteral.get("expressions");
if (expressions.length > 0) {
throw expressions[0].buildCodeFrameError(
`Using \`\${foo}\` in a <${COMPONENT_NAME}> default value is an anti-pattern: it won't work if the user edits the node. Did you mean to use \`{foo}\` (without the \`$\`) or \`[foo]\`?`
);
}
return templateLiteral.node.quasis[0].value.cooked;
}
return undefined;
}
return undefined;
}
function makeClientImport(clientId, t) {
return t.importDeclaration(
[t.importSpecifier(t.cloneNode(clientId), t.identifier(CLIENT_NAME))],
t.stringLiteral(MODULE_NAME)
);
}
function makeReportCall(clientId, uriId, defaultId, t) {
return t.expressionStatement(
t.callExpression(
t.memberExpression(
t.cloneNode(clientId),
t.identifier(CLIENT_METHOD_NAME)
),
[
t.objectExpression([
t.objectProperty(t.identifier(NODE_URI_KEY), t.cloneNode(uriId)),
t.objectProperty(
t.identifier(NODE_VALUE_KEY),
defaultId == null
? t.identifier("undefined")
: t.cloneNode(defaultId)
),
]),
]
)
);
}