Add estree parsing support for export * as A (#11254)
This commit is contained in:
parent
4a4845585c
commit
20d9a10186
@ -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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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 };");
|
||||
});
|
||||
|
||||
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
1
packages/babel-parser/test/fixtures/estree/export/batch/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/estree/export/batch/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
export * from "foo";
|
||||
67
packages/babel-parser/test/fixtures/estree/export/batch/output.json
vendored
Normal file
67
packages/babel-parser/test/fixtures/estree/export/batch/output.json
vendored
Normal file
@ -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
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/estree/export/ns-from/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/estree/export/ns-from/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
export * as A from 'test';
|
||||
83
packages/babel-parser/test/fixtures/estree/export/ns-from/output.json
vendored
Normal file
83
packages/babel-parser/test/fixtures/estree/export/ns-from/output.json
vendored
Normal file
@ -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"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
4
packages/babel-parser/test/fixtures/estree/export/options.json
vendored
Normal file
4
packages/babel-parser/test/fixtures/estree/export/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"plugins": ["estree"],
|
||||
"sourceType": "module"
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user