diff --git a/changelog_unreleased/typescript/14042.md b/changelog_unreleased/typescript/14042.md new file mode 100644 index 000000000000..9b37741bb68f --- /dev/null +++ b/changelog_unreleased/typescript/14042.md @@ -0,0 +1,19 @@ +#### Add parentheses for TSTypeQuery to improve readability (#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][]; + +// Prettier main +a as (typeof node.children)[number]; +a as (typeof node.children)[]; +a as (typeof node.children)[number][]; +``` diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 94d95febc44a..aa48189f4f6b 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -456,7 +456,11 @@ function needsParens(path, options) { (parent.type === "TSTypeAnnotation" && path.getParentNode(1).type.startsWith("TSJSDoc")) ); - + case "TSTypeQuery": + return ( + (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 new file mode 100644 index 000000000000..5f6e5fb92c96 --- /dev/null +++ b/tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,21 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`typeof.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +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/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..accada3df35a --- /dev/null +++ b/tests/format/typescript/typeof/typeof.ts @@ -0,0 +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)]