diff --git a/packages/babel-generator/src/generators/modules.ts b/packages/babel-generator/src/generators/modules.ts index e5f9b6816629..8029c04ae9b9 100644 --- a/packages/babel-generator/src/generators/modules.ts +++ b/packages/babel-generator/src/generators/modules.ts @@ -312,3 +312,15 @@ export function ImportNamespaceSpecifier( this.space(); this.print(node.local, node); } + +export function ImportExpression(this: Printer, node: t.ImportExpression) { + this.word("import"); + this.token("("); + this.print(node.source, node); + if (node.options != null) { + this.token(","); + this.space(); + this.print(node.options, node); + } + this.token(")"); +} diff --git a/packages/babel-generator/test/fixtures/comments/dynamic-import/input.js b/packages/babel-generator/test/fixtures/comments/dynamic-import/input.js new file mode 100644 index 000000000000..3973efbaca3c --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/dynamic-import/input.js @@ -0,0 +1 @@ +/*0*/import/*1*/(/*2*/"foo"/*3*/)/*4*/ diff --git a/packages/babel-generator/test/fixtures/comments/dynamic-import/options.json b/packages/babel-generator/test/fixtures/comments/dynamic-import/options.json new file mode 100644 index 000000000000..8528adf5b60a --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/dynamic-import/options.json @@ -0,0 +1,5 @@ +{ + "parserOpts": { + "createImportExpressions": true + } +} diff --git a/packages/babel-generator/test/fixtures/comments/dynamic-import/output.js b/packages/babel-generator/test/fixtures/comments/dynamic-import/output.js new file mode 100644 index 000000000000..2a68d92632f6 --- /dev/null +++ b/packages/babel-generator/test/fixtures/comments/dynamic-import/output.js @@ -0,0 +1 @@ +/*0*/import /*1*/( /*2*/"foo" /*3*/); /*4*/ \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/types/Import-createImportExpression-false/input.js b/packages/babel-generator/test/fixtures/types/Import-createImportExpression-false/input.js new file mode 100644 index 000000000000..66a063bcb484 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/Import-createImportExpression-false/input.js @@ -0,0 +1 @@ +import("module.js"); diff --git a/packages/babel-generator/test/fixtures/types/Import-createImportExpression-false/options.json b/packages/babel-generator/test/fixtures/types/Import-createImportExpression-false/options.json new file mode 100644 index 000000000000..2878ef3521e9 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/Import-createImportExpression-false/options.json @@ -0,0 +1 @@ +{ "parserOpts": { "createImportExpressions": false } } diff --git a/packages/babel-generator/test/fixtures/types/Import-createImportExpression-false/output.js b/packages/babel-generator/test/fixtures/types/Import-createImportExpression-false/output.js new file mode 100644 index 000000000000..31d3809c37ab --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/Import-createImportExpression-false/output.js @@ -0,0 +1 @@ +import("module.js"); \ No newline at end of file diff --git a/packages/babel-generator/test/fixtures/types/Import/options.json b/packages/babel-generator/test/fixtures/types/Import/options.json index 5390d4676593..351d49f98c58 100644 --- a/packages/babel-generator/test/fixtures/types/Import/options.json +++ b/packages/babel-generator/test/fixtures/types/Import/options.json @@ -1 +1 @@ -{ "plugins": ["dynamicImport"] } +{ "parserOpts": { "createImportExpressions": true } } diff --git a/packages/babel-generator/test/fixtures/types/ImportAttribute-createImportExpression-false/input.js b/packages/babel-generator/test/fixtures/types/ImportAttribute-createImportExpression-false/input.js new file mode 100644 index 000000000000..0c39ec9fc4de --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/ImportAttribute-createImportExpression-false/input.js @@ -0,0 +1 @@ +import("foo.json", { with: { type: "json" } }); diff --git a/packages/babel-generator/test/fixtures/types/ImportAttribute-createImportExpression-false/options.json b/packages/babel-generator/test/fixtures/types/ImportAttribute-createImportExpression-false/options.json new file mode 100644 index 000000000000..c2f3ad7a7e7a --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/ImportAttribute-createImportExpression-false/options.json @@ -0,0 +1,5 @@ +{ + "plugins": [["importAttributes", { "importAttributesKeyword": "with" }]], + "sourceType": "module", + "parserOpts": { "createImportExpressions": false } +} diff --git a/packages/babel-generator/test/fixtures/types/ImportAttribute-createImportExpression-false/output.js b/packages/babel-generator/test/fixtures/types/ImportAttribute-createImportExpression-false/output.js new file mode 100644 index 000000000000..bee81287dbea --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/ImportAttribute-createImportExpression-false/output.js @@ -0,0 +1,5 @@ +import("foo.json", { + with: { + type: "json" + } +}); diff --git a/packages/babel-generator/test/fixtures/types/ImportAttribute/input.js b/packages/babel-generator/test/fixtures/types/ImportAttribute/input.js new file mode 100644 index 000000000000..0c39ec9fc4de --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/ImportAttribute/input.js @@ -0,0 +1 @@ +import("foo.json", { with: { type: "json" } }); diff --git a/packages/babel-generator/test/fixtures/types/ImportAttribute/options.json b/packages/babel-generator/test/fixtures/types/ImportAttribute/options.json new file mode 100644 index 000000000000..3a0dd86df8a8 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/ImportAttribute/options.json @@ -0,0 +1,5 @@ +{ + "plugins": [["importAttributes", { "importAttributesKeyword": "with" }]], + "sourceType": "module", + "parserOpts": { "createImportExpressions": true } +} diff --git a/packages/babel-generator/test/fixtures/types/ImportAttribute/output.js b/packages/babel-generator/test/fixtures/types/ImportAttribute/output.js new file mode 100644 index 000000000000..be5a0c0d7d78 --- /dev/null +++ b/packages/babel-generator/test/fixtures/types/ImportAttribute/output.js @@ -0,0 +1,5 @@ +import("foo.json", { + with: { + type: "json" + } +}); \ No newline at end of file diff --git a/packages/babel-helper-module-transforms/src/dynamic-import.ts b/packages/babel-helper-module-transforms/src/dynamic-import.ts index d7ed02be7f09..42996e287ca8 100644 --- a/packages/babel-helper-module-transforms/src/dynamic-import.ts +++ b/packages/babel-helper-module-transforms/src/dynamic-import.ts @@ -21,12 +21,12 @@ if (!process.env.BABEL_8_BREAKING) { } export function buildDynamicImport( - node: t.CallExpression, + node: t.CallExpression | t.ImportExpression, deferToThen: boolean, wrapWithPromise: boolean, builder: (specifier: t.Expression) => t.Expression, ): t.Expression { - const [specifier] = node.arguments; + const specifier = t.isCallExpression(node) ? node.arguments[0] : node.source; if ( t.isStringLiteral(specifier) || diff --git a/packages/babel-helper-module-transforms/src/index.ts b/packages/babel-helper-module-transforms/src/index.ts index 898bba741b0b..f3a5928122ff 100644 --- a/packages/babel-helper-module-transforms/src/index.ts +++ b/packages/babel-helper-module-transforms/src/index.ts @@ -169,7 +169,7 @@ export function ensureStatementsHoisted(statements: t.Statement[]) { * wrap it in a call to the interop helpers based on the type. */ export function wrapInterop( - programPath: NodePath, + programPath: NodePath, expr: t.Expression, type: InteropType, ): t.CallExpression { diff --git a/packages/babel-parser/data/schema.json b/packages/babel-parser/data/schema.json index 370b14e0ac54..25732221a273 100644 --- a/packages/babel-parser/data/schema.json +++ b/packages/babel-parser/data/schema.json @@ -68,6 +68,11 @@ "description": "By default, exported identifiers must refer to a declared variable.\nSet this to true to allow export statements to reference undeclared variables.", "type": "boolean" }, + "createImportExpressions": { + "description": "By default, `import(foo)` is parsed as `CallExpression(Import, [Identifier(foo)])`.\nSet this to true to parse it as an `ImportExpression` node.", + "type": "boolean", + "default": false + }, "createParenthesizedExpressions": { "description": "By default, the parser adds information about parentheses by setting\n`extra.parenthesized` to `true` as needed.\nWhen this option is `true` the parser creates `ParenthesizedExpression`\nAST nodes instead of using the `extra` property.", "type": "boolean" diff --git a/packages/babel-parser/src/options.ts b/packages/babel-parser/src/options.ts index ee77b8822e5d..230a7014304d 100644 --- a/packages/babel-parser/src/options.ts +++ b/packages/babel-parser/src/options.ts @@ -20,6 +20,7 @@ export type Options = { strictMode: boolean | undefined | null; ranges: boolean; tokens: boolean; + createImportExpressions: boolean; createParenthesizedExpressions: boolean; errorRecovery: boolean; attachComment: boolean; @@ -68,6 +69,9 @@ export const defaultOptions: Options = { ranges: false, // Adds all parsed tokens to a `tokens` property on the `File` node tokens: false, + // Whether to create ImportExpression AST nodes (if false + // `import(foo)` will be parsed as CallExpression(Import, [Identifier(foo)]) + createImportExpressions: false, // Whether to create ParenthesizedExpression AST nodes (if false // the parser sets extra.parenthesized on the expression nodes instead). createParenthesizedExpressions: false, diff --git a/packages/babel-parser/src/parser/expression.ts b/packages/babel-parser/src/parser/expression.ts index 8301791d04b0..0d073646b729 100644 --- a/packages/babel-parser/src/parser/expression.ts +++ b/packages/babel-parser/src/parser/expression.ts @@ -1110,19 +1110,26 @@ export default abstract class ExpressionParser extends LValParser { return this.parseSuper(); case tt._import: - node = this.startNode(); + node = this.startNode(); this.next(); if (this.match(tt.dot)) { return this.parseImportMetaProperty(node as Undone); } - if (!this.match(tt.parenL)) { + if (this.match(tt.parenL)) { + if (this.options.createImportExpressions) { + return this.parseImportCall(node as Undone); + } else { + return this.finishNode(node, "Import"); + } + } else { this.raise(Errors.UnsupportedImport, { at: this.state.lastTokStartLoc, }); + return this.finishNode(node, "Import"); } - return this.finishNode(node, "Import"); + case tt._this: node = this.startNode(); this.next(); @@ -1920,9 +1927,14 @@ export default abstract class ExpressionParser extends LValParser { } parseNewCallee(this: Parser, node: Undone): void { - node.callee = this.parseNoCallExpr(); - if (node.callee.type === "Import") { - this.raise(Errors.ImportCallNotNewExpression, { at: node.callee }); + const isImport = this.match(tt._import); + const callee = this.parseNoCallExpr(); + node.callee = callee; + if ( + isImport && + (callee.type === "Import" || callee.type === "ImportExpression") + ) { + this.raise(Errors.ImportCallNotNewExpression, { at: callee }); } } @@ -2939,6 +2951,30 @@ export default abstract class ExpressionParser extends LValParser { return this.finishNode(node, "YieldExpression"); } + // https://tc39.es/ecma262/#prod-ImportCall + parseImportCall( + this: Parser, + node: Undone, + ): N.ImportExpression { + this.next(); // eat tt.parenL + node.source = this.parseMaybeAssignAllowIn(); + if ( + this.hasPlugin("importAttributes") || + this.hasPlugin("importAssertions") + ) { + node.options = null; + } + if (this.eat(tt.comma)) { + this.expectImportAttributesPlugin(); + if (!this.match(tt.parenR)) { + node.options = this.parseMaybeAssignAllowIn(); + this.eat(tt.comma); + } + } + this.expect(tt.parenR); + return this.finishNode(node, "ImportExpression"); + } + // Validates a pipeline (for any of the pipeline Babylon plugins) at the point // of the infix operator `|>`. diff --git a/packages/babel-parser/src/plugins/estree.ts b/packages/babel-parser/src/plugins/estree.ts index 1e5e02fd8f7b..98c83cc41702 100644 --- a/packages/babel-parser/src/plugins/estree.ts +++ b/packages/babel-parser/src/plugins/estree.ts @@ -424,6 +424,9 @@ export default (superClass: typeof Parser) => this.hasPlugin("importAttributes") || this.hasPlugin("importAssertions") ) { + (node as N.Node as N.EstreeImportExpression).options = + node.arguments[1] ?? null; + // compatibility with previous ESTree AST (node as N.Node as N.EstreeImportExpression).attributes = node.arguments[1] ?? null; } diff --git a/packages/babel-parser/src/types.d.ts b/packages/babel-parser/src/types.d.ts index 646ee0207e27..1c21761bafff 100644 --- a/packages/babel-parser/src/types.d.ts +++ b/packages/babel-parser/src/types.d.ts @@ -624,6 +624,12 @@ export interface NewExpression extends CallOrNewBase { optional?: boolean; // TODO: Not in spec } +export interface ImportExpression extends NodeBase { + type: "ImportExpression"; + source: Expression; + options: Expression | null; +} + export interface SequenceExpression extends NodeBase { type: "SequenceExpression"; expressions: Expression[]; @@ -1213,6 +1219,10 @@ export interface EstreeMethodDefinition extends NodeBase { export interface EstreeImportExpression extends NodeBase { type: "ImportExpression"; source: Expression; + options?: Expression | null; + /** + * @deprecated Use options instead + */ attributes?: Expression | null; } diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/direct-calls-only/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/direct-calls-only/input.js new file mode 100644 index 000000000000..7be98d3feb76 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/direct-calls-only/input.js @@ -0,0 +1,3 @@ +function failsParse() { + return import.then(); +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/direct-calls-only/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/direct-calls-only/output.json new file mode 100644 index 000000000000..517c435a7642 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/direct-calls-only/output.json @@ -0,0 +1,58 @@ +{ + "type": "File", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":49}}, + "errors": [ + "SyntaxError: The only valid meta property for import is import.meta. (2:16)" + ], + "program": { + "type": "Program", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":49}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":49}}, + "id": { + "type": "Identifier", + "start":9,"end":19,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":19,"index":19},"identifierName":"failsParse"}, + "name": "failsParse" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":22,"end":49,"loc":{"start":{"line":1,"column":22,"index":22},"end":{"line":3,"column":1,"index":49}}, + "body": [ + { + "type": "ReturnStatement", + "start":26,"end":47,"loc":{"start":{"line":2,"column":2,"index":26},"end":{"line":2,"column":23,"index":47}}, + "argument": { + "type": "CallExpression", + "start":33,"end":46,"loc":{"start":{"line":2,"column":9,"index":33},"end":{"line":2,"column":22,"index":46}}, + "callee": { + "type": "MetaProperty", + "start":33,"end":44,"loc":{"start":{"line":2,"column":9,"index":33},"end":{"line":2,"column":20,"index":44}}, + "meta": { + "type": "Identifier", + "start":33,"end":39,"loc":{"start":{"line":2,"column":9,"index":33},"end":{"line":2,"column":15,"index":39},"identifierName":"import"}, + "name": "import" + }, + "property": { + "type": "Identifier", + "start":40,"end":44,"loc":{"start":{"line":2,"column":16,"index":40},"end":{"line":2,"column":20,"index":44},"identifierName":"then"}, + "name": "then" + } + }, + "arguments": [] + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/generator/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/generator/input.js new file mode 100644 index 000000000000..5b09c31e6feb --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/generator/input.js @@ -0,0 +1,3 @@ +function* a() { + yield import('http'); +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/generator/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/generator/output.json new file mode 100644 index 000000000000..daca713c135b --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/generator/output.json @@ -0,0 +1,60 @@ +{ + "type": "File", + "start":0,"end":41,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":41}}, + "program": { + "type": "Program", + "start":0,"end":41,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":41}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":41,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":41}}, + "id": { + "type": "Identifier", + "start":10,"end":11,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":1,"column":11,"index":11},"identifierName":"a"}, + "name": "a" + }, + "generator": true, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":14,"end":41,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":3,"column":1,"index":41}}, + "body": [ + { + "type": "ExpressionStatement", + "start":18,"end":39,"loc":{"start":{"line":2,"column":2,"index":18},"end":{"line":2,"column":23,"index":39}}, + "expression": { + "type": "YieldExpression", + "start":18,"end":38,"loc":{"start":{"line":2,"column":2,"index":18},"end":{"line":2,"column":22,"index":38}}, + "delegate": false, + "argument": { + "type": "CallExpression", + "start":24,"end":38,"loc":{"start":{"line":2,"column":8,"index":24},"end":{"line":2,"column":22,"index":38}}, + "callee": { + "type": "Import", + "start":24,"end":30,"loc":{"start":{"line":2,"column":8,"index":24},"end":{"line":2,"column":14,"index":30}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":31,"end":37,"loc":{"start":{"line":2,"column":15,"index":31},"end":{"line":2,"column":21,"index":37}}, + "extra": { + "rawValue": "http", + "raw": "'http'" + }, + "value": "http" + } + ] + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/inside-function/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/inside-function/input.js new file mode 100644 index 000000000000..0dc972c97259 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/inside-function/input.js @@ -0,0 +1,3 @@ +function loadImport(file) { + return import(`test/${file}.js`); +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/inside-function/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/inside-function/output.json new file mode 100644 index 000000000000..873191449fce --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/inside-function/output.json @@ -0,0 +1,83 @@ +{ + "type": "File", + "start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":65}}, + "program": { + "type": "Program", + "start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":65}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":65}}, + "id": { + "type": "Identifier", + "start":9,"end":19,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":19,"index":19},"identifierName":"loadImport"}, + "name": "loadImport" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":20,"end":24,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":24,"index":24},"identifierName":"file"}, + "name": "file" + } + ], + "body": { + "type": "BlockStatement", + "start":26,"end":65,"loc":{"start":{"line":1,"column":26,"index":26},"end":{"line":3,"column":1,"index":65}}, + "body": [ + { + "type": "ReturnStatement", + "start":30,"end":63,"loc":{"start":{"line":2,"column":2,"index":30},"end":{"line":2,"column":35,"index":63}}, + "argument": { + "type": "CallExpression", + "start":37,"end":62,"loc":{"start":{"line":2,"column":9,"index":37},"end":{"line":2,"column":34,"index":62}}, + "callee": { + "type": "Import", + "start":37,"end":43,"loc":{"start":{"line":2,"column":9,"index":37},"end":{"line":2,"column":15,"index":43}} + }, + "arguments": [ + { + "type": "TemplateLiteral", + "start":44,"end":61,"loc":{"start":{"line":2,"column":16,"index":44},"end":{"line":2,"column":33,"index":61}}, + "expressions": [ + { + "type": "Identifier", + "start":52,"end":56,"loc":{"start":{"line":2,"column":24,"index":52},"end":{"line":2,"column":28,"index":56},"identifierName":"file"}, + "name": "file" + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start":45,"end":50,"loc":{"start":{"line":2,"column":17,"index":45},"end":{"line":2,"column":22,"index":50}}, + "value": { + "raw": "test/", + "cooked": "test/" + }, + "tail": false + }, + { + "type": "TemplateElement", + "start":57,"end":60,"loc":{"start":{"line":2,"column":29,"index":57},"end":{"line":2,"column":32,"index":60}}, + "value": { + "raw": ".js", + "cooked": ".js" + }, + "tail": true + } + ] + } + ] + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-arguments-spread/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-arguments-spread/input.js new file mode 100644 index 000000000000..87bfc54932a0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-arguments-spread/input.js @@ -0,0 +1 @@ +import(...[1]) diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-arguments-spread/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-arguments-spread/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-arguments-spread/output.json rename to packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-arguments-spread/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-lone-import/input.js similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/input.js rename to packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-lone-import/input.js diff --git a/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-lone-import/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/output.json rename to packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-lone-import/output.json diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-new/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-new/input.js new file mode 100644 index 000000000000..eb88f1a997e4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-new/input.js @@ -0,0 +1 @@ +new import("foo"); diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-new/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-new/output.json new file mode 100644 index 000000000000..3e70fd237ed0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-new/output.json @@ -0,0 +1,39 @@ +{ + "type": "File", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}}, + "errors": [ + "SyntaxError: Cannot use new with import(...). (1:4)" + ], + "program": { + "type": "Program", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}}, + "expression": { + "type": "NewExpression", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, + "callee": { + "type": "Import", + "start":4,"end":10,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":10,"index":10}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":11,"end":16,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":16,"index":16}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-trailing-comma/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-trailing-comma/input.js new file mode 100644 index 000000000000..6596802add47 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-trailing-comma/input.js @@ -0,0 +1 @@ +import("foo",); \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-trailing-comma/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-trailing-comma/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-trailing-comma/output.json rename to packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/invalid-trailing-comma/output.json diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/multiple-args/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/multiple-args/input.js new file mode 100644 index 000000000000..94e059c54bac --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/multiple-args/input.js @@ -0,0 +1 @@ +import('hello', 'world', '!'); diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/multiple-args/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/output.json rename to packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/multiple-args/output.json diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/no-args/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/no-args/input.js new file mode 100644 index 000000000000..6f375fea965b --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/no-args/input.js @@ -0,0 +1 @@ +import(); diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/no-args/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/no-args/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/es2020/dynamic-import/no-args/output.json rename to packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/no-args/output.json diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/options.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/options.json new file mode 100644 index 000000000000..8977da6fcc7e --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/options.json @@ -0,0 +1,3 @@ +{ + "createImportExpressions": false +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-module/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-module/input.js new file mode 100644 index 000000000000..b49ee994b11f --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-module/input.js @@ -0,0 +1 @@ +import('test.js'); diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-module/options.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-module/options.json new file mode 100644 index 000000000000..2104ca43283f --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-module/options.json @@ -0,0 +1,3 @@ +{ + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-module/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-module/output.json new file mode 100644 index 000000000000..6ed1da8dd614 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-module/output.json @@ -0,0 +1,36 @@ +{ + "type": "File", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}}, + "program": { + "type": "Program", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}}, + "expression": { + "type": "CallExpression", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, + "callee": { + "type": "Import", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":7,"end":16,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":16,"index":16}}, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-strict/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-strict/input.js new file mode 100644 index 000000000000..b77708a14fa4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-strict/input.js @@ -0,0 +1,3 @@ +"use strict"; + +import('test.js'); diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-strict/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-strict/output.json new file mode 100644 index 000000000000..c6a858508770 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/parses-strict/output.json @@ -0,0 +1,51 @@ +{ + "type": "File", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":18,"index":33}}, + "program": { + "type": "Program", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":18,"index":33}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":15,"end":33,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":18,"index":33}}, + "expression": { + "type": "CallExpression", + "start":15,"end":32,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":17,"index":32}}, + "callee": { + "type": "Import", + "start":15,"end":21,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":6,"index":21}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":22,"end":31,"loc":{"start":{"line":3,"column":7,"index":22},"end":{"line":3,"column":16,"index":31}}, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } + ] + } + } + ], + "directives": [ + { + "type": "Directive", + "start":0,"end":13,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":13,"index":13}}, + "value": { + "type": "DirectiveLiteral", + "start":0,"end":12,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":12,"index":12}}, + "extra": { + "rawValue": "use strict", + "raw": "\"use strict\"", + "expressionValue": "use strict" + }, + "value": "use strict" + } + } + ] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/return-value/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/return-value/input.js new file mode 100644 index 000000000000..603eb2f77be5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/return-value/input.js @@ -0,0 +1 @@ +const importResult = import('test.js'); diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/return-value/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/return-value/output.json new file mode 100644 index 000000000000..5a070046135b --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/return-value/output.json @@ -0,0 +1,48 @@ +{ + "type": "File", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":39,"index":39}}, + "program": { + "type": "Program", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":39,"index":39}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":39,"index":39}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":6,"end":38,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":38,"index":38}}, + "id": { + "type": "Identifier", + "start":6,"end":18,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":18,"index":18},"identifierName":"importResult"}, + "name": "importResult" + }, + "init": { + "type": "CallExpression", + "start":21,"end":38,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":38,"index":38}}, + "callee": { + "type": "Import", + "start":21,"end":27,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":27,"index":27}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":28,"end":37,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":37,"index":37}}, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } + ] + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/top-level/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/top-level/input.js new file mode 100644 index 000000000000..7517decff1d1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/top-level/input.js @@ -0,0 +1,6 @@ +import('testing.js'); + +const test = 'hello'; +import(`testing/${test}.js`); + +import('testing.js').then(() => {}); diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/top-level/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/top-level/output.json new file mode 100644 index 000000000000..a4abc131c256 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/top-level/output.json @@ -0,0 +1,159 @@ +{ + "type": "File", + "start":0,"end":112,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":36,"index":112}}, + "program": { + "type": "Program", + "start":0,"end":112,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":6,"column":36,"index":112}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":21,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":21,"index":21}}, + "expression": { + "type": "CallExpression", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":20,"index":20}}, + "callee": { + "type": "Import", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":7,"end":19,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":19,"index":19}}, + "extra": { + "rawValue": "testing.js", + "raw": "'testing.js'" + }, + "value": "testing.js" + } + ] + } + }, + { + "type": "VariableDeclaration", + "start":23,"end":44,"loc":{"start":{"line":3,"column":0,"index":23},"end":{"line":3,"column":21,"index":44}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":29,"end":43,"loc":{"start":{"line":3,"column":6,"index":29},"end":{"line":3,"column":20,"index":43}}, + "id": { + "type": "Identifier", + "start":29,"end":33,"loc":{"start":{"line":3,"column":6,"index":29},"end":{"line":3,"column":10,"index":33},"identifierName":"test"}, + "name": "test" + }, + "init": { + "type": "StringLiteral", + "start":36,"end":43,"loc":{"start":{"line":3,"column":13,"index":36},"end":{"line":3,"column":20,"index":43}}, + "extra": { + "rawValue": "hello", + "raw": "'hello'" + }, + "value": "hello" + } + } + ], + "kind": "const" + }, + { + "type": "ExpressionStatement", + "start":45,"end":74,"loc":{"start":{"line":4,"column":0,"index":45},"end":{"line":4,"column":29,"index":74}}, + "expression": { + "type": "CallExpression", + "start":45,"end":73,"loc":{"start":{"line":4,"column":0,"index":45},"end":{"line":4,"column":28,"index":73}}, + "callee": { + "type": "Import", + "start":45,"end":51,"loc":{"start":{"line":4,"column":0,"index":45},"end":{"line":4,"column":6,"index":51}} + }, + "arguments": [ + { + "type": "TemplateLiteral", + "start":52,"end":72,"loc":{"start":{"line":4,"column":7,"index":52},"end":{"line":4,"column":27,"index":72}}, + "expressions": [ + { + "type": "Identifier", + "start":63,"end":67,"loc":{"start":{"line":4,"column":18,"index":63},"end":{"line":4,"column":22,"index":67},"identifierName":"test"}, + "name": "test" + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start":53,"end":61,"loc":{"start":{"line":4,"column":8,"index":53},"end":{"line":4,"column":16,"index":61}}, + "value": { + "raw": "testing/", + "cooked": "testing/" + }, + "tail": false + }, + { + "type": "TemplateElement", + "start":68,"end":71,"loc":{"start":{"line":4,"column":23,"index":68},"end":{"line":4,"column":26,"index":71}}, + "value": { + "raw": ".js", + "cooked": ".js" + }, + "tail": true + } + ] + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":76,"end":112,"loc":{"start":{"line":6,"column":0,"index":76},"end":{"line":6,"column":36,"index":112}}, + "expression": { + "type": "CallExpression", + "start":76,"end":111,"loc":{"start":{"line":6,"column":0,"index":76},"end":{"line":6,"column":35,"index":111}}, + "callee": { + "type": "MemberExpression", + "start":76,"end":101,"loc":{"start":{"line":6,"column":0,"index":76},"end":{"line":6,"column":25,"index":101}}, + "object": { + "type": "CallExpression", + "start":76,"end":96,"loc":{"start":{"line":6,"column":0,"index":76},"end":{"line":6,"column":20,"index":96}}, + "callee": { + "type": "Import", + "start":76,"end":82,"loc":{"start":{"line":6,"column":0,"index":76},"end":{"line":6,"column":6,"index":82}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":83,"end":95,"loc":{"start":{"line":6,"column":7,"index":83},"end":{"line":6,"column":19,"index":95}}, + "extra": { + "rawValue": "testing.js", + "raw": "'testing.js'" + }, + "value": "testing.js" + } + ] + }, + "computed": false, + "property": { + "type": "Identifier", + "start":97,"end":101,"loc":{"start":{"line":6,"column":21,"index":97},"end":{"line":6,"column":25,"index":101},"identifierName":"then"}, + "name": "then" + } + }, + "arguments": [ + { + "type": "ArrowFunctionExpression", + "start":102,"end":110,"loc":{"start":{"line":6,"column":26,"index":102},"end":{"line":6,"column":34,"index":110}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":108,"end":110,"loc":{"start":{"line":6,"column":32,"index":108},"end":{"line":6,"column":34,"index":110}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/valid-new-parens/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/valid-new-parens/input.js new file mode 100644 index 000000000000..b93cb0b48994 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/valid-new-parens/input.js @@ -0,0 +1 @@ +new (import("foo")); diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/valid-new-parens/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/valid-new-parens/output.json new file mode 100644 index 000000000000..c189231f0317 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/valid-new-parens/output.json @@ -0,0 +1,45 @@ +{ + "type": "File", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":20,"index":20}}, + "program": { + "type": "Program", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":20,"index":20}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":20,"index":20}}, + "expression": { + "type": "NewExpression", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}}, + "callee": { + "type": "CallExpression", + "start":5,"end":18,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":18,"index":18}}, + "callee": { + "type": "Import", + "start":5,"end":11,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":11,"index":11}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":12,"end":17,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":17,"index":17}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + ], + "extra": { + "parenthesized": true, + "parenStart": 4 + } + }, + "arguments": [] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/variable-arguments/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/variable-arguments/input.js new file mode 100644 index 000000000000..5fd4901e23f9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/variable-arguments/input.js @@ -0,0 +1,2 @@ +const testVariable = 'test.js'; +import(testVariable).then(() => {}); diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/variable-arguments/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/variable-arguments/output.json new file mode 100644 index 000000000000..100d05683280 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import-createImportExpression-false/variable-arguments/output.json @@ -0,0 +1,87 @@ +{ + "type": "File", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":36,"index":68}}, + "program": { + "type": "Program", + "start":0,"end":68,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":36,"index":68}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":31,"index":31}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":6,"end":30,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":30,"index":30}}, + "id": { + "type": "Identifier", + "start":6,"end":18,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":18,"index":18},"identifierName":"testVariable"}, + "name": "testVariable" + }, + "init": { + "type": "StringLiteral", + "start":21,"end":30,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":30,"index":30}}, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } + } + ], + "kind": "const" + }, + { + "type": "ExpressionStatement", + "start":32,"end":68,"loc":{"start":{"line":2,"column":0,"index":32},"end":{"line":2,"column":36,"index":68}}, + "expression": { + "type": "CallExpression", + "start":32,"end":67,"loc":{"start":{"line":2,"column":0,"index":32},"end":{"line":2,"column":35,"index":67}}, + "callee": { + "type": "MemberExpression", + "start":32,"end":57,"loc":{"start":{"line":2,"column":0,"index":32},"end":{"line":2,"column":25,"index":57}}, + "object": { + "type": "CallExpression", + "start":32,"end":52,"loc":{"start":{"line":2,"column":0,"index":32},"end":{"line":2,"column":20,"index":52}}, + "callee": { + "type": "Import", + "start":32,"end":38,"loc":{"start":{"line":2,"column":0,"index":32},"end":{"line":2,"column":6,"index":38}} + }, + "arguments": [ + { + "type": "Identifier", + "start":39,"end":51,"loc":{"start":{"line":2,"column":7,"index":39},"end":{"line":2,"column":19,"index":51},"identifierName":"testVariable"}, + "name": "testVariable" + } + ] + }, + "computed": false, + "property": { + "type": "Identifier", + "start":53,"end":57,"loc":{"start":{"line":2,"column":21,"index":53},"end":{"line":2,"column":25,"index":57},"identifierName":"then"}, + "name": "then" + } + }, + "arguments": [ + { + "type": "ArrowFunctionExpression", + "start":58,"end":66,"loc":{"start":{"line":2,"column":26,"index":58},"end":{"line":2,"column":34,"index":66}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":64,"end":66,"loc":{"start":{"line":2,"column":32,"index":64},"end":{"line":2,"column":34,"index":66}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/generator/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/generator/output.json index daca713c135b..eba7bada3209 100644 --- a/packages/babel-parser/test/fixtures/es2020/dynamic-import/generator/output.json +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/generator/output.json @@ -30,23 +30,17 @@ "start":18,"end":38,"loc":{"start":{"line":2,"column":2,"index":18},"end":{"line":2,"column":22,"index":38}}, "delegate": false, "argument": { - "type": "CallExpression", + "type": "ImportExpression", "start":24,"end":38,"loc":{"start":{"line":2,"column":8,"index":24},"end":{"line":2,"column":22,"index":38}}, - "callee": { - "type": "Import", - "start":24,"end":30,"loc":{"start":{"line":2,"column":8,"index":24},"end":{"line":2,"column":14,"index":30}} - }, - "arguments": [ - { - "type": "StringLiteral", - "start":31,"end":37,"loc":{"start":{"line":2,"column":15,"index":31},"end":{"line":2,"column":21,"index":37}}, - "extra": { - "rawValue": "http", - "raw": "'http'" - }, - "value": "http" - } - ] + "source": { + "type": "StringLiteral", + "start":31,"end":37,"loc":{"start":{"line":2,"column":15,"index":31},"end":{"line":2,"column":21,"index":37}}, + "extra": { + "rawValue": "http", + "raw": "'http'" + }, + "value": "http" + } } } } diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/inside-function/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/inside-function/output.json index 873191449fce..187980e2b57c 100644 --- a/packages/babel-parser/test/fixtures/es2020/dynamic-import/inside-function/output.json +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/inside-function/output.json @@ -32,45 +32,39 @@ "type": "ReturnStatement", "start":30,"end":63,"loc":{"start":{"line":2,"column":2,"index":30},"end":{"line":2,"column":35,"index":63}}, "argument": { - "type": "CallExpression", + "type": "ImportExpression", "start":37,"end":62,"loc":{"start":{"line":2,"column":9,"index":37},"end":{"line":2,"column":34,"index":62}}, - "callee": { - "type": "Import", - "start":37,"end":43,"loc":{"start":{"line":2,"column":9,"index":37},"end":{"line":2,"column":15,"index":43}} - }, - "arguments": [ - { - "type": "TemplateLiteral", - "start":44,"end":61,"loc":{"start":{"line":2,"column":16,"index":44},"end":{"line":2,"column":33,"index":61}}, - "expressions": [ - { - "type": "Identifier", - "start":52,"end":56,"loc":{"start":{"line":2,"column":24,"index":52},"end":{"line":2,"column":28,"index":56},"identifierName":"file"}, - "name": "file" - } - ], - "quasis": [ - { - "type": "TemplateElement", - "start":45,"end":50,"loc":{"start":{"line":2,"column":17,"index":45},"end":{"line":2,"column":22,"index":50}}, - "value": { - "raw": "test/", - "cooked": "test/" - }, - "tail": false + "source": { + "type": "TemplateLiteral", + "start":44,"end":61,"loc":{"start":{"line":2,"column":16,"index":44},"end":{"line":2,"column":33,"index":61}}, + "expressions": [ + { + "type": "Identifier", + "start":52,"end":56,"loc":{"start":{"line":2,"column":24,"index":52},"end":{"line":2,"column":28,"index":56},"identifierName":"file"}, + "name": "file" + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start":45,"end":50,"loc":{"start":{"line":2,"column":17,"index":45},"end":{"line":2,"column":22,"index":50}}, + "value": { + "raw": "test/", + "cooked": "test/" }, - { - "type": "TemplateElement", - "start":57,"end":60,"loc":{"start":{"line":2,"column":29,"index":57},"end":{"line":2,"column":32,"index":60}}, - "value": { - "raw": ".js", - "cooked": ".js" - }, - "tail": true - } - ] - } - ] + "tail": false + }, + { + "type": "TemplateElement", + "start":57,"end":60,"loc":{"start":{"line":2,"column":29,"index":57},"end":{"line":2,"column":32,"index":60}}, + "value": { + "raw": ".js", + "cooked": ".js" + }, + "tail": true + } + ] + } } } ], diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-arguments-spread/options.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-arguments-spread/options.json new file mode 100644 index 000000000000..c958665c03e2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-arguments-spread/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (1:7)" +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-lone-import/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-lone-import/input.js new file mode 100644 index 000000000000..477eec265426 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-lone-import/input.js @@ -0,0 +1 @@ +(import) diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-lone-import/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-lone-import/output.json new file mode 100644 index 000000000000..998c2127ab45 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-lone-import/output.json @@ -0,0 +1,28 @@ +{ + "type": "File", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}}, + "errors": [ + "SyntaxError: `import` can only be used in `import()` or `import.meta`. (1:1)" + ], + "program": { + "type": "Program", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":8,"index":8}}, + "expression": { + "type": "Import", + "start":1,"end":7,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":7,"index":7}}, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-new/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-new/output.json index 3e70fd237ed0..68819443585b 100644 --- a/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-new/output.json +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-new/output.json @@ -17,11 +17,9 @@ "type": "NewExpression", "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, "callee": { - "type": "Import", - "start":4,"end":10,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":10,"index":10}} - }, - "arguments": [ - { + "type": "ImportExpression", + "start":4,"end":17,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":17,"index":17}}, + "source": { "type": "StringLiteral", "start":11,"end":16,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":16,"index":16}}, "extra": { @@ -30,7 +28,8 @@ }, "value": "foo" } - ] + }, + "arguments": [] } } ], diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-trailing-comma/options.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-trailing-comma/options.json new file mode 100644 index 000000000000..8fec4d2ea498 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/invalid-trailing-comma/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: \"importAttributes\". (1:13)" +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/options.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/options.json new file mode 100644 index 000000000000..0f21c601809e --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/multiple-args/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This experimental syntax requires enabling the parser plugin: \"importAttributes\". (1:16)" +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/no-args/options.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/no-args/options.json new file mode 100644 index 000000000000..c958665c03e2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/no-args/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (1:7)" +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/options.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/options.json new file mode 100644 index 000000000000..fc7a8f3052bc --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/options.json @@ -0,0 +1,3 @@ +{ + "createImportExpressions": true +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/parses-module/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/parses-module/output.json index 6ed1da8dd614..759f5bc25894 100644 --- a/packages/babel-parser/test/fixtures/es2020/dynamic-import/parses-module/output.json +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/parses-module/output.json @@ -11,23 +11,17 @@ "type": "ExpressionStatement", "start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}}, "expression": { - "type": "CallExpression", + "type": "ImportExpression", "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, - "callee": { - "type": "Import", - "start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}} - }, - "arguments": [ - { - "type": "StringLiteral", - "start":7,"end":16,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":16,"index":16}}, - "extra": { - "rawValue": "test.js", - "raw": "'test.js'" - }, - "value": "test.js" - } - ] + "source": { + "type": "StringLiteral", + "start":7,"end":16,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":16,"index":16}}, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } } } ], diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/parses-strict/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/parses-strict/output.json index c6a858508770..08f4cc8d817a 100644 --- a/packages/babel-parser/test/fixtures/es2020/dynamic-import/parses-strict/output.json +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/parses-strict/output.json @@ -11,23 +11,17 @@ "type": "ExpressionStatement", "start":15,"end":33,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":18,"index":33}}, "expression": { - "type": "CallExpression", + "type": "ImportExpression", "start":15,"end":32,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":17,"index":32}}, - "callee": { - "type": "Import", - "start":15,"end":21,"loc":{"start":{"line":3,"column":0,"index":15},"end":{"line":3,"column":6,"index":21}} - }, - "arguments": [ - { - "type": "StringLiteral", - "start":22,"end":31,"loc":{"start":{"line":3,"column":7,"index":22},"end":{"line":3,"column":16,"index":31}}, - "extra": { - "rawValue": "test.js", - "raw": "'test.js'" - }, - "value": "test.js" - } - ] + "source": { + "type": "StringLiteral", + "start":22,"end":31,"loc":{"start":{"line":3,"column":7,"index":22},"end":{"line":3,"column":16,"index":31}}, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } } } ], diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/return-value/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/return-value/output.json index 5a070046135b..ff9eb09c7601 100644 --- a/packages/babel-parser/test/fixtures/es2020/dynamic-import/return-value/output.json +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/return-value/output.json @@ -20,23 +20,17 @@ "name": "importResult" }, "init": { - "type": "CallExpression", + "type": "ImportExpression", "start":21,"end":38,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":38,"index":38}}, - "callee": { - "type": "Import", - "start":21,"end":27,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":27,"index":27}} - }, - "arguments": [ - { - "type": "StringLiteral", - "start":28,"end":37,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":37,"index":37}}, - "extra": { - "rawValue": "test.js", - "raw": "'test.js'" - }, - "value": "test.js" - } - ] + "source": { + "type": "StringLiteral", + "start":28,"end":37,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":37,"index":37}}, + "extra": { + "rawValue": "test.js", + "raw": "'test.js'" + }, + "value": "test.js" + } } } ], diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/top-level/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/top-level/output.json index a4abc131c256..5eee2cb531b2 100644 --- a/packages/babel-parser/test/fixtures/es2020/dynamic-import/top-level/output.json +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/top-level/output.json @@ -11,23 +11,17 @@ "type": "ExpressionStatement", "start":0,"end":21,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":21,"index":21}}, "expression": { - "type": "CallExpression", + "type": "ImportExpression", "start":0,"end":20,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":20,"index":20}}, - "callee": { - "type": "Import", - "start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}} - }, - "arguments": [ - { - "type": "StringLiteral", - "start":7,"end":19,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":19,"index":19}}, - "extra": { - "rawValue": "testing.js", - "raw": "'testing.js'" - }, - "value": "testing.js" - } - ] + "source": { + "type": "StringLiteral", + "start":7,"end":19,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":19,"index":19}}, + "extra": { + "rawValue": "testing.js", + "raw": "'testing.js'" + }, + "value": "testing.js" + } } }, { @@ -59,45 +53,39 @@ "type": "ExpressionStatement", "start":45,"end":74,"loc":{"start":{"line":4,"column":0,"index":45},"end":{"line":4,"column":29,"index":74}}, "expression": { - "type": "CallExpression", + "type": "ImportExpression", "start":45,"end":73,"loc":{"start":{"line":4,"column":0,"index":45},"end":{"line":4,"column":28,"index":73}}, - "callee": { - "type": "Import", - "start":45,"end":51,"loc":{"start":{"line":4,"column":0,"index":45},"end":{"line":4,"column":6,"index":51}} - }, - "arguments": [ - { - "type": "TemplateLiteral", - "start":52,"end":72,"loc":{"start":{"line":4,"column":7,"index":52},"end":{"line":4,"column":27,"index":72}}, - "expressions": [ - { - "type": "Identifier", - "start":63,"end":67,"loc":{"start":{"line":4,"column":18,"index":63},"end":{"line":4,"column":22,"index":67},"identifierName":"test"}, - "name": "test" - } - ], - "quasis": [ - { - "type": "TemplateElement", - "start":53,"end":61,"loc":{"start":{"line":4,"column":8,"index":53},"end":{"line":4,"column":16,"index":61}}, - "value": { - "raw": "testing/", - "cooked": "testing/" - }, - "tail": false + "source": { + "type": "TemplateLiteral", + "start":52,"end":72,"loc":{"start":{"line":4,"column":7,"index":52},"end":{"line":4,"column":27,"index":72}}, + "expressions": [ + { + "type": "Identifier", + "start":63,"end":67,"loc":{"start":{"line":4,"column":18,"index":63},"end":{"line":4,"column":22,"index":67},"identifierName":"test"}, + "name": "test" + } + ], + "quasis": [ + { + "type": "TemplateElement", + "start":53,"end":61,"loc":{"start":{"line":4,"column":8,"index":53},"end":{"line":4,"column":16,"index":61}}, + "value": { + "raw": "testing/", + "cooked": "testing/" }, - { - "type": "TemplateElement", - "start":68,"end":71,"loc":{"start":{"line":4,"column":23,"index":68},"end":{"line":4,"column":26,"index":71}}, - "value": { - "raw": ".js", - "cooked": ".js" - }, - "tail": true - } - ] - } - ] + "tail": false + }, + { + "type": "TemplateElement", + "start":68,"end":71,"loc":{"start":{"line":4,"column":23,"index":68},"end":{"line":4,"column":26,"index":71}}, + "value": { + "raw": ".js", + "cooked": ".js" + }, + "tail": true + } + ] + } } }, { @@ -110,23 +98,17 @@ "type": "MemberExpression", "start":76,"end":101,"loc":{"start":{"line":6,"column":0,"index":76},"end":{"line":6,"column":25,"index":101}}, "object": { - "type": "CallExpression", + "type": "ImportExpression", "start":76,"end":96,"loc":{"start":{"line":6,"column":0,"index":76},"end":{"line":6,"column":20,"index":96}}, - "callee": { - "type": "Import", - "start":76,"end":82,"loc":{"start":{"line":6,"column":0,"index":76},"end":{"line":6,"column":6,"index":82}} - }, - "arguments": [ - { - "type": "StringLiteral", - "start":83,"end":95,"loc":{"start":{"line":6,"column":7,"index":83},"end":{"line":6,"column":19,"index":95}}, - "extra": { - "rawValue": "testing.js", - "raw": "'testing.js'" - }, - "value": "testing.js" - } - ] + "source": { + "type": "StringLiteral", + "start":83,"end":95,"loc":{"start":{"line":6,"column":7,"index":83},"end":{"line":6,"column":19,"index":95}}, + "extra": { + "rawValue": "testing.js", + "raw": "'testing.js'" + }, + "value": "testing.js" + } }, "computed": false, "property": { diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/valid-new-parens/input.js b/packages/babel-parser/test/fixtures/es2020/dynamic-import/valid-new-parens/input.js new file mode 100644 index 000000000000..b93cb0b48994 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/valid-new-parens/input.js @@ -0,0 +1 @@ +new (import("foo")); diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/valid-new-parens/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/valid-new-parens/output.json new file mode 100644 index 000000000000..34eaf64851e7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/valid-new-parens/output.json @@ -0,0 +1,39 @@ +{ + "type": "File", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":20,"index":20}}, + "program": { + "type": "Program", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":20,"index":20}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":20,"index":20}}, + "expression": { + "type": "NewExpression", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}}, + "callee": { + "type": "ImportExpression", + "start":5,"end":18,"loc":{"start":{"line":1,"column":5,"index":5},"end":{"line":1,"column":18,"index":18}}, + "source": { + "type": "StringLiteral", + "start":12,"end":17,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":17,"index":17}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "extra": { + "parenthesized": true, + "parenStart": 4 + } + }, + "arguments": [] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/es2020/dynamic-import/variable-arguments/output.json b/packages/babel-parser/test/fixtures/es2020/dynamic-import/variable-arguments/output.json index 100d05683280..ba4014ca550d 100644 --- a/packages/babel-parser/test/fixtures/es2020/dynamic-import/variable-arguments/output.json +++ b/packages/babel-parser/test/fixtures/es2020/dynamic-import/variable-arguments/output.json @@ -42,27 +42,21 @@ "type": "MemberExpression", "start":32,"end":57,"loc":{"start":{"line":2,"column":0,"index":32},"end":{"line":2,"column":25,"index":57}}, "object": { - "type": "CallExpression", + "type": "ImportExpression", "start":32,"end":52,"loc":{"start":{"line":2,"column":0,"index":32},"end":{"line":2,"column":20,"index":52}}, - "callee": { - "type": "Import", - "start":32,"end":38,"loc":{"start":{"line":2,"column":0,"index":32},"end":{"line":2,"column":6,"index":38}} - }, - "arguments": [ - { - "type": "Identifier", - "start":39,"end":51,"loc":{"start":{"line":2,"column":7,"index":39},"end":{"line":2,"column":19,"index":51},"identifierName":"testVariable"}, - "name": "testVariable" + "source": { + "type": "Identifier", + "start":39,"end":51,"loc":{"start":{"line":2,"column":7,"index":39},"end":{"line":2,"column":19,"index":51},"identifierName":"testVariable"}, + "name": "testVariable" } - ] - }, + }, "computed": false, "property": { "type": "Identifier", "start":53,"end":57,"loc":{"start":{"line":2,"column":21,"index":53},"end":{"line":2,"column":25,"index":57},"identifierName":"then"}, "name": "then" - } - }, + } + }, "arguments": [ { "type": "ArrowFunctionExpression", diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/output.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/output.json index 2145426498da..24e73b5393a6 100644 --- a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/output.json +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions-null/output.json @@ -19,9 +19,10 @@ "value": "module", "raw": "\"module\"" }, + "options": null, "attributes": null } } ] } -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/output.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/output.json index bbbe1d4f8aef..cc8384ef8937 100644 --- a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/output.json +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-assertions/output.json @@ -19,6 +19,50 @@ "value": "module", "raw": "\"module\"" }, + "options": { + "type": "ObjectExpression", + "start":17,"end":45,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":45}}, + "properties": [ + { + "type": "Property", + "start":19,"end":43,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":43}}, + "method": false, + "key": { + "type": "Identifier", + "start":19,"end":25,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":25},"identifierName":"assert"}, + "name": "assert" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":27,"end":43,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":43}}, + "properties": [ + { + "type": "Property", + "start":29,"end":41,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":41}}, + "method": false, + "key": { + "type": "Identifier", + "start":29,"end":33,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":33},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "Literal", + "start":35,"end":41,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":41}}, + "value": "json", + "raw": "\"json\"" + }, + "kind": "init" + } + ] + }, + "kind": "init" + } + ] + }, "attributes": { "type": "ObjectExpression", "start":17,"end":45,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":45}}, @@ -67,4 +111,4 @@ } ] } -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/output.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/output.json index 2145426498da..24e73b5393a6 100644 --- a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/output.json +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes-null/output.json @@ -19,9 +19,10 @@ "value": "module", "raw": "\"module\"" }, + "options": null, "attributes": null } } ] } -} \ No newline at end of file +} diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/output.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/output.json index bdc9e256d222..167c122fd602 100644 --- a/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/output.json +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/import-attributes/output.json @@ -19,6 +19,50 @@ "value": "module", "raw": "\"module\"" }, + "options": { + "type": "ObjectExpression", + "start":17,"end":43,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":43}}, + "properties": [ + { + "type": "Property", + "start":19,"end":41,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":41}}, + "method": false, + "key": { + "type": "Identifier", + "start":19,"end":23,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":23},"identifierName":"with"}, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":25,"end":41,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":41}}, + "properties": [ + { + "type": "Property", + "start":27,"end":39,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":39}}, + "method": false, + "key": { + "type": "Identifier", + "start":27,"end":31,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":31},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "Literal", + "start":33,"end":39,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":39}}, + "value": "json", + "raw": "\"json\"" + }, + "kind": "init" + } + ] + }, + "kind": "init" + } + ] + }, "attributes": { "type": "ObjectExpression", "start":17,"end":43,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":43}}, diff --git a/packages/babel-parser/test/fixtures/estree/dynamic-import/options.json b/packages/babel-parser/test/fixtures/estree/dynamic-import/options.json index 9e48801bb9eb..9f00ecbdc1bb 100644 --- a/packages/babel-parser/test/fixtures/estree/dynamic-import/options.json +++ b/packages/babel-parser/test/fixtures/estree/dynamic-import/options.json @@ -1,3 +1,4 @@ { - "plugins": ["estree", "dynamicImport"] + "plugins": ["estree"], + "createImportExpressions": false } diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic-createImportExpression-false/input.js b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic-createImportExpression-false/input.js new file mode 100644 index 000000000000..0f1447dbe608 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic-createImportExpression-false/input.js @@ -0,0 +1 @@ +import('hello', 'world'); diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic-createImportExpression-false/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic-createImportExpression-false/options.json new file mode 100644 index 000000000000..231a405495a1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic-createImportExpression-false/options.json @@ -0,0 +1,6 @@ +{ + "sourceType": "module", + "plugins": [], + "createImportExpressions": false, + "throws": "This experimental syntax requires enabling the parser plugin: \"importAttributes\". (1:24)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/options.json b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/options.json index af247dab5724..85150ee192bc 100644 --- a/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/options.json +++ b/packages/babel-parser/test/fixtures/experimental/_no-plugin/import-assertions-dynamic/options.json @@ -1,5 +1,6 @@ { "sourceType": "module", "plugins": [], - "throws": "This experimental syntax requires enabling the parser plugin: \"importAttributes\". (1:24)" + "createImportExpressions": true, + "throws": "This experimental syntax requires enabling the parser plugin: \"importAttributes\". (1:16)" } diff --git a/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/options.json b/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/options.json deleted file mode 100644 index 1629df1f3678..000000000000 --- a/packages/babel-parser/test/fixtures/experimental/dynamic-import/invalid-lone-import/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "plugins": ["dynamicImport"] -} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions-with-keyword/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions-with-keyword/options.json index 94e3f3742509..daa9926a756d 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions-with-keyword/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions-with-keyword/options.json @@ -1,8 +1,5 @@ { - "plugins": [ - [ - "importAssertions" - ] - ], + "createImportExpressions": false, + "plugins": ["importAssertions"], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax-createImportExpression-true/input.js b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax-createImportExpression-true/input.js new file mode 100644 index 000000000000..481c52e1c75f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax-createImportExpression-true/input.js @@ -0,0 +1 @@ +import("foo.json", { assert: { type: "json" } }) diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax-createImportExpression-true/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax-createImportExpression-true/options.json new file mode 100644 index 000000000000..fc7a8f3052bc --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax-createImportExpression-true/options.json @@ -0,0 +1,3 @@ +{ + "createImportExpressions": true +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax-createImportExpression-true/output.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax-createImportExpression-true/output.json new file mode 100644 index 000000000000..9ddf010b6e22 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/dynamic-import-with-valid-syntax-createImportExpression-true/output.json @@ -0,0 +1,75 @@ +{ + "type": "File", + "start":0,"end":48,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":48,"index":48}}, + "program": { + "type": "Program", + "start":0,"end":48,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":48,"index":48}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":48,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":48,"index":48}}, + "expression": { + "type": "ImportExpression", + "start":0,"end":48,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":48,"index":48}}, + "source": { + "type": "StringLiteral", + "start":7,"end":17,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":17,"index":17}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "options": { + "type": "ObjectExpression", + "start":19,"end":47,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":47,"index":47}}, + "properties": [ + { + "type": "ObjectProperty", + "start":21,"end":45,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":45,"index":45}}, + "method": false, + "key": { + "type": "Identifier", + "start":21,"end":27,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":27,"index":27},"identifierName":"assert"}, + "name": "assert" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":29,"end":45,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":45,"index":45}}, + "properties": [ + { + "type": "ObjectProperty", + "start":31,"end":43,"loc":{"start":{"line":1,"column":31,"index":31},"end":{"line":1,"column":43,"index":43}}, + "method": false, + "key": { + "type": "Identifier", + "start":31,"end":35,"loc":{"start":{"line":1,"column":31,"index":31},"end":{"line":1,"column":35,"index":35},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start":37,"end":43,"loc":{"start":{"line":1,"column":37,"index":37},"end":{"line":1,"column":43,"index":43}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ] + } + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-assertions/options.json b/packages/babel-parser/test/fixtures/experimental/import-assertions/options.json index 51a9e56a5cbd..133bd51f2622 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-assertions/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-assertions/options.json @@ -1,4 +1,5 @@ { "plugins": ["importAssertions"], - "sourceType": "module" + "sourceType": "module", + "createImportExpressions": false } diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/dynamic-import-with-valid-syntax/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/dynamic-import-with-valid-syntax/input.js new file mode 100644 index 000000000000..baa60fc43c5f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/dynamic-import-with-valid-syntax/input.js @@ -0,0 +1 @@ +import("foo.json", { with: { type: "json" } }) diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/dynamic-import-with-valid-syntax/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/dynamic-import-with-valid-syntax/output.json new file mode 100644 index 000000000000..7583096e7b99 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/dynamic-import-with-valid-syntax/output.json @@ -0,0 +1,81 @@ +{ + "type": "File", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, + "program": { + "type": "Program", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, + "expression": { + "type": "CallExpression", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, + "callee": { + "type": "Import", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}} + }, + "arguments": [ + { + "type": "StringLiteral", + "start":7,"end":17,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":17,"index":17}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + { + "type": "ObjectExpression", + "start":19,"end":45,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":45,"index":45}}, + "properties": [ + { + "type": "ObjectProperty", + "start":21,"end":43,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":43,"index":43}}, + "method": false, + "key": { + "type": "Identifier", + "start":21,"end":25,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":25,"index":25},"identifierName":"with"}, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":27,"end":43,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":43,"index":43}}, + "properties": [ + { + "type": "ObjectProperty", + "start":29,"end":41,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":41,"index":41}}, + "method": false, + "key": { + "type": "Identifier", + "start":29,"end":33,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":33,"index":33},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start":35,"end":41,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":41,"index":41}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ] + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/import-with-call-expression/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/import-with-call-expression/input.js new file mode 100644 index 000000000000..bfa86f1acde6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/import-with-call-expression/input.js @@ -0,0 +1,2 @@ +import "x" +with ({}); diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/import-with-call-expression/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/import-with-call-expression/output.json new file mode 100644 index 000000000000..daa1d649fb2a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/import-with-call-expression/output.json @@ -0,0 +1,43 @@ +{ + "type": "File", + "start":0,"end":21,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":10,"index":21}}, + "errors": [ + "SyntaxError: 'with' in strict mode. (2:0)" + ], + "program": { + "type": "Program", + "start":0,"end":21,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":10,"index":21}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":10,"index":10}}, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "x" + } + }, + { + "type": "WithStatement", + "start":11,"end":21,"loc":{"start":{"line":2,"column":0,"index":11},"end":{"line":2,"column":10,"index":21}}, + "object": { + "type": "ObjectExpression", + "start":17,"end":19,"loc":{"start":{"line":2,"column":6,"index":17},"end":{"line":2,"column":8,"index":19}}, + "properties": [] + }, + "body": { + "type": "EmptyStatement", + "start":20,"end":21,"loc":{"start":{"line":2,"column":9,"index":20},"end":{"line":2,"column":10,"index":21}} + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/incorrect-arity/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/incorrect-arity/input.js new file mode 100644 index 000000000000..7f6df49300e8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/incorrect-arity/input.js @@ -0,0 +1,2 @@ +import(); +import("./foo.json", { with: { type: "json"} }, "unsupported"); diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes/incorrect-arity/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/incorrect-arity/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-attributes/incorrect-arity/output.json rename to packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/incorrect-arity/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-export-without-from/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-export-without-from/input.js new file mode 100644 index 000000000000..fe8bb1d93d4d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-export-without-from/input.js @@ -0,0 +1,2 @@ +const foo = 1; +export { foo } with { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-export-without-from/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-export-without-from/options.json new file mode 100644 index 000000000000..302206c7125f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-export-without-from/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \"(\" (2:20)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-spread-element-import-call/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-spread-element-import-call/input.js new file mode 100644 index 000000000000..0a9420c378f9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-spread-element-import-call/input.js @@ -0,0 +1 @@ +import("./foo.json", ...[]); diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes/invalid-spread-element-import-call/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-spread-element-import-call/output.json similarity index 100% rename from packages/babel-parser/test/fixtures/experimental/import-attributes/invalid-spread-element-import-call/output.json rename to packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-spread-element-import-call/output.json diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-with-repeated-type/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-with-repeated-type/input.js new file mode 100644 index 000000000000..2559fb8f1aa8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-with-repeated-type/input.js @@ -0,0 +1 @@ +export { default as foo } from "foo.json" with { type: "json", type: "html" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-with-repeated-type/output.json new file mode 100644 index 000000000000..7c8bed5fcfca --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-with-repeated-type/output.json @@ -0,0 +1,84 @@ +{ + "type": "File", + "start":0,"end":78,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":78,"index":78}}, + "errors": [ + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes. (1:63)" + ], + "program": { + "type": "Program", + "start":0,"end":78,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":78,"index":78}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":78,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":78,"index":78}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":23,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":23,"index":23}}, + "local": { + "type": "Identifier", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16},"identifierName":"default"}, + "name": "default" + }, + "exported": { + "type": "Identifier", + "start":20,"end":23,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":23,"index":23},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":31,"end":41,"loc":{"start":{"line":1,"column":31,"index":31},"end":{"line":1,"column":41,"index":41}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "attributes": [ + { + "type": "ImportAttribute", + "start":49,"end":61,"loc":{"start":{"line":1,"column":49,"index":49},"end":{"line":1,"column":61,"index":61}}, + "key": { + "type": "Identifier", + "start":49,"end":53,"loc":{"start":{"line":1,"column":49,"index":49},"end":{"line":1,"column":53,"index":53},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":55,"end":61,"loc":{"start":{"line":1,"column":55,"index":55},"end":{"line":1,"column":61,"index":61}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start":63,"end":75,"loc":{"start":{"line":1,"column":63,"index":63},"end":{"line":1,"column":75,"index":75}}, + "key": { + "type": "Identifier", + "start":63,"end":67,"loc":{"start":{"line":1,"column":63,"index":63},"end":{"line":1,"column":67,"index":67},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":69,"end":75,"loc":{"start":{"line":1,"column":69,"index":69},"end":{"line":1,"column":75,"index":75}}, + "extra": { + "rawValue": "html", + "raw": "\"html\"" + }, + "value": "html" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-without-attributes-identifier/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-without-attributes-identifier/input.js new file mode 100644 index 000000000000..4465bae1500e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-without-attributes-identifier/input.js @@ -0,0 +1 @@ +export { x } from "foo" with; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-without-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-without-attributes-identifier/options.json new file mode 100644 index 000000000000..162ec22ec51b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-export-without-attributes-identifier/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \"{\" (1:28)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type-string/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type-string/input.js new file mode 100644 index 000000000000..8959e2b5bbfa --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type-string/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with { "type": "json", type: "html", "type": "js" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type-string/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type-string/output.json new file mode 100644 index 000000000000..d35a44ec2db6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type-string/output.json @@ -0,0 +1,105 @@ +{ + "type": "File", + "start":0,"end":79,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":79,"index":79}}, + "errors": [ + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes. (1:50)", + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes. (1:64)" + ], + "program": { + "type": "Program", + "start":0,"end":79,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":79,"index":79}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":79,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":79,"index":79}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":26,"index":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":34,"end":48,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":48,"index":48}}, + "key": { + "type": "StringLiteral", + "start":34,"end":40,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":40,"index":40}}, + "extra": { + "rawValue": "type", + "raw": "\"type\"" + }, + "value": "type" + }, + "value": { + "type": "StringLiteral", + "start":42,"end":48,"loc":{"start":{"line":1,"column":42,"index":42},"end":{"line":1,"column":48,"index":48}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start":50,"end":62,"loc":{"start":{"line":1,"column":50,"index":50},"end":{"line":1,"column":62,"index":62}}, + "key": { + "type": "Identifier", + "start":50,"end":54,"loc":{"start":{"line":1,"column":50,"index":50},"end":{"line":1,"column":54,"index":54},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":56,"end":62,"loc":{"start":{"line":1,"column":56,"index":56},"end":{"line":1,"column":62,"index":62}}, + "extra": { + "rawValue": "html", + "raw": "\"html\"" + }, + "value": "html" + } + }, + { + "type": "ImportAttribute", + "start":64,"end":76,"loc":{"start":{"line":1,"column":64,"index":64},"end":{"line":1,"column":76,"index":76}}, + "key": { + "type": "StringLiteral", + "start":64,"end":70,"loc":{"start":{"line":1,"column":64,"index":64},"end":{"line":1,"column":70,"index":70}}, + "extra": { + "rawValue": "type", + "raw": "\"type\"" + }, + "value": "type" + }, + "value": { + "type": "StringLiteral", + "start":72,"end":76,"loc":{"start":{"line":1,"column":72,"index":72},"end":{"line":1,"column":76,"index":76}}, + "extra": { + "rawValue": "js", + "raw": "\"js\"" + }, + "value": "js" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type/input.js new file mode 100644 index 000000000000..e78284007f9d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with { type: "json", type: "html" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type/output.json new file mode 100644 index 000000000000..ddfd540e2fd9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-with-repeated-type/output.json @@ -0,0 +1,78 @@ +{ + "type": "File", + "start":0,"end":63,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":63,"index":63}}, + "errors": [ + "SyntaxError: Duplicate key \"type\" is not allowed in module attributes. (1:48)" + ], + "program": { + "type": "Program", + "start":0,"end":63,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":63,"index":63}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":63,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":63,"index":63}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":26,"index":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":34,"end":46,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":46,"index":46}}, + "key": { + "type": "Identifier", + "start":34,"end":38,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":38,"index":38},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":40,"end":46,"loc":{"start":{"line":1,"column":40,"index":40},"end":{"line":1,"column":46,"index":46}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start":48,"end":60,"loc":{"start":{"line":1,"column":48,"index":48},"end":{"line":1,"column":60,"index":60}}, + "key": { + "type": "Identifier", + "start":48,"end":52,"loc":{"start":{"line":1,"column":48,"index":48},"end":{"line":1,"column":52,"index":52},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":54,"end":60,"loc":{"start":{"line":1,"column":54,"index":54},"end":{"line":1,"column":60,"index":60}}, + "extra": { + "rawValue": "html", + "raw": "\"html\"" + }, + "value": "html" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-without-attributes-identifier/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-without-attributes-identifier/input.js new file mode 100644 index 000000000000..af54005dade7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-without-attributes-identifier/input.js @@ -0,0 +1 @@ +import "x" with; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-without-attributes-identifier/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-without-attributes-identifier/options.json new file mode 100644 index 000000000000..78ef8d224620 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-syntax-without-attributes-identifier/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \"{\" (1:15)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-with-entry-no-colon/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-with-entry-no-colon/input.js new file mode 100644 index 000000000000..a2083b0faf65 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-with-entry-no-colon/input.js @@ -0,0 +1 @@ +import "foo" with { type, "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-with-entry-no-colon/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-with-entry-no-colon/options.json new file mode 100644 index 000000000000..31e1035d1bbf --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/invalid-with-entry-no-colon/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token, expected \":\" (1:24)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/linebreak-before-with/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/linebreak-before-with/input.js new file mode 100644 index 000000000000..2320cac464a0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/linebreak-before-with/input.js @@ -0,0 +1,2 @@ +import "foo" +with { type: "json" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/linebreak-before-with/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/linebreak-before-with/output.json new file mode 100644 index 000000000000..aced338989cc --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/linebreak-before-with/output.json @@ -0,0 +1,47 @@ +{ + "type": "File", + "start":0,"end":34,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":21,"index":34}}, + "program": { + "type": "Program", + "start":0,"end":34,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":21,"index":34}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":34,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":21,"index":34}}, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start":7,"end":12,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":12,"index":12}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":20,"end":32,"loc":{"start":{"line":2,"column":7,"index":20},"end":{"line":2,"column":19,"index":32}}, + "key": { + "type": "Identifier", + "start":20,"end":24,"loc":{"start":{"line":2,"column":7,"index":20},"end":{"line":2,"column":11,"index":24},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":26,"end":32,"loc":{"start":{"line":2,"column":13,"index":26},"end":{"line":2,"column":19,"index":32}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/options.json new file mode 100644 index 000000000000..16d941092ef8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/options.json @@ -0,0 +1,5 @@ +{ + "createImportExpressions": false, + "plugins": ["importAttributes"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/string-literal/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/string-literal/input.js new file mode 100644 index 000000000000..8af887a3cc06 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/string-literal/input.js @@ -0,0 +1,2 @@ +import foo from "foo.json" with { for: "for" } +export { foo } from "foo.json" with { for: "for" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/string-literal/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/string-literal/output.json new file mode 100644 index 000000000000..9477e4bcc226 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/string-literal/output.json @@ -0,0 +1,107 @@ +{ + "type": "File", + "start":0,"end":97,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":50,"index":97}}, + "program": { + "type": "Program", + "start":0,"end":97,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":50,"index":97}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":26,"index":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":34,"end":44,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":44,"index":44}}, + "key": { + "type": "Identifier", + "start":34,"end":37,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":37,"index":37},"identifierName":"for"}, + "name": "for" + }, + "value": { + "type": "StringLiteral", + "start":39,"end":44,"loc":{"start":{"line":1,"column":39,"index":39},"end":{"line":1,"column":44,"index":44}}, + "extra": { + "rawValue": "for", + "raw": "\"for\"" + }, + "value": "for" + } + } + ] + }, + { + "type": "ExportNamedDeclaration", + "start":47,"end":97,"loc":{"start":{"line":2,"column":0,"index":47},"end":{"line":2,"column":50,"index":97}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":56,"end":59,"loc":{"start":{"line":2,"column":9,"index":56},"end":{"line":2,"column":12,"index":59}}, + "local": { + "type": "Identifier", + "start":56,"end":59,"loc":{"start":{"line":2,"column":9,"index":56},"end":{"line":2,"column":12,"index":59},"identifierName":"foo"}, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start":56,"end":59,"loc":{"start":{"line":2,"column":9,"index":56},"end":{"line":2,"column":12,"index":59},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":67,"end":77,"loc":{"start":{"line":2,"column":20,"index":67},"end":{"line":2,"column":30,"index":77}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "attributes": [ + { + "type": "ImportAttribute", + "start":85,"end":95,"loc":{"start":{"line":2,"column":38,"index":85},"end":{"line":2,"column":48,"index":95}}, + "key": { + "type": "Identifier", + "start":85,"end":88,"loc":{"start":{"line":2,"column":38,"index":85},"end":{"line":2,"column":41,"index":88},"identifierName":"for"}, + "name": "for" + }, + "value": { + "type": "StringLiteral", + "start":90,"end":95,"loc":{"start":{"line":2,"column":43,"index":90},"end":{"line":2,"column":48,"index":95}}, + "extra": { + "rawValue": "for", + "raw": "\"for\"" + }, + "value": "for" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma-dynamic/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma-dynamic/input.js new file mode 100644 index 000000000000..0be461789922 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma-dynamic/input.js @@ -0,0 +1,2 @@ +import("foo.js",); +import("foo.json", { with: { type: "json" } },); diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma-dynamic/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma-dynamic/output.json new file mode 100644 index 000000000000..c532394181e4 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma-dynamic/output.json @@ -0,0 +1,110 @@ +{ + "type": "File", + "start":0,"end":67,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":48,"index":67}}, + "program": { + "type": "Program", + "start":0,"end":67,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":48,"index":67}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}}, + "expression": { + "type": "CallExpression", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, + "callee": { + "type": "Import", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}} + }, + "extra": { + "trailingComma": 15 + }, + "arguments": [ + { + "type": "StringLiteral", + "start":7,"end":15,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":15,"index":15}}, + "extra": { + "rawValue": "foo.js", + "raw": "\"foo.js\"" + }, + "value": "foo.js" + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":19,"end":67,"loc":{"start":{"line":2,"column":0,"index":19},"end":{"line":2,"column":48,"index":67}}, + "expression": { + "type": "CallExpression", + "start":19,"end":66,"loc":{"start":{"line":2,"column":0,"index":19},"end":{"line":2,"column":47,"index":66}}, + "callee": { + "type": "Import", + "start":19,"end":25,"loc":{"start":{"line":2,"column":0,"index":19},"end":{"line":2,"column":6,"index":25}} + }, + "extra": { + "trailingComma": 64 + }, + "arguments": [ + { + "type": "StringLiteral", + "start":26,"end":36,"loc":{"start":{"line":2,"column":7,"index":26},"end":{"line":2,"column":17,"index":36}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + { + "type": "ObjectExpression", + "start":38,"end":64,"loc":{"start":{"line":2,"column":19,"index":38},"end":{"line":2,"column":45,"index":64}}, + "properties": [ + { + "type": "ObjectProperty", + "start":40,"end":62,"loc":{"start":{"line":2,"column":21,"index":40},"end":{"line":2,"column":43,"index":62}}, + "method": false, + "key": { + "type": "Identifier", + "start":40,"end":44,"loc":{"start":{"line":2,"column":21,"index":40},"end":{"line":2,"column":25,"index":44},"identifierName":"with"}, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":46,"end":62,"loc":{"start":{"line":2,"column":27,"index":46},"end":{"line":2,"column":43,"index":62}}, + "properties": [ + { + "type": "ObjectProperty", + "start":48,"end":60,"loc":{"start":{"line":2,"column":29,"index":48},"end":{"line":2,"column":41,"index":60}}, + "method": false, + "key": { + "type": "Identifier", + "start":48,"end":52,"loc":{"start":{"line":2,"column":29,"index":48},"end":{"line":2,"column":33,"index":52},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start":54,"end":60,"loc":{"start":{"line":2,"column":35,"index":54},"end":{"line":2,"column":41,"index":60}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + } + ] + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma/input.js new file mode 100644 index 000000000000..10bc32278776 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma/input.js @@ -0,0 +1,2 @@ +import foo from "foo" with { type: "json", } +export { default as foo } from "foo" with { type: "json", } diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma/output.json new file mode 100644 index 000000000000..3150a0048129 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/trailing-comma/output.json @@ -0,0 +1,107 @@ +{ + "type": "File", + "start":0,"end":104,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":59,"index":104}}, + "program": { + "type": "Program", + "start":0,"end":104,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":59,"index":104}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":44,"index":44}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":21,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":21,"index":21}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":29,"end":41,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":41,"index":41}}, + "key": { + "type": "Identifier", + "start":29,"end":33,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":33,"index":33},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":35,"end":41,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":41,"index":41}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + }, + { + "type": "ExportNamedDeclaration", + "start":45,"end":104,"loc":{"start":{"line":2,"column":0,"index":45},"end":{"line":2,"column":59,"index":104}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":54,"end":68,"loc":{"start":{"line":2,"column":9,"index":54},"end":{"line":2,"column":23,"index":68}}, + "local": { + "type": "Identifier", + "start":54,"end":61,"loc":{"start":{"line":2,"column":9,"index":54},"end":{"line":2,"column":16,"index":61},"identifierName":"default"}, + "name": "default" + }, + "exported": { + "type": "Identifier", + "start":65,"end":68,"loc":{"start":{"line":2,"column":20,"index":65},"end":{"line":2,"column":23,"index":68},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":76,"end":81,"loc":{"start":{"line":2,"column":31,"index":76},"end":{"line":2,"column":36,"index":81}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "declaration": null, + "attributes": [ + { + "type": "ImportAttribute", + "start":89,"end":101,"loc":{"start":{"line":2,"column":44,"index":89},"end":{"line":2,"column":56,"index":101}}, + "key": { + "type": "Identifier", + "start":89,"end":93,"loc":{"start":{"line":2,"column":44,"index":89},"end":{"line":2,"column":48,"index":93},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":95,"end":101,"loc":{"start":{"line":2,"column":50,"index":95},"end":{"line":2,"column":56,"index":101}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-empty-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-empty-attribute/input.js new file mode 100644 index 000000000000..8bde57a92d52 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-empty-attribute/input.js @@ -0,0 +1,4 @@ +import foo, { bar } from "foo" with {}; +import * as ns from "foo" with {}; +export { quux } from "foo" with {}; +export * as ns2 from "foo" with {}; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-empty-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-empty-attribute/output.json new file mode 100644 index 000000000000..ed172f70aeff --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-empty-attribute/output.json @@ -0,0 +1,133 @@ +{ + "type": "File", + "start":0,"end":146,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":35,"index":146}}, + "program": { + "type": "Program", + "start":0,"end":146,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":35,"index":146}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":39,"index":39}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10},"identifierName":"foo"}, + "name": "foo" + } + }, + { + "type": "ImportSpecifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17}}, + "imported": { + "type": "Identifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17},"identifierName":"bar"}, + "name": "bar" + }, + "local": { + "type": "Identifier", + "start":14,"end":17,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":17,"index":17},"identifierName":"bar"}, + "name": "bar" + } + } + ], + "source": { + "type": "StringLiteral", + "start":25,"end":30,"loc":{"start":{"line":1,"column":25,"index":25},"end":{"line":1,"column":30,"index":30}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "attributes": [] + }, + { + "type": "ImportDeclaration", + "start":40,"end":74,"loc":{"start":{"line":2,"column":0,"index":40},"end":{"line":2,"column":34,"index":74}}, + "specifiers": [ + { + "type": "ImportNamespaceSpecifier", + "start":47,"end":54,"loc":{"start":{"line":2,"column":7,"index":47},"end":{"line":2,"column":14,"index":54}}, + "local": { + "type": "Identifier", + "start":52,"end":54,"loc":{"start":{"line":2,"column":12,"index":52},"end":{"line":2,"column":14,"index":54},"identifierName":"ns"}, + "name": "ns" + } + } + ], + "source": { + "type": "StringLiteral", + "start":60,"end":65,"loc":{"start":{"line":2,"column":20,"index":60},"end":{"line":2,"column":25,"index":65}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "attributes": [] + }, + { + "type": "ExportNamedDeclaration", + "start":75,"end":110,"loc":{"start":{"line":3,"column":0,"index":75},"end":{"line":3,"column":35,"index":110}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":84,"end":88,"loc":{"start":{"line":3,"column":9,"index":84},"end":{"line":3,"column":13,"index":88}}, + "local": { + "type": "Identifier", + "start":84,"end":88,"loc":{"start":{"line":3,"column":9,"index":84},"end":{"line":3,"column":13,"index":88},"identifierName":"quux"}, + "name": "quux" + }, + "exported": { + "type": "Identifier", + "start":84,"end":88,"loc":{"start":{"line":3,"column":9,"index":84},"end":{"line":3,"column":13,"index":88},"identifierName":"quux"}, + "name": "quux" + } + } + ], + "source": { + "type": "StringLiteral", + "start":96,"end":101,"loc":{"start":{"line":3,"column":21,"index":96},"end":{"line":3,"column":26,"index":101}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "declaration": null, + "attributes": [] + }, + { + "type": "ExportNamedDeclaration", + "start":111,"end":146,"loc":{"start":{"line":4,"column":0,"index":111},"end":{"line":4,"column":35,"index":146}}, + "specifiers": [ + { + "type": "ExportNamespaceSpecifier", + "start":118,"end":126,"loc":{"start":{"line":4,"column":7,"index":118},"end":{"line":4,"column":15,"index":126}}, + "exported": { + "type": "Identifier", + "start":123,"end":126,"loc":{"start":{"line":4,"column":12,"index":123},"end":{"line":4,"column":15,"index":126},"identifierName":"ns2"}, + "name": "ns2" + } + } + ], + "source": { + "type": "StringLiteral", + "start":132,"end":137,"loc":{"start":{"line":4,"column":21,"index":132},"end":{"line":4,"column":26,"index":137}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "attributes": [] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-class/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-class/input.js new file mode 100644 index 000000000000..223d0a8b395b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-class/input.js @@ -0,0 +1 @@ +export class Foo {} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-class/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-class/output.json new file mode 100644 index 000000000000..f3d5a6da191c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-class/output.json @@ -0,0 +1,34 @@ +{ + "type": "File", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}}, + "program": { + "type": "Program", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":19,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":19,"index":19}}, + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start":7,"end":19,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":19,"index":19}}, + "id": { + "type": "Identifier", + "start":13,"end":16,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":16,"index":16},"identifierName":"Foo"}, + "name": "Foo" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":17,"end":19,"loc":{"start":{"line":1,"column":17,"index":17},"end":{"line":1,"column":19,"index":19}}, + "body": [] + } + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-function/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-function/input.js new file mode 100644 index 000000000000..f99d4277774f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-function/input.js @@ -0,0 +1 @@ +export function foo() {} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-function/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-function/output.json new file mode 100644 index 000000000000..8644fb8db119 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-function/output.json @@ -0,0 +1,37 @@ +{ + "type": "File", + "start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}}, + "program": { + "type": "Program", + "start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}}, + "specifiers": [], + "source": null, + "declaration": { + "type": "FunctionDeclaration", + "start":7,"end":24,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":24,"index":24}}, + "id": { + "type": "Identifier", + "start":16,"end":19,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":19,"index":19},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":22,"end":24,"loc":{"start":{"line":1,"column":22,"index":22},"end":{"line":1,"column":24,"index":24}}, + "body": [], + "directives": [] + } + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-variable/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-variable/input.js new file mode 100644 index 000000000000..b30bd489f951 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-variable/input.js @@ -0,0 +1 @@ +export const foo = ""; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-variable/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-variable/output.json new file mode 100644 index 000000000000..79a96b788df5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-variable/output.json @@ -0,0 +1,44 @@ +{ + "type": "File", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}}, + "program": { + "type": "Program", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":22,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":22,"index":22}}, + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start":7,"end":22,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":22,"index":22}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":13,"end":21,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":21,"index":21}}, + "id": { + "type": "Identifier", + "start":13,"end":16,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":16,"index":16},"identifierName":"foo"}, + "name": "foo" + }, + "init": { + "type": "StringLiteral", + "start":19,"end":21,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":21,"index":21}}, + "extra": { + "rawValue": "", + "raw": "\"\"" + }, + "value": "" + } + } + ], + "kind": "const" + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-without-from/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-without-from/input.js new file mode 100644 index 000000000000..b8017e6930e7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-without-from/input.js @@ -0,0 +1,3 @@ +const foo = ""; + +export { foo }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-without-from/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-without-from/output.json new file mode 100644 index 000000000000..03a1202728af --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-export-without-from/output.json @@ -0,0 +1,60 @@ +{ + "type": "File", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":15,"index":32}}, + "program": { + "type": "Program", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":15,"index":32}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":6,"end":14,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":14,"index":14}}, + "id": { + "type": "Identifier", + "start":6,"end":9,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":9,"index":9},"identifierName":"foo"}, + "name": "foo" + }, + "init": { + "type": "StringLiteral", + "start":12,"end":14,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":14,"index":14}}, + "extra": { + "rawValue": "", + "raw": "\"\"" + }, + "value": "" + } + } + ], + "kind": "const" + }, + { + "type": "ExportNamedDeclaration", + "start":17,"end":32,"loc":{"start":{"line":3,"column":0,"index":17},"end":{"line":3,"column":15,"index":32}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":26,"end":29,"loc":{"start":{"line":3,"column":9,"index":26},"end":{"line":3,"column":12,"index":29}}, + "local": { + "type": "Identifier", + "start":26,"end":29,"loc":{"start":{"line":3,"column":9,"index":26},"end":{"line":3,"column":12,"index":29},"identifierName":"foo"}, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start":26,"end":29,"loc":{"start":{"line":3,"column":9,"index":26},"end":{"line":3,"column":12,"index":29},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": null, + "declaration": null + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-string-attribute-key/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-string-attribute-key/input.js new file mode 100644 index 000000000000..74070b3d002e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-string-attribute-key/input.js @@ -0,0 +1 @@ +import "foo" with { "type": "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-string-attribute-key/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-string-attribute-key/output.json new file mode 100644 index 000000000000..12924620911d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-string-attribute-key/output.json @@ -0,0 +1,51 @@ +{ + "type": "File", + "start":0,"end":37,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":37,"index":37}}, + "program": { + "type": "Program", + "start":0,"end":37,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":37,"index":37}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":37,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":37,"index":37}}, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start":7,"end":12,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":12,"index":12}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":20,"end":34,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":34,"index":34}}, + "key": { + "type": "StringLiteral", + "start":20,"end":26,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":26,"index":26}}, + "extra": { + "rawValue": "type", + "raw": "\"type\"" + }, + "value": "type" + }, + "value": { + "type": "StringLiteral", + "start":28,"end":34,"loc":{"start":{"line":1,"column":28,"index":28},"end":{"line":1,"column":34,"index":34}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-as-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-as-with-attributes/input.js new file mode 100644 index 000000000000..4e9fbefb8919 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-as-with-attributes/input.js @@ -0,0 +1 @@ +export * as foo from "foo.json" with { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-as-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-as-with-attributes/output.json new file mode 100644 index 000000000000..0bc31dfbd1b5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-as-with-attributes/output.json @@ -0,0 +1,57 @@ +{ + "type": "File", + "start":0,"end":54,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":54,"index":54}}, + "program": { + "type": "Program", + "start":0,"end":54,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":54,"index":54}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":54,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":54,"index":54}}, + "specifiers": [ + { + "type": "ExportNamespaceSpecifier", + "start":7,"end":15,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":15,"index":15}}, + "exported": { + "type": "Identifier", + "start":12,"end":15,"loc":{"start":{"line":1,"column":12,"index":12},"end":{"line":1,"column":15,"index":15},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":21,"end":31,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":31,"index":31}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":39,"end":51,"loc":{"start":{"line":1,"column":39,"index":39},"end":{"line":1,"column":51,"index":51}}, + "key": { + "type": "Identifier", + "start":39,"end":43,"loc":{"start":{"line":1,"column":39,"index":39},"end":{"line":1,"column":43,"index":43},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":45,"end":51,"loc":{"start":{"line":1,"column":45,"index":45},"end":{"line":1,"column":51,"index":51}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-with-attributes/input.js new file mode 100644 index 000000000000..70566cfaf1f3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-with-attributes/input.js @@ -0,0 +1 @@ +export * from "foo.json" with { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-with-attributes/output.json new file mode 100644 index 000000000000..7d2294b4a354 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-star-with-attributes/output.json @@ -0,0 +1,46 @@ +{ + "type": "File", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":47,"index":47}}, + "program": { + "type": "Program", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":47,"index":47}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportAllDeclaration", + "start":0,"end":47,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":47,"index":47}}, + "source": { + "type": "StringLiteral", + "start":14,"end":24,"loc":{"start":{"line":1,"column":14,"index":14},"end":{"line":1,"column":24,"index":24}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":32,"end":44,"loc":{"start":{"line":1,"column":32,"index":32},"end":{"line":1,"column":44,"index":44}}, + "key": { + "type": "Identifier", + "start":32,"end":36,"loc":{"start":{"line":1,"column":32,"index":32},"end":{"line":1,"column":36,"index":36},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":38,"end":44,"loc":{"start":{"line":1,"column":38,"index":38},"end":{"line":1,"column":44,"index":44}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-and-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-and-attributes-multiple-lines/input.js new file mode 100644 index 000000000000..e8a58d263541 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-and-attributes-multiple-lines/input.js @@ -0,0 +1,2 @@ +export { default as x } from "foo" with +{ type: "json" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-and-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-and-attributes-multiple-lines/output.json new file mode 100644 index 000000000000..a5ede1e3b955 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-and-attributes-multiple-lines/output.json @@ -0,0 +1,63 @@ +{ + "type": "File", + "start":0,"end":56,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":16,"index":56}}, + "program": { + "type": "Program", + "start":0,"end":56,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":16,"index":56}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":56,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":16,"index":56}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":21,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":21,"index":21}}, + "local": { + "type": "Identifier", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16},"identifierName":"default"}, + "name": "default" + }, + "exported": { + "type": "Identifier", + "start":20,"end":21,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":21,"index":21},"identifierName":"x"}, + "name": "x" + } + } + ], + "source": { + "type": "StringLiteral", + "start":29,"end":34,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":34,"index":34}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "declaration": null, + "attributes": [ + { + "type": "ImportAttribute", + "start":42,"end":54,"loc":{"start":{"line":2,"column":2,"index":42},"end":{"line":2,"column":14,"index":54}}, + "key": { + "type": "Identifier", + "start":42,"end":46,"loc":{"start":{"line":2,"column":2,"index":42},"end":{"line":2,"column":6,"index":46},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":48,"end":54,"loc":{"start":{"line":2,"column":8,"index":48},"end":{"line":2,"column":14,"index":54}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes-and-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes-and-value/input.js new file mode 100644 index 000000000000..458d863897b5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes-and-value/input.js @@ -0,0 +1,2 @@ +export { "default" as x } from "foo" with { type: "json" } +[0] diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes-and-value/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes-and-value/output.json new file mode 100644 index 000000000000..21b9d8e6fae6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes-and-value/output.json @@ -0,0 +1,86 @@ +{ + "type": "File", + "start":0,"end":62,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":3,"index":62}}, + "program": { + "type": "Program", + "start":0,"end":62,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":3,"index":62}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":58,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":58,"index":58}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":23,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":23,"index":23}}, + "local": { + "type": "StringLiteral", + "start":9,"end":18,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":18,"index":18}}, + "extra": { + "rawValue": "default", + "raw": "\"default\"" + }, + "value": "default" + }, + "exported": { + "type": "Identifier", + "start":22,"end":23,"loc":{"start":{"line":1,"column":22,"index":22},"end":{"line":1,"column":23,"index":23},"identifierName":"x"}, + "name": "x" + } + } + ], + "source": { + "type": "StringLiteral", + "start":31,"end":36,"loc":{"start":{"line":1,"column":31,"index":31},"end":{"line":1,"column":36,"index":36}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "declaration": null, + "attributes": [ + { + "type": "ImportAttribute", + "start":44,"end":56,"loc":{"start":{"line":1,"column":44,"index":44},"end":{"line":1,"column":56,"index":56}}, + "key": { + "type": "Identifier", + "start":44,"end":48,"loc":{"start":{"line":1,"column":44,"index":44},"end":{"line":1,"column":48,"index":48},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":50,"end":56,"loc":{"start":{"line":1,"column":50,"index":50},"end":{"line":1,"column":56,"index":56}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + }, + { + "type": "ExpressionStatement", + "start":59,"end":62,"loc":{"start":{"line":2,"column":0,"index":59},"end":{"line":2,"column":3,"index":62}}, + "expression": { + "type": "ArrayExpression", + "start":59,"end":62,"loc":{"start":{"line":2,"column":0,"index":59},"end":{"line":2,"column":3,"index":62}}, + "elements": [ + { + "type": "NumericLiteral", + "start":60,"end":61,"loc":{"start":{"line":2,"column":1,"index":60},"end":{"line":2,"column":2,"index":61}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes/input.js new file mode 100644 index 000000000000..8d791f4fe480 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes/input.js @@ -0,0 +1 @@ +export { default as foo } from "foo.json" with { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes/output.json new file mode 100644 index 000000000000..b3f7ab928398 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-attributes/output.json @@ -0,0 +1,63 @@ +{ + "type": "File", + "start":0,"end":64,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":64,"index":64}}, + "program": { + "type": "Program", + "start":0,"end":64,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":64,"index":64}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":64,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":64,"index":64}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":23,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":23,"index":23}}, + "local": { + "type": "Identifier", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16},"identifierName":"default"}, + "name": "default" + }, + "exported": { + "type": "Identifier", + "start":20,"end":23,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":23,"index":23},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":31,"end":41,"loc":{"start":{"line":1,"column":31,"index":31},"end":{"line":1,"column":41,"index":41}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "attributes": [ + { + "type": "ImportAttribute", + "start":49,"end":61,"loc":{"start":{"line":1,"column":49,"index":49},"end":{"line":1,"column":61,"index":61}}, + "key": { + "type": "Identifier", + "start":49,"end":53,"loc":{"start":{"line":1,"column":49,"index":49},"end":{"line":1,"column":53,"index":53},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":55,"end":61,"loc":{"start":{"line":1,"column":55,"index":55},"end":{"line":1,"column":61,"index":61}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-invalid-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-invalid-value/input.js new file mode 100644 index 000000000000..7a70c7f04630 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-invalid-value/input.js @@ -0,0 +1 @@ +export { default } from "foo.json" with { type: "json", lazy: true, startAtLine: 1 }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-invalid-value/options.json new file mode 100644 index 000000000000..3b039e9a62e8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-invalid-value/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Only string literals are allowed as module attribute values. (1:62)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-no-type-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-no-type-attribute/input.js new file mode 100644 index 000000000000..4553d256b1cb --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-no-type-attribute/input.js @@ -0,0 +1 @@ +export { default } from "foo.json" with { lazy: "true" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-no-type-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-no-type-attribute/output.json new file mode 100644 index 000000000000..9c1d13f1237d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-no-type-attribute/output.json @@ -0,0 +1,63 @@ +{ + "type": "File", + "start":0,"end":57,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":57,"index":57}}, + "program": { + "type": "Program", + "start":0,"end":57,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":57,"index":57}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":57,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":57,"index":57}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16}}, + "local": { + "type": "Identifier", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16},"identifierName":"default"}, + "name": "default" + }, + "exported": { + "type": "Identifier", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16},"identifierName":"default"}, + "name": "default" + } + } + ], + "source": { + "type": "StringLiteral", + "start":24,"end":34,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":34,"index":34}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "attributes": [ + { + "type": "ImportAttribute", + "start":42,"end":54,"loc":{"start":{"line":1,"column":42,"index":42},"end":{"line":1,"column":54,"index":54}}, + "key": { + "type": "Identifier", + "start":42,"end":46,"loc":{"start":{"line":1,"column":42,"index":42},"end":{"line":1,"column":46,"index":46},"identifierName":"lazy"}, + "name": "lazy" + }, + "value": { + "type": "StringLiteral", + "start":48,"end":54,"loc":{"start":{"line":1,"column":48,"index":48},"end":{"line":1,"column":54,"index":54}}, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-object-method-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-object-method-attribute/input.js new file mode 100644 index 000000000000..0e8993714a3f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-object-method-attribute/input.js @@ -0,0 +1 @@ +export { default } from "foo.json" with { type: "json", hasOwnProperty: "true" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-object-method-attribute/output.json new file mode 100644 index 000000000000..375950a3df51 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-with-object-method-attribute/output.json @@ -0,0 +1,81 @@ +{ + "type": "File", + "start":0,"end":81,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":81,"index":81}}, + "program": { + "type": "Program", + "start":0,"end":81,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":81,"index":81}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":81,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":81,"index":81}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16}}, + "local": { + "type": "Identifier", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16},"identifierName":"default"}, + "name": "default" + }, + "exported": { + "type": "Identifier", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":16,"index":16},"identifierName":"default"}, + "name": "default" + } + } + ], + "source": { + "type": "StringLiteral", + "start":24,"end":34,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":34,"index":34}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "attributes": [ + { + "type": "ImportAttribute", + "start":42,"end":54,"loc":{"start":{"line":1,"column":42,"index":42},"end":{"line":1,"column":54,"index":54}}, + "key": { + "type": "Identifier", + "start":42,"end":46,"loc":{"start":{"line":1,"column":42,"index":42},"end":{"line":1,"column":46,"index":46},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":48,"end":54,"loc":{"start":{"line":1,"column":48,"index":48},"end":{"line":1,"column":54,"index":54}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start":56,"end":78,"loc":{"start":{"line":1,"column":56,"index":56},"end":{"line":1,"column":78,"index":78}}, + "key": { + "type": "Identifier", + "start":56,"end":70,"loc":{"start":{"line":1,"column":56,"index":56},"end":{"line":1,"column":70,"index":70},"identifierName":"hasOwnProperty"}, + "name": "hasOwnProperty" + }, + "value": { + "type": "StringLiteral", + "start":72,"end":78,"loc":{"start":{"line":1,"column":72,"index":72},"end":{"line":1,"column":78,"index":78}}, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-without-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-without-attributes/input.js new file mode 100644 index 000000000000..9408920b3f74 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-without-attributes/input.js @@ -0,0 +1 @@ +export { foo } from "foo.json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-without-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-without-attributes/output.json new file mode 100644 index 000000000000..65a4f9f1ed38 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-export-without-attributes/output.json @@ -0,0 +1,44 @@ +{ + "type": "File", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":31,"index":31}}, + "program": { + "type": "Program", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":31,"index":31}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":31,"index":31}}, + "specifiers": [ + { + "type": "ExportSpecifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":12,"index":12}}, + "local": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":12,"index":12},"identifierName":"foo"}, + "name": "foo" + }, + "exported": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":12,"index":12},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":20,"end":30,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":30,"index":30}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "declaration": null, + "attributes": [] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-and-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-and-value/input.js new file mode 100644 index 000000000000..b9907829ef29 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-and-value/input.js @@ -0,0 +1,2 @@ +import "x" with { type: "json" } +[0] diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-and-value/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-and-value/output.json new file mode 100644 index 000000000000..7bbeab6f8b80 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-and-value/output.json @@ -0,0 +1,66 @@ +{ + "type": "File", + "start":0,"end":36,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":3,"index":36}}, + "program": { + "type": "Program", + "start":0,"end":36,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":3,"index":36}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":32,"index":32}}, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "x" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":18,"end":30,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":30,"index":30}}, + "key": { + "type": "Identifier", + "start":18,"end":22,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":22,"index":22},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":24,"end":30,"loc":{"start":{"line":1,"column":24,"index":24},"end":{"line":1,"column":30,"index":30}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + }, + { + "type": "ExpressionStatement", + "start":33,"end":36,"loc":{"start":{"line":2,"column":0,"index":33},"end":{"line":2,"column":3,"index":36}}, + "expression": { + "type": "ArrayExpression", + "start":33,"end":36,"loc":{"start":{"line":2,"column":0,"index":33},"end":{"line":2,"column":3,"index":36}}, + "elements": [ + { + "type": "NumericLiteral", + "start":34,"end":35,"loc":{"start":{"line":2,"column":1,"index":34},"end":{"line":2,"column":2,"index":35}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-multiple-lines/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-multiple-lines/input.js new file mode 100644 index 000000000000..e78e2c90f72d --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-multiple-lines/input.js @@ -0,0 +1,2 @@ +import "x" with +{ type: "json" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-multiple-lines/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-multiple-lines/output.json new file mode 100644 index 000000000000..15988e0c945b --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes-multiple-lines/output.json @@ -0,0 +1,47 @@ +{ + "type": "File", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":16,"index":32}}, + "program": { + "type": "Program", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":16,"index":32}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":16,"index":32}}, + "specifiers": [], + "source": { + "type": "StringLiteral", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "extra": { + "rawValue": "x", + "raw": "\"x\"" + }, + "value": "x" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":18,"end":30,"loc":{"start":{"line":2,"column":2,"index":18},"end":{"line":2,"column":14,"index":30}}, + "key": { + "type": "Identifier", + "start":18,"end":22,"loc":{"start":{"line":2,"column":2,"index":18},"end":{"line":2,"column":6,"index":22},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":24,"end":30,"loc":{"start":{"line":2,"column":8,"index":24},"end":{"line":2,"column":14,"index":30}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes/input.js new file mode 100644 index 000000000000..83c51c971889 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes/output.json new file mode 100644 index 000000000000..de2bdc0bfb5f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-attributes/output.json @@ -0,0 +1,57 @@ +{ + "type": "File", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":49,"index":49}}, + "program": { + "type": "Program", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":49,"index":49}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":49,"index":49}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":26,"index":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":34,"end":46,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":46,"index":46}}, + "key": { + "type": "Identifier", + "start":34,"end":38,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":38,"index":38},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":40,"end":46,"loc":{"start":{"line":1,"column":40,"index":40},"end":{"line":1,"column":46,"index":46}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-invalid-value/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-invalid-value/input.js new file mode 100644 index 000000000000..3f350904e07f --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-invalid-value/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with { type: "json", lazy: true, startAtLine: 1 }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-invalid-value/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-invalid-value/options.json new file mode 100644 index 000000000000..9326901e4d93 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-invalid-value/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Only string literals are allowed as module attribute values. (1:54)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-no-type-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-no-type-attribute/input.js new file mode 100644 index 000000000000..1858e49c2022 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-no-type-attribute/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with { lazy: "true" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-no-type-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-no-type-attribute/output.json new file mode 100644 index 000000000000..97e5d13bd9e5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-no-type-attribute/output.json @@ -0,0 +1,57 @@ +{ + "type": "File", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":49,"index":49}}, + "program": { + "type": "Program", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":49,"index":49}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":49,"index":49}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":26,"index":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":34,"end":46,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":46,"index":46}}, + "key": { + "type": "Identifier", + "start":34,"end":38,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":38,"index":38},"identifierName":"lazy"}, + "name": "lazy" + }, + "value": { + "type": "StringLiteral", + "start":40,"end":46,"loc":{"start":{"line":1,"column":40,"index":40},"end":{"line":1,"column":46,"index":46}}, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-object-method-attribute/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-object-method-attribute/input.js new file mode 100644 index 000000000000..2ec8345c8e15 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-object-method-attribute/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with { type: "json", hasOwnProperty: "true" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-object-method-attribute/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-object-method-attribute/output.json new file mode 100644 index 000000000000..bc507378678e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-with-object-method-attribute/output.json @@ -0,0 +1,75 @@ +{ + "type": "File", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":73,"index":73}}, + "program": { + "type": "Program", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":73,"index":73}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":73,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":73,"index":73}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":26,"index":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [ + { + "type": "ImportAttribute", + "start":34,"end":46,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":46,"index":46}}, + "key": { + "type": "Identifier", + "start":34,"end":38,"loc":{"start":{"line":1,"column":34,"index":34},"end":{"line":1,"column":38,"index":38},"identifierName":"type"}, + "name": "type" + }, + "value": { + "type": "StringLiteral", + "start":40,"end":46,"loc":{"start":{"line":1,"column":40,"index":40},"end":{"line":1,"column":46,"index":46}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" + }, + "value": "json" + } + }, + { + "type": "ImportAttribute", + "start":48,"end":70,"loc":{"start":{"line":1,"column":48,"index":48},"end":{"line":1,"column":70,"index":70}}, + "key": { + "type": "Identifier", + "start":48,"end":62,"loc":{"start":{"line":1,"column":48,"index":48},"end":{"line":1,"column":62,"index":62},"identifierName":"hasOwnProperty"}, + "name": "hasOwnProperty" + }, + "value": { + "type": "StringLiteral", + "start":64,"end":70,"loc":{"start":{"line":1,"column":64,"index":64},"end":{"line":1,"column":70,"index":70}}, + "extra": { + "rawValue": "true", + "raw": "\"true\"" + }, + "value": "true" + } + } + ] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-without-attributes/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-without-attributes/input.js new file mode 100644 index 000000000000..92901ed56b38 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-without-attributes/input.js @@ -0,0 +1 @@ +import foo from "foo.json"; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-without-attributes/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-without-attributes/output.json new file mode 100644 index 000000000000..de12941db425 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/valid-syntax-without-attributes/output.json @@ -0,0 +1,38 @@ +{ + "type": "File", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}}, + "program": { + "type": "Program", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start":0,"end":27,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":27,"index":27}}, + "specifiers": [ + { + "type": "ImportDefaultSpecifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10}}, + "local": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":10,"index":10},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "source": { + "type": "StringLiteral", + "start":16,"end":26,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":26,"index":26}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" + }, + "value": "foo.json" + }, + "attributes": [] + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/without-plugin/input.js b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/without-plugin/input.js new file mode 100644 index 000000000000..83c51c971889 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/without-plugin/input.js @@ -0,0 +1 @@ +import foo from "foo.json" with { type: "json" }; diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/without-plugin/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/without-plugin/options.json new file mode 100644 index 000000000000..a1899f538d7e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-createImportExpression-false/without-plugin/options.json @@ -0,0 +1,5 @@ +{ + "plugins": [], + "sourceType": "module", + "throws": "This experimental syntax requires enabling the parser plugin: \"importAttributes\". (1:32)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/options.json index 49dd7647ae83..a0bb7e28a418 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes-deprecatedAssertKeyword/options.json @@ -1,6 +1,5 @@ { - "plugins": [ - ["importAttributes", { "deprecatedAssertSyntax": true }] - ], + "createImportExpressions": false, + "plugins": [["importAttributes", { "deprecatedAssertSyntax": true }]], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes/dynamic-import-with-valid-syntax/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes/dynamic-import-with-valid-syntax/output.json index 7583096e7b99..682053c07c09 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-attributes/dynamic-import-with-valid-syntax/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes/dynamic-import-with-valid-syntax/output.json @@ -11,68 +11,62 @@ "type": "ExpressionStatement", "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, "expression": { - "type": "CallExpression", + "type": "ImportExpression", "start":0,"end":46,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":46,"index":46}}, - "callee": { - "type": "Import", - "start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}} - }, - "arguments": [ - { - "type": "StringLiteral", - "start":7,"end":17,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":17,"index":17}}, - "extra": { - "rawValue": "foo.json", - "raw": "\"foo.json\"" - }, - "value": "foo.json" + "source": { + "type": "StringLiteral", + "start":7,"end":17,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":17,"index":17}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" }, - { - "type": "ObjectExpression", - "start":19,"end":45,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":45,"index":45}}, - "properties": [ - { - "type": "ObjectProperty", - "start":21,"end":43,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":43,"index":43}}, - "method": false, - "key": { - "type": "Identifier", - "start":21,"end":25,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":25,"index":25},"identifierName":"with"}, - "name": "with" - }, - "computed": false, - "shorthand": false, - "value": { - "type": "ObjectExpression", - "start":27,"end":43,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":43,"index":43}}, - "properties": [ - { - "type": "ObjectProperty", - "start":29,"end":41,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":41,"index":41}}, - "method": false, - "key": { - "type": "Identifier", - "start":29,"end":33,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":33,"index":33},"identifierName":"type"}, - "name": "type" + "value": "foo.json" + }, + "options": { + "type": "ObjectExpression", + "start":19,"end":45,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":45,"index":45}}, + "properties": [ + { + "type": "ObjectProperty", + "start":21,"end":43,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":43,"index":43}}, + "method": false, + "key": { + "type": "Identifier", + "start":21,"end":25,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":25,"index":25},"identifierName":"with"}, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":27,"end":43,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":43,"index":43}}, + "properties": [ + { + "type": "ObjectProperty", + "start":29,"end":41,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":41,"index":41}}, + "method": false, + "key": { + "type": "Identifier", + "start":29,"end":33,"loc":{"start":{"line":1,"column":29,"index":29},"end":{"line":1,"column":33,"index":33},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start":35,"end":41,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":41,"index":41}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" }, - "computed": false, - "shorthand": false, - "value": { - "type": "StringLiteral", - "start":35,"end":41,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":41,"index":41}}, - "extra": { - "rawValue": "json", - "raw": "\"json\"" - }, - "value": "json" - } + "value": "json" } - ] - } + } + ] } - ] - } - ] + } + ] + } } } ], diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes/incorrect-arity/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes/incorrect-arity/options.json new file mode 100644 index 000000000000..c958665c03e2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes/incorrect-arity/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (1:7)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes/invalid-spread-element-import-call/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes/invalid-spread-element-import-call/options.json new file mode 100644 index 000000000000..3e7adc2e54dd --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes/invalid-spread-element-import-call/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected token (1:21)" +} diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/import-attributes/options.json index cc7fb4a170d9..4e36f144ec15 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes/options.json @@ -1,4 +1,5 @@ { + "createImportExpressions": true, "plugins": ["importAttributes"], "sourceType": "module" } diff --git a/packages/babel-parser/test/fixtures/experimental/import-attributes/trailing-comma-dynamic/output.json b/packages/babel-parser/test/fixtures/experimental/import-attributes/trailing-comma-dynamic/output.json index c532394181e4..1a32eec9f35d 100644 --- a/packages/babel-parser/test/fixtures/experimental/import-attributes/trailing-comma-dynamic/output.json +++ b/packages/babel-parser/test/fixtures/experimental/import-attributes/trailing-comma-dynamic/output.json @@ -11,97 +11,80 @@ "type": "ExpressionStatement", "start":0,"end":18,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":18,"index":18}}, "expression": { - "type": "CallExpression", + "type": "ImportExpression", "start":0,"end":17,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":17,"index":17}}, - "callee": { - "type": "Import", - "start":0,"end":6,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":6,"index":6}} - }, - "extra": { - "trailingComma": 15 + "source": { + "type": "StringLiteral", + "start":7,"end":15,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":15,"index":15}}, + "extra": { + "rawValue": "foo.js", + "raw": "\"foo.js\"" + }, + "value": "foo.js" }, - "arguments": [ - { - "type": "StringLiteral", - "start":7,"end":15,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":15,"index":15}}, - "extra": { - "rawValue": "foo.js", - "raw": "\"foo.js\"" - }, - "value": "foo.js" - } - ] + "options": null } }, { "type": "ExpressionStatement", "start":19,"end":67,"loc":{"start":{"line":2,"column":0,"index":19},"end":{"line":2,"column":48,"index":67}}, "expression": { - "type": "CallExpression", + "type": "ImportExpression", "start":19,"end":66,"loc":{"start":{"line":2,"column":0,"index":19},"end":{"line":2,"column":47,"index":66}}, - "callee": { - "type": "Import", - "start":19,"end":25,"loc":{"start":{"line":2,"column":0,"index":19},"end":{"line":2,"column":6,"index":25}} - }, - "extra": { - "trailingComma": 64 - }, - "arguments": [ - { - "type": "StringLiteral", - "start":26,"end":36,"loc":{"start":{"line":2,"column":7,"index":26},"end":{"line":2,"column":17,"index":36}}, - "extra": { - "rawValue": "foo.json", - "raw": "\"foo.json\"" - }, - "value": "foo.json" + "source": { + "type": "StringLiteral", + "start":26,"end":36,"loc":{"start":{"line":2,"column":7,"index":26},"end":{"line":2,"column":17,"index":36}}, + "extra": { + "rawValue": "foo.json", + "raw": "\"foo.json\"" }, - { - "type": "ObjectExpression", - "start":38,"end":64,"loc":{"start":{"line":2,"column":19,"index":38},"end":{"line":2,"column":45,"index":64}}, - "properties": [ - { - "type": "ObjectProperty", - "start":40,"end":62,"loc":{"start":{"line":2,"column":21,"index":40},"end":{"line":2,"column":43,"index":62}}, - "method": false, - "key": { - "type": "Identifier", - "start":40,"end":44,"loc":{"start":{"line":2,"column":21,"index":40},"end":{"line":2,"column":25,"index":44},"identifierName":"with"}, - "name": "with" - }, - "computed": false, - "shorthand": false, - "value": { - "type": "ObjectExpression", - "start":46,"end":62,"loc":{"start":{"line":2,"column":27,"index":46},"end":{"line":2,"column":43,"index":62}}, - "properties": [ - { - "type": "ObjectProperty", - "start":48,"end":60,"loc":{"start":{"line":2,"column":29,"index":48},"end":{"line":2,"column":41,"index":60}}, - "method": false, - "key": { - "type": "Identifier", - "start":48,"end":52,"loc":{"start":{"line":2,"column":29,"index":48},"end":{"line":2,"column":33,"index":52},"identifierName":"type"}, - "name": "type" + "value": "foo.json" + }, + "options": { + "type": "ObjectExpression", + "start":38,"end":64,"loc":{"start":{"line":2,"column":19,"index":38},"end":{"line":2,"column":45,"index":64}}, + "properties": [ + { + "type": "ObjectProperty", + "start":40,"end":62,"loc":{"start":{"line":2,"column":21,"index":40},"end":{"line":2,"column":43,"index":62}}, + "method": false, + "key": { + "type": "Identifier", + "start":40,"end":44,"loc":{"start":{"line":2,"column":21,"index":40},"end":{"line":2,"column":25,"index":44},"identifierName":"with"}, + "name": "with" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "ObjectExpression", + "start":46,"end":62,"loc":{"start":{"line":2,"column":27,"index":46},"end":{"line":2,"column":43,"index":62}}, + "properties": [ + { + "type": "ObjectProperty", + "start":48,"end":60,"loc":{"start":{"line":2,"column":29,"index":48},"end":{"line":2,"column":41,"index":60}}, + "method": false, + "key": { + "type": "Identifier", + "start":48,"end":52,"loc":{"start":{"line":2,"column":29,"index":48},"end":{"line":2,"column":33,"index":52},"identifierName":"type"}, + "name": "type" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "StringLiteral", + "start":54,"end":60,"loc":{"start":{"line":2,"column":35,"index":54},"end":{"line":2,"column":41,"index":60}}, + "extra": { + "rawValue": "json", + "raw": "\"json\"" }, - "computed": false, - "shorthand": false, - "value": { - "type": "StringLiteral", - "start":54,"end":60,"loc":{"start":{"line":2,"column":35,"index":54},"end":{"line":2,"column":41,"index":60}}, - "extra": { - "rawValue": "json", - "raw": "\"json\"" - }, - "value": "json" - } + "value": "json" } - ] - } + } + ] } - ] - } - ] + } + ] + } } } ], diff --git a/packages/babel-parser/test/fixtures/experimental/module-attributes/options.json b/packages/babel-parser/test/fixtures/experimental/module-attributes/options.json index 29a3f0e84167..1d947d92eb51 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-attributes/options.json +++ b/packages/babel-parser/test/fixtures/experimental/module-attributes/options.json @@ -1,3 +1,4 @@ { - "BABEL_8_BREAKING": false + "BABEL_8_BREAKING": false, + "createImportExpressions": false } diff --git a/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks-createImportExpression-false/input.js b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks-createImportExpression-false/input.js new file mode 100644 index 000000000000..a0c4e3815195 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks-createImportExpression-false/input.js @@ -0,0 +1,4 @@ +const m = module { + export const foo = "foo"; +}; +import(module); diff --git a/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks-createImportExpression-false/options.json b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks-createImportExpression-false/options.json new file mode 100644 index 000000000000..8977da6fcc7e --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks-createImportExpression-false/options.json @@ -0,0 +1,3 @@ +{ + "createImportExpressions": false +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks-createImportExpression-false/output.json b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks-createImportExpression-false/output.json new file mode 100644 index 000000000000..2f6eb776e1e1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks-createImportExpression-false/output.json @@ -0,0 +1,92 @@ +{ + "type": "File", + "start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":15,"index":65}}, + "program": { + "type": "Program", + "start":0,"end":65,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":4,"column":15,"index":65}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start":0,"end":49,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":2,"index":49}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":6,"end":48,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":3,"column":1,"index":48}}, + "id": { + "type": "Identifier", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":7,"index":7},"identifierName":"m"}, + "name": "m" + }, + "init": { + "type": "ModuleExpression", + "start":10,"end":48,"loc":{"start":{"line":1,"column":10,"index":10},"end":{"line":3,"column":1,"index":48}}, + "body": { + "type": "Program", + "start":18,"end":47,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":3,"column":0,"index":47}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":21,"end":46,"loc":{"start":{"line":2,"column":2,"index":21},"end":{"line":2,"column":27,"index":46}}, + "specifiers": [], + "source": null, + "declaration": { + "type": "VariableDeclaration", + "start":28,"end":46,"loc":{"start":{"line":2,"column":9,"index":28},"end":{"line":2,"column":27,"index":46}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":34,"end":45,"loc":{"start":{"line":2,"column":15,"index":34},"end":{"line":2,"column":26,"index":45}}, + "id": { + "type": "Identifier", + "start":34,"end":37,"loc":{"start":{"line":2,"column":15,"index":34},"end":{"line":2,"column":18,"index":37},"identifierName":"foo"}, + "name": "foo" + }, + "init": { + "type": "StringLiteral", + "start":40,"end":45,"loc":{"start":{"line":2,"column":21,"index":40},"end":{"line":2,"column":26,"index":45}}, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + } + ], + "kind": "const" + } + } + ], + "directives": [] + } + } + } + ], + "kind": "const" + }, + { + "type": "ExpressionStatement", + "start":50,"end":65,"loc":{"start":{"line":4,"column":0,"index":50},"end":{"line":4,"column":15,"index":65}}, + "expression": { + "type": "CallExpression", + "start":50,"end":64,"loc":{"start":{"line":4,"column":0,"index":50},"end":{"line":4,"column":14,"index":64}}, + "callee": { + "type": "Import", + "start":50,"end":56,"loc":{"start":{"line":4,"column":0,"index":50},"end":{"line":4,"column":6,"index":56}} + }, + "arguments": [ + { + "type": "Identifier", + "start":57,"end":63,"loc":{"start":{"line":4,"column":7,"index":57},"end":{"line":4,"column":13,"index":63},"identifierName":"module"}, + "name": "module" + } + ] + } + } + ], + "directives": [] + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks/options.json b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks/options.json new file mode 100644 index 000000000000..fc7a8f3052bc --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks/options.json @@ -0,0 +1,3 @@ +{ + "createImportExpressions": true +} diff --git a/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks/output.json b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks/output.json index 2f6eb776e1e1..b67f25f4f839 100644 --- a/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks/output.json +++ b/packages/babel-parser/test/fixtures/experimental/module-blocks/valid-import-module-blocks/output.json @@ -71,19 +71,13 @@ "type": "ExpressionStatement", "start":50,"end":65,"loc":{"start":{"line":4,"column":0,"index":50},"end":{"line":4,"column":15,"index":65}}, "expression": { - "type": "CallExpression", + "type": "ImportExpression", "start":50,"end":64,"loc":{"start":{"line":4,"column":0,"index":50},"end":{"line":4,"column":14,"index":64}}, - "callee": { - "type": "Import", - "start":50,"end":56,"loc":{"start":{"line":4,"column":0,"index":50},"end":{"line":4,"column":6,"index":56}} - }, - "arguments": [ - { - "type": "Identifier", - "start":57,"end":63,"loc":{"start":{"line":4,"column":7,"index":57},"end":{"line":4,"column":13,"index":63},"identifierName":"module"}, - "name": "module" - } - ] + "source": { + "type": "Identifier", + "start":57,"end":63,"loc":{"start":{"line":4,"column":7,"index":57},"end":{"line":4,"column":13,"index":63},"identifierName":"module"}, + "name": "module" + } } } ], diff --git a/packages/babel-parser/typings/babel-parser.d.ts b/packages/babel-parser/typings/babel-parser.d.ts index a74b1bc46da8..f5f8f5f6146f 100644 --- a/packages/babel-parser/typings/babel-parser.d.ts +++ b/packages/babel-parser/typings/babel-parser.d.ts @@ -1,6 +1,6 @@ // This file is auto-generated! Do not modify it directly. /* eslint-disable import/no-extraneous-dependencies, @typescript-eslint/consistent-type-imports, prettier/prettier */ -import * as _babel_types from '@babel/types'; +import * as _babel_types from "@babel/types"; type Plugin = | "asyncDoExpressions" @@ -219,11 +219,16 @@ interface ParserOptions { * AST nodes instead of using the `extra` property. */ createParenthesizedExpressions?: boolean; + + /** + * By default, `import(foo)` is parsed as `CallExpression(Import, [Identifier(foo)])`. + * Set this to true to parse it as an `ImportExpression` node. + */ + createImportExpressions?: boolean; } type ParserPlugin = PluginConfig; - declare const tokTypes: { // todo(flow->ts) real token type [name: string]: any; @@ -238,4 +243,18 @@ type ParseResult = Result & { errors: ParseError[]; }; -export { DecoratorsPluginOptions, FlowPluginOptions, ParseError, ParseResult, ParserOptions, ParserPlugin, ParserPluginWithOptions, PipelineOperatorPluginOptions, RecordAndTuplePluginOptions, TypeScriptPluginOptions, parse, parseExpression, tokTypes }; +export { + DecoratorsPluginOptions, + FlowPluginOptions, + ParseError, + ParseResult, + ParserOptions, + ParserPlugin, + ParserPluginWithOptions, + PipelineOperatorPluginOptions, + RecordAndTuplePluginOptions, + TypeScriptPluginOptions, + parse, + parseExpression, + tokTypes, +}; diff --git a/packages/babel-parser/typings/babel-parser.source.d.ts b/packages/babel-parser/typings/babel-parser.source.d.ts index 32887ef14ea6..732ccf7c8df2 100644 --- a/packages/babel-parser/typings/babel-parser.source.d.ts +++ b/packages/babel-parser/typings/babel-parser.source.d.ts @@ -135,6 +135,12 @@ export interface ParserOptions { * AST nodes instead of using the `extra` property. */ createParenthesizedExpressions?: boolean; + + /** + * By default, `import(foo)` is parsed as `CallExpression(Import, [Identifier(foo)])`. + * Set this to true to parse it as an `ImportExpression` node. + */ + createImportExpressions?: boolean; } export type ParserPlugin = import("../src/typings").PluginConfig; diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin-babel-7/input.mjs b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/missing-plugin/input.mjs similarity index 100% rename from packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin-babel-7/input.mjs rename to packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/missing-plugin/input.mjs diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/missing-plugin/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/missing-plugin/options.json new file mode 100644 index 000000000000..8cbc099aab70 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/missing-plugin/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-modules-amd"] +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/missing-plugin/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/missing-plugin/output.js new file mode 100644 index 000000000000..90fcfd93e4b2 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/missing-plugin/output.js @@ -0,0 +1,5 @@ +define(["a"], function (_a) { + "use strict"; + + import("b"); +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/module/input.mjs b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/module/input.mjs new file mode 100644 index 000000000000..743a74ae3b73 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/module/input.mjs @@ -0,0 +1 @@ +var modP = import("mod"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/module/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/module/output.js new file mode 100644 index 000000000000..e0d86f169580 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/module/output.js @@ -0,0 +1,5 @@ +define(["require"], function (_require) { + "use strict"; + + var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)); +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/no-interop/input.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/no-interop/input.js new file mode 100644 index 000000000000..743a74ae3b73 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/no-interop/input.js @@ -0,0 +1 @@ +var modP = import("mod"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/no-interop/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/no-interop/options.json new file mode 100644 index 000000000000..1f5f3287a16f --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/no-interop/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "transform-dynamic-import", + ["transform-modules-amd", { "noInterop": true }] + ] +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/no-interop/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/no-interop/output.js new file mode 100644 index 000000000000..96dab9dce6f7 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/no-interop/output.js @@ -0,0 +1,3 @@ +define(["require"], function (_require) { + var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(imported), _reject)); +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/options.json new file mode 100644 index 000000000000..d18d00fd1522 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/options.json @@ -0,0 +1,6 @@ +{ + "plugins": ["transform-dynamic-import", "transform-modules-amd"], + "parserOpts": { + "createImportExpressions": false + } +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/script/input.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/script/input.js new file mode 100644 index 000000000000..743a74ae3b73 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/script/input.js @@ -0,0 +1 @@ +var modP = import("mod"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/script/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/script/output.js new file mode 100644 index 000000000000..0b8b11e86a92 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/script/output.js @@ -0,0 +1,3 @@ +define(["require"], function (_require) { + var modP = new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)); +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/to-string/input.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/to-string/input.js new file mode 100644 index 000000000000..130d3cafa5cb --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/to-string/input.js @@ -0,0 +1 @@ +import(2); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/to-string/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/to-string/output.js new file mode 100644 index 000000000000..f8ffd28bbf17 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/to-string/output.js @@ -0,0 +1,3 @@ +define(["require"], function (_require) { + (specifier => new Promise((_resolve, _reject) => _require([`${specifier}`], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)))(2); +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/with-other-import-export/input.mjs b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/with-other-import-export/input.mjs new file mode 100644 index 000000000000..4efa0f423b91 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/with-other-import-export/input.mjs @@ -0,0 +1,8 @@ +var mod; + +import foo from "foo"; + +import("mod").then(m => mod = m); + +export { mod }; + diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/with-other-import-export/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/with-other-import-export/output.js new file mode 100644 index 000000000000..d5871c47383f --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd-createImportExpression-false/with-other-import-export/output.js @@ -0,0 +1,12 @@ +define(["require", "exports", "foo"], function (_require, _exports, _foo) { + "use strict"; + + Object.defineProperty(_exports, "__esModule", { + value: true + }); + _exports.mod = void 0; + _foo = babelHelpers.interopRequireDefault(_foo); + var mod; + _exports.mod = mod; + new Promise((_resolve, _reject) => _require(["mod"], imported => _resolve(babelHelpers.interopRequireWildcard(imported)), _reject)).then(m => _exports.mod = mod = m); +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd/options.json index af5a84013fec..3282307dd3d3 100644 --- a/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd/options.json +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/amd/options.json @@ -1,3 +1,6 @@ { - "plugins": ["transform-dynamic-import", "transform-modules-amd"] + "plugins": ["transform-dynamic-import", "transform-modules-amd"], + "parserOpts": { + "createImportExpressions": true + } } diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-null/exec.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-null/exec.js new file mode 100644 index 000000000000..a1d2711151d0 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-null/exec.js @@ -0,0 +1,3 @@ +return import("./mod.js").then(({ default: def }) => { + expect(def).toBe(null); +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-null/mod.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-null/mod.js new file mode 100644 index 000000000000..c8a9c8ebee78 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-null/mod.js @@ -0,0 +1 @@ +module.exports = null; \ No newline at end of file diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-null/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-null/options.json new file mode 100644 index 000000000000..eeebc1a57738 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-null/options.json @@ -0,0 +1,5 @@ +{ + "parserOpts": { + "allowReturnOutsideFunction": true + } +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-string/exec.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-string/exec.js new file mode 100644 index 000000000000..e228b3fbc3a9 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-string/exec.js @@ -0,0 +1,3 @@ +return import("./mod.js").then(({ default: def }) => { + expect(def).toBe("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="); +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-string/mod.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-string/mod.js new file mode 100644 index 000000000000..bfd0466f4c1d --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-string/mod.js @@ -0,0 +1 @@ +module.exports = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="; \ No newline at end of file diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-string/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-string/options.json new file mode 100644 index 000000000000..eeebc1a57738 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop-string/options.json @@ -0,0 +1,5 @@ +{ + "parserOpts": { + "allowReturnOutsideFunction": true + } +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop/exec.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop/exec.js new file mode 100644 index 000000000000..edd0b1a34742 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop/exec.js @@ -0,0 +1,4 @@ +return import("./mod.js").then(({ default: def, named }) => { + expect(def()).toBe("foo"); + expect(named()).toBe("bar"); +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop/mod.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop/mod.js new file mode 100644 index 000000000000..d8a2bc2c6ed2 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop/mod.js @@ -0,0 +1,2 @@ +module.exports = () => "foo"; +module.exports.named = () => "bar"; diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop/options.json new file mode 100644 index 000000000000..eeebc1a57738 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-interop/options.json @@ -0,0 +1,5 @@ +{ + "parserOpts": { + "allowReturnOutsideFunction": true + } +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-template-literal/1.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-template-literal/1.js new file mode 100644 index 000000000000..bd816eaba4ca --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-template-literal/1.js @@ -0,0 +1 @@ +module.exports = 1; diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-template-literal/exec.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-template-literal/exec.js new file mode 100644 index 000000000000..2f3a13932e5c --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-template-literal/exec.js @@ -0,0 +1,3 @@ +let x = 1; +return expect(import(`./${x}.js`)) + .resolves.toHaveProperty("default", 1); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-template-literal/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-template-literal/options.json new file mode 100644 index 000000000000..eeebc1a57738 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-template-literal/options.json @@ -0,0 +1,5 @@ +{ + "parserOpts": { + "allowReturnOutsideFunction": true + } +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-throw/exec.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-throw/exec.js new file mode 100644 index 000000000000..6a80723db1f7 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-throw/exec.js @@ -0,0 +1,11 @@ +expect(() => { + function f() { throw "should throw"; } + import(f()); +}).toThrow("should throw"); + +expect(() => { + const a = { + get x() { throw "should throw"; }, + }; + import(a.x); +}).toThrow("should throw"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-primitive/exec.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-primitive/exec.js new file mode 100644 index 000000000000..09e8bf06d4e5 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-primitive/exec.js @@ -0,0 +1,6 @@ +return expect(import({ + [Symbol.toPrimitive](hint) { + if (hint === "string") return "./foo.js"; + return null; + } +})).resolves.toHaveProperty("default", "foo"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-primitive/foo.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-primitive/foo.js new file mode 100644 index 000000000000..e7134e7006d9 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-primitive/foo.js @@ -0,0 +1 @@ +module.exports = "foo"; diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-primitive/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-primitive/options.json new file mode 100644 index 000000000000..eeebc1a57738 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-primitive/options.json @@ -0,0 +1,5 @@ +{ + "parserOpts": { + "allowReturnOutsideFunction": true + } +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-order/1.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-order/1.js new file mode 100644 index 000000000000..bd816eaba4ca --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-order/1.js @@ -0,0 +1 @@ +module.exports = 1; diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-order/exec.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-order/exec.js new file mode 100644 index 000000000000..3e6bffbdfbb4 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-order/exec.js @@ -0,0 +1,7 @@ +let i = 0; +const promise = expect(import({ toString: () => `./${++i}.js` })) + .resolves.toHaveProperty("default", 1); +expect(i).toBe(1); +++i; +expect(i).toBe(2); +return promise; diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-order/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-order/options.json new file mode 100644 index 000000000000..eeebc1a57738 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-order/options.json @@ -0,0 +1,5 @@ +{ + "parserOpts": { + "allowReturnOutsideFunction": true + } +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-throw/exec.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-throw/exec.js new file mode 100644 index 000000000000..a4a59a3fec25 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-throw/exec.js @@ -0,0 +1,2 @@ +return expect(import({ toString: () => { throw "toString failed"; } })) + .rejects.toBe("toString failed"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-throw/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-throw/options.json new file mode 100644 index 000000000000..eeebc1a57738 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-to-string-throw/options.json @@ -0,0 +1,5 @@ +{ + "parserOpts": { + "allowReturnOutsideFunction": true + } +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-transpiled-dep/exec.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-transpiled-dep/exec.js new file mode 100644 index 000000000000..edd0b1a34742 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-transpiled-dep/exec.js @@ -0,0 +1,4 @@ +return import("./mod.js").then(({ default: def, named }) => { + expect(def()).toBe("foo"); + expect(named()).toBe("bar"); +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-transpiled-dep/mod.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-transpiled-dep/mod.js new file mode 100644 index 000000000000..799b79e96fd4 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-transpiled-dep/mod.js @@ -0,0 +1,5 @@ +module.exports = { + __esModule: true, + default: () => "foo", + named: () => "bar", +}; diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-transpiled-dep/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-transpiled-dep/options.json new file mode 100644 index 000000000000..eeebc1a57738 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/exec-transpiled-dep/options.json @@ -0,0 +1,5 @@ +{ + "parserOpts": { + "allowReturnOutsideFunction": true + } +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/missing-plugin/input.mjs b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/missing-plugin/input.mjs new file mode 100644 index 000000000000..968ad7d80111 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/missing-plugin/input.mjs @@ -0,0 +1,3 @@ +import "a"; + +import("b"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/missing-plugin/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/missing-plugin/options.json new file mode 100644 index 000000000000..81e5ce12436d --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/missing-plugin/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-modules-commonjs"] +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/missing-plugin/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/missing-plugin/output.js new file mode 100644 index 000000000000..5c4025eb0188 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/missing-plugin/output.js @@ -0,0 +1,4 @@ +"use strict"; + +require("a"); +import("b"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/module/input.mjs b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/module/input.mjs new file mode 100644 index 000000000000..743a74ae3b73 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/module/input.mjs @@ -0,0 +1 @@ +var modP = import("mod"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/module/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/module/output.js new file mode 100644 index 000000000000..2c4c89f6cf46 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/module/output.js @@ -0,0 +1,3 @@ +"use strict"; + +var modP = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require("mod"))); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/no-interop/input.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/no-interop/input.js new file mode 100644 index 000000000000..743a74ae3b73 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/no-interop/input.js @@ -0,0 +1 @@ +var modP = import("mod"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/no-interop/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/no-interop/options.json new file mode 100644 index 000000000000..364ba08fdad9 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/no-interop/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + "transform-dynamic-import", + ["transform-modules-commonjs", { "noInterop": true }] + ] +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/no-interop/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/no-interop/output.js new file mode 100644 index 000000000000..0eee33867600 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/no-interop/output.js @@ -0,0 +1 @@ +var modP = Promise.resolve().then(() => require("mod")); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/options.json new file mode 100644 index 000000000000..fdd0a82c652d --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/options.json @@ -0,0 +1,6 @@ +{ + "plugins": ["transform-dynamic-import", "transform-modules-commonjs"], + "parserOpts": { + "createImportExpressions": false + } +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/script/input.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/script/input.js new file mode 100644 index 000000000000..743a74ae3b73 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/script/input.js @@ -0,0 +1 @@ +var modP = import("mod"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/script/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/script/output.js new file mode 100644 index 000000000000..c1ee2ebe7ebf --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/script/output.js @@ -0,0 +1 @@ +var modP = Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require("mod"))); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/shadowed-require/input.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/shadowed-require/input.js new file mode 100644 index 000000000000..30d0f8b34ab7 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/shadowed-require/input.js @@ -0,0 +1,6 @@ +var require = "foo"; + +(async function () { + var require = "bar"; + await import("./mod"); +})(); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/shadowed-require/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/shadowed-require/output.js new file mode 100644 index 000000000000..2456adaec3df --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/shadowed-require/output.js @@ -0,0 +1,5 @@ +var _require2 = "foo"; +(async function () { + var _require = "bar"; + await Promise.resolve().then(() => babelHelpers.interopRequireWildcard(require("./mod"))); +})(); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/template-literal/input.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/template-literal/input.js new file mode 100644 index 000000000000..ebb8aaa09cd3 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/template-literal/input.js @@ -0,0 +1,2 @@ +const x = 1; +import(`./${x}.js`); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/template-literal/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/template-literal/output.js new file mode 100644 index 000000000000..6e90671b2706 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/template-literal/output.js @@ -0,0 +1,2 @@ +const x = 1; +(specifier => new Promise(r => r(specifier)).then(s => babelHelpers.interopRequireWildcard(require(s))))(`./${x}.js`); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/to-string/input.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/to-string/input.js new file mode 100644 index 000000000000..130d3cafa5cb --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/to-string/input.js @@ -0,0 +1 @@ +import(2); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/to-string/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/to-string/output.js new file mode 100644 index 000000000000..3618cfbe2b92 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs-createImportExpression-false/to-string/output.js @@ -0,0 +1 @@ +(specifier => new Promise(r => r(`${specifier}`)).then(s => babelHelpers.interopRequireWildcard(require(s))))(2); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs/options.json index 9f472c6b7649..a9779398a0f0 100644 --- a/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs/options.json +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/commonjs/options.json @@ -1,3 +1,6 @@ { - "plugins": ["transform-dynamic-import", "transform-modules-commonjs"] + "plugins": ["transform-dynamic-import", "transform-modules-commonjs"], + "parserOpts": { + "createImportExpressions": true + } } diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin-babel-7/input.mjs b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin-babel-7/input.mjs new file mode 100644 index 000000000000..968ad7d80111 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin-babel-7/input.mjs @@ -0,0 +1,3 @@ +import "a"; + +import("b"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin-babel-7/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin-babel-7/options.json similarity index 100% rename from packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin-babel-7/options.json rename to packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin-babel-7/options.json diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin-babel-7/output.mjs b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin-babel-7/output.mjs similarity index 100% rename from packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin-babel-7/output.mjs rename to packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin-babel-7/output.mjs diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin-babel-7/stderr.txt b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin-babel-7/stderr.txt similarity index 100% rename from packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin-babel-7/stderr.txt rename to packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin-babel-7/stderr.txt diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin/input.mjs b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin/input.mjs new file mode 100644 index 000000000000..968ad7d80111 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin/input.mjs @@ -0,0 +1,3 @@ +import "a"; + +import("b"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin/options.json new file mode 100644 index 000000000000..2e33aca2a551 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/missing-plugin/options.json @@ -0,0 +1,5 @@ +{ + "plugins": ["transform-modules-systemjs"], + "BABEL_8_BREAKING": true, + "throws": "ERROR: Dynamic import() transformation must be enabled using the\n @babel/plugin-transform-dynamic-import plugin. Babel 8\n no longer transforms import() without using that plugin." +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/module/input.mjs b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/module/input.mjs new file mode 100644 index 000000000000..743a74ae3b73 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/module/input.mjs @@ -0,0 +1 @@ +var modP = import("mod"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/module/output.mjs b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/module/output.mjs new file mode 100644 index 000000000000..8570884bf14d --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/module/output.mjs @@ -0,0 +1,11 @@ +System.register([], function (_export, _context) { + "use strict"; + + var modP; + return { + setters: [], + execute: function () { + modP = _context.import("mod"); + } + }; +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/options.json new file mode 100644 index 000000000000..3faf4c7b12b2 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/options.json @@ -0,0 +1,6 @@ +{ + "plugins": ["transform-dynamic-import", "transform-modules-systemjs"], + "parserOpts": { + "createImportExpressions": false + } +} diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/script/input.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/script/input.js new file mode 100644 index 000000000000..743a74ae3b73 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/script/input.js @@ -0,0 +1 @@ +var modP = import("mod"); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/script/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/script/output.js new file mode 100644 index 000000000000..8570884bf14d --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/script/output.js @@ -0,0 +1,11 @@ +System.register([], function (_export, _context) { + "use strict"; + + var modP; + return { + setters: [], + execute: function () { + modP = _context.import("mod"); + } + }; +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/to-string/input.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/to-string/input.js new file mode 100644 index 000000000000..130d3cafa5cb --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/to-string/input.js @@ -0,0 +1 @@ +import(2); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/to-string/output.js b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/to-string/output.js new file mode 100644 index 000000000000..6af1121eb491 --- /dev/null +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs-createImportExpression-false/to-string/output.js @@ -0,0 +1,10 @@ +System.register([], function (_export, _context) { + "use strict"; + + return { + setters: [], + execute: function () { + (specifier => new Promise(r => r(_context.import(`${specifier}`))))(2); + } + }; +}); diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin/options.json index 2e33aca2a551..8ac9e3e61718 100644 --- a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin/options.json +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/missing-plugin/options.json @@ -1,5 +1,4 @@ { "plugins": ["transform-modules-systemjs"], - "BABEL_8_BREAKING": true, "throws": "ERROR: Dynamic import() transformation must be enabled using the\n @babel/plugin-transform-dynamic-import plugin. Babel 8\n no longer transforms import() without using that plugin." } diff --git a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/options.json b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/options.json index 11c4bcf20cb9..169245c72dd9 100644 --- a/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/options.json +++ b/packages/babel-plugin-transform-dynamic-import/test/fixtures/systemjs/options.json @@ -1,3 +1,6 @@ { - "plugins": ["transform-dynamic-import", "transform-modules-systemjs"] + "plugins": ["transform-dynamic-import", "transform-modules-systemjs"], + "parserOpts": { + "createImportExpressions": true + } } diff --git a/packages/babel-plugin-transform-modules-amd/src/index.ts b/packages/babel-plugin-transform-modules-amd/src/index.ts index b46dff0e132f..5cd6cb07e617 100644 --- a/packages/babel-plugin-transform-modules-amd/src/index.ts +++ b/packages/babel-plugin-transform-modules-amd/src/index.ts @@ -11,7 +11,7 @@ import { wrapInterop, getModuleName, } from "@babel/helper-module-transforms"; -import { template, types as t } from "@babel/core"; +import { template, types as t, type PluginPass } from "@babel/core"; import type { PluginOptions } from "@babel/helper-module-transforms"; import type { NodePath } from "@babel/traverse"; @@ -79,9 +79,14 @@ export default declare((api, options: Options) => { }, visitor: { - CallExpression(path, state) { + ["CallExpression" + + (api.types.importExpression ? "|ImportExpression" : "")]( + this: State & PluginPass, + path: NodePath, + state: State, + ) { if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return; - if (!path.get("callee").isImport()) return; + if (path.isCallExpression() && !path.get("callee").isImport()) return; let { requireId, resolveId, rejectId } = state; if (!requireId) { @@ -96,7 +101,9 @@ export default declare((api, options: Options) => { } let result: t.Node = t.identifier("imported"); - if (!noInterop) result = wrapInterop(path, result, "namespace"); + if (!noInterop) { + result = wrapInterop(this.file.path, result, "namespace"); + } path.replaceWith( buildDynamicImport( @@ -115,7 +122,6 @@ export default declare((api, options: Options) => { ), ); }, - Program: { exit(path, { requireId }) { if (!isModule(path)) { diff --git a/packages/babel-plugin-transform-modules-commonjs/src/dynamic-import.ts b/packages/babel-plugin-transform-modules-commonjs/src/dynamic-import.ts index bb132d85a1ab..36eb869d8c87 100644 --- a/packages/babel-plugin-transform-modules-commonjs/src/dynamic-import.ts +++ b/packages/babel-plugin-transform-modules-commonjs/src/dynamic-import.ts @@ -14,7 +14,7 @@ const requireInterop = (source: t.Expression, file: File) => ]); export function transformDynamicImport( - path: NodePath, + path: NodePath, noInterop: boolean, file: File, ) { diff --git a/packages/babel-plugin-transform-modules-commonjs/src/index.ts b/packages/babel-plugin-transform-modules-commonjs/src/index.ts index b89169e56ad2..5b8a005ed1f1 100644 --- a/packages/babel-plugin-transform-modules-commonjs/src/index.ts +++ b/packages/babel-plugin-transform-modules-commonjs/src/index.ts @@ -10,9 +10,9 @@ import { getModuleName, } from "@babel/helper-module-transforms"; import simplifyAccess from "@babel/helper-simple-access"; -import { template, types as t } from "@babel/core"; +import { template, types as t, type PluginPass } from "@babel/core"; import type { PluginOptions } from "@babel/helper-module-transforms"; -import type { Visitor, Scope } from "@babel/traverse"; +import type { Visitor, Scope, NodePath } from "@babel/traverse"; import { transformDynamicImport } from "./dynamic-import"; @@ -169,9 +169,13 @@ export default declare((api, options: Options) => { }, visitor: { - CallExpression(path) { + ["CallExpression" + + (api.types.importExpression ? "|ImportExpression" : "")]( + this: PluginPass, + path: NodePath, + ) { if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return; - if (!t.isImport(path.node.callee)) return; + if (path.isCallExpression() && !t.isImport(path.node.callee)) return; let { scope } = path; do { diff --git a/packages/babel-plugin-transform-modules-systemjs/src/index.ts b/packages/babel-plugin-transform-modules-systemjs/src/index.ts index 721908487cb9..298f6ff36902 100644 --- a/packages/babel-plugin-transform-modules-systemjs/src/index.ts +++ b/packages/babel-plugin-transform-modules-systemjs/src/index.ts @@ -1,6 +1,6 @@ import { declare } from "@babel/helper-plugin-utils"; import hoistVariables from "@babel/helper-hoist-variables"; -import { template, types as t } from "@babel/core"; +import { template, types as t, type PluginPass } from "@babel/core"; import { buildDynamicImport, getModuleName, @@ -263,8 +263,14 @@ export default declare((api, options: Options) => { }, visitor: { - CallExpression(path, state: PluginState) { - if (t.isImport(path.node.callee)) { + ["CallExpression" + + (api.types.importExpression ? "|ImportExpression" : "")]( + this: PluginPass & PluginState, + path: NodePath, + state: PluginState, + ) { + if (path.isCallExpression() && !t.isImport(path.node.callee)) return; + if (path.isCallExpression()) { if (!this.file.has("@babel/plugin-proposal-dynamic-import")) { if (process.env.BABEL_8_BREAKING) { throw new Error(MISSING_PLUGIN_ERROR); @@ -272,19 +278,23 @@ export default declare((api, options: Options) => { console.warn(MISSING_PLUGIN_WARNING); } } - - path.replaceWith( - buildDynamicImport(path.node, false, true, specifier => - t.callExpression( - t.memberExpression( - t.identifier(state.contextIdent), - t.identifier("import"), - ), - [specifier], + } else { + // when createImportExpressions is true, we require the dynamic import transform + if (!this.file.has("@babel/plugin-proposal-dynamic-import")) { + throw new Error(MISSING_PLUGIN_ERROR); + } + } + path.replaceWith( + buildDynamicImport(path.node, false, true, specifier => + t.callExpression( + t.memberExpression( + t.identifier(state.contextIdent), + t.identifier("import"), ), + [specifier], ), - ); - } + ), + ); }, MetaProperty(path, state: PluginState) { diff --git a/packages/babel-traverse/src/path/generated/asserts.d.ts b/packages/babel-traverse/src/path/generated/asserts.d.ts index 24c21fbfdfeb..49aa405625bb 100644 --- a/packages/babel-traverse/src/path/generated/asserts.d.ts +++ b/packages/babel-traverse/src/path/generated/asserts.d.ts @@ -307,6 +307,9 @@ export interface NodePathAssertions { assertImportDefaultSpecifier( opts?: Opts, ): asserts this is NodePath; + assertImportExpression( + opts?: Opts, + ): asserts this is NodePath; assertImportNamespaceSpecifier( opts?: Opts, ): asserts this is NodePath; diff --git a/packages/babel-traverse/src/path/generated/validators.d.ts b/packages/babel-traverse/src/path/generated/validators.d.ts index 8854fa3d6c0e..0be11609fa67 100644 --- a/packages/babel-traverse/src/path/generated/validators.d.ts +++ b/packages/babel-traverse/src/path/generated/validators.d.ts @@ -435,6 +435,10 @@ interface BaseNodePathValidators { this: NodePath, opts?: Opts, ): this is NodePath; + isImportExpression( + this: NodePath, + opts?: Opts, + ): this is NodePath; isImportNamespaceSpecifier( this: NodePath, opts?: Opts, diff --git a/packages/babel-types/src/asserts/generated/index.ts b/packages/babel-types/src/asserts/generated/index.ts index abb8c6e795d4..c3cac57597c2 100644 --- a/packages/babel-types/src/asserts/generated/index.ts +++ b/packages/babel-types/src/asserts/generated/index.ts @@ -411,6 +411,12 @@ export function assertImportSpecifier( ): asserts node is t.ImportSpecifier { assert("ImportSpecifier", node, opts); } +export function assertImportExpression( + node: object | null | undefined, + opts?: object | null, +): asserts node is t.ImportExpression { + assert("ImportExpression", node, opts); +} export function assertMetaProperty( node: object | null | undefined, opts?: object | null, diff --git a/packages/babel-types/src/ast-types/generated/index.ts b/packages/babel-types/src/ast-types/generated/index.ts index ae36af585f52..2281fb58abed 100644 --- a/packages/babel-types/src/ast-types/generated/index.ts +++ b/packages/babel-types/src/ast-types/generated/index.ts @@ -131,6 +131,7 @@ export type Node = | ImportAttribute | ImportDeclaration | ImportDefaultSpecifier + | ImportExpression | ImportNamespaceSpecifier | ImportSpecifier | IndexedAccessType @@ -882,6 +883,12 @@ export interface ImportSpecifier extends BaseNode { importKind?: "type" | "typeof" | "value" | null; } +export interface ImportExpression extends BaseNode { + type: "ImportExpression"; + source: Expression; + options?: Expression | null; +} + export interface MetaProperty extends BaseNode { type: "MetaProperty"; meta: Identifier; @@ -2148,6 +2155,7 @@ export type Standardized = | ImportDefaultSpecifier | ImportNamespaceSpecifier | ImportSpecifier + | ImportExpression | MetaProperty | ClassMethod | ObjectPattern @@ -2193,6 +2201,7 @@ export type Expression = | UpdateExpression | ArrowFunctionExpression | ClassExpression + | ImportExpression | MetaProperty | Super | TaggedTemplateExpression @@ -2860,6 +2869,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -2967,6 +2977,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -3031,6 +3042,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -3112,6 +3124,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -3176,6 +3189,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -3241,6 +3255,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -3305,6 +3320,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -3394,6 +3410,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -3523,6 +3540,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -3606,6 +3624,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -3682,6 +3701,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -3774,6 +3794,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -4034,6 +4055,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -4347,6 +4369,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -4492,6 +4515,7 @@ export interface ParentMaps { | IfStatement | ImportAttribute | ImportDefaultSpecifier + | ImportExpression | ImportNamespaceSpecifier | ImportSpecifier | InterfaceDeclaration @@ -4604,6 +4628,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -4664,6 +4689,71 @@ export interface ParentMaps { | WhileStatement | WithStatement; ImportDefaultSpecifier: ImportDeclaration; + ImportExpression: + | ArrayExpression + | ArrowFunctionExpression + | AssignmentExpression + | AssignmentPattern + | AwaitExpression + | BinaryExpression + | BindExpression + | CallExpression + | ClassAccessorProperty + | ClassDeclaration + | ClassExpression + | ClassMethod + | ClassPrivateProperty + | ClassProperty + | ConditionalExpression + | Decorator + | DoWhileStatement + | ExportDefaultDeclaration + | ExpressionStatement + | ForInStatement + | ForOfStatement + | ForStatement + | IfStatement + | ImportExpression + | JSXExpressionContainer + | JSXSpreadAttribute + | JSXSpreadChild + | LogicalExpression + | MemberExpression + | NewExpression + | ObjectMethod + | ObjectProperty + | OptionalCallExpression + | OptionalMemberExpression + | ParenthesizedExpression + | PipelineBareFunction + | PipelineTopicExpression + | ReturnStatement + | SequenceExpression + | SpreadElement + | SwitchCase + | SwitchStatement + | TSAsExpression + | TSDeclareMethod + | TSEnumDeclaration + | TSEnumMember + | TSExportAssignment + | TSInstantiationExpression + | TSMethodSignature + | TSNonNullExpression + | TSPropertySignature + | TSSatisfiesExpression + | TSTypeAssertion + | TaggedTemplateExpression + | TemplateLiteral + | ThrowStatement + | TupleExpression + | TypeCastExpression + | UnaryExpression + | UpdateExpression + | VariableDeclarator + | WhileStatement + | WithStatement + | YieldExpression; ImportNamespaceSpecifier: ImportDeclaration; ImportSpecifier: ImportDeclaration; IndexedAccessType: @@ -4801,6 +4891,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXAttribute | JSXElement | JSXExpressionContainer @@ -4870,6 +4961,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXAttribute | JSXElement | JSXExpressionContainer @@ -4973,6 +5065,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -5038,6 +5131,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -5103,6 +5197,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -5192,6 +5287,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -5256,6 +5352,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -5339,6 +5436,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -5505,6 +5603,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -5570,6 +5669,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -5720,6 +5820,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -5809,6 +5910,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -5873,6 +5975,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -5937,6 +6040,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -6001,6 +6105,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -6065,6 +6170,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -6144,6 +6250,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -6208,6 +6315,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -6310,6 +6418,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -6393,6 +6502,7 @@ export interface ParentMaps { | IfStatement | ImportAttribute | ImportDeclaration + | ImportExpression | ImportSpecifier | JSXAttribute | JSXExpressionContainer @@ -6514,6 +6624,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -6661,6 +6772,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -6984,6 +7096,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -7203,6 +7316,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -7413,6 +7527,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -7604,6 +7719,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -7890,6 +8006,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -7955,6 +8072,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -8020,6 +8138,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -8123,6 +8242,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -8201,6 +8321,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -8329,6 +8450,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -8454,6 +8576,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -8544,6 +8667,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild @@ -8687,6 +8811,7 @@ export interface ParentMaps { | ForOfStatement | ForStatement | IfStatement + | ImportExpression | JSXExpressionContainer | JSXSpreadAttribute | JSXSpreadChild diff --git a/packages/babel-types/src/builders/generated/index.ts b/packages/babel-types/src/builders/generated/index.ts index b29b4d9cb530..9b0060d02a30 100644 --- a/packages/babel-types/src/builders/generated/index.ts +++ b/packages/babel-types/src/builders/generated/index.ts @@ -728,6 +728,16 @@ export function importSpecifier( imported, }); } +export function importExpression( + source: t.Expression, + options: t.Expression | null = null, +): t.ImportExpression { + return validateNode({ + type: "ImportExpression", + source, + options, + }); +} export function metaProperty( meta: t.Identifier, property: t.Identifier, diff --git a/packages/babel-types/src/builders/generated/uppercase.js b/packages/babel-types/src/builders/generated/uppercase.js index 24bdea27f963..a55eab20087f 100644 --- a/packages/babel-types/src/builders/generated/uppercase.js +++ b/packages/babel-types/src/builders/generated/uppercase.js @@ -75,6 +75,7 @@ export { importDefaultSpecifier as ImportDefaultSpecifier, importNamespaceSpecifier as ImportNamespaceSpecifier, importSpecifier as ImportSpecifier, + importExpression as ImportExpression, metaProperty as MetaProperty, classMethod as ClassMethod, objectPattern as ObjectPattern, diff --git a/packages/babel-types/src/definitions/core.ts b/packages/babel-types/src/definitions/core.ts index c73897e9bb2a..c6a656c40212 100644 --- a/packages/babel-types/src/definitions/core.ts +++ b/packages/babel-types/src/definitions/core.ts @@ -1806,6 +1806,20 @@ defineType("ImportSpecifier", { }, }); +defineType("ImportExpression", { + visitor: ["source", "options"], + aliases: ["Expression"], + fields: { + source: { + validate: assertNodeType("Expression"), + }, + options: { + validate: assertNodeType("Expression"), + optional: true, + }, + }, +}); + defineType("MetaProperty", { visitor: ["meta", "property"], aliases: ["Expression"], diff --git a/packages/babel-types/src/validators/generated/index.ts b/packages/babel-types/src/validators/generated/index.ts index 4ea3fb808ff9..bca7a5e557c0 100644 --- a/packages/babel-types/src/validators/generated/index.ts +++ b/packages/babel-types/src/validators/generated/index.ts @@ -677,6 +677,16 @@ export function isImportSpecifier( return opts == null || shallowEqual(node, opts); } +export function isImportExpression( + node: t.Node | null | undefined, + opts?: Opts | null, +): node is t.ImportExpression { + if (!node) return false; + + if (node.type !== "ImportExpression") return false; + + return opts == null || shallowEqual(node, opts); +} export function isMetaProperty( node: t.Node | null | undefined, opts?: Opts | null, @@ -2580,6 +2590,7 @@ export function isStandardized( case "ImportDefaultSpecifier": case "ImportNamespaceSpecifier": case "ImportSpecifier": + case "ImportExpression": case "MetaProperty": case "ClassMethod": case "ObjectPattern": @@ -2649,6 +2660,7 @@ export function isExpression( case "UpdateExpression": case "ArrowFunctionExpression": case "ClassExpression": + case "ImportExpression": case "MetaProperty": case "Super": case "TaggedTemplateExpression":