Skip to content

Commit bc09877

Browse files
authored
Add missing parentheses for decorator (#14393)
1 parent 2a5b391 commit bc09877

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#### Add missing parentheses for decorator (#14393 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```jsx
5+
// Input
6+
class Person {
7+
@(myDecoratorArray[0])
8+
greet() {}
9+
}
10+
11+
// Prettier stable
12+
class Person {
13+
@myDecoratorArray[0]
14+
greet() {}
15+
}
16+
17+
// Prettier main
18+
class Person {
19+
@(myDecoratorArray[0])
20+
greet() {}
21+
}
22+
```

src/language-js/needs-parens.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ function needsParens(path, options) {
187187
}
188188
case "Decorator": {
189189
if (name === "expression") {
190+
if (isMemberExpression(node) && node.computed) {
191+
return true;
192+
}
193+
190194
let hasCallExpression = false;
191195
let hasMemberExpression = false;
192196
let current = node;

tests/config/format-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ const meriyahDisabledTests = new Set([
8282
__dirname,
8383
"../format/js/babel-plugins/decorator-auto-accessors.js"
8484
),
85+
// Parsing to different ASTs
86+
path.join(__dirname, "../format/js/decorators/member-expression.js"),
8587
]);
8688

8789
const isUnstable = (filename, options) => {

tests/format/js/decorators/__snapshots__/jsfmt.spec.js.snap

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,144 @@ export class Bar {}
162162
================================================================================
163163
`;
164164
165+
exports[`member-expression.js [acorn] format 1`] = `
166+
"Unexpected character '@' (3:5)
167+
1 | [
168+
2 | class {
169+
> 3 | @(decorators[0])
170+
| ^
171+
4 | method() {}
172+
5 | },
173+
6 | class {"
174+
`;
175+
176+
exports[`member-expression.js [espree] format 1`] = `
177+
"Unexpected character '@' (3:5)
178+
1 | [
179+
2 | class {
180+
> 3 | @(decorators[0])
181+
| ^
182+
4 | method() {}
183+
5 | },
184+
6 | class {"
185+
`;
186+
187+
exports[`member-expression.js format 1`] = `
188+
====================================options=====================================
189+
parsers: ["babel"]
190+
printWidth: 80
191+
| printWidth
192+
=====================================input======================================
193+
[
194+
class {
195+
@(decorators[0])
196+
method() {}
197+
},
198+
class {
199+
@decorators[0]
200+
method() {}
201+
},
202+
class {
203+
@(decorators?.[0])
204+
method() {}
205+
},
206+
class {
207+
@(decorators.at(0))
208+
method() {}
209+
},
210+
class {
211+
@(decorators?.at(0))
212+
method() {}
213+
},
214+
class {
215+
@(decorators.first)
216+
method() {}
217+
},
218+
class {
219+
@(decorators?.first)
220+
method() {}
221+
},
222+
class {
223+
@(decorators[first])
224+
method() {}
225+
},
226+
class {
227+
@decorators[first]
228+
method() {}
229+
},
230+
class {
231+
@(decorators["first"])
232+
method() {}
233+
},
234+
@(decorators[first])
235+
class {
236+
method() {}
237+
},
238+
@(decorators[0])
239+
class {
240+
method() {}
241+
},
242+
]
243+
244+
=====================================output=====================================
245+
[
246+
class {
247+
@(decorators[0])
248+
method() {}
249+
},
250+
class {
251+
@decorators [0];
252+
method() {}
253+
},
254+
class {
255+
@(decorators?.[0])
256+
method() {}
257+
},
258+
class {
259+
@decorators.at(0)
260+
method() {}
261+
},
262+
class {
263+
@(decorators?.at(0))
264+
method() {}
265+
},
266+
class {
267+
@decorators.first
268+
method() {}
269+
},
270+
class {
271+
@(decorators?.first)
272+
method() {}
273+
},
274+
class {
275+
@(decorators[first])
276+
method() {}
277+
},
278+
class {
279+
@decorators [first];
280+
method() {}
281+
},
282+
class {
283+
@(decorators["first"])
284+
method() {}
285+
},
286+
(
287+
@(decorators[first])
288+
class {
289+
method() {}
290+
}
291+
),
292+
(
293+
@(decorators[0])
294+
class {
295+
method() {}
296+
}
297+
),
298+
];
299+
300+
================================================================================
301+
`;
302+
165303
exports[`methods.js [acorn] format 1`] = `
166304
"Unexpected character '@' (3:3)
167305
1 |
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[
2+
class {
3+
@(decorators[0])
4+
method() {}
5+
},
6+
class {
7+
@decorators[0]
8+
method() {}
9+
},
10+
class {
11+
@(decorators?.[0])
12+
method() {}
13+
},
14+
class {
15+
@(decorators.at(0))
16+
method() {}
17+
},
18+
class {
19+
@(decorators?.at(0))
20+
method() {}
21+
},
22+
class {
23+
@(decorators.first)
24+
method() {}
25+
},
26+
class {
27+
@(decorators?.first)
28+
method() {}
29+
},
30+
class {
31+
@(decorators[first])
32+
method() {}
33+
},
34+
class {
35+
@decorators[first]
36+
method() {}
37+
},
38+
class {
39+
@(decorators["first"])
40+
method() {}
41+
},
42+
@(decorators[first])
43+
class {
44+
method() {}
45+
},
46+
@(decorators[0])
47+
class {
48+
method() {}
49+
},
50+
]

0 commit comments

Comments
 (0)