Skip to content

Commit

Permalink
Rename module-attributes to import-assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
xtuc authored and JLHwung committed Oct 8, 2020
1 parent a52e2b6 commit b4175f7
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 95 deletions.
25 changes: 25 additions & 0 deletions packages/babel-parser/src/parser/statement.js
Original file line number Diff line number Diff line change
Expand Up @@ -2118,8 +2118,13 @@ export default class StatementParser extends ExpressionParser {
this.expectPlugin("importAssertions");
const attrs = [];
const attrNames = new Set();
do {
if (this.match(tt.braceR)) {
break;
}
const node = this.startNode();
// parse AssertionKey : IdentifierName, StringLiteral
Expand All @@ -2132,6 +2137,26 @@ export default class StatementParser extends ExpressionParser {
this.next();
node.key = assertionKeyNode;
// for now we are only allowing `type` as the only allowed module attribute
if (node.key.name !== "type") {
this.raise(
node.key.start,
Errors.ModuleAttributeDifferentFromType,
node.key.name,
);
}
// 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(node.key.name)) {
this.raise(
node.key.start,
Errors.ModuleAttributesWithDuplicateKeys,
node.key.name,
);
}
attrNames.add(node.key.name);
if (!this.match(tt.string)) {
throw this.unexpected(
this.state.start,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import foo from "foo.json" assert { type: "json" };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import foo from "foo.json" assert { for: "for" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"plugins": [
[
"importAssertions",
{
"version": "september-2020"
}
]
],
"sourceType": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"type": "File",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}},
"errors": [
"SyntaxError: The only accepted module attribute is `type` (1:36)"
],
"program": {
"type": "Program",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}},
"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"
}
}
],
"source": {
"type": "StringLiteral",
"start":16,"end":26,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":26}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
"assertions": [
{
"type": "ImportAttribute",
"start":36,"end":46,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":46}},
"key": {
"type": "Identifier",
"start":36,"end":39,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":39},"identifierName":"for"},
"name": "for"
},
"value": {
"type": "StringLiteral",
"start":41,"end":46,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":46}},
"extra": {
"rawValue": "for",
"raw": "\"for\""
},
"value": "for"
}
}
]
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import("foo.js",);
import("foo.json", { assert: { type: "json" } },);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"plugins": [
[
"importAssertions",
{
"version": "september-2020"
}
]
],
"sourceType": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"type": "File",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}},
"program": {
"type": "Program",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"expression": {
"type": "CallExpression",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}
},
"extra": {
"trailingComma": 15
},
"arguments": [
{
"type": "StringLiteral",
"start":7,"end":15,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":15}},
"extra": {
"rawValue": "foo.js",
"raw": "\"foo.js\""
},
"value": "foo.js"
}
]
}
},
{
"type": "ExpressionStatement",
"start":19,"end":69,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":50}},
"expression": {
"type": "CallExpression",
"start":19,"end":68,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":49}},
"callee": {
"type": "Import",
"start":19,"end":25,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}}
},
"extra": {
"trailingComma": 66
},
"arguments": [
{
"type": "StringLiteral",
"start":26,"end":36,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":17}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
{
"type": "ObjectExpression",
"start":38,"end":66,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":47}},
"properties": [
{
"type": "ObjectProperty",
"start":40,"end":64,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":45}},
"method": false,
"key": {
"type": "Identifier",
"start":40,"end":46,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":27},"identifierName":"assert"},
"name": "assert"
},
"computed": false,
"shorthand": false,
"value": {
"type": "ObjectExpression",
"start":48,"end":64,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":45}},
"properties": [
{
"type": "ObjectProperty",
"start":50,"end":62,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":43}},
"method": false,
"key": {
"type": "Identifier",
"start":50,"end":54,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":35},"identifierName":"type"},
"name": "type"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":56,"end":62,"loc":{"start":{"line":2,"column":37},"end":{"line":2,"column":43}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
}
]
}
]
}
}
],
"directives": []
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
import("foo.js",);
import("foo.json", { assert: { type: "json" } },);
import foo from "foo" assert { foo: "foo", }
Original file line number Diff line number Diff line change
@@ -1,108 +1,58 @@
{
"type": "File",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}},
"start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}},
"errors": [
"SyntaxError: The only accepted module attribute is `type` (1:31)"
],
"program": {
"type": "Program",
"start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":50}},
"start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"expression": {
"type": "CallExpression",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}
},
"extra": {
"trailingComma": 15
},
"arguments": [
{
"type": "StringLiteral",
"start":7,"end":15,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":15}},
"extra": {
"rawValue": "foo.js",
"raw": "\"foo.js\""
},
"value": "foo.js"
"type": "ImportDeclaration",
"start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}},
"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"
}
]
}
},
{
"type": "ExpressionStatement",
"start":19,"end":69,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":50}},
"expression": {
"type": "CallExpression",
"start":19,"end":68,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":49}},
"callee": {
"type": "Import",
"start":19,"end":25,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}}
},
}
],
"source": {
"type": "StringLiteral",
"start":16,"end":21,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":21}},
"extra": {
"trailingComma": 66
"rawValue": "foo",
"raw": "\"foo\""
},
"arguments": [
{
"value": "foo"
},
"assertions": [
{
"type": "ImportAttribute",
"start":31,"end":41,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":41}},
"key": {
"type": "Identifier",
"start":31,"end":34,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":34},"identifierName":"foo"},
"name": "foo"
},
"value": {
"type": "StringLiteral",
"start":26,"end":36,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":17}},
"start":36,"end":41,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":41}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo.json"
},
{
"type": "ObjectExpression",
"start":38,"end":66,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":47}},
"properties": [
{
"type": "ObjectProperty",
"start":40,"end":64,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":45}},
"method": false,
"key": {
"type": "Identifier",
"start":40,"end":46,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":27},"identifierName":"assert"},
"name": "assert"
},
"computed": false,
"shorthand": false,
"value": {
"type": "ObjectExpression",
"start":48,"end":64,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":45}},
"properties": [
{
"type": "ObjectProperty",
"start":50,"end":62,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":43}},
"method": false,
"key": {
"type": "Identifier",
"start":50,"end":54,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":35},"identifierName":"type"},
"name": "type"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":56,"end":62,"loc":{"start":{"line":2,"column":37},"end":{"line":2,"column":43}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
}
]
"value": "foo"
}
]
}
}
]
}
],
"directives": []
Expand Down

0 comments on commit b4175f7

Please sign in to comment.