diff --git a/src/language-js/parse/babel.js b/src/language-js/parse/babel.js index 8e901e995..88c02de4f 100644 --- a/src/language-js/parse/babel.js +++ b/src/language-js/parse/babel.js @@ -26,6 +26,7 @@ const parseOptions = { allowUndeclaredExports: true, errorRecovery: true, createParenthesizedExpressions: true, + createImportExpressions: true, plugins: [ // When adding a plugin, please add a test in `tests/format/js/babel-plugins`, // To remove plugins, remove it here and run `yarn test tests/format/js/babel-plugins` to verify @@ -45,6 +46,8 @@ const parseOptions = { "importReflection", "explicitResourceManagement", ["importAttributes", { deprecatedAssertSyntax: true }], + "sourcePhaseImports", + "deferredImportEvaluation", ["optionalChainingAssign", { version: "2023-07" }], ], tokens: true, diff --git a/src/language-js/print/call-expression.js b/src/language-js/print/call-expression.js index 77dc92ef5..3b2755447 100644 --- a/src/language-js/print/call-expression.js +++ b/src/language-js/print/call-expression.js @@ -68,7 +68,7 @@ function printCallExpression(path, options, print) { const contents = [ isNew ? "new " : "", - isDynamicImport ? "import" : print("callee"), + isDynamicImport ? printDynamicImportCallee(node) : print("callee"), optional, printFunctionTypeParameters(path, options, print), printCallArguments(path, options, print), @@ -83,6 +83,13 @@ function printCallExpression(path, options, print) { return contents; } +function printDynamicImportCallee(node) { + if (!node.phase) { + return "import"; + } + return `import.${node.phase}`; +} + function isCommonsJsOrAmdCall(node, parentNode) { if (node.callee.type !== "Identifier") { return false; diff --git a/src/language-js/print/module.js b/src/language-js/print/module.js index fdae117fe..f5ffd1697 100644 --- a/src/language-js/print/module.js +++ b/src/language-js/print/module.js @@ -34,6 +34,7 @@ function printImportDeclaration(path, options, print) { return [ "import", node.module ? " module" : "", + node.phase ? ` ${node.phase}` : "", printImportKind(node), printModuleSpecifiers(path, options, print), printModuleSource(path, options, print), diff --git a/src/language-js/utils/index.js b/src/language-js/utils/index.js index 754c0a99e..864fbd7a4 100644 --- a/src/language-js/utils/index.js +++ b/src/language-js/utils/index.js @@ -1050,9 +1050,15 @@ function getCallArguments(node) { if (node.type === "ImportExpression") { args = [node.source]; + // import attributes if (node.attributes) { args.push(node.attributes); } + + // deprecated import assertions + if (node.options) { + args.push(node.options); + } } callArgumentsCache.set(node, args); @@ -1064,9 +1070,15 @@ function iterateCallArgumentsPath(path, iteratee) { if (node.type === "ImportExpression") { path.call((sourcePath) => iteratee(sourcePath, 0), "source"); + // import attributes if (node.attributes) { path.call((sourcePath) => iteratee(sourcePath, 1), "attributes"); } + + // deprecated import assertions + if (node.options) { + path.call((sourcePath) => iteratee(sourcePath, 1), "options"); + } } else { path.each(iteratee, "arguments"); } @@ -1074,12 +1086,17 @@ function iterateCallArgumentsPath(path, iteratee) { function getCallArgumentSelector(node, index) { if (node.type === "ImportExpression") { - if (index === 0 || index === (node.attributes ? -2 : -1)) { + if (index === 0 || index === (node.attributes || node.options ? -2 : -1)) { return "source"; } + // import attributes if (node.attributes && (index === 1 || index === -1)) { return "attributes"; } + // deprecated import assertions + if (node.options && (index === 1 || index === -1)) { + return "options"; + } throw new RangeError("Invalid argument index"); } if (index < 0) { diff --git a/tests/format/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap index 99ddbe419..2170d484e 100644 --- a/tests/format/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap @@ -651,6 +651,41 @@ function enumerable(value) { ================================================================================ `; +exports[`deferred-import-evaluation.js [acorn] format 1`] = ` +"Unexpected token (1:14) +> 1 | import defer * as ns from "x"; + | ^ + 2 |" +`; + +exports[`deferred-import-evaluation.js [espree] format 1`] = ` +"Unexpected token * (1:14) +> 1 | import defer * as ns from "x"; + | ^ + 2 |" +`; + +exports[`deferred-import-evaluation.js [meriyah] format 1`] = ` +"Import source must be string (1:14) +> 1 | import defer * as ns from "x"; + | ^ + 2 |" +`; + +exports[`deferred-import-evaluation.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "babel-ts", "babel-flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +import defer * as ns from "x"; + +=====================================output===================================== +import defer * as ns from "x"; + +================================================================================ +`; + exports[`destructuring-private.js [acorn] format 1`] = ` "Unexpected token (5:13) 3 | constructor() { @@ -2496,6 +2531,46 @@ const re = /^(?i:[a-z])[a-z]$/; ================================================================================ `; +exports[`source-phase-imports.js [acorn] format 1`] = ` +"Unexpected token (1:15) +> 1 | import source fooSource from "foo"; + | ^ + 2 | import.source("x"); + 3 |" +`; + +exports[`source-phase-imports.js [espree] format 1`] = ` +"Unexpected token fooSource (1:15) +> 1 | import source fooSource from "foo"; + | ^ + 2 | import.source("x"); + 3 |" +`; + +exports[`source-phase-imports.js [meriyah] format 1`] = ` +"Import source must be string (1:23) +> 1 | import source fooSource from "foo"; + | ^ + 2 | import.source("x"); + 3 |" +`; + +exports[`source-phase-imports.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "babel-ts", "babel-flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +import source fooSource from "foo"; +import.source("x"); + +=====================================output===================================== +import source fooSource from "foo"; +import.source("x"); + +================================================================================ +`; + exports[`throw-expressions.js [acorn] format 1`] = ` "Unexpected token (3:23) 1 | // https://babeljs.io/docs/en/babel-plugin-proposal-throw-expressions diff --git a/tests/format/js/babel-plugins/deferred-import-evaluation.js b/tests/format/js/babel-plugins/deferred-import-evaluation.js new file mode 100644 index 000000000..806ae2bb7 --- /dev/null +++ b/tests/format/js/babel-plugins/deferred-import-evaluation.js @@ -0,0 +1 @@ +import defer * as ns from "x"; diff --git a/tests/format/js/babel-plugins/jsfmt.spec.js b/tests/format/js/babel-plugins/jsfmt.spec.js index 5aeacace8..c59b8a2f6 100644 --- a/tests/format/js/babel-plugins/jsfmt.spec.js +++ b/tests/format/js/babel-plugins/jsfmt.spec.js @@ -31,6 +31,8 @@ run_spec(import.meta, ["babel", "babel-ts", "babel-flow"], { "import-reflection.js", "explicit-resource-management.js", "regexp-modifiers.js", + "source-phase-imports.js", + "deferred-import-evaluation.js", "optional-chaining-assignment.js", ], espree: [ @@ -61,6 +63,8 @@ run_spec(import.meta, ["babel", "babel-ts", "babel-flow"], { "import-reflection.js", "explicit-resource-management.js", "regexp-modifiers.js", + "source-phase-imports.js", + "deferred-import-evaluation.js", "optional-chaining-assignment.js", ], meriyah: [ @@ -93,6 +97,8 @@ run_spec(import.meta, ["babel", "babel-ts", "babel-flow"], { "import-reflection.js", "explicit-resource-management.js", "regexp-modifiers.js", + "source-phase-imports.js", + "deferred-import-evaluation.js", "optional-chaining-assignment.js", ], babel: ["flow.js", "typescript.js"], diff --git a/tests/format/js/babel-plugins/source-phase-imports.js b/tests/format/js/babel-plugins/source-phase-imports.js new file mode 100644 index 000000000..fe8697e02 --- /dev/null +++ b/tests/format/js/babel-plugins/source-phase-imports.js @@ -0,0 +1,2 @@ +import source fooSource from "foo"; +import.source("x"); diff --git a/tests/format/js/deferred-import-evaluation/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/deferred-import-evaluation/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..082f67737 --- /dev/null +++ b/tests/format/js/deferred-import-evaluation/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,141 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`import-defer.js [acorn] format 1`] = ` +"Unexpected token (1:14) +> 1 | import defer * as ns from "x"; + | ^ + 2 |" +`; + +exports[`import-defer.js [espree] format 1`] = ` +"Unexpected token * (1:14) +> 1 | import defer * as ns from "x"; + | ^ + 2 |" +`; + +exports[`import-defer.js [meriyah] format 1`] = ` +"Import source must be string (1:14) +> 1 | import defer * as ns from "x"; + | ^ + 2 |" +`; + +exports[`import-defer.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import defer * as ns from "x"; + +=====================================output===================================== +import defer * as ns from "x"; + +================================================================================ +`; + +exports[`import-defer-attributes-declaration.js [acorn] format 1`] = ` +"Unexpected token (1:14) +> 1 | import defer * as ns from "x" with { attr: "val" }; + | ^ + 2 |" +`; + +exports[`import-defer-attributes-declaration.js [espree] format 1`] = ` +"Unexpected token * (1:14) +> 1 | import defer * as ns from "x" with { attr: "val" }; + | ^ + 2 |" +`; + +exports[`import-defer-attributes-declaration.js [meriyah] format 1`] = ` +"Import source must be string (1:14) +> 1 | import defer * as ns from "x" with { attr: "val" }; + | ^ + 2 |" +`; + +exports[`import-defer-attributes-declaration.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import defer * as ns from "x" with { attr: "val" }; + +=====================================output===================================== +import defer * as ns from "x" with { attr: "val" }; + +================================================================================ +`; + +exports[`no-default.js [__babel_estree] format 1`] = ` +"Only \`import defer * as x from "./module"\` is valid. (1:14) +> 1 | import defer x from "x"; + | ^ + 2 |" +`; + +exports[`no-default.js [acorn] format 1`] = ` +"Unexpected token (1:14) +> 1 | import defer x from "x"; + | ^ + 2 |" +`; + +exports[`no-default.js [babel] format 1`] = ` +"Only \`import defer * as x from "./module"\` is valid. (1:14) +> 1 | import defer x from "x"; + | ^ + 2 |" +`; + +exports[`no-default.js [espree] format 1`] = ` +"Unexpected token x (1:14) +> 1 | import defer x from "x"; + | ^ + 2 |" +`; + +exports[`no-default.js [meriyah] format 1`] = ` +"Import source must be string (1:14) +> 1 | import defer x from "x"; + | ^ + 2 |" +`; + +exports[`no-named.js [__babel_estree] format 1`] = ` +"Only \`import defer * as x from "./module"\` is valid. (1:16) +> 1 | import defer { x } from "x"; + | ^ + 2 |" +`; + +exports[`no-named.js [acorn] format 1`] = ` +"Unexpected token (1:14) +> 1 | import defer { x } from "x"; + | ^ + 2 |" +`; + +exports[`no-named.js [babel] format 1`] = ` +"Only \`import defer * as x from "./module"\` is valid. (1:16) +> 1 | import defer { x } from "x"; + | ^ + 2 |" +`; + +exports[`no-named.js [espree] format 1`] = ` +"Unexpected token { (1:14) +> 1 | import defer { x } from "x"; + | ^ + 2 |" +`; + +exports[`no-named.js [meriyah] format 1`] = ` +"Import source must be string (1:14) +> 1 | import defer { x } from "x"; + | ^ + 2 |" +`; diff --git a/tests/format/js/deferred-import-evaluation/import-defer-attributes-declaration.js b/tests/format/js/deferred-import-evaluation/import-defer-attributes-declaration.js new file mode 100644 index 000000000..e8e5628b5 --- /dev/null +++ b/tests/format/js/deferred-import-evaluation/import-defer-attributes-declaration.js @@ -0,0 +1 @@ +import defer * as ns from "x" with { attr: "val" }; diff --git a/tests/format/js/deferred-import-evaluation/import-defer.js b/tests/format/js/deferred-import-evaluation/import-defer.js new file mode 100644 index 000000000..806ae2bb7 --- /dev/null +++ b/tests/format/js/deferred-import-evaluation/import-defer.js @@ -0,0 +1 @@ +import defer * as ns from "x"; diff --git a/tests/format/js/deferred-import-evaluation/jsfmt.spec.js b/tests/format/js/deferred-import-evaluation/jsfmt.spec.js new file mode 100644 index 000000000..bb7a108f8 --- /dev/null +++ b/tests/format/js/deferred-import-evaluation/jsfmt.spec.js @@ -0,0 +1,15 @@ +const importDeferTests = [ + "import-defer.js", + "import-defer-attributes-declaration.js", +]; +const invalidSyntaxTests = ["no-default.js", "no-named.js"]; + +run_spec(import.meta, ["babel"], { + errors: { + acorn: [...importDeferTests, ...invalidSyntaxTests], + espree: [...importDeferTests, ...invalidSyntaxTests], + meriyah: [...importDeferTests, ...invalidSyntaxTests], + babel: invalidSyntaxTests, + __babel_estree: invalidSyntaxTests, + }, +}); diff --git a/tests/format/js/deferred-import-evaluation/no-default.js b/tests/format/js/deferred-import-evaluation/no-default.js new file mode 100644 index 000000000..d96ed9fc9 --- /dev/null +++ b/tests/format/js/deferred-import-evaluation/no-default.js @@ -0,0 +1 @@ +import defer x from "x"; diff --git a/tests/format/js/deferred-import-evaluation/no-named.js b/tests/format/js/deferred-import-evaluation/no-named.js new file mode 100644 index 000000000..5c307da2a --- /dev/null +++ b/tests/format/js/deferred-import-evaluation/no-named.js @@ -0,0 +1 @@ +import defer { x } from "x"; diff --git a/tests/format/js/source-phase-imports/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/source-phase-imports/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..70cd79bfc --- /dev/null +++ b/tests/format/js/source-phase-imports/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,295 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`default-binding.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import source from "x"; + +=====================================output===================================== +import source from "x"; + +================================================================================ +`; + +exports[`import-source.js [acorn] format 1`] = ` +"Unexpected token (1:15) +> 1 | import source x from "x"; + | ^ + 2 |" +`; + +exports[`import-source.js [espree] format 1`] = ` +"Unexpected token x (1:15) +> 1 | import source x from "x"; + | ^ + 2 |" +`; + +exports[`import-source.js [meriyah] format 1`] = ` +"Import source must be string (1:15) +> 1 | import source x from "x"; + | ^ + 2 |" +`; + +exports[`import-source.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import source x from "x"; + +=====================================output===================================== +import source x from "x"; + +================================================================================ +`; + +exports[`import-source-attributes-declaration.js [acorn] format 1`] = ` +"Unexpected token (1:15) +> 1 | import source s from "x" with { attr: "val" }; + | ^ + 2 |" +`; + +exports[`import-source-attributes-declaration.js [espree] format 1`] = ` +"Unexpected token s (1:15) +> 1 | import source s from "x" with { attr: "val" }; + | ^ + 2 |" +`; + +exports[`import-source-attributes-declaration.js [meriyah] format 1`] = ` +"Import source must be string (1:15) +> 1 | import source s from "x" with { attr: "val" }; + | ^ + 2 |" +`; + +exports[`import-source-attributes-declaration.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import source s from "x" with { attr: "val" }; + +=====================================output===================================== +import source s from "x" with { attr: "val" }; + +================================================================================ +`; + +exports[`import-source-attributes-expression.js [acorn] format 1`] = ` +"The only valid meta property for import is 'import.meta' (1:8) +> 1 | import.source("x", { with: { attr: "val" } }); + | ^ + 2 |" +`; + +exports[`import-source-attributes-expression.js [espree] format 1`] = ` +"The only valid meta property for import is 'import.meta' (1:8) +> 1 | import.source("x", { with: { attr: "val" } }); + | ^ + 2 |" +`; + +exports[`import-source-attributes-expression.js [meriyah] format 1`] = ` +"Unexpected token: 'identifier' (1:13) +> 1 | import.source("x", { with: { attr: "val" } }); + | ^ + 2 |" +`; + +exports[`import-source-attributes-expression.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import.source("x", { with: { attr: "val" } }); + +=====================================output===================================== +import.source("x", { with: { attr: "val" } }); + +================================================================================ +`; + +exports[`import-source-binding-from.js [acorn] format 1`] = ` +"Unexpected token (1:20) +> 1 | import source from from "x"; + | ^ + 2 |" +`; + +exports[`import-source-binding-from.js [espree] format 1`] = ` +"Unexpected token from (1:20) +> 1 | import source from from "x"; + | ^ + 2 |" +`; + +exports[`import-source-binding-from.js [meriyah] format 1`] = ` +"Import source must be string (1:23) +> 1 | import source from from "x"; + | ^ + 2 |" +`; + +exports[`import-source-binding-from.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import source from from "x"; + +=====================================output===================================== +import source from from "x"; + +================================================================================ +`; + +exports[`import-source-binding-source.js [acorn] format 1`] = ` +"Unexpected token (1:15) +> 1 | import source source from "x"; + | ^ + 2 |" +`; + +exports[`import-source-binding-source.js [espree] format 1`] = ` +"Unexpected token source (1:15) +> 1 | import source source from "x"; + | ^ + 2 |" +`; + +exports[`import-source-binding-source.js [meriyah] format 1`] = ` +"Import source must be string (1:20) +> 1 | import source source from "x"; + | ^ + 2 |" +`; + +exports[`import-source-binding-source.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import source source from "x"; + +=====================================output===================================== +import source source from "x"; + +================================================================================ +`; + +exports[`import-source-dynamic-import.js [acorn] format 1`] = ` +"The only valid meta property for import is 'import.meta' (1:8) +> 1 | import.source("foo"); + | ^ + 2 |" +`; + +exports[`import-source-dynamic-import.js [espree] format 1`] = ` +"The only valid meta property for import is 'import.meta' (1:8) +> 1 | import.source("foo"); + | ^ + 2 |" +`; + +exports[`import-source-dynamic-import.js [meriyah] format 1`] = ` +"Unexpected token: 'identifier' (1:13) +> 1 | import.source("foo"); + | ^ + 2 |" +`; + +exports[`import-source-dynamic-import.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import.source("foo"); + +=====================================output===================================== +import.source("foo"); + +================================================================================ +`; + +exports[`no-named.js [__babel_estree] format 1`] = ` +"Only \`import source x from "./module"\` is valid. (1:17) +> 1 | import source { x } from "x"; + | ^ + 2 |" +`; + +exports[`no-named.js [acorn] format 1`] = ` +"Unexpected token (1:15) +> 1 | import source { x } from "x"; + | ^ + 2 |" +`; + +exports[`no-named.js [babel] format 1`] = ` +"Only \`import source x from "./module"\` is valid. (1:17) +> 1 | import source { x } from "x"; + | ^ + 2 |" +`; + +exports[`no-named.js [espree] format 1`] = ` +"Unexpected token { (1:15) +> 1 | import source { x } from "x"; + | ^ + 2 |" +`; + +exports[`no-named.js [meriyah] format 1`] = ` +"Import source must be string (1:15) +> 1 | import source { x } from "x"; + | ^ + 2 |" +`; + +exports[`no-namespace.js [__babel_estree] format 1`] = ` +"Only \`import source x from "./module"\` is valid. (1:15) +> 1 | import source * as x from "x"; + | ^ + 2 |" +`; + +exports[`no-namespace.js [acorn] format 1`] = ` +"Unexpected token (1:15) +> 1 | import source * as x from "x"; + | ^ + 2 |" +`; + +exports[`no-namespace.js [babel] format 1`] = ` +"Only \`import source x from "./module"\` is valid. (1:15) +> 1 | import source * as x from "x"; + | ^ + 2 |" +`; + +exports[`no-namespace.js [espree] format 1`] = ` +"Unexpected token * (1:15) +> 1 | import source * as x from "x"; + | ^ + 2 |" +`; + +exports[`no-namespace.js [meriyah] format 1`] = ` +"Import source must be string (1:15) +> 1 | import source * as x from "x"; + | ^ + 2 |" +`; diff --git a/tests/format/js/source-phase-imports/default-binding.js b/tests/format/js/source-phase-imports/default-binding.js new file mode 100644 index 000000000..a88842d5e --- /dev/null +++ b/tests/format/js/source-phase-imports/default-binding.js @@ -0,0 +1 @@ +import source from "x"; diff --git a/tests/format/js/source-phase-imports/import-source-attributes-declaration.js b/tests/format/js/source-phase-imports/import-source-attributes-declaration.js new file mode 100644 index 000000000..d2b020b59 --- /dev/null +++ b/tests/format/js/source-phase-imports/import-source-attributes-declaration.js @@ -0,0 +1 @@ +import source s from "x" with { attr: "val" }; diff --git a/tests/format/js/source-phase-imports/import-source-attributes-expression.js b/tests/format/js/source-phase-imports/import-source-attributes-expression.js new file mode 100644 index 000000000..329aa1d6c --- /dev/null +++ b/tests/format/js/source-phase-imports/import-source-attributes-expression.js @@ -0,0 +1 @@ +import.source("x", { with: { attr: "val" } }); diff --git a/tests/format/js/source-phase-imports/import-source-binding-from.js b/tests/format/js/source-phase-imports/import-source-binding-from.js new file mode 100644 index 000000000..a5c6af299 --- /dev/null +++ b/tests/format/js/source-phase-imports/import-source-binding-from.js @@ -0,0 +1 @@ +import source from from "x"; diff --git a/tests/format/js/source-phase-imports/import-source-binding-source.js b/tests/format/js/source-phase-imports/import-source-binding-source.js new file mode 100644 index 000000000..426fb0a9a --- /dev/null +++ b/tests/format/js/source-phase-imports/import-source-binding-source.js @@ -0,0 +1 @@ +import source source from "x"; diff --git a/tests/format/js/source-phase-imports/import-source-dynamic-import.js b/tests/format/js/source-phase-imports/import-source-dynamic-import.js new file mode 100644 index 000000000..05a554458 --- /dev/null +++ b/tests/format/js/source-phase-imports/import-source-dynamic-import.js @@ -0,0 +1 @@ +import.source("foo"); diff --git a/tests/format/js/source-phase-imports/import-source.js b/tests/format/js/source-phase-imports/import-source.js new file mode 100644 index 000000000..6cfa837a5 --- /dev/null +++ b/tests/format/js/source-phase-imports/import-source.js @@ -0,0 +1 @@ +import source x from "x"; diff --git a/tests/format/js/source-phase-imports/jsfmt.spec.js b/tests/format/js/source-phase-imports/jsfmt.spec.js new file mode 100644 index 000000000..3b425b082 --- /dev/null +++ b/tests/format/js/source-phase-imports/jsfmt.spec.js @@ -0,0 +1,19 @@ +const importSourceTests = [ + "import-source-attributes-declaration.js", + "import-source-attributes-expression.js", + "import-source-binding-from.js", + "import-source-binding-source.js", + "import-source-dynamic-import.js", + "import-source.js", +]; +const invalidSyntaxTests = ["no-namespace.js", "no-named.js"]; + +run_spec(import.meta, ["babel"], { + errors: { + acorn: [...importSourceTests, ...invalidSyntaxTests], + espree: [...importSourceTests, ...invalidSyntaxTests], + meriyah: [...importSourceTests, ...invalidSyntaxTests], + babel: invalidSyntaxTests, + __babel_estree: invalidSyntaxTests, + }, +}); diff --git a/tests/format/js/source-phase-imports/no-named.js b/tests/format/js/source-phase-imports/no-named.js new file mode 100644 index 000000000..1cd5d593c --- /dev/null +++ b/tests/format/js/source-phase-imports/no-named.js @@ -0,0 +1 @@ +import source { x } from "x"; diff --git a/tests/format/js/source-phase-imports/no-namespace.js b/tests/format/js/source-phase-imports/no-namespace.js new file mode 100644 index 000000000..185a8b107 --- /dev/null +++ b/tests/format/js/source-phase-imports/no-namespace.js @@ -0,0 +1 @@ +import source * as x from "x"; diff --git a/tests/format/misc/errors/js/import/__snapshots__/jsfmt.spec.js.snap b/tests/format/misc/errors/js/import/__snapshots__/jsfmt.spec.js.snap index 353b50835..73c6d58de 100644 --- a/tests/format/misc/errors/js/import/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/misc/errors/js/import/__snapshots__/jsfmt.spec.js.snap @@ -1,21 +1,21 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`snippet: #0 [babel] format 1`] = ` -"\`import()\` requires exactly one or two arguments. (1:1) +"Unexpected token (1:8) > 1 | import(); - | ^" + | ^" `; exports[`snippet: #0 [babel-flow] format 1`] = ` -"\`import()\` requires exactly one or two arguments. (1:1) +"Unexpected token (1:8) > 1 | import(); - | ^" + | ^" `; exports[`snippet: #0 [babel-ts] format 1`] = ` -"\`import()\` requires exactly one or two arguments. (1:1) +"Unexpected token (1:8) > 1 | import(); - | ^" + | ^" `; exports[`snippet: #0 [flow] format 1`] = ` @@ -37,21 +37,21 @@ exports[`snippet: #0 [typescript] format 1`] = ` `; exports[`snippet: #1 [babel] format 1`] = ` -"\`import()\` requires exactly one or two arguments. (1:1) +"Unexpected token (1:21) > 1 | import(/* comment */); - | ^" + | ^" `; exports[`snippet: #1 [babel-flow] format 1`] = ` -"\`import()\` requires exactly one or two arguments. (1:1) +"Unexpected token (1:21) > 1 | import(/* comment */); - | ^" + | ^" `; exports[`snippet: #1 [babel-ts] format 1`] = ` -"\`import()\` requires exactly one or two arguments. (1:1) +"Unexpected token (1:21) > 1 | import(/* comment */); - | ^" + | ^" `; exports[`snippet: #1 [flow] format 1`] = ` @@ -109,21 +109,21 @@ exports[`snippet: #2 [typescript] format 1`] = ` `; exports[`snippet: #3 [babel] format 1`] = ` -"Cannot use new with import(...). (1:5) +"Unexpected token (1:12) > 1 | new import(); - | ^" + | ^" `; exports[`snippet: #3 [babel-flow] format 1`] = ` -"Cannot use new with import(...). (1:5) +"Unexpected token (1:12) > 1 | new import(); - | ^" + | ^" `; exports[`snippet: #3 [babel-ts] format 1`] = ` -"Cannot use new with import(...). (1:5) +"Unexpected token (1:12) > 1 | new import(); - | ^" + | ^" `; exports[`snippet: #3 [flow] format 1`] = `