@babel/eslint-parser: fix ImportExpression node to match ESTree spec (#10828)

* @babel/eslint-parser: fix ImportExpression node to match ESTree spec

* Update caller name for @babel/core.parseSync

* Move logic into estree plugin

* Add estree plugin tests

* Fix Flow error

* Fix flow

* Remove extra properties on ImportExpression node

* Incorporate review feedback
This commit is contained in:
Kai Cataldo 2019-12-11 05:13:57 -05:00 committed by Nicolò Ribaudo
parent 5156d3ea06
commit 7b54a94389
15 changed files with 174 additions and 30 deletions

View File

@ -34,7 +34,7 @@ export default function(code, options) {
plugins: ["estree"], plugins: ["estree"],
}, },
caller: { caller: {
name: "babel-eslint", name: "@babel/eslint-parser",
}, },
}; };

View File

@ -524,5 +524,11 @@ describe("babylon-to-espree", () => {
const a = 1n; const a = 1n;
`); `);
}); });
it("Dynamic Import", () => {
parseAndAssertSame(`
const a = import('a');
`);
});
}); });
}); });

View File

@ -758,7 +758,7 @@ export default class ExpressionParser extends LValParser {
finishCallExpression<T: N.CallExpression | N.OptionalCallExpression>( finishCallExpression<T: N.CallExpression | N.OptionalCallExpression>(
node: T, node: T,
optional: boolean, optional: boolean,
): T { ): N.Expression {
if (node.callee.type === "Import") { if (node.callee.type === "Import") {
if (node.arguments.length !== 1) { if (node.arguments.length !== 1) {
this.raise(node.start, "import() requires exactly one argument"); this.raise(node.start, "import() requires exactly one argument");

View File

@ -228,7 +228,7 @@ export default class LValParser extends NodeUtils {
toReferencedListDeep( toReferencedListDeep(
exprList: $ReadOnlyArray<?Expression>, exprList: $ReadOnlyArray<?Expression>,
isParenthesizedExpr?: boolean, isParenthesizedExpr?: boolean,
): $ReadOnlyArray<?Expression> { ): void {
this.toReferencedList(exprList, isParenthesizedExpr); this.toReferencedList(exprList, isParenthesizedExpr);
for (const expr of exprList) { for (const expr of exprList) {
@ -236,8 +236,6 @@ export default class LValParser extends NodeUtils {
this.toReferencedListDeep(expr.elements); this.toReferencedListDeep(expr.elements);
} }
} }
return exprList;
} }
// Parses spread element. // Parses spread element.

View File

@ -411,4 +411,32 @@ export default (superClass: Class<Parser>): Class<Parser> =>
super.toAssignableObjectExpressionProp(prop, isBinding, isLast); super.toAssignableObjectExpressionProp(prop, isBinding, isLast);
} }
} }
finishCallExpression<T: N.CallExpression | N.OptionalCallExpression>(
node: T,
optional: boolean,
): N.Expression {
super.finishCallExpression(node, optional);
if (node.callee.type === "Import") {
((node: N.Node): N.EstreeImportExpression).type = "ImportExpression";
((node: N.Node): N.EstreeImportExpression).source = node.arguments[0];
delete node.arguments;
delete node.callee;
}
return node;
}
toReferencedListDeep(
exprList: $ReadOnlyArray<?N.Expression>,
isParenthesizedExpr?: boolean,
): void {
// ImportExpressions do not have an arguments array.
if (!exprList) {
return;
}
super.toReferencedListDeep(exprList, isParenthesizedExpr);
}
}; };

View File

@ -1013,7 +1013,7 @@ export type FlowInterfaceType = NodeBase & {
body: FlowObjectTypeAnnotation, body: FlowObjectTypeAnnotation,
}; };
// estree // ESTree
export type EstreeProperty = NodeBase & { export type EstreeProperty = NodeBase & {
type: "Property", type: "Property",
@ -1039,6 +1039,11 @@ export type EstreeMethodDefinition = NodeBase & {
variance?: ?FlowVariance, variance?: ?FlowVariance,
}; };
export type EstreeImportExpression = NodeBase & {
type: "ImportExpression",
source: Expression,
};
// === === === === // === === === ===
// TypeScript // TypeScript
// === === === === // === === === ===

View File

@ -1,5 +1,3 @@
{ {
"plugins": [ "plugins": ["estree"]
"estree" }
]
}

View File

@ -1,5 +1,3 @@
{ {
"plugins": [ "plugins": ["estree"]
"estree" }
]
}

View File

@ -1,5 +1,3 @@
{ {
"plugins": [ "plugins": ["estree"]
"estree" }
]
}

View File

@ -1,5 +1,3 @@
{ {
"plugins": [ "plugins": ["estree"]
"estree" }
]
}

View File

@ -1,5 +1,3 @@
{ {
"plugins": [ "plugins": ["estree"]
"estree" }
]
}

View File

@ -1,6 +1,3 @@
{ {
"plugins": [ "plugins": ["estree", "bigInt"]
"estree",
"bigInt"
]
} }

View File

@ -0,0 +1 @@
const a = import("a");

View File

@ -0,0 +1,116 @@
{
"type": "File",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
}
},
"program": {
"type": "Program",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 21
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "a"
},
"name": "a"
},
"init": {
"type": "ImportExpression",
"start": 10,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 1,
"column": 21
}
},
"source": {
"type": "Literal",
"start": 17,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 20
}
},
"value": "a",
"raw": "\"a\""
}
}
}
],
"kind": "const"
}
]
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["estree", "dynamicImport"]
}