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

Add missing parentheses for decorator #14393

Merged
merged 2 commits into from Feb 24, 2023
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
22 changes: 22 additions & 0 deletions changelog_unreleased/javascript/14393.md
@@ -0,0 +1,22 @@
#### Add missing parentheses for decorator (#14393 by @fisker)

<!-- prettier-ignore -->
```jsx
// Input
class Person {
@(myDecoratorArray[0])
greet() {}
}

// Prettier stable
class Person {
@myDecoratorArray[0]
greet() {}
}

// Prettier main
class Person {
@(myDecoratorArray[0])
greet() {}
}
```
4 changes: 4 additions & 0 deletions src/language-js/needs-parens.js
Expand Up @@ -187,6 +187,10 @@ function needsParens(path, options) {
}
case "Decorator": {
if (name === "expression") {
if (isMemberExpression(node) && node.computed) {
return true;
}

let hasCallExpression = false;
let hasMemberExpression = false;
let current = node;
Expand Down
2 changes: 2 additions & 0 deletions tests/config/format-test.js
Expand Up @@ -82,6 +82,8 @@ const meriyahDisabledTests = new Set([
__dirname,
"../format/js/babel-plugins/decorator-auto-accessors.js"
),
// Parsing to different ASTs
path.join(__dirname, "../format/js/decorators/member-expression.js"),
]);

const isUnstable = (filename, options) => {
Expand Down
138 changes: 138 additions & 0 deletions tests/format/js/decorators/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -162,6 +162,144 @@ export class Bar {}
================================================================================
`;

exports[`member-expression.js [acorn] format 1`] = `
"Unexpected character '@' (3:5)
1 | [
2 | class {
> 3 | @(decorators[0])
| ^
4 | method() {}
5 | },
6 | class {"
`;

exports[`member-expression.js [espree] format 1`] = `
"Unexpected character '@' (3:5)
1 | [
2 | class {
> 3 | @(decorators[0])
| ^
4 | method() {}
5 | },
6 | class {"
`;

exports[`member-expression.js format 1`] = `
====================================options=====================================
parsers: ["babel"]
printWidth: 80
| printWidth
=====================================input======================================
[
class {
@(decorators[0])
method() {}
},
class {
@decorators[0]
method() {}
},
class {
@(decorators?.[0])
method() {}
},
class {
@(decorators.at(0))
method() {}
},
class {
@(decorators?.at(0))
method() {}
},
class {
@(decorators.first)
method() {}
},
class {
@(decorators?.first)
method() {}
},
class {
@(decorators[first])
method() {}
},
class {
@decorators[first]
method() {}
},
class {
@(decorators["first"])
method() {}
},
@(decorators[first])
class {
method() {}
},
@(decorators[0])
class {
method() {}
},
]

=====================================output=====================================
[
class {
@(decorators[0])
method() {}
},
class {
@decorators [0];
method() {}
},
class {
@(decorators?.[0])
method() {}
},
class {
@decorators.at(0)
method() {}
},
class {
@(decorators?.at(0))
method() {}
},
class {
@decorators.first
method() {}
},
class {
@(decorators?.first)
method() {}
},
class {
@(decorators[first])
method() {}
},
class {
@decorators [first];
method() {}
},
class {
@(decorators["first"])
method() {}
},
(
@(decorators[first])
class {
method() {}
}
),
(
@(decorators[0])
class {
method() {}
}
),
];

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

exports[`methods.js [acorn] format 1`] = `
"Unexpected character '@' (3:3)
1 |
Expand Down
50 changes: 50 additions & 0 deletions tests/format/js/decorators/member-expression.js
@@ -0,0 +1,50 @@
[
class {
@(decorators[0])
method() {}
},
class {
@decorators[0]
method() {}
},
class {
@(decorators?.[0])
method() {}
},
class {
@(decorators.at(0))
method() {}
},
class {
@(decorators?.at(0))
method() {}
},
class {
@(decorators.first)
method() {}
},
class {
@(decorators?.first)
method() {}
},
class {
@(decorators[first])
method() {}
},
class {
@decorators[first]
method() {}
},
class {
@(decorators["first"])
method() {}
},
@(decorators[first])
class {
method() {}
},
@(decorators[0])
class {
method() {}
},
]