Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion eslint/babel-eslint-parser/src/parse.js
Expand Up @@ -34,7 +34,7 @@ export default function(code, options) {
plugins: ["estree"],
},
caller: {
name: "babel-eslint",
name: "@babel/eslint-parser",
},
};

Expand Down
6 changes: 6 additions & 0 deletions eslint/babel-eslint-parser/test/babel-eslint-parser.js
Expand Up @@ -524,5 +524,11 @@ describe("babylon-to-espree", () => {
const a = 1n;
`);
});

it("Dynamic Import", () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to be able to test this across the parser and the estree plugin :)

parseAndAssertSame(`
const a = import('a');
`);
});
});
});
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -758,7 +758,7 @@ export default class ExpressionParser extends LValParser {
finishCallExpression<T: N.CallExpression | N.OptionalCallExpression>(
node: T,
optional: boolean,
): T {
): N.Expression {
if (node.callee.type === "Import") {
if (node.arguments.length !== 1) {
this.raise(node.start, "import() requires exactly one argument");
Expand Down
4 changes: 1 addition & 3 deletions packages/babel-parser/src/parser/lval.js
Expand Up @@ -228,16 +228,14 @@ export default class LValParser extends NodeUtils {
toReferencedListDeep(
exprList: $ReadOnlyArray<?Expression>,
isParenthesizedExpr?: boolean,
): $ReadOnlyArray<?Expression> {
): void {
this.toReferencedList(exprList, isParenthesizedExpr);

for (const expr of exprList) {
if (expr && expr.type === "ArrayExpression") {
this.toReferencedListDeep(expr.elements);
}
}

return exprList;
}

// Parses spread element.
Expand Down
28 changes: 28 additions & 0 deletions packages/babel-parser/src/plugins/estree.js
Expand Up @@ -411,4 +411,32 @@ export default (superClass: Class<Parser>): Class<Parser> =>
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);
}
};
7 changes: 6 additions & 1 deletion packages/babel-parser/src/types.js
Expand Up @@ -1013,7 +1013,7 @@ export type FlowInterfaceType = NodeBase & {
body: FlowObjectTypeAnnotation,
};

// estree
// ESTree

export type EstreeProperty = NodeBase & {
type: "Property",
Expand All @@ -1039,6 +1039,11 @@ export type EstreeMethodDefinition = NodeBase & {
variance?: ?FlowVariance,
};

export type EstreeImportExpression = NodeBase & {
type: "ImportExpression",
source: Expression,
};

// === === === ===
// TypeScript
// === === === ===
Expand Down
@@ -1,5 +1,3 @@
{
"plugins": [
"estree"
]
}
"plugins": ["estree"]
}
@@ -1,5 +1,3 @@
{
"plugins": [
"estree"
]
}
"plugins": ["estree"]
}
@@ -1,5 +1,3 @@
{
"plugins": [
"estree"
]
}
"plugins": ["estree"]
}
@@ -1,5 +1,3 @@
{
"plugins": [
"estree"
]
}
"plugins": ["estree"]
}
@@ -1,5 +1,3 @@
{
"plugins": [
"estree"
]
}
"plugins": ["estree"]
}
@@ -1,6 +1,3 @@
{
"plugins": [
"estree",
"bigInt"
]
"plugins": ["estree", "bigInt"]
}
@@ -0,0 +1 @@
const a = import("a");
@@ -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"
}
]
}
}
@@ -0,0 +1,3 @@
{
"plugins": ["estree", "dynamicImport"]
}