inject.js
1.34 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
/* eslint-disable no-underscore-dangle */
export default function injectDynamicImport(acorn) {
const tt = acorn.tokTypes;
// NOTE: This allows `yield import()` to parse correctly.
tt._import.startsExpr = true;
function parseDynamicImport() {
const node = this.startNode();
this.next();
if (this.type !== tt.parenL) {
this.unexpected();
}
return this.finishNode(node, 'Import');
}
function peekNext() {
return this.input[this.pos];
}
// eslint-disable-next-line no-param-reassign
acorn.plugins.dynamicImport = function dynamicImportPlugin(instance) {
instance.extend('parseStatement', nextMethod => (
function parseStatement(...args) {
const node = this.startNode();
if (this.type === tt._import) {
const nextToken = peekNext.call(this);
if (nextToken === tt.parenL.label) {
const expr = this.parseExpression();
return this.parseExpressionStatement(node, expr);
}
}
return nextMethod.apply(this, args);
}
));
instance.extend('parseExprAtom', nextMethod => (
function parseExprAtom(refDestructuringErrors) {
if (this.type === tt._import) {
return parseDynamicImport.call(this);
}
return nextMethod.call(this, refDestructuringErrors);
}
));
};
return acorn;
}