Skip to content

Commit

Permalink
Remove unnecessary parentheses for decorators (#11849)
Browse files Browse the repository at this point in the history
* Remove unnecesary parans for typescript parser

* Add tests

* Update changelog
  • Loading branch information
sosukesuzuki committed Nov 21, 2021
1 parent c9b3518 commit 7acdac3
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
13 changes: 11 additions & 2 deletions 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)

<!-- prettier-ignore -->
```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";
}
```
2 changes: 1 addition & 1 deletion src/language-js/needs-parens.js
Expand Up @@ -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;
Expand Down
Expand Up @@ -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"]
Expand Down
@@ -0,0 +1,2 @@
@(test().x("global").y())
class X {}
16 changes: 16 additions & 0 deletions tests/format/misc/typescript-only/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -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"]
Expand Down
@@ -0,0 +1,2 @@
@(test().x("global").y())
class X {}

0 comments on commit 7acdac3

Please sign in to comment.