From d31b438de955c64685ccfc07bf2e5f7e0621bec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 13 Apr 2023 12:10:17 +0200 Subject: [PATCH] Update missed `hasPlugin` checks, and add estree tests --- .../babel-parser/src/parser/expression.ts | 2 + packages/babel-parser/src/plugins/estree.ts | 5 +- .../import-attributes-null/input.js | 1 + .../import-attributes-null/options.json | 3 + .../import-attributes-null/output.json | 27 +++++++ .../dynamic-import/import-attributes/input.js | 1 + .../import-attributes/options.json | 3 + .../import-attributes/output.json | 70 +++++++++++++++++++ .../valid-syntax-with-attributes/input.js | 1 + .../valid-syntax-with-attributes/options.json | 9 +++ .../valid-syntax-with-attributes/output.json | 51 ++++++++++++++ .../incorrect-arity/output.json | 4 +- .../trailing-comma-dynamic/output.json | 4 -- .../incorrect-arity/output.json | 4 +- .../trailing-comma-dynamic/output.json | 4 -- 15 files changed, 176 insertions(+), 13 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/input.js create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/options.json create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/output.json create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/options.json create mode 100644 packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/output.json create mode 100644 packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/input.js create mode 100644 packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/options.json create mode 100644 packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/output.json diff --git a/packages/babel-parser/src/parser/expression.ts b/packages/babel-parser/src/parser/expression.ts index d272d26c4c67..f3a28e8775a6 100644 --- a/packages/babel-parser/src/parser/expression.ts +++ b/packages/babel-parser/src/parser/expression.ts @@ -991,6 +991,7 @@ export default abstract class ExpressionParser extends LValParser { this.raise(Errors.ImportCallArity, { at: node, maxArgumentCount: + this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions") || this.hasPlugin("moduleAttributes") ? 2 @@ -1031,6 +1032,7 @@ export default abstract class ExpressionParser extends LValParser { if (this.match(close)) { if ( dynamicImport && + !this.hasPlugin("importAttributes") && !this.hasPlugin("importAssertions") && !this.hasPlugin("moduleAttributes") ) { diff --git a/packages/babel-parser/src/plugins/estree.ts b/packages/babel-parser/src/plugins/estree.ts index af13e32f09c6..b9bba14b78d7 100644 --- a/packages/babel-parser/src/plugins/estree.ts +++ b/packages/babel-parser/src/plugins/estree.ts @@ -420,7 +420,10 @@ export default (superClass: typeof Parser) => if (node.callee.type === "Import") { (node as N.Node as N.EstreeImportExpression).type = "ImportExpression"; (node as N.Node as N.EstreeImportExpression).source = node.arguments[0]; - if (this.hasPlugin("importAssertions")) { + if ( + this.hasPlugin("importAttributes") || + this.hasPlugin("importAssertions") + ) { (node as N.Node as N.EstreeImportExpression).attributes = node.arguments[1] ?? null; } diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/input.js b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/input.js new file mode 100644 index 000000000000..23f5a7e925a2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/input.js @@ -0,0 +1 @@ +import("module"); diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/options.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/options.json new file mode 100644 index 000000000000..81d81c6809b9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["estree", "importAttributes"] +} diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/output.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/output.json new file mode 100644 index 000000000000..2145426498da --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-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-attributes/input.js b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/input.js new file mode 100644 index 000000000000..ffdeb0095ae3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/input.js @@ -0,0 +1 @@ +import("module", { with: { type: "json" } }); diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/options.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/options.json new file mode 100644 index 000000000000..81d81c6809b9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["estree", "importAttributes"] +} diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/output.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/output.json new file mode 100644 index 000000000000..bdc9e256d222 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/output.json @@ -0,0 +1,70 @@ +{ + "type": "File", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "program": { + "type": "Program", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":45,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":45}}, + "expression": { + "type": "ImportExpression", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "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":43,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":43}}, + "properties": [ + { + "type": "Property", + "start":19,"end":41,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":41}}, + "method": false, + "key": { + "type": "Identifier", + "start":19,"end":23,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":23},"identifierName":"with"}, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":25,"end":41,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":41}}, + "properties": [ + { + "type": "Property", + "start":27,"end":39,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":39}}, + "method": false, + "key": { + "type": "Identifier", + "start":27,"end":31,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":31},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "Literal", + "start":33,"end":39,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":39}}, + "value": "json", + "raw": "\"json\"" + }, + "kind": "init" + } + ] + }, + "kind": "init" + } + ] + } + } + } + ] + } +} diff --git a/packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/input.js b/packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/input.js new file mode 100644 index 000000000000..83c51c971889 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/options.json b/packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/options.json new file mode 100644 index 000000000000..4e003309eb3d --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/options.json @@ -0,0 +1,9 @@ +{ + "plugins": [ + "flow", + "jsx", + "estree", + "importAttributes" + ], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/output.json new file mode 100644 index 000000000000..1041a11fffc7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/import-attributes/valid-syntax-with-attributes/output.json @@ -0,0 +1,51 @@ +{ + "type": "File", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}}, + "program": { + "type": "Program", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":49}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "importKind": "value", + "source": { + "type": "Literal", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}}, + "value": "foo.json", + "raw": "\"foo.json\"" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":34,"end":46,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":46}}, + "key": { + "type": "Identifier", + "start":34,"end":38,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":38},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "Literal", + "start":40,"end":46,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":46}}, + "value": "json", + "raw": "\"json\"" + } + } + ] + } + ] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/incorrect-arity/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/incorrect-arity/output.json index 5d65bac43138..1075711b669a 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/incorrect-arity/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/incorrect-arity/output.json @@ -2,8 +2,8 @@ "type": "File", "start":0,"end":75,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":65,"index":75}}, "errors": [ - "SyntaxError: `import()` requires exactly one argument. (1:0)", - "SyntaxError: `import()` requires exactly one argument. (2:0)" + "SyntaxError: `import()` requires exactly one or two arguments. (1:0)", + "SyntaxError: `import()` requires exactly one or two arguments. (2:0)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/trailing-comma-dynamic/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/trailing-comma-dynamic/output.json index f817c2504477..e484d6873f0f 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/trailing-comma-dynamic/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/trailing-comma-dynamic/output.json @@ -1,10 +1,6 @@ { "type": "File", "start":0,"end":69,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":50,"index":69}}, - "errors": [ - "SyntaxError: Trailing comma is disallowed inside import(...) arguments. (1:15)", - "SyntaxError: Trailing comma is disallowed inside import(...) arguments. (2:47)" - ], "program": { "type": "Program", "start":0,"end":69,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":50,"index":69}}, diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes/incorrect-arity/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes/incorrect-arity/output.json index 37f157047780..04c76d684ff8 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-attributes/incorrect-arity/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes/incorrect-arity/output.json @@ -2,8 +2,8 @@ "type": "File", "start":0,"end":73,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":63,"index":73}}, "errors": [ - "SyntaxError: `import()` requires exactly one argument. (1:0)", - "SyntaxError: `import()` requires exactly one argument. (2:0)" + "SyntaxError: `import()` requires exactly one or two arguments. (1:0)", + "SyntaxError: `import()` requires exactly one or two arguments. (2:0)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes/trailing-comma-dynamic/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes/trailing-comma-dynamic/output.json index b48b300031ea..c532394181e4 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-attributes/trailing-comma-dynamic/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes/trailing-comma-dynamic/output.json @@ -1,10 +1,6 @@ { "type": "File", "start":0,"end":67,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":48,"index":67}}, - "errors": [ - "SyntaxError: Trailing comma is disallowed inside import(...) arguments. (1:15)", - "SyntaxError: Trailing comma is disallowed inside import(...) arguments. (2:45)" - ], "program": { "type": "Program", "start":0,"end":67,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":48,"index":67}},