From 1d54419ec4cb9eac579b65e4730a16cc8a12654c Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Sat, 8 May 2021 05:37:20 +0900 Subject: [PATCH] Parse attributes of import expression with estree plugin (#13284) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Parse attributes of import expression with estree plugin * Add tests * Update packages/babel-parser/src/types.js Co-authored-by: Huáng Jùnliàng Co-authored-by: Huáng Jùnliàng --- packages/babel-parser/src/plugins/estree.js | 4 ++ packages/babel-parser/src/types.js | 1 + .../import-assertions-null/input.js | 1 + .../import-assertions-null/options.json | 3 + .../import-assertions-null/output.json | 27 +++++++ .../dynamic-import/import-assertions/input.js | 1 + .../import-assertions/options.json | 3 + .../import-assertions/output.json | 70 +++++++++++++++++++ 8 files changed, 110 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/input.js create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/options.json create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/output.json create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/input.js create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/options.json create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/output.json diff --git a/packages/babel-parser/src/plugins/estree.js b/packages/babel-parser/src/plugins/estree.js index 762ce6c3ea..9a592c18b6 100644 --- a/packages/babel-parser/src/plugins/estree.js +++ b/packages/babel-parser/src/plugins/estree.js @@ -377,6 +377,10 @@ export default (superClass: Class): Class => if (node.callee.type === "Import") { ((node: N.Node): N.EstreeImportExpression).type = "ImportExpression"; ((node: N.Node): N.EstreeImportExpression).source = node.arguments[0]; + if (this.hasPlugin("importAssertions")) { + ((node: N.Node): N.EstreeImportExpression).attributes = + node.arguments[1] ?? null; + } // $FlowIgnore - arguments isn't optional in the type definition delete node.arguments; // $FlowIgnore - callee isn't optional in the type definition diff --git a/packages/babel-parser/src/types.js b/packages/babel-parser/src/types.js index 6a890ac8f4..0f58039dd2 100644 --- a/packages/babel-parser/src/types.js +++ b/packages/babel-parser/src/types.js @@ -1095,6 +1095,7 @@ export type EstreeMethodDefinition = NodeBase & { export type EstreeImportExpression = NodeBase & { type: "ImportExpression", source: Expression, + attributes?: Expression | null, }; export type EstreePrivateIdentifier = NodeBase & { diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/input.js b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/input.js new file mode 100644 index 0000000000..23f5a7e925 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/input.js @@ -0,0 +1 @@ +import("module"); diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/options.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/options.json new file mode 100644 index 0000000000..604246aa38 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["estree", "importAssertions"] +} diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/output.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/output.json new file mode 100644 index 0000000000..2145426498 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/output.json @@ -0,0 +1,27 @@ +{ + "type": "File", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "program": { + "type": "Program", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "expression": { + "type": "ImportExpression", + "start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}}, + "source": { + "type": "Literal", + "start":7,"end":15,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":15}}, + "value": "module", + "raw": "\"module\"" + }, + "attributes": null + } + } + ] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/input.js b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/input.js new file mode 100644 index 0000000000..c96af3ff4e --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/input.js @@ -0,0 +1 @@ +import("module", { assert: { type: "json" } }); diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/options.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/options.json new file mode 100644 index 0000000000..604246aa38 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["estree", "importAssertions"] +} diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/output.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/output.json new file mode 100644 index 0000000000..bbbe1d4f8a --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/output.json @@ -0,0 +1,70 @@ +{ + "type": "File", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":47}}, + "program": { + "type": "Program", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":47}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":47}}, + "expression": { + "type": "ImportExpression", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}}, + "source": { + "type": "Literal", + "start":7,"end":15,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":15}}, + "value": "module", + "raw": "\"module\"" + }, + "attributes": { + "type": "ObjectExpression", + "start":17,"end":45,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":45}}, + "properties": [ + { + "type": "Property", + "start":19,"end":43,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":43}}, + "method": false, + "key": { + "type": "Identifier", + "start":19,"end":25,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":25},"identifierName":"assert"}, + "name": "assert" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":27,"end":43,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":43}}, + "properties": [ + { + "type": "Property", + "start":29,"end":41,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":41}}, + "method": false, + "key": { + "type": "Identifier", + "start":29,"end":33,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":33},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "Literal", + "start":35,"end":41,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":41}}, + "value": "json", + "raw": "\"json\"" + }, + "kind": "init" + } + ] + }, + "kind": "init" + } + ] + } + } + } + ] + } +} \ No newline at end of file