From 43574636a670f0cfe03dfc60a234e8337a4457f6 Mon Sep 17 00:00:00 2001 From: "onishi.kohei" Date: Wed, 21 Dec 2022 22:11:58 +0900 Subject: [PATCH 01/13] add TSTypeQuery to needsParens --- src/language-js/needs-parens.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 94d95febc44a..a94167a0f0ed 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -456,7 +456,14 @@ function needsParens(path, options) { (parent.type === "TSTypeAnnotation" && path.getParentNode(1).type.startsWith("TSJSDoc")) ); - + case "TSTypeQuery": + if( + parent.type === "TSIndexedAccessType" || + parent.type === "TSArrayType" + ) { + return true; + } + // fallthrough case "ArrayTypeAnnotation": return parent.type === "NullableTypeAnnotation"; From 8bd1d578b97a4669b80cae6b792021430253684a Mon Sep 17 00:00:00 2001 From: "onishi.kohei" Date: Fri, 23 Dec 2022 21:14:49 +0900 Subject: [PATCH 02/13] fix to lint prettier --- src/language-js/needs-parens.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index a94167a0f0ed..5b5ba5069b6e 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -457,7 +457,7 @@ function needsParens(path, options) { path.getParentNode(1).type.startsWith("TSJSDoc")) ); case "TSTypeQuery": - if( + if ( parent.type === "TSIndexedAccessType" || parent.type === "TSArrayType" ) { From 6075690eade18feed1e1a2daa807b70f43657395 Mon Sep 17 00:00:00 2001 From: "onishi.kohei" Date: Sun, 25 Dec 2022 23:54:42 +0900 Subject: [PATCH 03/13] add test --- .../typeof/__snapshots__/jsfmt.spec.js.snap | 20 +++++++++++++++++++ tests/format/typescript/typeof/jsfmt.spec.js | 1 + tests/format/typescript/typeof/typeof.ts | 4 ++++ 3 files changed, 25 insertions(+) create mode 100644 tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/format/typescript/typeof/jsfmt.spec.js create mode 100644 tests/format/typescript/typeof/typeof.ts diff --git a/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..51535961462f --- /dev/null +++ b/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,20 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`typeof.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +// https://github.com/typescript-eslint/typescript-eslint/pull/4382 +a as (typeof node.children)[number] +a as (typeof node.children)[] +a as ((typeof node.children)[number])[] +=====================================output===================================== +// https://github.com/typescript-eslint/typescript-eslint/pull/4382 +a as (typeof node.children)[number]; +a as (typeof node.children)[]; +a as (typeof node.children)[number][]; + +================================================================================ +`; diff --git a/tests/format/typescript/typeof/jsfmt.spec.js b/tests/format/typescript/typeof/jsfmt.spec.js new file mode 100644 index 000000000000..2ea3bb6eb2e4 --- /dev/null +++ b/tests/format/typescript/typeof/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["typescript"]); diff --git a/tests/format/typescript/typeof/typeof.ts b/tests/format/typescript/typeof/typeof.ts new file mode 100644 index 000000000000..153788ab34e0 --- /dev/null +++ b/tests/format/typescript/typeof/typeof.ts @@ -0,0 +1,4 @@ +// https://github.com/typescript-eslint/typescript-eslint/pull/4382 +a as (typeof node.children)[number] +a as (typeof node.children)[] +a as ((typeof node.children)[number])[] \ No newline at end of file From 13d63c26fa16c737607676ba628252f9c5c31edc Mon Sep 17 00:00:00 2001 From: "onishi.kohei" Date: Mon, 26 Dec 2022 08:24:53 +0900 Subject: [PATCH 04/13] remove comment --- tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap | 2 -- tests/format/typescript/typeof/typeof.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap index 51535961462f..80a5b40790ed 100644 --- a/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap @@ -6,12 +6,10 @@ parsers: ["typescript"] printWidth: 80 | printWidth =====================================input====================================== -// https://github.com/typescript-eslint/typescript-eslint/pull/4382 a as (typeof node.children)[number] a as (typeof node.children)[] a as ((typeof node.children)[number])[] =====================================output===================================== -// https://github.com/typescript-eslint/typescript-eslint/pull/4382 a as (typeof node.children)[number]; a as (typeof node.children)[]; a as (typeof node.children)[number][]; diff --git a/tests/format/typescript/typeof/typeof.ts b/tests/format/typescript/typeof/typeof.ts index 153788ab34e0..dd7caf2521df 100644 --- a/tests/format/typescript/typeof/typeof.ts +++ b/tests/format/typescript/typeof/typeof.ts @@ -1,4 +1,3 @@ -// https://github.com/typescript-eslint/typescript-eslint/pull/4382 a as (typeof node.children)[number] a as (typeof node.children)[] a as ((typeof node.children)[number])[] \ No newline at end of file From 20e5a29a87985e269d2d7079c47fb50024f4eadd Mon Sep 17 00:00:00 2001 From: "onishi.kohei" Date: Mon, 26 Dec 2022 08:55:27 +0900 Subject: [PATCH 05/13] add changelog --- changelog_unreleased/typescript/14042.md | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 changelog_unreleased/typescript/14042.md diff --git a/changelog_unreleased/typescript/14042.md b/changelog_unreleased/typescript/14042.md new file mode 100644 index 000000000000..7e2232d2035f --- /dev/null +++ b/changelog_unreleased/typescript/14042.md @@ -0,0 +1,38 @@ + + +#### Fix to add parentheses for TSTypeQuery in some case (#13546, #14042 by @onishi-kohei) + + +```tsx +// Input +a as (typeof node.children)[number] +a as (typeof node.children)[] +a as ((typeof node.children)[number])[] + +// Prettier stable +a as (typeof node.children)[number]; +a as (typeof node.children)[]; +a as (typeof node.children)[number][]; +``` \ No newline at end of file From 2fa910b3c9c8136bb8b44a1b36a82892688e39b9 Mon Sep 17 00:00:00 2001 From: "onishi.kohei" Date: Wed, 28 Dec 2022 20:26:21 +0900 Subject: [PATCH 06/13] fix lint --- changelog_unreleased/typescript/14042.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog_unreleased/typescript/14042.md b/changelog_unreleased/typescript/14042.md index 7e2232d2035f..4afb2ae4b965 100644 --- a/changelog_unreleased/typescript/14042.md +++ b/changelog_unreleased/typescript/14042.md @@ -35,4 +35,4 @@ a as ((typeof node.children)[number])[] a as (typeof node.children)[number]; a as (typeof node.children)[]; a as (typeof node.children)[number][]; -``` \ No newline at end of file +``` From 338124ef0f1d30ddea3f50897438456ab378e6cd Mon Sep 17 00:00:00 2001 From: "onishi.kohei" Date: Wed, 28 Dec 2022 20:46:22 +0900 Subject: [PATCH 07/13] remove template comments at top in changelog --- changelog_unreleased/typescript/14042.md | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/changelog_unreleased/typescript/14042.md b/changelog_unreleased/typescript/14042.md index 4afb2ae4b965..616ff9d28030 100644 --- a/changelog_unreleased/typescript/14042.md +++ b/changelog_unreleased/typescript/14042.md @@ -1,27 +1,3 @@ - - #### Fix to add parentheses for TSTypeQuery in some case (#13546, #14042 by @onishi-kohei) From 15096f1f06a92293b828853b197479675faa104f Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 10:17:35 +0800 Subject: [PATCH 08/13] Fix changelog --- changelog_unreleased/typescript/14042.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/changelog_unreleased/typescript/14042.md b/changelog_unreleased/typescript/14042.md index 616ff9d28030..c973ce830f8f 100644 --- a/changelog_unreleased/typescript/14042.md +++ b/changelog_unreleased/typescript/14042.md @@ -1,4 +1,4 @@ -#### Fix to add parentheses for TSTypeQuery in some case (#13546, #14042 by @onishi-kohei) +#### Add parentheses for TSTypeQuery in some case (#14042 by @onishi-kohei) ```tsx @@ -8,6 +8,11 @@ a as (typeof node.children)[] a as ((typeof node.children)[number])[] // Prettier stable +a as typeof node.children[number]; +a as typeof node.children[]; +a as typeof node.children[number][]; + +// Prettier main a as (typeof node.children)[number]; a as (typeof node.children)[]; a as (typeof node.children)[number][]; From 57d3a186fc8bb6c31f8e44bc8f72ac52c069b84a Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 10:25:48 +0800 Subject: [PATCH 09/13] Add new line --- changelog_unreleased/typescript/14042.md | 2 +- tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap | 1 + tests/format/typescript/typeof/typeof.ts | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/changelog_unreleased/typescript/14042.md b/changelog_unreleased/typescript/14042.md index c973ce830f8f..9b37741bb68f 100644 --- a/changelog_unreleased/typescript/14042.md +++ b/changelog_unreleased/typescript/14042.md @@ -1,4 +1,4 @@ -#### Add parentheses for TSTypeQuery in some case (#14042 by @onishi-kohei) +#### Add parentheses for TSTypeQuery to improve readability (#14042 by @onishi-kohei) ```tsx diff --git a/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap index 80a5b40790ed..7a9c5292bbd3 100644 --- a/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap @@ -9,6 +9,7 @@ printWidth: 80 a as (typeof node.children)[number] a as (typeof node.children)[] a as ((typeof node.children)[number])[] + =====================================output===================================== a as (typeof node.children)[number]; a as (typeof node.children)[]; diff --git a/tests/format/typescript/typeof/typeof.ts b/tests/format/typescript/typeof/typeof.ts index dd7caf2521df..12a15124de8b 100644 --- a/tests/format/typescript/typeof/typeof.ts +++ b/tests/format/typescript/typeof/typeof.ts @@ -1,3 +1,3 @@ a as (typeof node.children)[number] a as (typeof node.children)[] -a as ((typeof node.children)[number])[] \ No newline at end of file +a as ((typeof node.children)[number])[] From 59a7a561c5c73f80f37c33faece3add51107420b Mon Sep 17 00:00:00 2001 From: "onishi.kohei" Date: Thu, 29 Dec 2022 12:55:21 +0900 Subject: [PATCH 10/13] check objectType --- src/language-js/needs-parens.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 5b5ba5069b6e..12758215f9e5 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -458,8 +458,8 @@ function needsParens(path, options) { ); case "TSTypeQuery": if ( - parent.type === "TSIndexedAccessType" || - parent.type === "TSArrayType" + name === "objectType" && + (parent.type === "TSIndexedAccessType" || parent.type === "TSArrayType") ) { return true; } From f3f7503d1cdf72299bf7b6ae1eb3850b54eaaa06 Mon Sep 17 00:00:00 2001 From: "onishi.kohei" Date: Thu, 29 Dec 2022 13:01:35 +0900 Subject: [PATCH 11/13] remove fallthrough comment --- src/language-js/needs-parens.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 12758215f9e5..89660759897b 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -457,13 +457,10 @@ function needsParens(path, options) { path.getParentNode(1).type.startsWith("TSJSDoc")) ); case "TSTypeQuery": - if ( + return ( name === "objectType" && (parent.type === "TSIndexedAccessType" || parent.type === "TSArrayType") - ) { - return true; - } - // fallthrough + ); case "ArrayTypeAnnotation": return parent.type === "NullableTypeAnnotation"; From 70353171ed013c8e40b0a425d7d8b927ea690bbc Mon Sep 17 00:00:00 2001 From: "onishi.kohei" Date: Thu, 29 Dec 2022 15:54:49 +0900 Subject: [PATCH 12/13] check property name --- changelog_unreleased/typescript/14042.md | 3 +++ src/language-js/needs-parens.js | 4 ++-- .../format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap | 2 ++ tests/format/typescript/typeof/typeof.ts | 1 + 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/changelog_unreleased/typescript/14042.md b/changelog_unreleased/typescript/14042.md index 9b37741bb68f..a3c99acdd1be 100644 --- a/changelog_unreleased/typescript/14042.md +++ b/changelog_unreleased/typescript/14042.md @@ -6,14 +6,17 @@ a as (typeof node.children)[number] a as (typeof node.children)[] a as ((typeof node.children)[number])[] +a as number[(typeof node.children)] // Prettier stable a as typeof node.children[number]; a as typeof node.children[]; a as typeof node.children[number][]; +a as number[typeof node.children]; // Prettier main a as (typeof node.children)[number]; a as (typeof node.children)[]; a as (typeof node.children)[number][]; +a as number[typeof node.children]; ``` diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 89660759897b..aa48189f4f6b 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -458,8 +458,8 @@ function needsParens(path, options) { ); case "TSTypeQuery": return ( - name === "objectType" && - (parent.type === "TSIndexedAccessType" || parent.type === "TSArrayType") + (name === "objectType" && parent.type === "TSIndexedAccessType") || + (name === "elementType" && parent.type === "TSArrayType") ); case "ArrayTypeAnnotation": return parent.type === "NullableTypeAnnotation"; diff --git a/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap index 7a9c5292bbd3..5f6e5fb92c96 100644 --- a/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap @@ -9,11 +9,13 @@ printWidth: 80 a as (typeof node.children)[number] a as (typeof node.children)[] a as ((typeof node.children)[number])[] +a as number[(typeof node.children)] =====================================output===================================== a as (typeof node.children)[number]; a as (typeof node.children)[]; a as (typeof node.children)[number][]; +a as number[typeof node.children]; ================================================================================ `; diff --git a/tests/format/typescript/typeof/typeof.ts b/tests/format/typescript/typeof/typeof.ts index 12a15124de8b..accada3df35a 100644 --- a/tests/format/typescript/typeof/typeof.ts +++ b/tests/format/typescript/typeof/typeof.ts @@ -1,3 +1,4 @@ a as (typeof node.children)[number] a as (typeof node.children)[] a as ((typeof node.children)[number])[] +a as number[(typeof node.children)] From 7ea60a694351ff7b772489b48e243fc252843d08 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 29 Dec 2022 15:11:28 +0800 Subject: [PATCH 13/13] Simplify changelog --- changelog_unreleased/typescript/14042.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/changelog_unreleased/typescript/14042.md b/changelog_unreleased/typescript/14042.md index a3c99acdd1be..9b37741bb68f 100644 --- a/changelog_unreleased/typescript/14042.md +++ b/changelog_unreleased/typescript/14042.md @@ -6,17 +6,14 @@ a as (typeof node.children)[number] a as (typeof node.children)[] a as ((typeof node.children)[number])[] -a as number[(typeof node.children)] // Prettier stable a as typeof node.children[number]; a as typeof node.children[]; a as typeof node.children[number][]; -a as number[typeof node.children]; // Prettier main a as (typeof node.children)[number]; a as (typeof node.children)[]; a as (typeof node.children)[number][]; -a as number[typeof node.children]; ```