-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
placeholders.ts
386 lines (333 loc) 路 12.3 KB
/
placeholders.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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import * as charCodes from "charcodes";
import { tokenLabelName, tt } from "../tokenizer/types";
import type Parser from "../parser";
import type * as N from "../types";
import { ParseErrorEnum } from "../parse-error";
import type { Undone } from "../parser/node";
import type { ExpressionErrors } from "../parser/util";
import type { BindingTypes } from "../util/scopeflags";
import type { Position } from "../util/location";
type PossiblePlaceholedrs = {
Identifier: N.Identifier;
StringLiteral: N.StringLiteral;
Expression: N.Expression;
Statement: N.Statement;
Declaration: N.Declaration;
BlockStatement: N.BlockStatement;
ClassBody: N.ClassBody;
Pattern: N.Pattern;
};
export type PlaceholderTypes = keyof PossiblePlaceholedrs;
type NodeOf<T extends keyof PossiblePlaceholedrs> = PossiblePlaceholedrs[T];
// todo: when there is proper union type for Node
// type NodeOf<T extends PlaceholderTypes> = Extract<N.Node, { type: T }>;
// todo: Placeholder<T> breaks everything, because its type is incompatible with
// the substituted nodes.
type MaybePlaceholder<T extends PlaceholderTypes> = NodeOf<T>; // | Placeholder<T>
/* eslint sort-keys: "error" */
const PlaceholderErrors = ParseErrorEnum`placeholders`({
ClassNameIsRequired: "A class name is required.",
UnexpectedSpace: "Unexpected space in placeholder.",
});
/* eslint-disable sort-keys */
export default (superClass: typeof Parser) =>
class PlaceholdersParserMixin extends superClass implements Parser {
parsePlaceholder<T extends PlaceholderTypes>(
expectedNode: T,
): /*?N.Placeholder<T>*/ MaybePlaceholder<T> | undefined | null {
if (this.match(tt.placeholder)) {
const node = this.startNode();
this.next();
this.assertNoSpace();
// We can't use this.parseIdentifier because
// we don't want nested placeholders.
node.name = super.parseIdentifier(/* liberal */ true);
this.assertNoSpace();
this.expect(tt.placeholder);
// @ts-expect-error placeholder typings
return this.finishPlaceholder(node, expectedNode);
}
}
finishPlaceholder<T extends PlaceholderTypes>(
node: N.Node,
expectedNode: T,
): /*N.Placeholder<T>*/ MaybePlaceholder<T> {
const isFinished = !!(node.expectedNode && node.type === "Placeholder");
node.expectedNode = expectedNode;
// @ts-expect-error todo(flow->ts)
return isFinished ? node : this.finishNode(node, "Placeholder");
}
/* ============================================================ *
* tokenizer/index.js *
* ============================================================ */
getTokenFromCode(code: number) {
if (
code === charCodes.percentSign &&
this.input.charCodeAt(this.state.pos + 1) === charCodes.percentSign
) {
return this.finishOp(tt.placeholder, 2);
}
return super.getTokenFromCode(code);
}
/* ============================================================ *
* parser/expression.js *
* ============================================================ */
parseExprAtom(
refExpressionErrors?: ExpressionErrors | null,
): MaybePlaceholder<"Expression"> {
return (
this.parsePlaceholder("Expression") ||
super.parseExprAtom(refExpressionErrors)
);
}
parseIdentifier(liberal?: boolean): MaybePlaceholder<"Identifier"> {
// NOTE: This function only handles identifiers outside of
// expressions and binding patterns, since they are already
// handled by the parseExprAtom and parseBindingAtom functions.
// This is needed, for example, to parse "class %%NAME%% {}".
return (
this.parsePlaceholder("Identifier") || super.parseIdentifier(liberal)
);
}
checkReservedWord(
word: string,
startLoc: Position,
checkKeywords: boolean,
isBinding: boolean,
) {
// Sometimes we call #checkReservedWord(node.name), expecting
// that node is an Identifier. If it is a Placeholder, name
// will be undefined.
if (word !== undefined) {
super.checkReservedWord(word, startLoc, checkKeywords, isBinding);
}
}
/* ============================================================ *
* parser/lval.js *
* ============================================================ */
parseBindingAtom(): MaybePlaceholder<"Pattern"> {
return this.parsePlaceholder("Pattern") || super.parseBindingAtom();
}
isValidLVal(type: string, isParenthesized: boolean, binding: BindingTypes) {
return (
type === "Placeholder" ||
super.isValidLVal(type, isParenthesized, binding)
);
}
toAssignable(node: N.Node, isLHS: boolean): void {
if (
node &&
node.type === "Placeholder" &&
node.expectedNode === "Expression"
) {
node.expectedNode = "Pattern";
} else {
super.toAssignable(node, isLHS);
}
}
/* ============================================================ *
* parser/statement.js *
* ============================================================ */
isLet(context?: string | null): boolean {
if (super.isLet(context)) {
return true;
}
// Replicate the original checks that lead to looking ahead for an
// identifier.
if (!this.isContextual(tt._let)) {
return false;
}
if (context) return false;
// Accept "let %%" as the start of "let %%placeholder%%", as though the
// placeholder were an identifier.
const nextToken = this.lookahead();
if (nextToken.type === tt.placeholder) {
return true;
}
return false;
}
verifyBreakContinue(
node: N.BreakStatement | N.ContinueStatement,
isBreak: boolean,
) {
// @ts-expect-error: node.label could be Placeholder
if (node.label && node.label.type === "Placeholder") return;
super.verifyBreakContinue(node, isBreak);
}
// @ts-expect-error Plugin will override parser interface
parseExpressionStatement(
node: MaybePlaceholder<"Statement">,
expr: N.Expression,
): MaybePlaceholder<"Statement"> {
if (
expr.type !== "Placeholder" ||
(expr.extra && expr.extra.parenthesized)
) {
// @ts-expect-error placeholder typings
return super.parseExpressionStatement(node, expr);
}
if (this.match(tt.colon)) {
// @ts-expect-error
const stmt: N.LabeledStatement = node;
stmt.label = this.finishPlaceholder(expr, "Identifier");
this.next();
stmt.body = super.parseStatement("label");
return this.finishNode(stmt, "LabeledStatement");
}
this.semicolon();
node.name = expr.name;
return this.finishPlaceholder(node, "Statement");
}
parseBlock(
allowDirectives?: boolean,
createNewLexicalScope?: boolean,
afterBlockParse?: (hasStrictModeDirective: boolean) => void,
): MaybePlaceholder<"BlockStatement"> {
return (
this.parsePlaceholder("BlockStatement") ||
super.parseBlock(
allowDirectives,
createNewLexicalScope,
afterBlockParse,
)
);
}
parseFunctionId(
requireId?: boolean,
): MaybePlaceholder<"Identifier"> | undefined | null {
return (
this.parsePlaceholder("Identifier") || super.parseFunctionId(requireId)
);
}
// @ts-expect-error Plugin will override parser interface
parseClass<T extends N.Class>(
node: T,
isStatement: /* T === ClassDeclaration */ boolean,
optionalId?: boolean,
): T {
const type = isStatement ? "ClassDeclaration" : "ClassExpression";
this.next();
this.takeDecorators(node);
const oldStrict = this.state.strict;
const placeholder = this.parsePlaceholder("Identifier");
if (placeholder) {
if (
this.match(tt._extends) ||
this.match(tt.placeholder) ||
this.match(tt.braceL)
) {
node.id = placeholder;
} else if (optionalId || !isStatement) {
node.id = null;
node.body = this.finishPlaceholder(placeholder, "ClassBody");
return this.finishNode(node, type);
} else {
throw this.raise(PlaceholderErrors.ClassNameIsRequired, {
at: this.state.startLoc,
});
}
} else {
this.parseClassId(node, isStatement, optionalId);
}
super.parseClassSuper(node);
node.body =
this.parsePlaceholder("ClassBody") ||
super.parseClassBody(!!node.superClass, oldStrict);
return this.finishNode(node, type);
}
parseExport(node: N.Node): N.AnyExport {
const placeholder = this.parsePlaceholder("Identifier");
if (!placeholder) return super.parseExport(node);
if (!this.isContextual(tt._from) && !this.match(tt.comma)) {
// export %%DECL%%;
node.specifiers = [];
node.source = null;
node.declaration = this.finishPlaceholder(placeholder, "Declaration");
return this.finishNode(node, "ExportNamedDeclaration");
}
// export %%NAME%% from "foo";
this.expectPlugin("exportDefaultFrom");
const specifier = this.startNode();
specifier.exported = placeholder;
node.specifiers = [this.finishNode(specifier, "ExportDefaultSpecifier")];
return super.parseExport(node);
}
isExportDefaultSpecifier(): boolean {
if (this.match(tt._default)) {
const next = this.nextTokenStart();
if (this.isUnparsedContextual(next, "from")) {
if (
this.input.startsWith(
tokenLabelName(tt.placeholder),
this.nextTokenStartSince(next + 4),
)
) {
return true;
}
}
}
return super.isExportDefaultSpecifier();
}
maybeParseExportDefaultSpecifier(node: N.Node): boolean {
if (node.specifiers && node.specifiers.length > 0) {
// "export %%NAME%%" has already been parsed by #parseExport.
return true;
}
return super.maybeParseExportDefaultSpecifier(node);
}
checkExport(node: N.ExportNamedDeclaration): void {
const { specifiers } = node;
if (specifiers?.length) {
node.specifiers = specifiers.filter(
// @ts-expect-error placeholder typings
node => node.exported.type === "Placeholder",
);
}
super.checkExport(node);
node.specifiers = specifiers;
}
parseImport(
node: Undone<N.ImportDeclaration>,
): N.ImportDeclaration | N.TsImportEqualsDeclaration {
const placeholder = this.parsePlaceholder("Identifier");
if (!placeholder) return super.parseImport(node);
node.specifiers = [];
if (!this.isContextual(tt._from) && !this.match(tt.comma)) {
// import %%STRING%%;
node.source = this.finishPlaceholder(placeholder, "StringLiteral");
this.semicolon();
return this.finishNode(node, "ImportDeclaration");
}
// import %%DEFAULT%% ...
const specifier =
this.startNodeAtNode<N.ImportDefaultSpecifier>(placeholder);
specifier.local = placeholder;
node.specifiers.push(
this.finishNode(specifier, "ImportDefaultSpecifier"),
);
if (this.eat(tt.comma)) {
// import %%DEFAULT%%, * as ...
const hasStarImport = this.maybeParseStarImportSpecifier(node);
// import %%DEFAULT%%, { ...
if (!hasStarImport) this.parseNamedImportSpecifiers(node);
}
this.expectContextual(tt._from);
node.source = this.parseImportSource();
this.semicolon();
return this.finishNode(node, "ImportDeclaration");
}
parseImportSource(): MaybePlaceholder<"StringLiteral"> {
// import ... from %%STRING%%;
return (
this.parsePlaceholder("StringLiteral") || super.parseImportSource()
);
}
// Throws if the current token and the prev one are separated by a space.
assertNoSpace(): void {
if (this.state.start > this.state.lastTokEndLoc.index) {
this.raise(PlaceholderErrors.UnexpectedSpace, {
at: this.state.lastTokEndLoc,
});
}
}
};