Parse import-assertions (#12139)

Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Sven Sauleau
2020-10-14 20:18:16 +01:00
committed by GitHub
parent 59d97d9bca
commit af8e0facc1
71 changed files with 1199 additions and 112 deletions

View File

@@ -1303,7 +1303,7 @@ interface ImportDeclaration <: ModuleDeclaration {
importKind: null | "type" | "typeof" | "value";
specifiers: [ ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier ];
source: StringLiteral;
attributes?: [ ImportAttribute ];
assertions?: [ ImportAttribute ];
}
```

View File

@@ -829,13 +829,18 @@ export default class ExpressionParser extends LValParser {
): N.Expression {
if (node.callee.type === "Import") {
if (node.arguments.length === 2) {
this.expectPlugin("moduleAttributes");
// todo(Babel 8): remove the if condition,
// moduleAttributes is renamed to importAssertions
if (!this.hasPlugin("moduleAttributes")) {
this.expectPlugin("importAssertions");
}
}
if (node.arguments.length === 0 || node.arguments.length > 2) {
this.raise(
node.start,
Errors.ImportCallArity,
this.hasPlugin("moduleAttributes")
this.hasPlugin("importAssertions") ||
this.hasPlugin("moduleAttributes")
? "one or two arguments"
: "one argument",
);
@@ -872,7 +877,11 @@ export default class ExpressionParser extends LValParser {
} else {
this.expect(tt.comma);
if (this.match(close)) {
if (dynamicImport && !this.hasPlugin("moduleAttributes")) {
if (
dynamicImport &&
!this.hasPlugin("importAssertions") &&
!this.hasPlugin("moduleAttributes")
) {
this.raise(
this.state.lastTokStart,
Errors.ImportCallArgumentTrailingComma,

View File

@@ -2144,12 +2144,21 @@ export default class StatementParser extends ExpressionParser {
this.expectContextual("from");
}
node.source = this.parseImportSource();
// https://github.com/tc39/proposal-module-attributes
// parse module attributes if the next token is `with` or ignore and finish the ImportDeclaration node.
const attributes = this.maybeParseModuleAttributes();
if (attributes) {
node.attributes = attributes;
// https://github.com/tc39/proposal-import-assertions
// parse module import assertions if the next token is `assert` or ignore
// and finish the ImportDeclaration node.
const assertions = this.maybeParseImportAssertions();
if (assertions) {
node.assertions = assertions;
}
// todo(Babel 8): remove module attributes support
else {
const attributes = this.maybeParseModuleAttributes();
if (attributes) {
node.attributes = attributes;
}
}
this.semicolon();
return this.finishNode(node, "ImportDeclaration");
}
@@ -2180,6 +2189,69 @@ export default class StatementParser extends ExpressionParser {
node.specifiers.push(this.finishNode(specifier, type));
}
parseAssertEntries() {
this.expectPlugin("importAssertions");
const attrs = [];
const attrNames = new Set();
do {
if (this.match(tt.braceR)) {
break;
}
const node = this.startNode();
// parse AssertionKey : IdentifierName, StringLiteral
let assertionKeyNode;
if (this.match(tt.string)) {
assertionKeyNode = this.parseLiteral(this.state.value, "StringLiteral");
} else {
assertionKeyNode = this.parseIdentifier(true);
}
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,
Errors.ModuleAttributeInvalidValue,
);
}
node.value = this.parseLiteral(this.state.value, "StringLiteral");
this.finishNode(node, "ImportAttribute");
attrs.push(node);
} while (this.eat(tt.comma));
return attrs;
}
/**
* parse module attributes
* @deprecated It will be removed in Babel 8
* @returns
* @memberof StatementParser
*/
maybeParseModuleAttributes() {
if (this.match(tt._with) && !this.hasPrecedingLineBreak()) {
this.expectPlugin("moduleAttributes");
@@ -2191,13 +2263,9 @@ export default class StatementParser extends ExpressionParser {
const attrs = [];
const attributes = new Set();
do {
// we are trying to parse a node which has the following syntax
// with type: "json"
// [with -> keyword], [type -> Identifier], [":" -> token for colon], ["json" -> StringLiteral]
const node = this.startNode();
node.key = this.parseIdentifier(true);
// for now we are only allowing `type` as the only allowed module attribute
if (node.key.name !== "type") {
this.raise(
node.key.start,
@@ -2206,9 +2274,6 @@ export default class StatementParser extends ExpressionParser {
);
}
// 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 (attributes.has(node.key.name)) {
this.raise(
node.key.start,
@@ -2218,7 +2283,6 @@ export default class StatementParser extends ExpressionParser {
}
attributes.add(node.key.name);
this.expect(tt.colon);
// check if the value set to the module attribute is a string as we only allow string literals
if (!this.match(tt.string)) {
throw this.unexpected(
this.state.start,
@@ -2233,6 +2297,26 @@ export default class StatementParser extends ExpressionParser {
return attrs;
}
maybeParseImportAssertions() {
if (
this.match(tt.name) &&
this.state.value === "assert" &&
!this.hasPrecedingLineBreak()
) {
this.expectPlugin("importAssertions");
this.next();
} else {
if (this.hasPlugin("importAssertions")) return [];
return null;
}
this.eat(tt.braceL);
const attrs = this.parseAssertEntries();
this.eat(tt.braceR);
return attrs;
}
maybeParseDefaultImportSpecifier(node: N.ImportDeclaration): boolean {
if (this.shouldParseDefaultImport(node)) {
// import defaultObj, { x, y as z } from '...'

View File

@@ -87,6 +87,11 @@ export function validatePlugins(plugins: PluginList) {
}
if (hasPlugin(plugins, "moduleAttributes")) {
if (hasPlugin(plugins, "importAssertions")) {
throw new Error(
"Cannot combine importAssertions and moduleAttributes plugins.",
);
}
const moduleAttributesVerionPluginOption = getPluginOption(
plugins,
"moduleAttributes",
@@ -100,6 +105,7 @@ export function validatePlugins(plugins: PluginList) {
);
}
}
if (
hasPlugin(plugins, "recordAndTuple") &&
!RECORD_AND_TUPLE_SYNTAX_TYPES.includes(

View File

@@ -1,5 +1,5 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:24)",
"throws": "This experimental syntax requires enabling the parser plugin: 'importAssertions' (1:24)",
"sourceType": "module",
"plugins": []
}

View File

@@ -0,0 +1 @@
import foo from "foo.json" assert { type: "json" };

View File

@@ -0,0 +1,5 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'importAssertions' (1:27)",
"sourceType": "module",
"plugins": []
}

View File

@@ -1,5 +1,3 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)",
"sourceType": "module",
"plugins": []
}
"throws": "This experimental syntax requires enabling the parser plugin: 'moduleAttributes' (1:27)"
}

View File

@@ -0,0 +1 @@
import("foo.json", { assert: { type: "json" } })

View File

@@ -0,0 +1,81 @@
{
"type": "File",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}},
"program": {
"type": "Program",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}},
"expression": {
"type": "CallExpression",
"start":0,"end":48,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":48}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}
},
"arguments": [
{
"type": "StringLiteral",
"start":7,"end":17,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":17}},
"extra": {
"rawValue": "foo.json",
"raw": "\"foo.json\""
},
"value": "foo.json"
},
{
"type": "ObjectExpression",
"start":19,"end":47,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":47}},
"properties": [
{
"type": "ObjectProperty",
"start":21,"end":45,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":45}},
"method": false,
"key": {
"type": "Identifier",
"start":21,"end":27,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":27},"identifierName":"assert"},
"name": "assert"
},
"computed": false,
"shorthand": false,
"value": {
"type": "ObjectExpression",
"start":29,"end":45,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":45}},
"properties": [
{
"type": "ObjectProperty",
"start":31,"end":43,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":43}},
"method": false,
"key": {
"type": "Identifier",
"start":31,"end":35,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":35},"identifierName":"type"},
"name": "type"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":37,"end":43,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":43}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
}
]
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,2 @@
import "x"
assert ({});

View File

@@ -0,0 +1,48 @@
{
"type": "File",
"start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}},
"program": {
"type": "Program",
"start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}},
"specifiers": [],
"source": {
"type": "StringLiteral",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}},
"extra": {
"rawValue": "x",
"raw": "\"x\""
},
"value": "x"
},
"assertions": []
},
{
"type": "ExpressionStatement",
"start":11,"end":23,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}},
"expression": {
"type": "CallExpression",
"start":11,"end":22,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":11}},
"callee": {
"type": "Identifier",
"start":11,"end":17,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6},"identifierName":"assert"},
"name": "assert"
},
"arguments": [
{
"type": "ObjectExpression",
"start":19,"end":21,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":10}},
"properties": []
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,2 @@
import();
import("./foo.json", { assert: { type: "json"} }, "unsupported");

View File

@@ -0,0 +1,107 @@
{
"type": "File",
"start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":65}},
"errors": [
"SyntaxError: import() requires exactly one or two arguments (1:0)",
"SyntaxError: import() requires exactly one or two arguments (2:0)"
],
"program": {
"type": "Program",
"start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":65}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":9}},
"expression": {
"type": "CallExpression",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}
},
"arguments": []
}
},
{
"type": "ExpressionStatement",
"start":10,"end":75,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":65}},
"expression": {
"type": "CallExpression",
"start":10,"end":74,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":64}},
"callee": {
"type": "Import",
"start":10,"end":16,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":6}}
},
"arguments": [
{
"type": "StringLiteral",
"start":17,"end":29,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":19}},
"extra": {
"rawValue": "./foo.json",
"raw": "\"./foo.json\""
},
"value": "./foo.json"
},
{
"type": "ObjectExpression",
"start":31,"end":58,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":48}},
"properties": [
{
"type": "ObjectProperty",
"start":33,"end":56,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":46}},
"method": false,
"key": {
"type": "Identifier",
"start":33,"end":39,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":29},"identifierName":"assert"},
"name": "assert"
},
"computed": false,
"shorthand": false,
"value": {
"type": "ObjectExpression",
"start":41,"end":56,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":46}},
"properties": [
{
"type": "ObjectProperty",
"start":43,"end":55,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":45}},
"method": false,
"key": {
"type": "Identifier",
"start":43,"end":47,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":37},"identifierName":"type"},
"name": "type"
},
"computed": false,
"shorthand": false,
"value": {
"type": "StringLiteral",
"start":49,"end":55,"loc":{"start":{"line":2,"column":39},"end":{"line":2,"column":45}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
}
]
},
{
"type": "StringLiteral",
"start":60,"end":73,"loc":{"start":{"line":2,"column":50},"end":{"line":2,"column":63}},
"extra": {
"rawValue": "unsupported",
"raw": "\"unsupported\""
},
"value": "unsupported"
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
import("./foo.json", ...[]);

View File

@@ -0,0 +1,48 @@
{
"type": "File",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"errors": [
"SyntaxError: ... is not allowed in import() (1:21)"
],
"program": {
"type": "Program",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":28,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":28}},
"expression": {
"type": "CallExpression",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"callee": {
"type": "Import",
"start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}}
},
"arguments": [
{
"type": "StringLiteral",
"start":7,"end":19,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":19}},
"extra": {
"rawValue": "./foo.json",
"raw": "\"./foo.json\""
},
"value": "./foo.json"
},
{
"type": "SpreadElement",
"start":21,"end":26,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":26}},
"argument": {
"type": "ArrayExpression",
"start":24,"end":26,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":26}},
"elements": []
}
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1,2 @@
import "x" assert
{ type: "json" }

View File

@@ -0,0 +1,47 @@
{
"type": "File",
"start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}},
"program": {
"type": "Program",
"start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}},
"specifiers": [],
"source": {
"type": "StringLiteral",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}},
"extra": {
"rawValue": "x",
"raw": "\"x\""
},
"value": "x"
},
"assertions": [
{
"type": "ImportAttribute",
"start":20,"end":32,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":14}},
"key": {
"type": "Identifier",
"start":20,"end":24,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":6},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":26,"end":32,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":14}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
],
"directives": []
}
}

View File

@@ -0,0 +1,9 @@
{
"plugins": [
[
"importAssertions"
]
],
"sourceType": "module",
"throws": "Unexpected token (1:17)"
}

View File

@@ -0,0 +1,8 @@
{
"plugins": [
[
"importAssertions"
]
],
"sourceType": "module"
}

View File

@@ -0,0 +1 @@
import foo from "foo.json" assert { for: "for" }

View File

@@ -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": []
}
}

View File

@@ -0,0 +1,2 @@
import("foo.js",);
import("foo.json", { assert: { type: "json" } },);

View File

@@ -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": []
}
}

View File

@@ -0,0 +1 @@
import foo from "foo" assert { type: "json", }

View File

@@ -0,0 +1,57 @@
{
"type": "File",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}},
"program": {
"type": "Program",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":46}},
"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":21,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":21}},
"extra": {
"rawValue": "foo",
"raw": "\"foo\""
},
"value": "foo"
},
"assertions": [
{
"type": "ImportAttribute",
"start":31,"end":43,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":43}},
"key": {
"type": "Identifier",
"start":31,"end":35,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":35},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":37,"end":43,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":43}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
],
"directives": []
}
}

View File

@@ -0,0 +1,2 @@
import "x" assert { type: "json" }
[0]

View File

@@ -0,0 +1,66 @@
{
"type": "File",
"start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}},
"program": {
"type": "Program",
"start":0,"end":38,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":3}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":34,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":34}},
"specifiers": [],
"source": {
"type": "StringLiteral",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10}},
"extra": {
"rawValue": "x",
"raw": "\"x\""
},
"value": "x"
},
"assertions": [
{
"type": "ImportAttribute",
"start":20,"end":32,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":32}},
"key": {
"type": "Identifier",
"start":20,"end":24,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":24},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":26,"end":32,"loc":{"start":{"line":1,"column":26},"end":{"line":1,"column":32}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
},
{
"type": "ExpressionStatement",
"start":35,"end":38,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}},
"expression": {
"type": "ArrayExpression",
"start":35,"end":38,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}},
"elements": [
{
"type": "NumericLiteral",
"start":36,"end":37,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2}},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
]
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
import foo from "foo.json" assert { type: "json" };

View File

@@ -0,0 +1,57 @@
{
"type": "File",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}},
"program": {
"type": "Program",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}},
"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":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}},
"key": {
"type": "Identifier",
"start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
}
]
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
import foo from "foo.json" assert { type: "json", lazy: true, startAtLine: 1 };

View File

@@ -0,0 +1,9 @@
{
"plugins": [
[
"importAssertions"
]
],
"sourceType": "module",
"throws": "Only string literals are allowed as module attribute values (1:56)"
}

View File

@@ -0,0 +1 @@
import foo from "foo.json" assert { lazy: "true" };

View File

@@ -0,0 +1,60 @@
{
"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}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":51,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":51}},
"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":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}},
"key": {
"type": "Identifier",
"start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"lazy"},
"name": "lazy"
},
"value": {
"type": "StringLiteral",
"start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}},
"extra": {
"rawValue": "true",
"raw": "\"true\""
},
"value": "true"
}
}
]
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
import foo from "foo.json" assert { type: "json", hasOwnProperty: "true" };

View File

@@ -0,0 +1,78 @@
{
"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}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":75}},
"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":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}},
"key": {
"type": "Identifier",
"start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
},
{
"type": "ImportAttribute",
"start":50,"end":72,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":72}},
"key": {
"type": "Identifier",
"start":50,"end":64,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":64},"identifierName":"hasOwnProperty"},
"name": "hasOwnProperty"
},
"value": {
"type": "StringLiteral",
"start":66,"end":72,"loc":{"start":{"line":1,"column":66},"end":{"line":1,"column":72}},
"extra": {
"rawValue": "true",
"raw": "\"true\""
},
"value": "true"
}
}
]
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
import foo from "foo.json" assert { type: "json", type: "html" };

View File

@@ -0,0 +1,78 @@
{
"type": "File",
"start":0,"end":65,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":65}},
"errors": [
"SyntaxError: Duplicate key \"type\" is not allowed in module attributes (1:50)"
],
"program": {
"type": "Program",
"start":0,"end":65,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":65}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":65,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":65}},
"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":48,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":48}},
"key": {
"type": "Identifier",
"start":36,"end":40,"loc":{"start":{"line":1,"column":36},"end":{"line":1,"column":40},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":42,"end":48,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":48}},
"extra": {
"rawValue": "json",
"raw": "\"json\""
},
"value": "json"
}
},
{
"type": "ImportAttribute",
"start":50,"end":62,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":62}},
"key": {
"type": "Identifier",
"start":50,"end":54,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":54},"identifierName":"type"},
"name": "type"
},
"value": {
"type": "StringLiteral",
"start":56,"end":62,"loc":{"start":{"line":1,"column":56},"end":{"line":1,"column":62}},
"extra": {
"rawValue": "html",
"raw": "\"html\""
},
"value": "html"
}
}
]
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
import foo from "foo.json";

View File

@@ -0,0 +1,38 @@
{
"type": "File",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"program": {
"type": "Program",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ImportDeclaration",
"start":0,"end":27,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":27}},
"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": []
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
import foo from "foo.json" assert { type: "json" };

View File

@@ -0,0 +1,4 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'importAssertions' (1:27)",
"plugins": []
}

View File

@@ -116,7 +116,7 @@ export type ParserPlugin =
'importMeta' |
'jsx' |
'logicalAssignment' |
'moduleAttributes' |
'importAssertions' |
'moduleStringNames' |
'nullishCoalescingOperator' |
'numericSeparator' |