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:
parent
59d97d9bca
commit
af8e0facc1
@ -139,10 +139,10 @@ const pluginNameMap = {
|
||||
url: "https://git.io/JfeDR",
|
||||
},
|
||||
},
|
||||
moduleAttributes: {
|
||||
importAssertions: {
|
||||
syntax: {
|
||||
name: "@babel/plugin-syntax-module-attributes",
|
||||
url: "https://git.io/JfK3k",
|
||||
name: "@babel/plugin-syntax-import-assertions",
|
||||
url: "https://git.io/JUbkv",
|
||||
},
|
||||
},
|
||||
moduleStringNames: {
|
||||
|
||||
@ -180,7 +180,19 @@ export function ImportDeclaration(node: Object) {
|
||||
|
||||
this.print(node.source, node);
|
||||
|
||||
if (node.attributes?.length) {
|
||||
if (node.assertions?.length) {
|
||||
this.space();
|
||||
this.word("assert");
|
||||
this.space();
|
||||
this.token("{");
|
||||
this.space();
|
||||
this.printList(node.assertions, node);
|
||||
this.space();
|
||||
this.token("}");
|
||||
}
|
||||
// todo(Babel 8): remove this if branch
|
||||
// `module-attributes` support is discontinued, use `import-assertions` instead.
|
||||
else if (node.attributes?.length) {
|
||||
this.space();
|
||||
this.word("with");
|
||||
this.space();
|
||||
|
||||
1
packages/babel-generator/test/fixtures/types/ImportAssertion/input.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/types/ImportAssertion/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import foo from "foo.json" assert { type: "json" };
|
||||
8
packages/babel-generator/test/fixtures/types/ImportAssertion/options.json
vendored
Normal file
8
packages/babel-generator/test/fixtures/types/ImportAssertion/options.json
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"plugins": [
|
||||
[
|
||||
"importAssertions"
|
||||
]
|
||||
],
|
||||
"sourceType": "module"
|
||||
}
|
||||
1
packages/babel-generator/test/fixtures/types/ImportAssertion/output.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/types/ImportAssertion/output.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import foo from "foo.json" assert { type: "json" };
|
||||
@ -1 +0,0 @@
|
||||
import foo from "foo.json" with type: "json";
|
||||
@ -1,11 +0,0 @@
|
||||
{
|
||||
"plugins": [
|
||||
[
|
||||
"moduleAttributes",
|
||||
{
|
||||
"version": "may-2020"
|
||||
}
|
||||
]
|
||||
],
|
||||
"sourceType": "module"
|
||||
}
|
||||
@ -1 +0,0 @@
|
||||
import foo from "foo.json" with type: "json";
|
||||
@ -1 +1,3 @@
|
||||
import "foo.json" with type: "json";
|
||||
import("foo.json", { with: { type: "json" } },);
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import "foo.json" with type: "json";
|
||||
import("foo.json", {
|
||||
with: {
|
||||
type: "json"
|
||||
@ -1303,7 +1303,7 @@ interface ImportDeclaration <: ModuleDeclaration {
|
||||
importKind: null | "type" | "typeof" | "value";
|
||||
specifiers: [ ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier ];
|
||||
source: StringLiteral;
|
||||
attributes?: [ ImportAttribute ];
|
||||
assertions?: [ ImportAttribute ];
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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 '...'
|
||||
|
||||
@ -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(
|
||||
|
||||
@ -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": []
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import foo from "foo.json" assert { type: "json" };
|
||||
5
packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/options.json
vendored
Normal file
5
packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions/options.json
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"throws": "This experimental syntax requires enabling the parser plugin: 'importAssertions' (1:27)",
|
||||
"sourceType": "module",
|
||||
"plugins": []
|
||||
}
|
||||
@ -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)"
|
||||
}
|
||||
|
||||
@ -0,0 +1 @@
|
||||
import("foo.json", { assert: { type: "json" } })
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,2 @@
|
||||
import "x"
|
||||
assert ({});
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
2
packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
import();
|
||||
import("./foo.json", { assert: { type: "json"} }, "unsupported");
|
||||
107
packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/output.json
vendored
Normal file
107
packages/babel-parser/test/fixtures/experimental/import-assertions/incorrect-arity/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
import("./foo.json", ...[]);
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,2 @@
|
||||
import "x" assert
|
||||
{ type: "json" }
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
import "x" assert;
|
||||
@ -0,0 +1,9 @@
|
||||
{
|
||||
"plugins": [
|
||||
[
|
||||
"importAssertions"
|
||||
]
|
||||
],
|
||||
"sourceType": "module",
|
||||
"throws": "Unexpected token (1:17)"
|
||||
}
|
||||
8
packages/babel-parser/test/fixtures/experimental/import-assertions/options.json
vendored
Normal file
8
packages/babel-parser/test/fixtures/experimental/import-assertions/options.json
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"plugins": [
|
||||
[
|
||||
"importAssertions"
|
||||
]
|
||||
],
|
||||
"sourceType": "module"
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import foo from "foo.json" assert { for: "for" }
|
||||
60
packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json
vendored
Normal file
60
packages/babel-parser/test/fixtures/experimental/import-assertions/string-literal/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,2 @@
|
||||
import("foo.js",);
|
||||
import("foo.json", { assert: { type: "json" } },);
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import foo from "foo" assert { type: "json", }
|
||||
57
packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/output.json
vendored
Normal file
57
packages/babel-parser/test/fixtures/experimental/import-assertions/trailing-comma/output.json
vendored
Normal 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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,2 @@
|
||||
import "x" assert { type: "json" }
|
||||
[0]
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
import foo from "foo.json" assert { type: "json" };
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
import foo from "foo.json" assert { type: "json", lazy: true, startAtLine: 1 };
|
||||
@ -0,0 +1,9 @@
|
||||
{
|
||||
"plugins": [
|
||||
[
|
||||
"importAssertions"
|
||||
]
|
||||
],
|
||||
"sourceType": "module",
|
||||
"throws": "Only string literals are allowed as module attribute values (1:56)"
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
import foo from "foo.json" assert { lazy: "true" };
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
import foo from "foo.json" assert { type: "json", hasOwnProperty: "true" };
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
import foo from "foo.json" assert { type: "json", type: "html" };
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
import foo from "foo.json";
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/experimental/import-assertions/without-plugin/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
import foo from "foo.json" assert { type: "json" };
|
||||
@ -0,0 +1,4 @@
|
||||
{
|
||||
"throws": "This experimental syntax requires enabling the parser plugin: 'importAssertions' (1:27)",
|
||||
"plugins": []
|
||||
}
|
||||
@ -116,7 +116,7 @@ export type ParserPlugin =
|
||||
'importMeta' |
|
||||
'jsx' |
|
||||
'logicalAssignment' |
|
||||
'moduleAttributes' |
|
||||
'importAssertions' |
|
||||
'moduleStringNames' |
|
||||
'nullishCoalescingOperator' |
|
||||
'numericSeparator' |
|
||||
|
||||
19
packages/babel-plugin-syntax-import-assertions/README.md
Normal file
19
packages/babel-plugin-syntax-import-assertions/README.md
Normal file
@ -0,0 +1,19 @@
|
||||
# @babel/plugin-syntax-import-assertions
|
||||
|
||||
> Allow parsing of the module assertion attributes in the import statements
|
||||
|
||||
See our website [@babel/plugin-syntax-import-assertions](https://babeljs.io/docs/en/next/babel-plugin-syntax-import-assertions.html) for more information.
|
||||
|
||||
## Install
|
||||
|
||||
Using npm:
|
||||
|
||||
```sh
|
||||
npm install --save-dev @babel/plugin-syntax-import-assertions
|
||||
```
|
||||
|
||||
or using yarn:
|
||||
|
||||
```sh
|
||||
yarn add @babel/plugin-syntax-import-assertions --dev
|
||||
```
|
||||
@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "@babel/plugin-syntax-module-attributes",
|
||||
"name": "@babel/plugin-syntax-import-assertions",
|
||||
"version": "7.10.4",
|
||||
"description": "Allow parsing of the module attributes in the import statement",
|
||||
"description": "Allow parsing of the module assertion attributes in the import statement",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/babel/babel.git",
|
||||
"directory": "packages/babel-plugin-syntax-module-attributes"
|
||||
"directory": "packages/babel-plugin-syntax-import-assertions"
|
||||
},
|
||||
"license": "MIT",
|
||||
"publishConfig": {
|
||||
13
packages/babel-plugin-syntax-import-assertions/src/index.js
Normal file
13
packages/babel-plugin-syntax-import-assertions/src/index.js
Normal file
@ -0,0 +1,13 @@
|
||||
import { declare } from "@babel/helper-plugin-utils";
|
||||
|
||||
export default declare(api => {
|
||||
api.assertVersion(7);
|
||||
|
||||
return {
|
||||
name: "syntax-import-assertions",
|
||||
|
||||
manipulateOptions(opts, parserOpts) {
|
||||
parserOpts.plugins.push(["importAssertions"]);
|
||||
},
|
||||
};
|
||||
});
|
||||
@ -1,3 +0,0 @@
|
||||
*.log
|
||||
src
|
||||
test
|
||||
@ -1,19 +0,0 @@
|
||||
# @babel/plugin-syntax-module-attributes
|
||||
|
||||
> Allow parsing of the module attributes in the import statements
|
||||
|
||||
See our website [@babel/plugin-syntax-module-attributes](https://babeljs.io/docs/en/next/babel-plugin-syntax-module-attributes.html) for more information.
|
||||
|
||||
## Install
|
||||
|
||||
Using npm:
|
||||
|
||||
```sh
|
||||
npm install --save-dev @babel/plugin-syntax-module-attributes
|
||||
```
|
||||
|
||||
or using yarn:
|
||||
|
||||
```sh
|
||||
yarn add @babel/plugin-syntax-module-attributes --dev
|
||||
```
|
||||
@ -1,21 +0,0 @@
|
||||
import { declare } from "@babel/helper-plugin-utils";
|
||||
|
||||
export default declare((api, { version }) => {
|
||||
api.assertVersion(7);
|
||||
|
||||
if (typeof version !== "string" || version !== "may-2020") {
|
||||
throw new Error(
|
||||
"The 'moduleAttributes' plugin requires a 'version' option," +
|
||||
" representing the last proposal update. Currently, the" +
|
||||
" only supported value is 'may-2020'.",
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
name: "syntax-module-attributes",
|
||||
|
||||
manipulateOptions(opts, parserOpts) {
|
||||
parserOpts.plugins.push(["moduleAttributes", { version }]);
|
||||
},
|
||||
};
|
||||
});
|
||||
@ -43,9 +43,9 @@
|
||||
"@babel/plugin-syntax-flow": "workspace:^7.10.4",
|
||||
"@babel/plugin-syntax-function-bind": "workspace:^7.10.4",
|
||||
"@babel/plugin-syntax-function-sent": "workspace:^7.10.4",
|
||||
"@babel/plugin-syntax-import-assertions": "workspace:^7.10.4",
|
||||
"@babel/plugin-syntax-import-meta": "^7.10.4",
|
||||
"@babel/plugin-syntax-jsx": "workspace:^7.10.4",
|
||||
"@babel/plugin-syntax-module-attributes": "workspace:^7.10.4",
|
||||
"@babel/plugin-syntax-object-rest-spread": "^7.8.0",
|
||||
"@babel/plugin-syntax-optional-catch-binding": "^7.8.0",
|
||||
"@babel/plugin-syntax-pipeline-operator": "workspace:^7.10.4",
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
"syntax-function-sent",
|
||||
"syntax-import-meta",
|
||||
"syntax-jsx",
|
||||
"syntax-module-attributes",
|
||||
"syntax-import-assertions",
|
||||
"syntax-object-rest-spread",
|
||||
"syntax-optional-catch-binding",
|
||||
"syntax-pipeline-operator",
|
||||
|
||||
@ -15,7 +15,7 @@ import syntaxFunctionBind from "@babel/plugin-syntax-function-bind";
|
||||
import syntaxFunctionSent from "@babel/plugin-syntax-function-sent";
|
||||
import syntaxImportMeta from "@babel/plugin-syntax-import-meta";
|
||||
import syntaxJsx from "@babel/plugin-syntax-jsx";
|
||||
import syntaxModuleAttributes from "@babel/plugin-syntax-module-attributes";
|
||||
import syntaxImportAssertions from "@babel/plugin-syntax-import-assertions";
|
||||
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread";
|
||||
import syntaxOptionalCatchBinding from "@babel/plugin-syntax-optional-catch-binding";
|
||||
import syntaxPipelineOperator from "@babel/plugin-syntax-pipeline-operator";
|
||||
@ -110,7 +110,7 @@ export {
|
||||
syntaxFunctionSent,
|
||||
syntaxImportMeta,
|
||||
syntaxJsx,
|
||||
syntaxModuleAttributes,
|
||||
syntaxImportAssertions,
|
||||
syntaxObjectRestSpread,
|
||||
syntaxOptionalCatchBinding,
|
||||
syntaxPipelineOperator,
|
||||
@ -206,7 +206,7 @@ export const all = {
|
||||
"syntax-function-sent": syntaxFunctionSent,
|
||||
"syntax-import-meta": syntaxImportMeta,
|
||||
"syntax-jsx": syntaxJsx,
|
||||
"syntax-module-attributes": syntaxModuleAttributes,
|
||||
"syntax-import-assertions": syntaxImportAssertions,
|
||||
"syntax-object-rest-spread": syntaxObjectRestSpread,
|
||||
"syntax-optional-catch-binding": syntaxOptionalCatchBinding,
|
||||
"syntax-pipeline-operator": syntaxPipelineOperator,
|
||||
|
||||
@ -9,7 +9,7 @@ export default (_: any, opts: Object = {}) => {
|
||||
decoratorsLegacy = false,
|
||||
decoratorsBeforeExport,
|
||||
pipelineProposal = "minimal",
|
||||
moduleAttributesVersion = "may-2020",
|
||||
importAssertionsVersion = "september-2020",
|
||||
} = opts;
|
||||
|
||||
return {
|
||||
@ -22,7 +22,7 @@ export default (_: any, opts: Object = {}) => {
|
||||
decoratorsLegacy,
|
||||
decoratorsBeforeExport,
|
||||
pipelineProposal,
|
||||
moduleAttributesVersion,
|
||||
importAssertionsVersion,
|
||||
},
|
||||
],
|
||||
],
|
||||
|
||||
@ -10,7 +10,6 @@ export default (_: any, opts: Object = {}) => {
|
||||
decoratorsBeforeExport,
|
||||
pipelineProposal = "minimal",
|
||||
recordAndTupleSyntax: recordAndTupleSyntax = "hash",
|
||||
moduleAttributesVersion = "may-2020",
|
||||
} = opts;
|
||||
|
||||
return {
|
||||
@ -22,10 +21,6 @@ export default (_: any, opts: Object = {}) => {
|
||||
],
|
||||
plugins: [
|
||||
babelPlugins.syntaxDecimal,
|
||||
[
|
||||
babelPlugins.syntaxModuleAttributes,
|
||||
{ version: moduleAttributesVersion },
|
||||
],
|
||||
[babelPlugins.syntaxRecordAndTuple, { syntaxType: recordAndTupleSyntax }],
|
||||
babelPlugins.proposalExportDefaultFrom,
|
||||
[babelPlugins.proposalPipelineOperator, { proposal: pipelineProposal }],
|
||||
|
||||
@ -10,6 +10,7 @@ export default (_: any, opts: Object) => {
|
||||
|
||||
return {
|
||||
plugins: [
|
||||
babelPlugins.syntaxImportAssertions,
|
||||
babelPlugins.syntaxImportMeta,
|
||||
babelPlugins.syntaxTopLevelAwait,
|
||||
babelPlugins.proposalExportNamespaceFrom,
|
||||
|
||||
@ -203,7 +203,7 @@
|
||||
Babel.transform("/a*/u", { presets: ["es2015"] }),
|
||||
).not.toThrow();
|
||||
});
|
||||
it("#11628 - supports stage-0 passing moduleAttributesVersion to stage-1", () => {
|
||||
it("#11628 - supports stage-0 passing importAssertionsVersion to stage-1", () => {
|
||||
expect(() =>
|
||||
Babel.transform("const getMessage = () => 'Hello World'", {
|
||||
presets: [["stage-0", { decoratorsBeforeExport: false }]],
|
||||
|
||||
@ -162,7 +162,7 @@ defineType("ImportAttribute", {
|
||||
visitor: ["key", "value"],
|
||||
fields: {
|
||||
key: {
|
||||
validate: assertNodeType("Identifier"),
|
||||
validate: assertNodeType("Identifier", "StringLiteral"),
|
||||
},
|
||||
value: {
|
||||
validate: assertNodeType("StringLiteral"),
|
||||
|
||||
24
yarn.lock
24
yarn.lock
@ -1564,6 +1564,17 @@ __metadata:
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@babel/plugin-syntax-import-assertions@workspace:^7.10.4, @babel/plugin-syntax-import-assertions@workspace:packages/babel-plugin-syntax-import-assertions":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@babel/plugin-syntax-import-assertions@workspace:packages/babel-plugin-syntax-import-assertions"
|
||||
dependencies:
|
||||
"@babel/core": "workspace:^7.10.4"
|
||||
"@babel/helper-plugin-utils": "workspace:^7.10.4"
|
||||
peerDependencies:
|
||||
"@babel/core": ^7.0.0-0
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@babel/plugin-syntax-import-meta@npm:^7.10.4":
|
||||
version: 7.10.4
|
||||
resolution: "@babel/plugin-syntax-import-meta@npm:7.10.4"
|
||||
@ -1608,17 +1619,6 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@babel/plugin-syntax-module-attributes@workspace:^7.10.4, @babel/plugin-syntax-module-attributes@workspace:packages/babel-plugin-syntax-module-attributes":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@babel/plugin-syntax-module-attributes@workspace:packages/babel-plugin-syntax-module-attributes"
|
||||
dependencies:
|
||||
"@babel/core": "workspace:^7.10.4"
|
||||
"@babel/helper-plugin-utils": "workspace:^7.10.4"
|
||||
peerDependencies:
|
||||
"@babel/core": ^7.0.0-0
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
"@babel/plugin-syntax-module-string-names@workspace:packages/babel-plugin-syntax-module-string-names":
|
||||
version: 0.0.0-use.local
|
||||
resolution: "@babel/plugin-syntax-module-string-names@workspace:packages/babel-plugin-syntax-module-string-names"
|
||||
@ -3219,9 +3219,9 @@ __metadata:
|
||||
"@babel/plugin-syntax-flow": "workspace:^7.10.4"
|
||||
"@babel/plugin-syntax-function-bind": "workspace:^7.10.4"
|
||||
"@babel/plugin-syntax-function-sent": "workspace:^7.10.4"
|
||||
"@babel/plugin-syntax-import-assertions": "workspace:^7.10.4"
|
||||
"@babel/plugin-syntax-import-meta": ^7.10.4
|
||||
"@babel/plugin-syntax-jsx": "workspace:^7.10.4"
|
||||
"@babel/plugin-syntax-module-attributes": "workspace:^7.10.4"
|
||||
"@babel/plugin-syntax-object-rest-spread": ^7.8.0
|
||||
"@babel/plugin-syntax-optional-catch-binding": ^7.8.0
|
||||
"@babel/plugin-syntax-pipeline-operator": "workspace:^7.10.4"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user