From c87b672bbdcbd961daa129600aabec565ef803d9 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Thu, 15 Oct 2020 12:19:16 +0900 Subject: [PATCH 01/25] Install babel/parser 7.12 --- package.json | 2 +- yarn.lock | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 9b2276dcb25d..dc27a4ab89ed 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dependencies": { "@angular/compiler": "10.1.5", "@babel/code-frame": "7.10.4", - "@babel/parser": "7.11.5", + "@babel/parser": "7.12.0", "@glimmer/syntax": "0.62.1", "@iarna/toml": "2.2.5", "@typescript-eslint/typescript-estree": "3.10.1", diff --git a/yarn.lock b/yarn.lock index 15636396a725..5d910f306db6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -279,7 +279,12 @@ chalk "^2.0.0" js-tokens "^4.0.0" -"@babel/parser@7.11.5", "@babel/parser@^7.1.0", "@babel/parser@^7.10.4", "@babel/parser@^7.11.5", "@babel/parser@^7.7.0": +"@babel/parser@7.12.0": + version "7.12.0" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.12.0.tgz#2ad388f3960045b22f9b7d4bf85e80b15a1c9e3a" + integrity sha512-dYmySMYnlus2jwl7JnnajAj11obRStZoW9cG04wh4ZuhozDn11tDUrhHcUZ9iuNHqALAhh60XqNaYXpvuuE/Gg== + +"@babel/parser@^7.1.0", "@babel/parser@^7.10.4", "@babel/parser@^7.11.5", "@babel/parser@^7.7.0": version "7.11.5" resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.11.5.tgz#c7ff6303df71080ec7a4f5b8c003c58f1cf51037" integrity sha512-X9rD8qqm695vgmeaQ4fvz/o3+Wk4ZzQvSHkDBgpYKxpD4qTAUm88ZKtHkVqIOsYFFbIQ6wQYhC6q7pjqVK0E0Q== From b5308bd7e6df0fb94a76f684e4979171cc5e72ce Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Thu, 15 Oct 2020 16:37:36 +0900 Subject: [PATCH 02/25] Add tests for babel/babel#12161 --- src/language-js/printer-estree.js | 3 ++- .../misc/errors/babel-ts/__snapshots__/jsfmt.spec.js.snap | 8 ++++---- tests/misc/errors/babel-ts/issue-8041.ts | 1 - tests/misc/errors/babel-ts/type-annotation.ts | 1 + 4 files changed, 7 insertions(+), 6 deletions(-) delete mode 100644 tests/misc/errors/babel-ts/issue-8041.ts create mode 100644 tests/misc/errors/babel-ts/type-annotation.ts diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index e303d3cf2bd8..c17df7b7ecfd 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -2886,7 +2886,8 @@ function printPathNoParens(path, options, print, args) { return group(concat(parts)); } - case "TypeCastExpression": { + case "TypeCastExpression": + case "TSTypeCastExpression": { return concat([ "(", path.call(print, "expression"), 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..6d96116982a4 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,8 @@ // 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.ts error test 1`] = ` +"Did not expect a type annotation here. (1:9) +> 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.ts b/tests/misc/errors/babel-ts/type-annotation.ts new file mode 100644 index 000000000000..837f402397e7 --- /dev/null +++ b/tests/misc/errors/babel-ts/type-annotation.ts @@ -0,0 +1 @@ +func(a: T); From 7979066a85168804ce63cf59c8f0c73a4e58c9d5 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Thu, 15 Oct 2020 23:29:41 +0900 Subject: [PATCH 03/25] Add test for babel/babel#12076 --- tests/js/objects/__snapshots__/jsfmt.spec.js.snap | 14 ++++++++++++++ tests/js/objects/invalid-setter.js | 1 + tests/js/objects/jsfmt.spec.js | 8 +++++++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/js/objects/invalid-setter.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..5ac39ca0386f 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", + ] + }, }); From b19743ffc6c28a98553ed3751ac85f1e0fdc4cd3 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Thu, 15 Oct 2020 23:35:35 +0900 Subject: [PATCH 04/25] Add test for babel/babel#12085 --- tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap | 4 ++++ tests/typescript/declare/declare_function.ts | 2 ++ 2 files changed, 6 insertions(+) diff --git a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap index 82d434d08950..fba2ff4d264e 100644 --- a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap @@ -50,6 +50,8 @@ declare namespace A { function y(): void; } +declare function f([]?) + =====================================output===================================== declare function x(); declare function y(): void; @@ -59,6 +61,8 @@ declare namespace A { function y(): void; } +declare function f([]?); + ================================================================================ `; 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([]?) From d599e726a3515a8fd6f45ec330d91376574ffc5c Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Thu, 15 Oct 2020 23:47:31 +0900 Subject: [PATCH 05/25] Add test for babel/babel#12108 --- .../declare/__snapshots__/jsfmt.spec.js.snap | 50 +++++++++++++++++++ .../declare/declare_class_fields.ts | 3 ++ tests/typescript/declare/declare_module.ts | 5 ++ tests/typescript/declare/declare_namespace.ts | 5 ++ 4 files changed, 63 insertions(+) create mode 100644 tests/typescript/declare/declare_module.ts create mode 100644 tests/typescript/declare/declare_namespace.ts diff --git a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap index fba2ff4d264e..1fcc04e83438 100644 --- a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap @@ -8,6 +8,9 @@ printWidth: 80 =====================================input====================================== class B {p: number;} class C extends B {declare p: 256 | 1000;} +declare class D { + field = "field"; +} =====================================output===================================== class B { @@ -16,6 +19,9 @@ class B { class C extends B { declare p: 256 | 1000; } +declare class D { + field = "field"; +} ================================================================================ `; @@ -84,6 +90,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..aaa8193f812e 100644 --- a/tests/typescript/declare/declare_class_fields.ts +++ b/tests/typescript/declare/declare_class_fields.ts @@ -1,2 +1,5 @@ class B {p: number;} class C extends B {declare p: 256 | 1000;} +declare class D { + field = "field"; +} 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"; + } +} From 7417d90a16b549fe9eeb0409efbee1229ba0a7a1 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Fri, 16 Oct 2020 00:08:25 +0900 Subject: [PATCH 06/25] Add test for babel/babel#12120 --- tests/js/objects/jsfmt.spec.js | 2 +- .../__snapshots__/jsfmt.spec.js.snap | 14 ++++++++++++++ tests/js/variable_declarator/invalid-const.js | 1 + tests/js/variable_declarator/jsfmt.spec.js | 7 ++++++- 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/js/variable_declarator/invalid-const.js diff --git a/tests/js/objects/jsfmt.spec.js b/tests/js/objects/jsfmt.spec.js index 5ac39ca0386f..74867c290696 100644 --- a/tests/js/objects/jsfmt.spec.js +++ b/tests/js/objects/jsfmt.spec.js @@ -4,6 +4,6 @@ run_spec(__dirname, ["babel"], { "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"], + }, +}); From cf0ed635d42c0519561cae4340ab5aaec1150679 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Fri, 16 Oct 2020 00:27:58 +0900 Subject: [PATCH 07/25] Add test for babel/babel#12054 --- src/language-js/printer-estree.js | 3 ++- .../declare/__snapshots__/jsfmt.spec.js.snap | 22 +++++++++++++++++++ .../declare/declare_function_with_body.ts | 5 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/typescript/declare/declare_function_with_body.ts diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index c17df7b7ecfd..ab57ee5b3c48 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -913,7 +913,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 "{}"; } diff --git a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap index 1fcc04e83438..cf4b00955da3 100644 --- a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap @@ -72,6 +72,28 @@ 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 +}; + +================================================================================ +`; + exports[`declare_interface.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] 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 +} From 2e42f06480232eec17f887adc39aaac740133caf Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Fri, 16 Oct 2020 00:40:25 +0900 Subject: [PATCH 08/25] Add test for babel/babel#12061 --- .../types/parameterProperty/__snapshots__/jsfmt.spec.js.snap | 3 +++ .../conformance/types/parameterProperty/parameterProperty.ts | 2 ++ 2 files changed, 5 insertions(+) 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) { + } } From 6c20b0dc7c2d76fbd0804f1e015c05eb28e4d04a Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Fri, 16 Oct 2020 01:06:39 +0900 Subject: [PATCH 09/25] Add test babel/babel#12093 --- tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap | 6 ++++++ tests/typescript/declare/declare_class_fields.ts | 3 +++ 2 files changed, 9 insertions(+) diff --git a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap index cf4b00955da3..9f673f0da2f3 100644 --- a/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/declare/__snapshots__/jsfmt.spec.js.snap @@ -8,6 +8,9 @@ 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"; } @@ -19,6 +22,9 @@ class B { 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_class_fields.ts b/tests/typescript/declare/declare_class_fields.ts index aaa8193f812e..d805d9997f0a 100644 --- a/tests/typescript/declare/declare_class_fields.ts +++ b/tests/typescript/declare/declare_class_fields.ts @@ -1,5 +1,8 @@ class B {p: number;} class C extends B {declare p: 256 | 1000;} +class D { + declare field = "field"; +} declare class D { field = "field"; } From 58cbf8b1aef93279fe65a19930f8e4b575939b71 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Fri, 16 Oct 2020 01:23:46 +0900 Subject: [PATCH 10/25] Add test for babel/babel#12065 --- tests/typescript/class/__snapshots__/jsfmt.spec.js.snap | 8 ++++++++ tests/typescript/class/constructor.ts | 4 ++++ 2 files changed, 12 insertions(+) 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<>() {} +} From cab1ee70ad49999dde4691284bf0ba728bd9e2b2 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Sat, 17 Oct 2020 00:32:08 +0900 Subject: [PATCH 11/25] Add test for babel/babel#12111 --- src/language-js/printer-estree.js | 1 + .../__snapshots__/jsfmt.spec.js.snap | 20 +++++++++++++++++++ .../declare-index-signature.ts | 4 ++++ 3 files changed, 25 insertions(+) create mode 100644 tests/misc/typescript-babel-only/declare-index-signature.ts diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index ab57ee5b3c48..6d0d8c8b356b 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -3118,6 +3118,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/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; +} From e41828240d390fc845119fbead2d94856e2c4672 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Sat, 17 Oct 2020 00:59:01 +0900 Subject: [PATCH 12/25] Add test for babel/babel#12072 --- tests/js/literal/__snapshots__/jsfmt.spec.js.snap | 14 ++++++++++++++ tests/js/literal/invalid-exponent.js | 1 + tests/js/literal/jsfmt.spec.js | 4 +++- 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/js/literal/invalid-exponent.js 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"] }, +}); From d2365f674138e7510597c418d4e417a650ffc9e5 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Sun, 18 Oct 2020 18:11:09 +0900 Subject: [PATCH 13/25] Switch syntax-module-attributes to syntax-import-assertion --- src/language-js/parser-babel.js | 2 +- src/language-js/printer-estree.js | 8 ++- .../__snapshots__/jsfmt.spec.js.snap | 56 +++++++++---------- .../import-assertions-dynamic.js | 1 + .../babel-plugins/import-assertions-static.js | 1 + tests/js/babel-plugins/jsfmt.spec.js | 4 +- .../module-attributes-dynamic.js | 1 - .../babel-plugins/module-attributes-static.js | 1 - .../__snapshots__/jsfmt.spec.js.snap | 43 ++++++++++++++ .../import-assertions-dynamic.js | 1 + .../import-assertions-multi-types.js | 1 + .../import-assertions-static.js | 1 + .../jsfmt.spec.js | 0 .../__snapshots__/jsfmt.spec.js.snap | 29 ---------- .../module-attributes-dynamic.js | 1 - .../module-attributes-static.js | 1 - 16 files changed, 85 insertions(+), 66 deletions(-) create mode 100644 tests/js/babel-plugins/import-assertions-dynamic.js create mode 100644 tests/js/babel-plugins/import-assertions-static.js delete mode 100644 tests/js/babel-plugins/module-attributes-dynamic.js delete mode 100644 tests/js/babel-plugins/module-attributes-static.js create mode 100644 tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/js/import-assertions/import-assertions-dynamic.js create mode 100644 tests/js/import-assertions/import-assertions-multi-types.js create mode 100644 tests/js/import-assertions/import-assertions-static.js rename tests/js/{module-attributes => import-assertions}/jsfmt.spec.js (100%) delete mode 100644 tests/js/module-attributes/__snapshots__/jsfmt.spec.js.snap delete mode 100644 tests/js/module-attributes/module-attributes-dynamic.js delete mode 100644 tests/js/module-attributes/module-attributes-static.js diff --git a/src/language-js/parser-babel.js b/src/language-js/parser-babel.js index 8f43d5af634d..982bfc925ba6 100644 --- a/src/language-js/parser-babel.js +++ b/src/language-js/parser-babel.js @@ -35,7 +35,7 @@ function babelOptions({ sourceType, extraPlugins = [] }) { "partialApplication", ["decorators", { decoratorsBeforeExport: false }], "privateIn", - ["moduleAttributes", { version: "may-2020" }], + "importAssertions", ["recordAndTuple", { syntaxType: "hash" }], "decimal", ...extraPlugins, diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 6d0d8c8b356b..0e97df0a9ac9 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -875,8 +875,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); diff --git a/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap b/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap index ce07e8eafb2c..ad2930a691af 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,34 +811,6 @@ 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`] = ` -====================================options===================================== -parsers: ["babel", "babel-ts", "babel-flow"] -printWidth: 80 - | printWidth -=====================================input====================================== -import foo from "foo.json" with type: "json"; - -=====================================output===================================== -import foo from "foo.json" with type: "json"; - -================================================================================ -`; - exports[`nullish-coalescing-operator.js format 1`] = ` ====================================options===================================== parsers: ["babel", "babel-ts", "babel-flow"] 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..b85bc01d31bb 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", 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/import-assertions/__snapshots__/jsfmt.spec.js.snap b/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..a2e60e8ccf73 --- /dev/null +++ b/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,43 @@ +// 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" }; + +================================================================================ +`; 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/module-attributes/jsfmt.spec.js b/tests/js/import-assertions/jsfmt.spec.js similarity index 100% rename from tests/js/module-attributes/jsfmt.spec.js rename to tests/js/import-assertions/jsfmt.spec.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"; From 26ec800e5439fa8d58bd6afe68b4a578933cd421 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Sun, 18 Oct 2020 21:03:22 +0900 Subject: [PATCH 14/25] Support "String import/export specifier" --- src/language-js/parser-babel.js | 1 + src/language-js/printer-estree.js | 11 +++++-- .../__snapshots__/jsfmt.spec.js.snap | 14 +++++++++ tests/js/babel-plugins/jsfmt.spec.js | 1 + tests/js/babel-plugins/module-string-names.js | 1 + .../__snapshots__/jsfmt.spec.js.snap | 30 +++++++++++++++++++ tests/js/module-string-names/jsfmt.spec.js | 1 + .../module-string-names.js | 7 +++++ 8 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/js/babel-plugins/module-string-names.js create mode 100644 tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/js/module-string-names/jsfmt.spec.js create mode 100644 tests/js/module-string-names/module-string-names.js diff --git a/src/language-js/parser-babel.js b/src/language-js/parser-babel.js index 982bfc925ba6..2685fbf5fd73 100644 --- a/src/language-js/parser-babel.js +++ b/src/language-js/parser-babel.js @@ -38,6 +38,7 @@ function babelOptions({ sourceType, extraPlugins = [] }) { "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 0e97df0a9ac9..1e5b182ca0be 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -808,14 +808,21 @@ function printPathNoParens(path, options, print, args) { } return concat(parts); - case "ExportSpecifier": + case "ExportSpecifier": { parts.push(path.call(print, "local")); - if (n.exported && n.exported.name !== n.local.name) { + const { exported, local } = n; + if ( + exported && + (exported.type !== local.type || + (exported.type === "Identifier" && exported.name !== local.name) || + (exported.type === "StringLiteral" && exported.value !== local.value)) + ) { parts.push(" as ", path.call(print, "exported")); } return concat(parts); + } case "ImportNamespaceSpecifier": parts.push("* as "); parts.push(path.call(print, "local")); diff --git a/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap b/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap index ad2930a691af..3fb0f223cc0f 100644 --- a/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/babel-plugins/__snapshots__/jsfmt.spec.js.snap @@ -811,6 +811,20 @@ obj.a.b &&= c; ================================================================================ `; +exports[`module-string-names.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "babel-ts", "babel-flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +export { smile as "😄" } from "./emojis.js"; + +=====================================output===================================== +export { smile as "😄" } from "./emojis.js"; + +================================================================================ +`; + exports[`nullish-coalescing-operator.js format 1`] = ` ====================================options===================================== parsers: ["babel", "babel-ts", "babel-flow"] diff --git a/tests/js/babel-plugins/jsfmt.spec.js b/tests/js/babel-plugins/jsfmt.spec.js index b85bc01d31bb..35b39843d803 100644 --- a/tests/js/babel-plugins/jsfmt.spec.js +++ b/tests/js/babel-plugins/jsfmt.spec.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-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/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..75ffa47105f2 --- /dev/null +++ b/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,30 @@ +// 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"; + +=====================================output===================================== +export { smile as "smile" } from "./emojis.js"; +export { "smile" as smile } from "./emojis.js"; +export { "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"; + +================================================================================ +`; diff --git a/tests/js/module-string-names/jsfmt.spec.js b/tests/js/module-string-names/jsfmt.spec.js new file mode 100644 index 000000000000..726eaec01590 --- /dev/null +++ b/tests/js/module-string-names/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["babel"], { errors: { espree: true } }); 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..e4a2a1e79a17 --- /dev/null +++ b/tests/js/module-string-names/module-string-names.js @@ -0,0 +1,7 @@ +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"; From ac9f5583cbd388b4c4863f472e6a9ed15afa0e77 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Sun, 18 Oct 2020 21:31:13 +0900 Subject: [PATCH 15/25] Remove tests for module-attributes --- tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap | 7 ------- tests/misc/errors/js/module-attributes.js | 1 - 2 files changed, 8 deletions(-) delete mode 100644 tests/misc/errors/js/module-attributes.js diff --git a/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap b/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap index 02535f581236..8985c5fa7a9d 100644 --- a/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap +++ b/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap @@ -11,13 +11,6 @@ exports[`html-like-comments.js error test 1`] = ` 6 | " `; -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\\"; - | ^ - 2 | " -`; - 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/module-attributes.js b/tests/misc/errors/js/module-attributes.js deleted file mode 100644 index ecc33f5eb941..000000000000 --- a/tests/misc/errors/js/module-attributes.js +++ /dev/null @@ -1 +0,0 @@ -import foo from "foo.json" with foo: "json"; From 71128fed5a12259df4bd9d9c2179d4a28ef10447 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Mon, 19 Oct 2020 10:22:43 +0900 Subject: [PATCH 16/25] Add changelog --- changelog_unreleased/javascript/pr-9408.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 changelog_unreleased/javascript/pr-9408.md diff --git a/changelog_unreleased/javascript/pr-9408.md b/changelog_unreleased/javascript/pr-9408.md new file mode 100644 index 000000000000..a1cac18b412b --- /dev/null +++ b/changelog_unreleased/javascript/pr-9408.md @@ -0,0 +1,21 @@ +#### Update to `@babel/parser` 7.12 + +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 = "wooo!"; +export { happy as "😃" }; +``` From 766c4cf45f05c604d6333e687cdc630de2776d18 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Mon, 19 Oct 2020 10:26:54 +0900 Subject: [PATCH 17/25] Update to 7.12.3 --- package.json | 2 +- .../babel-ts/__snapshots__/jsfmt.spec.js.snap | 13 ++++++++++--- .../babel-ts/type-annotation-expr-statement.ts | 1 + .../{type-annotation.ts => type-annotation-func.ts} | 0 yarn.lock | 8 ++++---- 5 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 tests/misc/errors/babel-ts/type-annotation-expr-statement.ts rename tests/misc/errors/babel-ts/{type-annotation.ts => type-annotation-func.ts} (100%) diff --git a/package.json b/package.json index b1924a94f4c8..b17d5d25a085 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.12.0", + "@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/tests/misc/errors/babel-ts/__snapshots__/jsfmt.spec.js.snap b/tests/misc/errors/babel-ts/__snapshots__/jsfmt.spec.js.snap index 6d96116982a4..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[`type-annotation.ts error test 1`] = ` -"Did not expect a type annotation here. (1:9) +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/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.ts b/tests/misc/errors/babel-ts/type-annotation-func.ts similarity index 100% rename from tests/misc/errors/babel-ts/type-annotation.ts rename to tests/misc/errors/babel-ts/type-annotation-func.ts diff --git a/yarn.lock b/yarn.lock index 0e406597c4ad..cb866837ff3f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -277,10 +277,10 @@ chalk "^2.0.0" js-tokens "^4.0.0" -"@babel/parser@7.12.0": - version "7.12.0" - resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.12.0.tgz#2ad388f3960045b22f9b7d4bf85e80b15a1c9e3a" - integrity sha512-dYmySMYnlus2jwl7JnnajAj11obRStZoW9cG04wh4ZuhozDn11tDUrhHcUZ9iuNHqALAhh60XqNaYXpvuuE/Gg== +"@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.7.0": version "7.12.1" From c06180e968575531c9df9d826c63338747fda2cc Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Mon, 19 Oct 2020 10:28:19 +0900 Subject: [PATCH 18/25] Fix by linter --- changelog_unreleased/javascript/pr-9408.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/changelog_unreleased/javascript/pr-9408.md b/changelog_unreleased/javascript/pr-9408.md index a1cac18b412b..5e634735f8a7 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 +#### 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. @@ -13,6 +13,7 @@ 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 From b1558d197b0626c5a658cbdac75d87848d0a8a1d Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Mon, 19 Oct 2020 10:32:15 +0900 Subject: [PATCH 19/25] Fix by spellchecker --- changelog_unreleased/javascript/pr-9408.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog_unreleased/javascript/pr-9408.md b/changelog_unreleased/javascript/pr-9408.md index 5e634735f8a7..c1503f46d3e0 100644 --- a/changelog_unreleased/javascript/pr-9408.md +++ b/changelog_unreleased/javascript/pr-9408.md @@ -17,6 +17,6 @@ import json from "./foo.json" assert { type: "json" }; 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 = "wooo!"; +let happy = "happy"; export { happy as "😃" }; ``` From 645200833b90119477ed256cb2108ea374327b88 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Mon, 19 Oct 2020 11:47:49 +0900 Subject: [PATCH 20/25] Add tests for module attributes to errors --- tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap | 7 +++++++ tests/misc/errors/js/module-attributes.js | 1 + 2 files changed, 8 insertions(+) create mode 100644 tests/misc/errors/js/module-attributes.js diff --git a/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap b/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap index 8985c5fa7a9d..7c032898bcb4 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[`module-attributes.js error test 1`] = ` +"Unexpected token, expected \\";\\" (1:28) +> 1 | import foo from \\"foo.json\\" with type: \\"json\\"; + | ^ + 2 | " +`; + 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/module-attributes.js b/tests/misc/errors/js/module-attributes.js new file mode 100644 index 000000000000..fb31508a0860 --- /dev/null +++ b/tests/misc/errors/js/module-attributes.js @@ -0,0 +1 @@ +import foo from "foo.json" with type: "json"; From f1182e7c927d6ba85dc9d25ffe46cb2cfc507167 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Mon, 19 Oct 2020 12:17:41 +0900 Subject: [PATCH 21/25] Add error test for module string name with import --- tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap | 8 ++++++++ tests/misc/errors/js/module-string-name-import.js | 2 ++ 2 files changed, 10 insertions(+) create mode 100644 tests/misc/errors/js/module-string-name-import.js diff --git a/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap b/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap index 7c032898bcb4..beea9da8ccba 100644 --- a/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap +++ b/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap @@ -18,6 +18,14 @@ 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/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"; From 226b1dbe79050d35db09a127b13124cadca8d11a Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Mon, 19 Oct 2020 18:59:55 +0900 Subject: [PATCH 22/25] Remove TSTypeCastExpression --- src/language-js/printer-estree.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 1e5b182ca0be..491ef8375087 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -2898,8 +2898,7 @@ function printPathNoParens(path, options, print, args) { return group(concat(parts)); } - case "TypeCastExpression": - case "TSTypeCastExpression": { + case "TypeCastExpression": { return concat([ "(", path.call(print, "expression"), From bc5251de62d01d8140b3ef0fcb59c874c60834e5 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 20 Oct 2020 03:02:25 +0900 Subject: [PATCH 23/25] Add tests for funny import-assertions --- .../__snapshots__/jsfmt.spec.js.snap | 16 ++++++++++++++++ tests/js/import-assertions/jsfmt.spec.js | 10 +++++++++- .../import-assertions/not-import-assertions.js | 2 ++ .../errors/js/__snapshots__/jsfmt.spec.js.snap | 7 +++++++ .../errors/js/import-assertions-with-parens.js | 1 + 5 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/js/import-assertions/not-import-assertions.js create mode 100644 tests/misc/errors/js/import-assertions-with-parens.js diff --git a/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap b/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap index a2e60e8ccf73..df01ffc7403d 100644 --- a/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/import-assertions/__snapshots__/jsfmt.spec.js.snap @@ -41,3 +41,19 @@ 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/jsfmt.spec.js b/tests/js/import-assertions/jsfmt.spec.js index 726eaec01590..c3095e9d8d7f 100644 --- a/tests/js/import-assertions/jsfmt.spec.js +++ b/tests/js/import-assertions/jsfmt.spec.js @@ -1 +1,9 @@ -run_spec(__dirname, ["babel"], { errors: { espree: true } }); +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/misc/errors/js/__snapshots__/jsfmt.spec.js.snap b/tests/misc/errors/js/__snapshots__/jsfmt.spec.js.snap index beea9da8ccba..5354a8b1c146 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-with-parens.js error test 1`] = ` +"Unexpected token (1:19) +> 1 | import \\"x\\" assert ({type: 'json'}); + | ^ + 2 | " +`; + exports[`module-attributes.js error test 1`] = ` "Unexpected token, expected \\";\\" (1:28) > 1 | import foo from \\"foo.json\\" with type: \\"json\\"; 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'}); From 7042f57400fe01c8cc01dd16d97be463a58ded96 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 20 Oct 2020 17:22:33 +0900 Subject: [PATCH 24/25] Update snapshots| --- tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 75ffa47105f2..ae069d276f1f 100644 --- a/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap @@ -17,7 +17,7 @@ export * as "foo", { default as "quux" } from "module-b"; =====================================output===================================== export { smile as "smile" } from "./emojis.js"; export { "smile" as smile } from "./emojis.js"; -export { "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 { From aaa7326b68aabb4afd1dd72893cacb809644b90e Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 20 Oct 2020 18:17:13 +0900 Subject: [PATCH 25/25] Add more tests --- tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap | 2 ++ tests/js/module-string-names/module-string-names.js | 1 + 2 files changed, 3 insertions(+) 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 ae069d276f1f..b2f9e1c02d49 100644 --- a/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap +++ b/tests/js/module-string-names/__snapshots__/jsfmt.spec.js.snap @@ -13,6 +13,7 @@ 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"; @@ -25,6 +26,7 @@ export { "吾道一以貫之。" as "忠恕。", } from "Confucius"; export * as "foo", { default as "quux" } from "module-b"; +export { "smile" } from "./emojis.js"; ================================================================================ `; diff --git a/tests/js/module-string-names/module-string-names.js b/tests/js/module-string-names/module-string-names.js index e4a2a1e79a17..cab6023a156e 100644 --- a/tests/js/module-string-names/module-string-names.js +++ b/tests/js/module-string-names/module-string-names.js @@ -5,3 +5,4 @@ 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";