diff --git a/changelog_unreleased/typescript/11717.md b/changelog_unreleased/typescript/11717.md index 0a35341c11a4..e45402b58fe0 100644 --- a/changelog_unreleased/typescript/11717.md +++ b/changelog_unreleased/typescript/11717.md @@ -1,22 +1,31 @@ -#### Fix formatting for tagged template decorators (#11717 by @sosukesuzuki) +#### Remove unnecessary parentheses for decorators (#11717, #11849 by @sosukesuzuki) ```tsx // Input class Test { @foo`bar` - test: string = "test" + test1: string = "test" + + @test().x("global").y() + test2: string = "test"; } // Prettier stable class Test { @(foo`bar`) test: string = "test" + + @(test().x("global").y()) + test2: string = "test"; } // Prettier main class Test { @foo`bar` test: string = "test" + + @test().x("global").y() + test2: string = "test"; } ``` diff --git a/src/language-js/needs-parens.js b/src/language-js/needs-parens.js index 220f40f243de..c4ca50ca9821 100644 --- a/src/language-js/needs-parens.js +++ b/src/language-js/needs-parens.js @@ -133,7 +133,7 @@ function needsParens(path, options) { /** @(x().y) */ hasMemberExpression || /** @(x().y()) */ hasCallExpression ) { - return true; + return options.parser !== "typescript"; } hasCallExpression = true; current = current.callee; diff --git a/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap b/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap index 4f08e7695cbd..9a2dd84d4b00 100644 --- a/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/misc/typescript-babel-only/__snapshots__/jsfmt.spec.js.snap @@ -60,6 +60,22 @@ class Bar { ================================================================================ `; +exports[`parenthesized-decorators-call-expression.ts format 1`] = ` +====================================options===================================== +parsers: ["babel-ts"] +printWidth: 80 + | printWidth +=====================================input====================================== +@(test().x("global").y()) +class X {} + +=====================================output===================================== +@(test().x("global").y()) +class X {} + +================================================================================ +`; + exports[`parenthesized-decorators-tagged-template.ts format 1`] = ` ====================================options===================================== parsers: ["babel-ts"] diff --git a/tests/format/misc/typescript-babel-only/parenthesized-decorators-call-expression.ts b/tests/format/misc/typescript-babel-only/parenthesized-decorators-call-expression.ts new file mode 100644 index 000000000000..f5205dc73ec1 --- /dev/null +++ b/tests/format/misc/typescript-babel-only/parenthesized-decorators-call-expression.ts @@ -0,0 +1,2 @@ +@(test().x("global").y()) +class X {} diff --git a/tests/format/misc/typescript-only/__snapshots__/jsfmt.spec.js.snap b/tests/format/misc/typescript-only/__snapshots__/jsfmt.spec.js.snap index 244e6d83168d..17d4458d1963 100644 --- a/tests/format/misc/typescript-only/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/misc/typescript-only/__snapshots__/jsfmt.spec.js.snap @@ -93,6 +93,22 @@ class Bar { ================================================================================ `; +exports[`parenthesized-decorators-call-expression.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +@(test().x("global").y()) +class X {} + +=====================================output===================================== +@test().x("global").y() +class X {} + +================================================================================ +`; + exports[`parenthesized-decorators-tagged-template.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/misc/typescript-only/parenthesized-decorators-call-expression.ts b/tests/format/misc/typescript-only/parenthesized-decorators-call-expression.ts new file mode 100644 index 000000000000..f5205dc73ec1 --- /dev/null +++ b/tests/format/misc/typescript-only/parenthesized-decorators-call-expression.ts @@ -0,0 +1,2 @@ +@(test().x("global").y()) +class X {}