Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(javascript): add necessary parentheses for decorators #5785

Merged
merged 5 commits into from Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions CHANGELOG.unreleased.md
Expand Up @@ -76,5 +76,33 @@ Examples:
);
```

- JavaScript: Add necessary parentheses for decorators ([#5785] by [@ikatyang])

Parentheses for decorators with nested call expressions are optional for legacy decorators
but they're required for decorators in the current [proposal](https://tc39.github.io/proposal-decorators/#sec-syntax).

<!-- prettier-ignore -->
```js
// Input
class X {
@(computed().volatile())
prop
}

// Output (Prettier stable)
class X {
@computed().volatile()
prop
}

// Output (Prettier master)
class X {
@(computed().volatile())
prop
}
```

[@ikatyang]: https://github.com/ikatyang
[@simenb]: https://github.com/SimenB
[#5778]: https://github.com/prettier/prettier/pull/5778
[#5785]: https://github.com/prettier/prettier/pull/5785
34 changes: 29 additions & 5 deletions src/language-js/needs-parens.js
Expand Up @@ -127,6 +127,35 @@ function needsParens(path, options) {
return true;
}

if (parent.type === "Decorator" && parent.expression === node) {
let hasCallExpression = false;
let hasMemberExpression = false;
let current = node;
while (current) {
switch (current.type) {
case "MemberExpression":
hasMemberExpression = true;
current = current.object;
break;
case "CallExpression":
if (
/** @(x().y) */ hasMemberExpression ||
/** @(x().y()) */ hasCallExpression
) {
return true;
}
hasCallExpression = true;
current = current.callee;
break;
case "Identifier":
return false;
default:
return true;
}
}
return true;
}

if (
(parent.type === "ArrowFunctionExpression" &&
parent.body === node &&
Expand Down Expand Up @@ -276,11 +305,6 @@ function needsParens(path, options) {
parent.left === node &&
(node.type === "TSTypeAssertion" || node.type === "TSAsExpression")
);
case "Decorator":
return (
parent.expression === node &&
(node.type === "TSTypeAssertion" || node.type === "TSAsExpression")
);

case "BinaryExpression":
case "LogicalExpression": {
Expand Down
20 changes: 20 additions & 0 deletions tests/decorators/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -311,6 +311,26 @@ const foo = {
================================================================================
`;

exports[`parens.js 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
class X {
@(computed().volatile())
x
}

=====================================output=====================================
class X {
@(computed().volatile())
x;
}

================================================================================
`;

exports[`redux.js 1`] = `
====================================options=====================================
parsers: ["babel"]
Expand Down
4 changes: 4 additions & 0 deletions tests/decorators/parens.js
@@ -0,0 +1,4 @@
class X {
@(computed().volatile())
x
}