Skip to content

Commit

Permalink
Relax import assertion key-is-type constraint (#13409)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jun 2, 2021
1 parent b281fe3 commit b8175ec
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 48 deletions.
23 changes: 7 additions & 16 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -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(
Expand Down
@@ -0,0 +1,2 @@
import "foo"
assert { type: "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": []
}
}
@@ -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}},
Expand Down
@@ -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}},
Expand Down
@@ -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}},
Expand Down
@@ -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}},
Expand Down
@@ -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}},
Expand Down
17 changes: 1 addition & 16 deletions 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)

0 comments on commit b8175ec

Please sign in to comment.