From 26a31c249c5b8f413a37ea46ace3d6c32f33965f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Jun 2021 16:14:59 -0400 Subject: [PATCH 1/4] fix: relax ModuleAttributeDifferentFromType error --- packages/babel-parser/src/parser/statement.js | 23 ++++++------------- .../string-literal/output.json | 4 ---- .../output.json | 3 --- .../output.json | 3 --- .../output.json | 3 --- .../output.json | 3 --- 6 files changed, 7 insertions(+), 32 deletions(-) diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index b9c6b9822b74..1f895ee973f9 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -2255,32 +2255,23 @@ export default class StatementParser extends ExpressionParser { // parse AssertionKey : IdentifierName, StringLiteral const keyName = this.state.value; - if (this.match(tt.string)) { - node.key = this.parseStringLiteral(keyName); - } else { - node.key = this.parseIdentifier(true); - } - this.expect(tt.colon); - - // for now we are only allowing `type` as the only allowed module attribute - if (keyName !== "type") { - this.raise( - node.key.start, - Errors.ModuleAttributeDifferentFromType, - keyName, - ); - } // check if we already have an entry for an attribute // if a duplicate entry is found, throw an error // for now this logic will come into play only when someone declares `type` twice if (attrNames.has(keyName)) { this.raise( - node.key.start, + this.state.start, Errors.ModuleAttributesWithDuplicateKeys, keyName, ); } attrNames.add(keyName); + if (this.match(tt.string)) { + node.key = this.parseStringLiteral(keyName); + } else { + node.key = this.parseIdentifier(true); + } + this.expect(tt.colon); if (!this.match(tt.string)) { throw this.unexpected( diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json index dce442f8ffc0..3889a7fd045a 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json @@ -1,10 +1,6 @@ { "type": "File", "start":0,"end":101,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":52}}, - "errors": [ - "SyntaxError: The only accepted module attribute is `type`. (1:36)", - "SyntaxError: The only accepted module attribute is `type`. (2:40)" - ], "program": { "type": "Program", "start":0,"end":101,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":52}}, diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-no-type-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-no-type-attribute/output.json index 487d980a1d3b..d1c8fdf80619 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-no-type-attribute/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-no-type-attribute/output.json @@ -1,9 +1,6 @@ { "type": "File", "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}}, - "errors": [ - "SyntaxError: The only accepted module attribute is `type`. (1:40)" - ], "program": { "type": "Program", "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":55}}, diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-object-method-attribute/output.json index d17d1e8bd120..20fef03c5d6e 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-object-method-attribute/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-object-method-attribute/output.json @@ -1,9 +1,6 @@ { "type": "File", "start":0,"end":79,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":79}}, - "errors": [ - "SyntaxError: The only accepted module attribute is `type`. (1:54)" - ], "program": { "type": "Program", "start":0,"end":79,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":79}}, diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json index 1328bd89fc18..8d5cffa682dc 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-no-type-attribute/output.json @@ -1,9 +1,6 @@ { "type": "File", "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, - "errors": [ - "SyntaxError: The only accepted module attribute is `type`. (1:36)" - ], "program": { "type": "Program", "start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}}, diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json index d29b9970cd24..5fa35065f83f 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-object-method-attribute/output.json @@ -1,9 +1,6 @@ { "type": "File", "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}}, - "errors": [ - "SyntaxError: The only accepted module attribute is `type`. (1:50)" - ], "program": { "type": "Program", "start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}}, From 9411a7435beaf0c3ed69b18f10c7c1c983913f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Jun 2021 16:23:37 -0400 Subject: [PATCH 2/4] chore: rename test cases --- .../input.js | 0 .../output.json | 0 .../input.js | 0 .../output.json | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename packages/babel-parser/test/fixtures/experimental/import-assertions/{invalid-syntax-export-with-and-attributes-multiple-lines => valid-syntax-export-with-and-attributes-multiple-lines}/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/import-assertions/{invalid-syntax-export-with-and-attributes-multiple-lines => valid-syntax-export-with-and-attributes-multiple-lines}/output.json (100%) rename packages/babel-parser/test/fixtures/experimental/import-assertions/{invalid-syntax-with-attributes-multiple-lines => valid-syntax-with-attributes-multiple-lines}/input.js (100%) rename packages/babel-parser/test/fixtures/experimental/import-assertions/{invalid-syntax-with-attributes-multiple-lines => valid-syntax-with-attributes-multiple-lines}/output.json (100%) diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-with-and-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-and-attributes-multiple-lines/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-with-and-attributes-multiple-lines/input.js rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-and-attributes-multiple-lines/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-with-and-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-and-attributes-multiple-lines/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-export-with-and-attributes-multiple-lines/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-export-with-and-attributes-multiple-lines/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-multiple-lines/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/input.js rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-multiple-lines/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-multiple-lines/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-with-attributes-multiple-lines/output.json rename to packages/babel-parser/test/fixtures/experimental/import-assertions/valid-syntax-with-attributes-multiple-lines/output.json From 5cb19daca44a0a35c106d6554cdabad723dac218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Jun 2021 16:23:59 -0400 Subject: [PATCH 3/4] test: add test case on uncovered branch --- .../input.js | 2 + .../output.json | 69 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-import-linebreak-asserts/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-import-linebreak-asserts/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-import-linebreak-asserts/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-import-linebreak-asserts/input.js new file mode 100644 index 000000000000..ce0c366a9c9a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-import-linebreak-asserts/input.js @@ -0,0 +1,2 @@ +import "foo" +assert { type: "json" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-import-linebreak-asserts/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-import-linebreak-asserts/output.json new file mode 100644 index 000000000000..939b99ed8e3e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/invalid-syntax-import-linebreak-asserts/output.json @@ -0,0 +1,69 @@ +{ + "type": "File", + "start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":23}}, + "errors": [ + "SyntaxError: Missing semicolon. (2:6)" + ], + "program": { + "type": "Program", + "start":0,"end":36,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":23}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}}, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start":7,"end":12,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":12}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "assertions": [] + }, + { + "type": "ExpressionStatement", + "start":13,"end":19,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}}, + "expression": { + "type": "Identifier", + "start":13,"end":19,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6},"identifierName":"assert"}, + "name": "assert" + } + }, + { + "type": "BlockStatement", + "start":20,"end":36,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":23}}, + "body": [ + { + "type": "LabeledStatement", + "start":22,"end":34,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":21}}, + "body": { + "type": "ExpressionStatement", + "start":28,"end":34,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":21}}, + "expression": { + "type": "StringLiteral", + "start":28,"end":34,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":21}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + "label": { + "type": "Identifier", + "start":22,"end":26,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":13},"identifierName":"type"}, + "name": "type" + } + } + ], + "directives": [] + } + ], + "directives": [] + } +} \ No newline at end of file From 752602e81ce193bb3db13a29d345dce5e693391f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 1 Jun 2021 16:26:53 -0400 Subject: [PATCH 4/4] chore: update test262 allowlist --- scripts/parser-tests/test262/allowlist.txt | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/scripts/parser-tests/test262/allowlist.txt b/scripts/parser-tests/test262/allowlist.txt index 2f22511f8f2a..8b137891791f 100644 --- a/scripts/parser-tests/test262/allowlist.txt +++ b/scripts/parser-tests/test262/allowlist.txt @@ -1,16 +1 @@ -language/module-code/import-assertion-key-identifiername.js(default) -language/module-code/import-assertion-key-identifiername.js(strict mode) -language/module-code/import-assertion-key-string-double.js(default) -language/module-code/import-assertion-key-string-double.js(strict mode) -language/module-code/import-assertion-key-string-single.js(default) -language/module-code/import-assertion-key-string-single.js(strict mode) -language/module-code/import-assertion-many.js(default) -language/module-code/import-assertion-many.js(strict mode) -language/module-code/import-assertion-newlines.js(default) -language/module-code/import-assertion-newlines.js(strict mode) -language/module-code/import-assertion-trlng-comma.js(default) -language/module-code/import-assertion-trlng-comma.js(strict mode) -language/module-code/import-assertion-value-string-double.js(default) -language/module-code/import-assertion-value-string-double.js(strict mode) -language/module-code/import-assertion-value-string-single.js(default) -language/module-code/import-assertion-value-string-single.js(strict mode) +