Skip to content

Commit

Permalink
Add missing parentheses for decorator (#14393)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 24, 2023
1 parent 2a5b391 commit bc09877
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 0 deletions.
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() {}
},
]

0 comments on commit bc09877

Please sign in to comment.