diff --git a/changelog_unreleased/javascript/pr-9408.md b/changelog_unreleased/javascript/pr-9408.md new file mode 100644 index 000000000000..c1503f46d3e0 --- /dev/null +++ b/changelog_unreleased/javascript/pr-9408.md @@ -0,0 +1,22 @@ +#### Update to `@babel/parser` 7.12 (#9408 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. + +##### Support Import Assertions + +[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" }; +``` + +##### 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 58216c12c2f9..e59abe7f06f2 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dependencies": { "@angular/compiler": "10.1.6", "@babel/code-frame": "7.10.4", - "@babel/parser": "7.11.5", + "@babel/parser": "7.12.3", "@glimmer/syntax": "0.62.3", "@iarna/toml": "2.2.5", "@typescript-eslint/typescript-estree": "3.10.1", diff --git a/src/language-js/parser-babel.js b/src/language-js/parser-babel.js index fa1f949b2dde..810131166b8b 100644 --- a/src/language-js/parser-babel.js +++ b/src/language-js/parser-babel.js @@ -35,9 +35,10 @@ function babelOptions({ sourceType, extraPlugins = [] }) { "partialApplication", ["decorators", { decoratorsBeforeExport: false }], "privateIn", - ["moduleAttributes", { version: "may-2020" }], + "importAssertions", ["recordAndTuple", { syntaxType: "hash" }], "decimal", + "moduleStringNames", ...extraPlugins, ], tokens: true, diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 2d0ba20ff63b..69e9f2d2e16b 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -809,7 +809,7 @@ function printPathNoParens(path, options, print, args) { } return concat(parts); - case "ExportSpecifier": + case "ExportSpecifier": { parts.push(path.call(print, "local")); if (n.exported && !hasSameLoc(n.local, n.exported, options)) { @@ -817,6 +817,7 @@ function printPathNoParens(path, options, print, args) { } return concat(parts); + } case "ImportNamespaceSpecifier": parts.push("* as "); parts.push(path.call(print, "local")); @@ -876,8 +877,12 @@ function printPathNoParens(path, options, print, args) { parts.push(" ", path.call(print, "source")); } - if (Array.isArray(n.attributes) && n.attributes.length !== 0) { - parts.push(" with ", concat(path.map(print, "attributes"))); + if (Array.isArray(n.assertions) && n.assertions.length !== 0) { + parts.push( + " assert { ", + join(", ", path.map(print, "assertions")), + " }" + ); } parts.push(semi); @@ -914,7 +919,8 @@ function printPathNoParens(path, options, print, args) { parent.type === "DoWhileStatement" || parent.type === "DoExpression" || (parent.type === "CatchClause" && !parentParent.finalizer) || - parent.type === "TSModuleDeclaration") + parent.type === "TSModuleDeclaration" || + parent.type === "TSDeclareFunction") ) { return "{}"; } @@ -3124,6 +3130,7 @@ function printPathNoParens(path, options, print, args) { n.accessibility ? concat([n.accessibility, " "]) : "", n.static ? "static " : "", n.readonly ? "readonly " : "", + n.declare ? "declare " : "", "[", n.parameters ? parametersGroup : "", n.typeAnnotation ? "]: " : "]", diff --git a/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap b/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap index ce07e8eafb2c..3fb0f223cc0f 100644 --- a/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap @@ -683,6 +683,34 @@ iterator.next(2); // Logs "Yield 2" ================================================================================ `; +exports[`import-assertions-dynamic.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "babel-ts", "babel-flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +import("./foo.json", { assert: { type: "json" } }); + +=====================================output===================================== +import("./foo.json", { assert: { type: "json" } }); + +================================================================================ +`; + +exports[`import-assertions-static.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "babel-ts", "babel-flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +import json from "./foo.json" assert { type: "json" }; + +=====================================output===================================== +import json from "./foo.json" assert { type: "json" }; + +================================================================================ +`; + exports[`import-meta.js format 1`] = ` ====================================options===================================== parsers: ["babel", "babel-ts", "babel-flow"] @@ -783,30 +811,16 @@ obj.a.b &&= c; ================================================================================ `; -exports[`module-attributes-dynamic.js format 1`] = ` -====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] -printWidth: 80 - | printWidth -=====================================input====================================== -import("foo.json", { with: { type: "json" } }) - -=====================================output===================================== -import("foo.json", { with: { type: "json" } }); - -================================================================================ -`; - -exports[`module-attributes-static.js format 1`] = ` +exports[`module-string-names.js format 1`] = ` ====================================options===================================== parsers: ["babel", "babel-ts", "babel-flow"] printWidth: 80 | printWidth =====================================input====================================== -import foo from "foo.json" with type: "json"; +export { smile as "😄" } from "./emojis.js"; =====================================output===================================== -import foo from "foo.json" with type: "json"; +export { smile as "😄" } from "./emojis.js"; ================================================================================ `; diff --git a/tests/js/babel-plugins/import-assertions-dynamic.js b/tests/js/babel-plugins/import-assertions-dynamic.js new file mode 100644 index 000000000000..2411a524bf12 --- /dev/null +++ b/tests/js/babel-plugins/import-assertions-dynamic.js @@ -0,0 +1 @@ +import("./foo.json", { assert: { type: "json" } }); diff --git a/tests/js/babel-plugins/import-assertions-static.js b/tests/js/babel-plugins/import-assertions-static.js new file mode 100644 index 000000000000..890e4290079e --- /dev/null +++ b/tests/js/babel-plugins/import-assertions-static.js @@ -0,0 +1 @@ +import json from "./foo.json" assert { type: "json" }; diff --git a/tests/js/babel-plugins/jsfmt.spec.js b/tests/js/babel-plugins/jsfmt.spec.js index 40fa5ab94930..35b39843d803 100644 --- a/tests/js/babel-plugins/jsfmt.spec.js +++ b/tests/js/babel-plugins/jsfmt.spec.js @@ -12,8 +12,8 @@ run_spec(__dirname, ["babel", "babel-ts", "babel-flow"], { "flow.js", "function-bind.js", "function-sent.js", - "module-attributes-dynamic.js", - "module-attributes-static.js", + "import-assertions-dynamic.js", + "import-assertions-static.js", "partial-application.js", "pipeline-operator-fsharp.js", "pipeline-operator-minimal.js", @@ -26,6 +26,7 @@ run_spec(__dirname, ["babel", "babel-ts", "babel-flow"], { "typescript.js", "v8intrinsic.js", "optional-chaining.js", + "module-string-names.js", ], }, }); diff --git a/tests/js/babel-plugins/module-attributes-dynamic.js b/tests/js/babel-plugins/module-attributes-dynamic.js deleted file mode 100644 index baa60fc43c5f..000000000000 --- a/tests/js/babel-plugins/module-attributes-dynamic.js +++ /dev/null @@ -1 +0,0 @@ -import("foo.json", { with: { type: "json" } }) diff --git a/tests/js/babel-plugins/module-attributes-static.js b/tests/js/babel-plugins/module-attributes-static.js deleted file mode 100644 index fb31508a0860..000000000000 --- a/tests/js/babel-plugins/module-attributes-static.js +++ /dev/null @@ -1 +0,0 @@ -import foo from "foo.json" with type: "json"; diff --git a/tests/js/babel-plugins/module-string-names.js b/tests/js/babel-plugins/module-string-names.js new file mode 100644 index 000000000000..631c04f79793 --- /dev/null +++ b/tests/js/babel-plugins/module-string-names.js @@ -0,0 +1 @@ +export { smile as "😄" } from "./emojis.js"; diff --git a/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap b/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..df01ffc7403d --- /dev/null +++ b/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,59 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`import-assertions-dynamic.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import("./foo.json", { assert: { type: "json" } }); + +=====================================output===================================== +import("./foo.json", { assert: { type: "json" } }); + +================================================================================ +`; + +exports[`import-assertions-multi-types.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import json from "./foo.json" assert { type: "json", type: "bar" }; + +=====================================output===================================== +import json from "./foo.json" assert { type: "json", type: "bar" }; + +================================================================================ +`; + +exports[`import-assertions-static.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +import json from "./foo.json" assert { type: "json" }; + +=====================================output===================================== +import json from "./foo.json" assert { type: "json" }; + +================================================================================ +`; + +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" }); + +================================================================================ +`; diff --git a/tests/js/import-assertions/import-assertions-dynamic.js b/tests/js/import-assertions/import-assertions-dynamic.js new file mode 100644 index 000000000000..2411a524bf12 --- /dev/null +++ b/tests/js/import-assertions/import-assertions-dynamic.js @@ -0,0 +1 @@ +import("./foo.json", { assert: { type: "json" } }); diff --git a/tests/js/import-assertions/import-assertions-multi-types.js b/tests/js/import-assertions/import-assertions-multi-types.js new file mode 100644 index 000000000000..3afaa0d2b7cd --- /dev/null +++ b/tests/js/import-assertions/import-assertions-multi-types.js @@ -0,0 +1 @@ +import json from "./foo.json" assert { type: "json", type: "bar" }; diff --git a/tests/js/import-assertions/import-assertions-static.js b/tests/js/import-assertions/import-assertions-static.js new file mode 100644 index 000000000000..890e4290079e --- /dev/null +++ b/tests/js/import-assertions/import-assertions-static.js @@ -0,0 +1 @@ +import json from "./foo.json" assert { type: "json" }; diff --git a/tests/js/import-assertions/jsfmt.spec.js b/tests/js/import-assertions/jsfmt.spec.js new file mode 100644 index 000000000000..c3095e9d8d7f --- /dev/null +++ b/tests/js/import-assertions/jsfmt.spec.js @@ -0,0 +1,9 @@ +run_spec(__dirname, ["babel"], { + errors: { + espree: [ + "import-assertions-dynamic.js", + "import-assertions-multi-types.js", + "import-assertions-static.js", + ], + }, +}); diff --git a/tests/js/import-assertions/not-import-assertions.js b/tests/js/import-assertions/not-import-assertions.js new file mode 100644 index 000000000000..e6461bad63c5 --- /dev/null +++ b/tests/js/import-assertions/not-import-assertions.js @@ -0,0 +1,2 @@ +import "x" +assert ({type: 'json'}); diff --git a/tests/js/literal/__snapshots__/jsfmt.spec.js.snap b/tests/js/literal/__snapshots__/jsfmt.spec.js.snap index 272a89dc91bb..0f5a81730ae3 100644 --- a/tests/js/literal/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/literal/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`invalid-exponent.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +12.3e + +=====================================output===================================== +12.3e; + +================================================================================ +`; + exports[`number.js format 1`] = ` ====================================options===================================== parsers: ["babel", "typescript"] diff --git a/tests/js/literal/invalid-exponent.js b/tests/js/literal/invalid-exponent.js new file mode 100644 index 000000000000..979d49d447f1 --- /dev/null +++ b/tests/js/literal/invalid-exponent.js @@ -0,0 +1 @@ +12.3e diff --git a/tests/js/literal/jsfmt.spec.js b/tests/js/literal/jsfmt.spec.js index 23f3c3a3b6e6..6c46c013dc71 100644 --- a/tests/js/literal/jsfmt.spec.js +++ b/tests/js/literal/jsfmt.spec.js @@ -1,2 +1,4 @@ // flow-parser@0.38.0 fails to parse `1.e1`, so use babel here. -run_spec(__dirname, ["babel", "typescript"], { errors: { espree: true } }); +run_spec(__dirname, ["babel", "typescript"], { + errors: { espree: true, typescript: ["invalid-exponent.js"] }, +}); diff --git a/tests/js/module-attributes/__snapshots__/jsfmt.spec.js.snap b/tests/js/module-attributes/__snapshots__/jsfmt.spec.js.snap deleted file mode 100644 index 43233aa6c419..000000000000 --- a/tests/js/module-attributes/__snapshots__/jsfmt.spec.js.snap +++ /dev/null @@ -1,29 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`module-attributes-dynamic.js format 1`] = ` -====================================options===================================== -parsers: ["babel"] -printWidth: 80 - | printWidth -=====================================input====================================== -import("foo.json", { with: { type: "json" } }) - -=====================================output===================================== -import("foo.json", { with: { type: "json" } }); - -================================================================================ -`; - -exports[`module-attributes-static.js format 1`] = ` -====================================options===================================== -parsers: ["babel"] -printWidth: 80 - | printWidth -=====================================input====================================== -import foo from "foo.json" with type: "json"; - -=====================================output===================================== -import foo from "foo.json" with type: "json"; - -================================================================================ -`; diff --git a/tests/js/module-attributes/module-attributes-dynamic.js b/tests/js/module-attributes/module-attributes-dynamic.js deleted file mode 100644 index baa60fc43c5f..000000000000 --- a/tests/js/module-attributes/module-attributes-dynamic.js +++ /dev/null @@ -1 +0,0 @@ -import("foo.json", { with: { type: "json" } }) diff --git a/tests/js/module-attributes/module-attributes-static.js b/tests/js/module-attributes/module-attributes-static.js deleted file mode 100644 index fb31508a0860..000000000000 --- a/tests/js/module-attributes/module-attributes-static.js +++ /dev/null @@ -1 +0,0 @@ -import foo from "foo.json" with type: "json"; diff --git a/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap b/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..b2f9e1c02d49 --- /dev/null +++ b/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,32 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`module-string-names.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +export { smile as "smile" } from "./emojis.js"; +export { "smile" as smile } from "./emojis.js"; +export { "smile" as "smile" } from "./emojis.js"; +export { foo, bar as "foo" } from "./emojis.js"; +export { foo, bar as "foo" }; +export { "學而時習之,不亦說乎?", "吾道一以貫之。" as "忠恕。" } from "Confucius"; +export * as "foo", { default as "quux" } from "module-b"; +export { "smile" } from "./emojis.js"; + +=====================================output===================================== +export { smile as "smile" } from "./emojis.js"; +export { "smile" as smile } from "./emojis.js"; +export { "smile" as "smile" } from "./emojis.js"; +export { foo, bar as "foo" } from "./emojis.js"; +export { foo, bar as "foo" }; +export { + "學而時習之,不亦說乎?", + "吾道一以貫之。" as "忠恕。", +} from "Confucius"; +export * as "foo", { default as "quux" } from "module-b"; +export { "smile" } from "./emojis.js"; + +================================================================================ +`; diff --git a/tests/js/module-attributes/jsfmt.spec.js b/tests/js/module-string-names/jsfmt.spec.js similarity index 100% rename from tests/js/module-attributes/jsfmt.spec.js rename to tests/js/module-string-names/jsfmt.spec.js diff --git a/tests/js/module-string-names/module-string-names.js b/tests/js/module-string-names/module-string-names.js new file mode 100644 index 000000000000..cab6023a156e --- /dev/null +++ b/tests/js/module-string-names/module-string-names.js @@ -0,0 +1,8 @@ +export { smile as "smile" } from "./emojis.js"; +export { "smile" as smile } from "./emojis.js"; +export { "smile" as "smile" } from "./emojis.js"; +export { foo, bar as "foo" } from "./emojis.js"; +export { foo, bar as "foo" }; +export { "學而時習之,不亦說乎?", "吾道一以貫之。" as "忠恕。" } from "Confucius"; +export * as "foo", { default as "quux" } from "module-b"; +export { "smile" } from "./emojis.js"; diff --git a/tests/js/objects/__snapshots__/jsfmt.spec.js.snap b/tests/js/objects/__snapshots__/jsfmt.spec.js.snap index fe84e6fa3470..e47156c4dc6d 100644 --- a/tests/js/objects/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/objects/__snapshots__/jsfmt.spec.js.snap @@ -143,6 +143,20 @@ printWidth: 80 ================================================================================ `; +exports[`invalid-setter.js format 1`] = ` +====================================options===================================== +parsers: ["babel"] +printWidth: 80 + | printWidth +=====================================input====================================== +({ set x(){} }); + +=====================================output===================================== +({ set x() {} }); + +================================================================================ +`; + exports[`method.js format 1`] = ` ====================================options===================================== parsers: ["babel"] diff --git a/tests/js/objects/invalid-setter.js b/tests/js/objects/invalid-setter.js new file mode 100644 index 000000000000..0dcad3093338 --- /dev/null +++ b/tests/js/objects/invalid-setter.js @@ -0,0 +1 @@ +({ set x(){} }); diff --git a/tests/js/objects/jsfmt.spec.js b/tests/js/objects/jsfmt.spec.js index b113fb8352d0..74867c290696 100644 --- a/tests/js/objects/jsfmt.spec.js +++ b/tests/js/objects/jsfmt.spec.js @@ -1,3 +1,9 @@ run_spec(__dirname, ["babel"], { - errors: { espree: ["expression.js", "invalid-accessor-generator.js"] }, + errors: { + espree: [ + "expression.js", + "invalid-accessor-generator.js", + "invalid-setter.js", + ], + }, }); diff --git a/tests/js/variable_declarator/__snapshots__/jsfmt.spec.js.snap b/tests/js/variable_declarator/__snapshots__/jsfmt.spec.js.snap index 8176ccad8608..a89b178417dc 100644 --- a/tests/js/variable_declarator/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/variable_declarator/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,19 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`invalid-const.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +const foo; + +=====================================output===================================== +const foo; + +================================================================================ +`; + exports[`multiple.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] diff --git a/tests/js/variable_declarator/invalid-const.js b/tests/js/variable_declarator/invalid-const.js new file mode 100644 index 000000000000..f15614ea39c3 --- /dev/null +++ b/tests/js/variable_declarator/invalid-const.js @@ -0,0 +1 @@ +const foo; diff --git a/tests/js/variable_declarator/jsfmt.spec.js b/tests/js/variable_declarator/jsfmt.spec.js index eb85eda6bd02..7ad187763274 100644 --- a/tests/js/variable_declarator/jsfmt.spec.js +++ b/tests/js/variable_declarator/jsfmt.spec.js @@ -1 +1,6 @@ -run_spec(__dirname, ["babel", "flow", "typescript"]); +run_spec(__dirname, ["babel", "flow", "typescript"], { + errors: { + espree: ["invalid-const.js"], + flow: ["invalid-const.js"], + }, +}); 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 f7efaedf67d4..63d4dde0ac58 100644 --- a/tests/misc/errors/babel-ts/__snapshots__/jsfmt.spec.js.snap +++ b/tests/misc/errors/babel-ts/__snapshots__/jsfmt.spec.js.snap @@ -1,8 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`issue-8041.ts error test 1`] = ` -"Did not expect a type annotation here. (1:2) -> 1 | (a:b) - | ^ +exports[`type-annotation-expr-statement.ts error test 1`] = ` +"Did not expect a type annotation here. (1:3) +> 1 | (a: T); + | ^ + 2 | " +`; + +exports[`type-annotation-func.ts error test 1`] = ` +"Did not expect a type annotation here. (1:10) +> 1 | func(a: T); + | ^ 2 | " `; diff --git a/tests/misc/errors/babel-ts/issue-8041.ts b/tests/misc/errors/babel-ts/issue-8041.ts deleted file mode 100644 index 4163d9c50531..000000000000 --- a/tests/misc/errors/babel-ts/issue-8041.ts +++ /dev/null @@ -1 +0,0 @@ -(a:b) diff --git a/tests/misc/errors/babel-ts/type-annotation-expr-statement.ts b/tests/misc/errors/babel-ts/type-annotation-expr-statement.ts new file mode 100644 index 000000000000..25f14e1b9984 --- /dev/null +++ b/tests/misc/errors/babel-ts/type-annotation-expr-statement.ts @@ -0,0 +1 @@ +(a: T); diff --git a/tests/misc/errors/babel-ts/type-annotation-func.ts b/tests/misc/errors/babel-ts/type-annotation-func.ts new file mode 100644 index 000000000000..837f402397e7 --- /dev/null +++ b/tests/misc/errors/babel-ts/type-annotation-func.ts @@ -0,0 +1 @@ +func(a: T); diff --git a/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap b/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap index 02535f581236..5354a8b1c146 100644 --- a/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap +++ b/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap @@ -11,13 +11,28 @@ exports[`html-like-comments.js error test 1`] = ` 6 | " `; +exports[`import-assertions-with-parens.js error test 1`] = ` +"Unexpected token (1:19) +> 1 | import \\"x\\" assert ({type: 'json'}); + | ^ + 2 | " +`; + exports[`module-attributes.js error test 1`] = ` -"The only accepted module attribute is \`type\` (1:33) -> 1 | import foo from \\"foo.json\\" with foo: \\"json\\"; - | ^ +"Unexpected token, expected \\";\\" (1:28) +> 1 | import foo from \\"foo.json\\" with type: \\"json\\"; + | ^ 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-with-parens.js b/tests/misc/errors/js/import-assertions-with-parens.js new file mode 100644 index 000000000000..f20676b37cb0 --- /dev/null +++ b/tests/misc/errors/js/import-assertions-with-parens.js @@ -0,0 +1 @@ +import "x" assert ({type: 'json'}); diff --git a/tests/misc/errors/js/module-attributes.js b/tests/misc/errors/js/module-attributes.js index ecc33f5eb941..fb31508a0860 100644 --- a/tests/misc/errors/js/module-attributes.js +++ b/tests/misc/errors/js/module-attributes.js @@ -1 +1 @@ -import foo from "foo.json" with foo: "json"; +import foo from "foo.json" with type: "json"; diff --git a/tests/misc/errors/js/module-string-name-import.js b/tests/misc/errors/js/module-string-name-import.js new file mode 100644 index 000000000000..53167a0d5443 --- /dev/null +++ b/tests/misc/errors/js/module-string-name-import.js @@ -0,0 +1,2 @@ +// https://github.com/babel/babel/issues/12209 +import { "foo" as foo } from "module-a"; diff --git a/tests/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap b/tests/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap index 42500d9ead56..2d7dba38a3f5 100644 --- a/tests/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap +++ b/tests/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,25 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`declare-index-signature.ts format 1`] = ` +====================================options===================================== +parsers: ["babel-ts"] +printWidth: 80 + | printWidth +=====================================input====================================== +// Invalid, but recoverable +class C { + declare [key: string]: string; +} + +=====================================output===================================== +// Invalid, but recoverable +class C { + declare [key: string]: string; +} + +================================================================================ +`; + exports[`tuple-labeled-ts.ts format 1`] = ` ====================================options===================================== parsers: ["babel-ts"] diff --git a/tests/misc/typescript-babel-only/declare-index-signature.ts b/tests/misc/typescript-babel-only/declare-index-signature.ts new file mode 100644 index 000000000000..2ba1b7e1c876 --- /dev/null +++ b/tests/misc/typescript-babel-only/declare-index-signature.ts @@ -0,0 +1,4 @@ +// Invalid, but recoverable +class C { + declare [key: string]: string; +} diff --git a/tests/typescript/class/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/class/__snapshots__/jsfmt.spec.js.snap index 1928de49ef7b..c8f5488ae1a5 100644 --- a/tests/typescript/class/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/class/__snapshots__/jsfmt.spec.js.snap @@ -21,6 +21,10 @@ class A { } } +class B { + constructor<>() {} +} + =====================================output===================================== class foo { constructor(static a: number) {} @@ -37,6 +41,10 @@ class A { } } +class B { + constructor<>() {} +} + ================================================================================ `; diff --git a/tests/typescript/class/constructor.ts b/tests/typescript/class/constructor.ts index 86297c73acfd..9934c471ab5a 100644 --- a/tests/typescript/class/constructor.ts +++ b/tests/typescript/class/constructor.ts @@ -12,3 +12,7 @@ class A { return new A() } } + +class B { + constructor<>() {} +} diff --git a/tests/typescript/conformance/types/parameterProperty/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/types/parameterProperty/__snapshots__/jsfmt.spec.js.snap index 46af62801656..1fc8509a406f 100644 --- a/tests/typescript/conformance/types/parameterProperty/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/conformance/types/parameterProperty/__snapshots__/jsfmt.spec.js.snap @@ -10,6 +10,8 @@ class c3 { constructor(public arg: number = 10) { // fails because of comment } + not_constructor(public arg: number = 10) { + } } =====================================output===================================== @@ -17,6 +19,7 @@ class c3 { constructor(public arg: number = 10) { // fails because of comment } + not_constructor(public arg: number = 10) {} } ================================================================================ diff --git a/tests/typescript/conformance/types/parameterProperty/parameterProperty.ts b/tests/typescript/conformance/types/parameterProperty/parameterProperty.ts index 7928eb4ca428..56527665c176 100644 --- a/tests/typescript/conformance/types/parameterProperty/parameterProperty.ts +++ b/tests/typescript/conformance/types/parameterProperty/parameterProperty.ts @@ -2,4 +2,6 @@ class c3 { constructor(public arg: number = 10) { // fails because of comment } + not_constructor(public arg: number = 10) { + } } diff --git a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap index 82d434d08950..9f673f0da2f3 100644 --- a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap @@ -8,6 +8,12 @@ printWidth: 80 =====================================input====================================== class B {p: number;} class C extends B {declare p: 256 | 1000;} +class D { + declare field = "field"; +} +declare class D { + field = "field"; +} =====================================output===================================== class B { @@ -16,6 +22,12 @@ class B { class C extends B { declare p: 256 | 1000; } +class D { + declare field = "field"; +} +declare class D { + field = "field"; +} ================================================================================ `; @@ -50,6 +62,8 @@ declare namespace A { function y(): void; } +declare function f([]?) + =====================================output===================================== declare function x(); declare function y(): void; @@ -59,6 +73,30 @@ declare namespace A { function y(): void; } +declare function f([]?); + +================================================================================ +`; + +exports[`declare_function_with_body.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +// Invalid, but recoverable +declare function foo() {} +declare function bar() { + // comment +} + +=====================================output===================================== +// Invalid, but recoverable +declare function foo() {}; +declare function bar() { + // comment +}; + ================================================================================ `; @@ -80,6 +118,50 @@ declare interface Dictionary { ================================================================================ `; +exports[`declare_module.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +declare module m { + class C { + field = "field"; + } +} + +=====================================output===================================== +declare module m { + class C { + field = "field"; + } +} + +================================================================================ +`; + +exports[`declare_namespace.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +declare namespace m { + class C { + field = "field"; + } +} + +=====================================output===================================== +declare namespace m { + class C { + field = "field"; + } +} + +================================================================================ +`; + exports[`declare_var.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/typescript/declare/declare_class_fields.ts b/tests/typescript/declare/declare_class_fields.ts index 447d7a37ef53..d805d9997f0a 100644 --- a/tests/typescript/declare/declare_class_fields.ts +++ b/tests/typescript/declare/declare_class_fields.ts @@ -1,2 +1,8 @@ class B {p: number;} class C extends B {declare p: 256 | 1000;} +class D { + declare field = "field"; +} +declare class D { + field = "field"; +} diff --git a/tests/typescript/declare/declare_function.ts b/tests/typescript/declare/declare_function.ts index 67710eb24f78..d6b56a0c2c05 100644 --- a/tests/typescript/declare/declare_function.ts +++ b/tests/typescript/declare/declare_function.ts @@ -5,3 +5,5 @@ declare namespace A { function x(); function y(): void; } + +declare function f([]?) diff --git a/tests/typescript/declare/declare_function_with_body.ts b/tests/typescript/declare/declare_function_with_body.ts new file mode 100644 index 000000000000..34c28e8fc893 --- /dev/null +++ b/tests/typescript/declare/declare_function_with_body.ts @@ -0,0 +1,5 @@ +// Invalid, but recoverable +declare function foo() {} +declare function bar() { + // comment +} diff --git a/tests/typescript/declare/declare_module.ts b/tests/typescript/declare/declare_module.ts new file mode 100644 index 000000000000..3e9ccd1749f7 --- /dev/null +++ b/tests/typescript/declare/declare_module.ts @@ -0,0 +1,5 @@ +declare module m { + class C { + field = "field"; + } +} diff --git a/tests/typescript/declare/declare_namespace.ts b/tests/typescript/declare/declare_namespace.ts new file mode 100644 index 000000000000..9d74551c7292 --- /dev/null +++ b/tests/typescript/declare/declare_namespace.ts @@ -0,0 +1,5 @@ +declare namespace m { + class C { + field = "field"; + } +} diff --git a/yarn.lock b/yarn.lock index b64b4993cc00..c4ec2e866f64 100644 --- a/yarn.lock +++ b/yarn.lock @@ -277,10 +277,10 @@ chalk "^2.0.0" js-tokens "^4.0.0" -"@babel/parser@7.11.5": - version "7.11.5" - resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.11.5.tgz#c7ff6303df71080ec7a4f5b8c003c58f1cf51037" - integrity sha512-X9rD8qqm695vgmeaQ4fvz/o3+Wk4ZzQvSHkDBgpYKxpD4qTAUm88ZKtHkVqIOsYFFbIQ6wQYhC6q7pjqVK0E0Q== +"@babel/parser@7.12.3": + 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.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"