Skip to content

Commit

Permalink
Add estree parsing support for export * as A (#11254)
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism committed Mar 16, 2020
1 parent 4a48455 commit 20d9a10
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 7 deletions.
35 changes: 28 additions & 7 deletions eslint/babel-eslint-parser/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,38 @@ const BABEL_OPTIONS = {
"@babel/eslint-shared-fixtures/config/babel.config.js",
),
};
const ALLOWED_PROPERTIES = [
const PROPS_TO_REMOVE = [
"importKind",
"exportKind",
"variance",
"typeArguments",
];
// We can remove needing to drop "exported" if null once this lands:
// https://github.com/acornjs/acorn/pull/889
const PROPS_TO_REMOVE_IF_NULL = ["exported"];

function deeplyRemoveProperties(obj, props) {
function deeplyRemoveProperties(obj, props, propsIfNull) {
for (const [k, v] of Object.entries(obj)) {
if (typeof v === "object") {
if (Array.isArray(v)) {
for (const el of v) {
if (el != null) deeplyRemoveProperties(el, props);
if (el != null) {
deeplyRemoveProperties(el, props, propsIfNull);
}
}
}

if (props.includes(k)) delete obj[k];
else if (v != null) deeplyRemoveProperties(v, props);
if (props.includes(k) || (propsIfNull.includes(k) && v === null)) {
delete obj[k];
} else if (v != null) {
deeplyRemoveProperties(v, props, propsIfNull);
}
continue;
}

if (props.includes(k)) delete obj[k];
if (props.includes(k) || (propsIfNull.includes(k) && v === null)) {
delete obj[k];
}
}
}

Expand Down Expand Up @@ -62,7 +72,7 @@ describe("Babel and Espree", () => {
eslintScopeManager: true,
babelOptions: BABEL_OPTIONS,
}).ast;
deeplyRemoveProperties(babelAST, ALLOWED_PROPERTIES);
deeplyRemoveProperties(babelAST, PROPS_TO_REMOVE, PROPS_TO_REMOVE_IF_NULL);
expect(babelAST).toEqual(espreeAST);
}

Expand Down Expand Up @@ -266,6 +276,17 @@ describe("Babel and Espree", () => {
parseAndAssertSame('export * from "foo";');
});

// Espree doesn't support `export * as ns` yet
it("export * as ns", () => {
const code = 'export * as Foo from "foo";';
const babylonAST = parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
babelOptions: BABEL_OPTIONS,
}).ast;
expect(babylonAST.tokens[1].type).toEqual("Punctuator");
});

it("export named", () => {
parseAndAssertSame("var foo = 1;export { foo };");
});
Expand Down
24 changes: 24 additions & 0 deletions packages/babel-parser/src/plugins/estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,28 @@ export default (superClass: Class<Parser>): Class<Parser> =>
super.toReferencedListDeep(exprList, isParenthesizedExpr);
}
parseExport(node: N.Node) {
super.parseExport(node);
switch (node.type) {
case "ExportAllDeclaration":
node.exported = null;
break;
case "ExportNamedDeclaration":
if (
node.specifiers.length === 1 &&
node.specifiers[0].type === "ExportNamespaceSpecifier"
) {
node.type = "ExportAllDeclaration";
node.exported = node.specifiers[0].exported;
delete node.specifiers;
}

break;
}

return node;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "foo";
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"type": "File",
"start": 0,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 20
}
},
"program": {
"type": "Program",
"start": 0,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 20
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportAllDeclaration",
"start": 0,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 20
}
},
"source": {
"type": "Literal",
"start": 14,
"end": 19,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 19
}
},
"value": "foo",
"raw": "\"foo\""
},
"exported": null
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as A from 'test';
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"type": "File",
"start": 0,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 26
}
},
"program": {
"type": "Program",
"start": 0,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 26
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportAllDeclaration",
"start": 0,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 26
}
},
"source": {
"type": "Literal",
"start": 19,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 1,
"column": 25
}
},
"value": "test",
"raw": "'test'"
},
"exported": {
"type": "Identifier",
"start": 12,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "A"
},
"name": "A"
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["estree"],
"sourceType": "module"
}

0 comments on commit 20d9a10

Please sign in to comment.