diff --git a/changelog_unreleased/javascript/pr-9408.md b/changelog_unreleased/javascript/pr-9408.md index b496ce45f6a5..0444bb337109 100644 --- a/changelog_unreleased/javascript/pr-9408.md +++ b/changelog_unreleased/javascript/pr-9408.md @@ -1,4 +1,4 @@ -#### Update to `@babel/parser` 7.12 (#9408, #9476 by @sosukesuzuki) +#### Update to `@babel/parser` 7.12 (#9408, #9476, #9597 by @sosukesuzuki) Updated the JavaScript parser to [`@babel/parser` 7.12](https://babeljs.io/blog/2020/10/15/7.12.0). This fixes several bugs and supports some new syntax. @@ -7,15 +7,11 @@ Updated the JavaScript parser to [`@babel/parser` 7.12](https://babeljs.io/blog/ [The "module attributes" proposal supported on 2.1](https://prettier.io/blog/2020/08/24/2.1.0.html#support-es-module-attributes-and-json-modules-8436httpsgithubcomprettierprettierpull8436-by-fiskerhttpsgithubcomfisker) has been significantly changed and also renamed to "import assertions". ```js -import json from "./foo.json" assert { type: "json" }; +import foo from "./foo.json" assert { type: "json" }; ``` ##### Support imports and exports with string names - - -Due to a [bug in `@babel/parser`](https://github.com/babel/babel/issues/12209), Prettier can only use exports, but that will be fixed. - ```js let happy = "happy"; export { happy as "😃" }; diff --git a/package.json b/package.json index 94875c42634b..9ccaf159e70e 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dependencies": { "@angular/compiler": "10.2.2", "@babel/code-frame": "7.10.4", - "@babel/parser": "7.12.3", + "@babel/parser": "7.12.5", "@glimmer/syntax": "0.62.4", "@iarna/toml": "2.2.5", "@typescript-eslint/typescript-estree": "3.10.1", diff --git a/src/language-js/print/module.js b/src/language-js/print/module.js index dc0f8ccf506d..1076773416dd 100644 --- a/src/language-js/print/module.js +++ b/src/language-js/print/module.js @@ -94,7 +94,22 @@ function printModuleSpecifiers(path, options, print) { return concat(parts); } +function printImportAssertions(path, options, print) { + const node = path.getNode(); + if (Array.isArray(node.assertions) && node.assertions.length !== 0) { + return concat([ + " assert {", + options.bracketSpacing ? " " : "", + join(", ", path.map(print, "assertions")), + options.bracketSpacing ? " " : "", + "}", + ]); + } + return ""; +} + module.exports = { printModuleSource, printModuleSpecifiers, + printImportAssertions, }; diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index de47a4c34256..6f3224cc4fae 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -117,7 +117,11 @@ const { printMemberLookup, printBindExpressionCallee, } = require("./print/misc"); -const { printModuleSource, printModuleSpecifiers } = require("./print/module"); +const { + printModuleSource, + printModuleSpecifiers, + printImportAssertions, +} = require("./print/module"); const printTernaryOperator = require("./print/ternary"); const { printFunctionParameters, @@ -844,7 +848,11 @@ function printPathNoParens(path, options, print, args) { parts.push(" as ", path.call(print, "exported")); } - parts.push(printModuleSource(path, options, print), semi); + parts.push( + printModuleSource(path, options, print), + printImportAssertions(path, options, print), + semi + ); return concat(parts); @@ -874,13 +882,7 @@ function printPathNoParens(path, options, print, args) { parts.push(" ", path.call(print, "source")); } - if (Array.isArray(n.assertions) && n.assertions.length !== 0) { - parts.push( - " assert { ", - join(", ", path.map(print, "assertions")), - " }" - ); - } + parts.push(printImportAssertions(path, options, print)); parts.push(semi); @@ -4003,6 +4005,7 @@ function printExportDeclaration(path, options, print) { parts.push(decl.exportKind === "type" ? "type " : ""); parts.push(printModuleSpecifiers(path, options, print)); parts.push(printModuleSource(path, options, print)); + parts.push(printImportAssertions(path, options, print)); parts.push(semi); } diff --git a/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap b/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap index e8abbb3661bf..ac88339fa089 100644 --- a/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap @@ -1052,17 +1052,19 @@ obj.a.b &&= c; `; exports[`module-string-names.js [espree] format 1`] = ` -"Unexpected token \\"😄\\" (1:19) -> 1 | export { smile as \\"😄\\" } from \\"./emojis.js\\"; - | ^ - 2 | " +"Unexpected token \\"😄\\" (1:10) +> 1 | import { \\"😄\\" as smile } from \\"./emojis.js\\"; + | ^ + 2 | export { smile as \\"😄\\" } from \\"./emojis.js\\"; + 3 | " `; exports[`module-string-names.js [meriyah] format 1`] = ` -"[1:22]: Only a identifier can be used to indicate alias (1:22) -> 1 | export { smile as \\"😄\\" } from \\"./emojis.js\\"; - | ^ - 2 | " +"[1:13]: Expected '}' (1:13) +> 1 | import { \\"😄\\" as smile } from \\"./emojis.js\\"; + | ^ + 2 | export { smile as \\"😄\\" } from \\"./emojis.js\\"; + 3 | " `; exports[`module-string-names.js format 1`] = ` @@ -1071,9 +1073,11 @@ parsers: ["babel", "babel-ts", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== +import { "😄" as smile } from "./emojis.js"; export { smile as "😄" } from "./emojis.js"; =====================================output===================================== +import { "😄" as smile } from "./emojis.js"; export { smile as "😄" } from "./emojis.js"; ================================================================================ diff --git a/tests/js/babel-plugins/module-string-names.js b/tests/js/babel-plugins/module-string-names.js index 631c04f79793..2bdb75fb7037 100644 --- a/tests/js/babel-plugins/module-string-names.js +++ b/tests/js/babel-plugins/module-string-names.js @@ -1 +1,2 @@ +import { "😄" as smile } from "./emojis.js"; export { smile as "😄" } from "./emojis.js"; diff --git a/tests/js/binary-expressions/__snapshots__/jsfmt.spec.js.snap b/tests/js/binary-expressions/__snapshots__/jsfmt.spec.js.snap index 7599f115f1e4..912e5c4eb4a8 100644 --- a/tests/js/binary-expressions/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/binary-expressions/__snapshots__/jsfmt.spec.js.snap @@ -740,6 +740,27 @@ printWidth: 80 ================================================================================ `; +exports[`like-regexp.js [espree] format 1`] = ` +"Unterminated regular expression (1:19) +> 1 | 0 ? a : { b : 1 }/2; + | ^ + 2 | " +`; + +exports[`like-regexp.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +0 ? a : { b : 1 }/2; + +=====================================output===================================== +0 ? a : { b: 1 } / 2; + +================================================================================ +`; + exports[`math.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] diff --git a/tests/js/binary-expressions/jsfmt.spec.js b/tests/js/binary-expressions/jsfmt.spec.js index eb85eda6bd02..7872139988cb 100644 --- a/tests/js/binary-expressions/jsfmt.spec.js +++ b/tests/js/binary-expressions/jsfmt.spec.js @@ -1 +1,3 @@ -run_spec(__dirname, ["babel", "flow", "typescript"]); +run_spec(__dirname, ["babel", "flow", "typescript"], { + errors: { espree: ["like-regexp.js"] }, +}); diff --git a/tests/js/binary-expressions/like-regexp.js b/tests/js/binary-expressions/like-regexp.js new file mode 100644 index 000000000000..b424d657d3b4 --- /dev/null +++ b/tests/js/binary-expressions/like-regexp.js @@ -0,0 +1 @@ +0 ? a : { b : 1 }/2; diff --git a/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap b/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap index 7686b5f67ee6..1dfb06751cb3 100644 --- a/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap @@ -1,20 +1,20 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`import-assertions-dynamic.js [espree] format 1`] = ` +exports[`dynamic-import.js [espree] format 1`] = ` "Unexpected token , (1:20) > 1 | import(\\"./foo.json\\", { assert: { type: \\"json\\" } }); | ^ 2 | " `; -exports[`import-assertions-dynamic.js [meriyah] format 1`] = ` +exports[`dynamic-import.js [meriyah] format 1`] = ` "[1:20]: Expected ')' (1:20) > 1 | import(\\"./foo.json\\", { assert: { type: \\"json\\" } }); | ^ 2 | " `; -exports[`import-assertions-dynamic.js format 1`] = ` +exports[`dynamic-import.js format 1`] = ` ====================================options===================================== parsers: ["babel"] printWidth: 80 @@ -28,21 +28,66 @@ import("./foo.json", { assert: { type: "json" } }); ================================================================================ `; -exports[`import-assertions-multi-types.js [espree] format 1`] = ` +exports[`empty.js [espree] format 1`] = ` +"Unexpected token assert (2:33) + 1 | export * as foo from \\"foo.json\\" +> 2 | export * as bar from \\"bar.json\\" assert { } + | ^ + 3 | export * as baz from \\"baz.json\\" assert { /* comment */ } + 4 | + 5 | import * as foo from \\"foo.json\\"" +`; + +exports[`empty.js [meriyah] format 1`] = ` +"[2:38]: Unexpected token: 'identifier' (2:38) + 1 | export * as foo from \\"foo.json\\" +> 2 | export * as bar from \\"bar.json\\" assert { } + | ^ + 3 | export * as baz from \\"baz.json\\" assert { /* comment */ } + 4 | + 5 | import * as foo from \\"foo.json\\"" +`; + +exports[`empty.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +export * as foo from "foo.json" +export * as bar from "bar.json" assert { } +export * as baz from "baz.json" assert { /* comment */ } + +import * as foo from "foo.json" +import * as bar from "bar.json" assert { } +import * as baz from "baz.json" assert { /* comment */ } +=====================================output===================================== +export * as foo from "foo.json"; +export * as bar from "bar.json"; +export * as baz from "baz.json" /* comment */; + +import * as foo from "foo.json"; +import * as bar from "bar.json"; +import * as baz from "baz.json" /* comment */; + +================================================================================ +`; + +exports[`multi-types.js [espree] format 1`] = ` "Unexpected token assert (1:31) > 1 | import json from \\"./foo.json\\" assert { type: \\"json\\", type: \\"bar\\" }; | ^ 2 | " `; -exports[`import-assertions-multi-types.js [meriyah] format 1`] = ` +exports[`multi-types.js [meriyah] format 1`] = ` "[1:36]: Unexpected token: 'identifier' (1:36) > 1 | import json from \\"./foo.json\\" assert { type: \\"json\\", type: \\"bar\\" }; | ^ 2 | " `; -exports[`import-assertions-multi-types.js format 1`] = ` +exports[`multi-types.js format 1`] = ` ====================================options===================================== parsers: ["babel"] printWidth: 80 @@ -56,21 +101,73 @@ import json from "./foo.json" assert { type: "json", type: "bar" }; ================================================================================ `; -exports[`import-assertions-static.js [espree] format 1`] = ` +exports[`not-import-assertions.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import "x" +assert ({type: 'json'}); + +=====================================output===================================== +import "x"; +assert({ type: "json" }); + +================================================================================ +`; + +exports[`re-export.js [espree] format 1`] = ` +"Unexpected token assert (1:33) +> 1 | export { foo2 } from \\"foo.json\\" assert { type: \\"json\\" }; + | ^ + 2 | export * from \\"foo.json\\" assert { type: \\"json\\" }; + 3 | export * as foo3 from \\"foo.json\\" assert { type: \\"json\\" }; + 4 | " +`; + +exports[`re-export.js [meriyah] format 1`] = ` +"[1:38]: Unexpected token: 'identifier' (1:38) +> 1 | export { foo2 } from \\"foo.json\\" assert { type: \\"json\\" }; + | ^ + 2 | export * from \\"foo.json\\" assert { type: \\"json\\" }; + 3 | export * as foo3 from \\"foo.json\\" assert { type: \\"json\\" }; + 4 | " +`; + +exports[`re-export.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +export { foo2 } from "foo.json" assert { type: "json" }; +export * from "foo.json" assert { type: "json" }; +export * as foo3 from "foo.json" assert { type: "json" }; + +=====================================output===================================== +export { foo2 } from "foo.json" assert { type: "json" }; +export * from "foo.json" assert { type: "json" }; +export * as foo3 from "foo.json" assert { type: "json" }; + +================================================================================ +`; + +exports[`static-import.js [espree] format 1`] = ` "Unexpected token assert (1:31) > 1 | import json from \\"./foo.json\\" assert { type: \\"json\\" }; | ^ 2 | " `; -exports[`import-assertions-static.js [meriyah] format 1`] = ` +exports[`static-import.js [meriyah] format 1`] = ` "[1:36]: Unexpected token: 'identifier' (1:36) > 1 | import json from \\"./foo.json\\" assert { type: \\"json\\" }; | ^ 2 | " `; -exports[`import-assertions-static.js format 1`] = ` +exports[`static-import.js format 1`] = ` ====================================options===================================== parsers: ["babel"] printWidth: 80 @@ -84,18 +181,30 @@ import json from "./foo.json" assert { type: "json" }; ================================================================================ `; -exports[`not-import-assertions.js format 1`] = ` +exports[`without-from.js [espree] format 1`] = ` +"Unexpected token assert (1:14) +> 1 | import \\"foo\\" assert { type: \\"json\\" } + | ^ + 2 | " +`; + +exports[`without-from.js [meriyah] format 1`] = ` +"[1:19]: Unexpected token: 'identifier' (1:19) +> 1 | import \\"foo\\" assert { type: \\"json\\" } + | ^ + 2 | " +`; + +exports[`without-from.js format 1`] = ` ====================================options===================================== parsers: ["babel"] printWidth: 80 | printWidth =====================================input====================================== -import "x" -assert ({type: 'json'}); +import "foo" assert { type: "json" } =====================================output===================================== -import "x"; -assert({ type: "json" }); +import "foo" assert { type: "json" }; ================================================================================ `; diff --git a/tests/js/import-assertions/bracket-spacing/__snapshots__/jsfmt.spec.js.snap b/tests/js/import-assertions/bracket-spacing/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..3e06a9d9effe --- /dev/null +++ b/tests/js/import-assertions/bracket-spacing/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,114 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`dynamic-import.js - {"bracketSpacing":false} [espree] format 1`] = ` +"Unexpected token , (1:20) +> 1 | import(\\"./foo.json\\", { assert: { type: \\"json\\" } }); + | ^ + 2 | " +`; + +exports[`dynamic-import.js - {"bracketSpacing":false} [meriyah] format 1`] = ` +"[1:20]: Expected ')' (1:20) +> 1 | import(\\"./foo.json\\", { assert: { type: \\"json\\" } }); + | ^ + 2 | " +`; + +exports[`dynamic-import.js - {"bracketSpacing":false} format 1`] = ` +====================================options===================================== +bracketSpacing: false +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import("./foo.json", { assert: { type: "json" } }); + +=====================================output===================================== +import("./foo.json", {assert: {type: "json"}}); + +================================================================================ +`; + +exports[`empty.js - {"bracketSpacing":false} [espree] format 1`] = ` +"Unexpected token assert (1:33) +> 1 | export * as bar from \\"bar.json\\" assert { } + | ^" +`; + +exports[`empty.js - {"bracketSpacing":false} [meriyah] format 1`] = ` +"[1:38]: Unexpected token: 'identifier' (1:38) +> 1 | export * as bar from \\"bar.json\\" assert { } + | ^" +`; + +exports[`empty.js - {"bracketSpacing":false} format 1`] = ` +====================================options===================================== +bracketSpacing: false +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +export * as bar from "bar.json" assert { } +=====================================output===================================== +export * as bar from "bar.json"; + +================================================================================ +`; + +exports[`re-export.js - {"bracketSpacing":false} [espree] format 1`] = ` +"Unexpected token assert (1:33) +> 1 | export { foo2 } from \\"foo.json\\" assert { type: \\"json\\" }; + | ^ + 2 | " +`; + +exports[`re-export.js - {"bracketSpacing":false} [meriyah] format 1`] = ` +"[1:38]: Unexpected token: 'identifier' (1:38) +> 1 | export { foo2 } from \\"foo.json\\" assert { type: \\"json\\" }; + | ^ + 2 | " +`; + +exports[`re-export.js - {"bracketSpacing":false} format 1`] = ` +====================================options===================================== +bracketSpacing: false +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +export { foo2 } from "foo.json" assert { type: "json" }; + +=====================================output===================================== +export {foo2} from "foo.json" assert {type: "json"}; + +================================================================================ +`; + +exports[`static-import.js - {"bracketSpacing":false} [espree] format 1`] = ` +"Unexpected token assert (1:31) +> 1 | import json from \\"./foo.json\\" assert { type: \\"json\\" }; + | ^ + 2 | " +`; + +exports[`static-import.js - {"bracketSpacing":false} [meriyah] format 1`] = ` +"[1:36]: Unexpected token: 'identifier' (1:36) +> 1 | import json from \\"./foo.json\\" assert { type: \\"json\\" }; + | ^ + 2 | " +`; + +exports[`static-import.js - {"bracketSpacing":false} format 1`] = ` +====================================options===================================== +bracketSpacing: false +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import json from "./foo.json" assert { type: "json" }; + +=====================================output===================================== +import json from "./foo.json" assert {type: "json"}; + +================================================================================ +`; diff --git a/tests/js/import-assertions/import-assertions-dynamic.js b/tests/js/import-assertions/bracket-spacing/dynamic-import.js similarity index 100% rename from tests/js/import-assertions/import-assertions-dynamic.js rename to tests/js/import-assertions/bracket-spacing/dynamic-import.js diff --git a/tests/js/import-assertions/bracket-spacing/empty.js b/tests/js/import-assertions/bracket-spacing/empty.js new file mode 100644 index 000000000000..f6b005613c7d --- /dev/null +++ b/tests/js/import-assertions/bracket-spacing/empty.js @@ -0,0 +1 @@ +export * as bar from "bar.json" assert { } \ No newline at end of file diff --git a/tests/js/import-assertions/bracket-spacing/jsfmt.spec.js b/tests/js/import-assertions/bracket-spacing/jsfmt.spec.js new file mode 100644 index 000000000000..141e96b4f27f --- /dev/null +++ b/tests/js/import-assertions/bracket-spacing/jsfmt.spec.js @@ -0,0 +1,17 @@ +run_spec(__dirname, ["babel"], { + bracketSpacing: false, + errors: { + espree: [ + "dynamic-import.js", + "static-import.js", + "re-export.js", + "empty.js", + ], + meriyah: [ + "dynamic-import.js", + "static-import.js", + "re-export.js", + "empty.js", + ], + }, +}); diff --git a/tests/js/import-assertions/bracket-spacing/re-export.js b/tests/js/import-assertions/bracket-spacing/re-export.js new file mode 100644 index 000000000000..d3a40791589d --- /dev/null +++ b/tests/js/import-assertions/bracket-spacing/re-export.js @@ -0,0 +1 @@ +export { foo2 } from "foo.json" assert { type: "json" }; diff --git a/tests/js/import-assertions/import-assertions-static.js b/tests/js/import-assertions/bracket-spacing/static-import.js similarity index 100% rename from tests/js/import-assertions/import-assertions-static.js rename to tests/js/import-assertions/bracket-spacing/static-import.js diff --git a/tests/js/import-assertions/dynamic-import.js b/tests/js/import-assertions/dynamic-import.js new file mode 100644 index 000000000000..2411a524bf12 --- /dev/null +++ b/tests/js/import-assertions/dynamic-import.js @@ -0,0 +1 @@ +import("./foo.json", { assert: { type: "json" } }); diff --git a/tests/js/import-assertions/empty.js b/tests/js/import-assertions/empty.js new file mode 100644 index 000000000000..25de961c2745 --- /dev/null +++ b/tests/js/import-assertions/empty.js @@ -0,0 +1,7 @@ +export * as foo from "foo.json" +export * as bar from "bar.json" assert { } +export * as baz from "baz.json" assert { /* comment */ } + +import * as foo from "foo.json" +import * as bar from "bar.json" assert { } +import * as baz from "baz.json" assert { /* comment */ } \ No newline at end of file diff --git a/tests/js/import-assertions/jsfmt.spec.js b/tests/js/import-assertions/jsfmt.spec.js index 7599ddea9386..69aab7bca670 100644 --- a/tests/js/import-assertions/jsfmt.spec.js +++ b/tests/js/import-assertions/jsfmt.spec.js @@ -1,14 +1,20 @@ run_spec(__dirname, ["babel"], { errors: { espree: [ - "import-assertions-dynamic.js", - "import-assertions-multi-types.js", - "import-assertions-static.js", + "dynamic-import.js", + "empty.js", + "multi-types.js", + "static-import.js", + "re-export.js", + "without-from.js", ], meriyah: [ - "import-assertions-dynamic.js", - "import-assertions-multi-types.js", - "import-assertions-static.js", + "dynamic-import.js", + "empty.js", + "multi-types.js", + "static-import.js", + "re-export.js", + "without-from.js", ], }, }); diff --git a/tests/js/import-assertions/import-assertions-multi-types.js b/tests/js/import-assertions/multi-types.js similarity index 100% rename from tests/js/import-assertions/import-assertions-multi-types.js rename to tests/js/import-assertions/multi-types.js diff --git a/tests/js/import-assertions/re-export.js b/tests/js/import-assertions/re-export.js new file mode 100644 index 000000000000..dd9640ec20ad --- /dev/null +++ b/tests/js/import-assertions/re-export.js @@ -0,0 +1,3 @@ +export { foo2 } from "foo.json" assert { type: "json" }; +export * from "foo.json" assert { type: "json" }; +export * as foo3 from "foo.json" assert { type: "json" }; diff --git a/tests/js/import-assertions/static-import.js b/tests/js/import-assertions/static-import.js new file mode 100644 index 000000000000..890e4290079e --- /dev/null +++ b/tests/js/import-assertions/static-import.js @@ -0,0 +1 @@ +import json from "./foo.json" assert { type: "json" }; diff --git a/tests/js/import-assertions/without-from.js b/tests/js/import-assertions/without-from.js new file mode 100644 index 000000000000..daf38156ebb5 --- /dev/null +++ b/tests/js/import-assertions/without-from.js @@ -0,0 +1 @@ +import "foo" assert { type: "json" } diff --git a/tests/js/label/__snapshots__/jsfmt.spec.js.snap b/tests/js/label/__snapshots__/jsfmt.spec.js.snap index 0eb99749d530..d012d92b6198 100644 --- a/tests/js/label/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/label/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,22 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`block-statement-and-regexp.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +a : { b : 1 }/2/; + +=====================================output===================================== +a: { + b: 1; +} +/2/; + +================================================================================ +`; + exports[`comment.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] diff --git a/tests/js/label/block-statement-and-regexp.js b/tests/js/label/block-statement-and-regexp.js new file mode 100644 index 000000000000..b69bf681e7d9 --- /dev/null +++ b/tests/js/label/block-statement-and-regexp.js @@ -0,0 +1 @@ +a : { b : 1 }/2/; diff --git a/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap b/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap index 070940e13274..2dab89f0b1d8 100644 --- a/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`module-string-names.js [espree] format 1`] = ` +exports[`module-string-names-export.js [espree] format 1`] = ` "Unexpected token \\"smile\\" (1:19) > 1 | export { smile as \\"smile\\" } from \\"./emojis.js\\"; | ^ @@ -9,7 +9,7 @@ exports[`module-string-names.js [espree] format 1`] = ` 4 | export { foo, bar as \\"foo\\" } from \\"./emojis.js\\";" `; -exports[`module-string-names.js [meriyah] format 1`] = ` +exports[`module-string-names-export.js [meriyah] format 1`] = ` "[1:25]: Only a identifier can be used to indicate alias (1:25) > 1 | export { smile as \\"smile\\" } from \\"./emojis.js\\"; | ^ @@ -18,7 +18,7 @@ exports[`module-string-names.js [meriyah] format 1`] = ` 4 | export { foo, bar as \\"foo\\" } from \\"./emojis.js\\";" `; -exports[`module-string-names.js format 1`] = ` +exports[`module-string-names-export.js format 1`] = ` ====================================options===================================== parsers: ["babel"] printWidth: 80 @@ -48,3 +48,39 @@ export { "smile" } from "./emojis.js"; ================================================================================ `; + +exports[`module-string-names-import.js [espree] format 1`] = ` +"Unexpected token \\"default\\" (1:10) +> 1 | import { \\"default\\" as quotation } from \\"Confucius\\"; + | ^ + 2 | import { \\"foo\\" as bar, \\"default\\" as qux } from \\"module-a\\"; + 3 | import { \\"學而時習之,不亦說乎?\\" as quotation } from \\"Confucius\\"; + 4 | " +`; + +exports[`module-string-names-import.js [meriyah] format 1`] = ` +"[1:18]: Expected '}' (1:18) +> 1 | import { \\"default\\" as quotation } from \\"Confucius\\"; + | ^ + 2 | import { \\"foo\\" as bar, \\"default\\" as qux } from \\"module-a\\"; + 3 | import { \\"學而時習之,不亦說乎?\\" as quotation } from \\"Confucius\\"; + 4 | " +`; + +exports[`module-string-names-import.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import { "default" as quotation } from "Confucius"; +import { "foo" as bar, "default" as qux } from "module-a"; +import { "學而時習之,不亦說乎?" as quotation } from "Confucius"; + +=====================================output===================================== +import { "default" as quotation } from "Confucius"; +import { "foo" as bar, "default" as qux } from "module-a"; +import { "學而時習之,不亦說乎?" as quotation } from "Confucius"; + +================================================================================ +`; diff --git a/tests/js/module-string-names/module-string-names.js b/tests/js/module-string-names/module-string-names-export.js similarity index 100% rename from tests/js/module-string-names/module-string-names.js rename to tests/js/module-string-names/module-string-names-export.js diff --git a/tests/js/module-string-names/module-string-names-import.js b/tests/js/module-string-names/module-string-names-import.js new file mode 100644 index 000000000000..84ca0d81bcec --- /dev/null +++ b/tests/js/module-string-names/module-string-names-import.js @@ -0,0 +1,3 @@ +import { "default" as quotation } from "Confucius"; +import { "foo" as bar, "default" as qux } from "module-a"; +import { "學而時習之,不亦說乎?" as quotation } from "Confucius"; diff --git a/tests/js/reserved-word/__snapshots__/jsfmt.spec.js.snap b/tests/js/reserved-word/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..797ffc7d403c --- /dev/null +++ b/tests/js/reserved-word/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,59 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`interfaces.js [espree] format 1`] = ` +"The keyword 'interface' is reserved (1:1) +> 1 | interface = \\"foo\\"; + | ^ + 2 | interface + 3; + 3 | interface(); + 4 | class interface {}" +`; + +exports[`interfaces.js [meriyah] format 1`] = ` +"[1:9]: Unexpected token: 'interface' (1:9) +> 1 | interface = \\"foo\\"; + | ^ + 2 | interface + 3; + 3 | interface(); + 4 | class interface {}" +`; + +exports[`interfaces.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +interface = "foo"; +interface + 3; +interface(); +class interface {} +interface ? true : false; +function interface() {} +import interface from "foo"; +foo.interface; +interface.foo; +new interface(); +({ interface: "foo" }); +(interface, "foo"); +void interface; +const interface = "foo"; + +=====================================output===================================== +interface = "foo"; +interface + 3; +interface(); +class interface {} +interface ? true : false; +function interface() {} +import interface from "foo"; +foo.interface; +interface.foo; +new interface(); +({ interface: "foo" }); +interface, "foo"; +void interface; +const interface = "foo"; + +================================================================================ +`; diff --git a/tests/js/reserved-word/interfaces.js b/tests/js/reserved-word/interfaces.js new file mode 100644 index 000000000000..2ce057e27fd8 --- /dev/null +++ b/tests/js/reserved-word/interfaces.js @@ -0,0 +1,14 @@ +interface = "foo"; +interface + 3; +interface(); +class interface {} +interface ? true : false; +function interface() {} +import interface from "foo"; +foo.interface; +interface.foo; +new interface(); +({ interface: "foo" }); +(interface, "foo"); +void interface; +const interface = "foo"; diff --git a/tests/js/reserved-word/jsfmt.spec.js b/tests/js/reserved-word/jsfmt.spec.js new file mode 100644 index 000000000000..0fab4456dc8e --- /dev/null +++ b/tests/js/reserved-word/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["babel"], { errors: { espree: true, meriyah: true } }); diff --git a/tests/misc/errors/babel-ts/__snapshots__/jsfmt.spec.js.snap b/tests/misc/errors/babel-ts/__snapshots__/jsfmt.spec.js.snap index 63d4dde0ac58..e5c3104547cb 100644 --- a/tests/misc/errors/babel-ts/__snapshots__/jsfmt.spec.js.snap +++ b/tests/misc/errors/babel-ts/__snapshots__/jsfmt.spec.js.snap @@ -13,3 +13,14 @@ exports[`type-annotation-func.ts error test 1`] = ` | ^ 2 | " `; + +exports[`type-annotation-in-jsx.tsx error test 1`] = ` +"Did not expect a type annotation here. (3:22) + 1 | function Foo() { + 2 | return ( +> 3 |
+ | ^ + 4 | ); + 5 | } + 6 | " +`; diff --git a/tests/misc/errors/babel-ts/type-annotation-in-jsx.tsx b/tests/misc/errors/babel-ts/type-annotation-in-jsx.tsx new file mode 100644 index 000000000000..ae4343ec3e21 --- /dev/null +++ b/tests/misc/errors/babel-ts/type-annotation-in-jsx.tsx @@ -0,0 +1,5 @@ +function Foo() { + return ( +
+ ); +} diff --git a/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap b/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap index 5354a8b1c146..8c01590d2d70 100644 --- a/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap +++ b/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap @@ -11,6 +11,13 @@ exports[`html-like-comments.js error test 1`] = ` 6 | " `; +exports[`import-assertions-for-export-without-from.js error test 1`] = ` +"Unexpected token, expected \\";\\" (1:16) +> 1 | export { foo } assert { type: \\"json\\" }; + | ^ + 2 | " +`; + exports[`import-assertions-with-parens.js error test 1`] = ` "Unexpected token (1:19) > 1 | import \\"x\\" assert ({type: 'json'}); @@ -25,14 +32,6 @@ exports[`module-attributes.js error test 1`] = ` 2 | " `; -exports[`module-string-name-import.js error test 1`] = ` -"Unexpected token (2:10) - 1 | // https://github.com/babel/babel/issues/12209 -> 2 | import { \\"foo\\" as foo } from \\"module-a\\"; - | ^ - 3 | " -`; - exports[`no-for-in-init-concise-binary-in.js error test 1`] = ` "Unexpected token, expected \\")\\" (3:18) 1 | // https://github.com/babel/babel/pull/11931 diff --git a/tests/misc/errors/js/import-assertions-for-export-without-from.js b/tests/misc/errors/js/import-assertions-for-export-without-from.js new file mode 100644 index 000000000000..bf54727d56a0 --- /dev/null +++ b/tests/misc/errors/js/import-assertions-for-export-without-from.js @@ -0,0 +1 @@ +export { foo } assert { type: "json" }; diff --git a/tests/misc/errors/js/module-string-name-import.js b/tests/misc/errors/js/module-string-name-import.js deleted file mode 100644 index 53167a0d5443..000000000000 --- a/tests/misc/errors/js/module-string-name-import.js +++ /dev/null @@ -1,2 +0,0 @@ -// https://github.com/babel/babel/issues/12209 -import { "foo" as foo } from "module-a"; diff --git a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap index 9f673f0da2f3..fe39e136e522 100644 --- a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap @@ -110,11 +110,23 @@ declare interface Dictionary { [index: string]: T } +declare interface B { + foo([]?): void; + bar({}, []?): any; + baz(a: string, b: number, []?): void; +} + =====================================output===================================== declare interface Dictionary { [index: string]: T; } +declare interface B { + foo([]?): void; + bar({}, []?): any; + baz(a: string, b: number, []?): void; +} + ================================================================================ `; diff --git a/tests/typescript/declare/declare_interface.ts b/tests/typescript/declare/declare_interface.ts index 77a9962cd03a..14e4bfe6177d 100644 --- a/tests/typescript/declare/declare_interface.ts +++ b/tests/typescript/declare/declare_interface.ts @@ -1,3 +1,9 @@ declare interface Dictionary { [index: string]: T } + +declare interface B { + foo([]?): void; + bar({}, []?): any; + baz(a: string, b: number, []?): void; +} diff --git a/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap index 2cffbf1606ca..da391f1508f9 100644 --- a/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/interface/__snapshots__/jsfmt.spec.js.snap @@ -862,6 +862,51 @@ export interface ThirdVeryLongAndBoringInterfaceName ================================================================================ `; +exports[`pattern-parameters.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +interface B { + foo([]?): void; + bar({}, []?): any; + baz(a: string, b: number, []?): void; +} + +=====================================output===================================== +interface B { + foo([]?): void + bar({}, []?): any + baz(a: string, b: number, []?): void +} + +================================================================================ +`; + +exports[`pattern-parameters.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +interface B { + foo([]?): void; + bar({}, []?): any; + baz(a: string, b: number, []?): void; +} + +=====================================output===================================== +interface B { + foo([]?): void; + bar({}, []?): any; + baz(a: string, b: number, []?): void; +} + +================================================================================ +`; + exports[`separator.ts - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/typescript/interface/pattern-parameters.ts b/tests/typescript/interface/pattern-parameters.ts new file mode 100644 index 000000000000..f1cf8d0d3d73 --- /dev/null +++ b/tests/typescript/interface/pattern-parameters.ts @@ -0,0 +1,5 @@ +interface B { + foo([]?): void; + bar({}, []?): any; + baz(a: string, b: number, []?): void; +} diff --git a/tests/typescript/type-alias/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/type-alias/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..7f2c78b4eb82 --- /dev/null +++ b/tests/typescript/type-alias/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,23 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`pattern-parameter.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +type C = { + foo([]?): void; + bar({}, []?): any; + baz(a: string, b: number, []?): void; +}; + +=====================================output===================================== +type C = { + foo([]?): void; + bar({}, []?): any; + baz(a: string, b: number, []?): void; +}; + +================================================================================ +`; diff --git a/tests/typescript/type-alias/jsfmt.spec.js b/tests/typescript/type-alias/jsfmt.spec.js new file mode 100644 index 000000000000..2ea3bb6eb2e4 --- /dev/null +++ b/tests/typescript/type-alias/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["typescript"]); diff --git a/tests/typescript/type-alias/pattern-parameter.ts b/tests/typescript/type-alias/pattern-parameter.ts new file mode 100644 index 000000000000..6880a5bff23a --- /dev/null +++ b/tests/typescript/type-alias/pattern-parameter.ts @@ -0,0 +1,5 @@ +type C = { + foo([]?): void; + bar({}, []?): any; + baz(a: string, b: number, []?): void; +}; diff --git a/yarn.lock b/yarn.lock index d348230b2a82..10fef9ae286c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -277,10 +277,10 @@ chalk "^2.0.0" js-tokens "^4.0.0" -"@babel/parser@7.12.3", "@babel/parser@^7.1.0", "@babel/parser@^7.10.4", "@babel/parser@^7.12.1", "@babel/parser@^7.12.3", "@babel/parser@^7.7.0": - version "7.12.3" - resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.12.3.tgz#a305415ebe7a6c7023b40b5122a0662d928334cd" - integrity sha512-kFsOS0IbsuhO5ojF8Hc8z/8vEIOkylVBrjiZUbLTE3XFe0Qi+uu6HjzQixkFaqr0ZPAMZcBVxEwmsnsLPZ2Xsw== +"@babel/parser@7.12.5", "@babel/parser@^7.1.0", "@babel/parser@^7.10.4", "@babel/parser@^7.12.1", "@babel/parser@^7.12.3", "@babel/parser@^7.7.0": + version "7.12.5" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.12.5.tgz#b4af32ddd473c0bfa643bd7ff0728b8e71b81ea0" + integrity sha512-FVM6RZQ0mn2KCf1VUED7KepYeUWoVShczewOCfm3nzoBybaih51h+sYVVGthW9M6lPByEPTQf+xm27PBdlpwmQ== "@babel/plugin-proposal-async-generator-functions@^7.12.1": version "7.12.1"