Skip to content

Commit 36bebe9

Browse files
authored
Parenthesize computed names if not an assignment expression (microsoft#22280)
1 parent 10e3b73 commit 36bebe9

3 files changed

Lines changed: 21 additions & 14 deletions

File tree

src/compiler/factory.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,16 @@ namespace ts {
230230
: node;
231231
}
232232

233+
function parenthesizeForComputedName(expression: Expression): Expression {
234+
return (isBinaryExpression(expression) && expression.operatorToken.kind === SyntaxKind.CommaToken) ||
235+
expression.kind === SyntaxKind.CommaListExpression ?
236+
createParen(expression) :
237+
expression;
238+
}
239+
233240
export function createComputedPropertyName(expression: Expression) {
234241
const node = <ComputedPropertyName>createSynthesizedNode(SyntaxKind.ComputedPropertyName);
235-
node.expression = expression;
242+
node.expression = parenthesizeForComputedName(expression);
236243
return node;
237244
}
238245

tests/baselines/reference/decoratorsOnComputedProperties.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ class C {
262262
this[_k] = null;
263263
this[_l] = null;
264264
}
265-
[foo(), _m = foo(), _k = foo(), _o = fieldNameB, _l = fieldNameC, "some" + "method"]() { }
265+
[(foo(), _m = foo(), _k = foo(), _o = fieldNameB, _l = fieldNameC, "some" + "method")]() { }
266266
}
267267
__decorate([
268268
x
@@ -297,7 +297,7 @@ void class D {
297297
this[_p] = null;
298298
this[_q] = null;
299299
}
300-
[foo(), _r = foo(), _p = foo(), _s = fieldNameB, _q = fieldNameC, "some" + "method"]() { }
300+
[(foo(), _r = foo(), _p = foo(), _s = fieldNameB, _q = fieldNameC, "some" + "method")]() { }
301301
};
302302
class E {
303303
constructor() {
@@ -308,7 +308,7 @@ class E {
308308
this[_t] = null;
309309
this[_u] = null;
310310
}
311-
[foo(), _v = foo(), _t = foo(), "some" + "method"]() { }
311+
[(foo(), _v = foo(), _t = foo(), "some" + "method")]() { }
312312
}
313313
_w = fieldNameB, _u = fieldNameC;
314314
__decorate([
@@ -344,7 +344,7 @@ void (_x = class F {
344344
this[_y] = null;
345345
this[_z] = null;
346346
}
347-
[foo(), _0 = foo(), _y = foo(), "some" + "method"]() { }
347+
[(foo(), _0 = foo(), _y = foo(), "some" + "method")]() { }
348348
},
349349
_1 = fieldNameB,
350350
_z = fieldNameC,
@@ -358,8 +358,8 @@ class G {
358358
this[_2] = null;
359359
this[_3] = null;
360360
}
361-
[foo(), _4 = foo(), _2 = foo(), "some" + "method"]() { }
362-
[_5 = fieldNameB, "some" + "method2"]() { }
361+
[(foo(), _4 = foo(), _2 = foo(), "some" + "method")]() { }
362+
[(_5 = fieldNameB, "some" + "method2")]() { }
363363
}
364364
_3 = fieldNameC;
365365
__decorate([
@@ -395,8 +395,8 @@ void (_6 = class H {
395395
this[_7] = null;
396396
this[_8] = null;
397397
}
398-
[foo(), _9 = foo(), _7 = foo(), "some" + "method"]() { }
399-
[_10 = fieldNameB, "some" + "method2"]() { }
398+
[(foo(), _9 = foo(), _7 = foo(), "some" + "method")]() { }
399+
[(_10 = fieldNameB, "some" + "method2")]() { }
400400
},
401401
_8 = fieldNameC,
402402
_6);
@@ -409,8 +409,8 @@ class I {
409409
this[_11] = null;
410410
this[_12] = null;
411411
}
412-
[foo(), _13 = foo(), _11 = foo(), _14 = "some" + "method"]() { }
413-
[_15 = fieldNameB, "some" + "method2"]() { }
412+
[(foo(), _13 = foo(), _11 = foo(), _14 = "some" + "method")]() { }
413+
[(_15 = fieldNameB, "some" + "method2")]() { }
414414
}
415415
_12 = fieldNameC;
416416
__decorate([
@@ -449,8 +449,8 @@ void (_16 = class J {
449449
this[_17] = null;
450450
this[_18] = null;
451451
}
452-
[foo(), _19 = foo(), _17 = foo(), _20 = "some" + "method"]() { }
453-
[_21 = fieldNameB, "some" + "method2"]() { }
452+
[(foo(), _19 = foo(), _17 = foo(), _20 = "some" + "method")]() { }
453+
[(_21 = fieldNameB, "some" + "method2")]() { }
454454
},
455455
_18 = fieldNameC,
456456
_16);

tests/baselines/reference/symbolProperty7.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class C {
1313
constructor() {
1414
this[_a] = 0;
1515
}
16-
[_a = Symbol(), Symbol(), Symbol()]() { }
16+
[(_a = Symbol(), Symbol(), Symbol())]() { }
1717
get [Symbol()]() {
1818
return 0;
1919
}

0 commit comments

Comments
 (0)