Skip to content

Commit c0072aa

Browse files
authoredAug 1, 2022
fix(49935): omit parentheses in the operand of the unary expression (microsoft#50111)
1 parent 55f2c0c commit c0072aa

File tree

5 files changed

+449
-50
lines changed

5 files changed

+449
-50
lines changed
 

‎src/compiler/transformers/classFields.ts

+15-14
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,11 @@ namespace ts {
576576

577577
function visitPreOrPostfixUnaryExpression(node: PrefixUnaryExpression | PostfixUnaryExpression, valueIsDiscarded: boolean) {
578578
if (node.operator === SyntaxKind.PlusPlusToken || node.operator === SyntaxKind.MinusMinusToken) {
579-
if (shouldTransformPrivateElementsOrClassStaticBlocks && isPrivateIdentifierPropertyAccessExpression(node.operand)) {
579+
const operand = skipParentheses(node.operand);
580+
if (shouldTransformPrivateElementsOrClassStaticBlocks && isPrivateIdentifierPropertyAccessExpression(operand)) {
580581
let info: PrivateIdentifierInfo | undefined;
581-
if (info = accessPrivateIdentifier(node.operand.name)) {
582-
const receiver = visitNode(node.operand.expression, visitor, isExpression);
582+
if (info = accessPrivateIdentifier(operand.name)) {
583+
const receiver = visitNode(operand.expression, visitor, isExpression);
583584
const { readExpression, initializeExpression } = createCopiableReceiverExpr(receiver);
584585

585586
let expression: Expression = createPrivateIdentifierAccess(info, readExpression);
@@ -601,7 +602,7 @@ namespace ts {
601602
}
602603
}
603604
else if (shouldTransformSuperInStaticInitializers &&
604-
isSuperProperty(node.operand) &&
605+
isSuperProperty(operand) &&
605606
currentStaticPropertyDeclarationOrStaticBlock &&
606607
currentClassLexicalEnvironment) {
607608
// converts `++super.a` into `(Reflect.set(_baseTemp, "a", (_a = Reflect.get(_baseTemp, "a", _classTemp), _b = ++_a), _classTemp), _b)`
@@ -614,31 +615,31 @@ namespace ts {
614615
// converts `super[f()]--` into `(Reflect.set(_baseTemp, _a = f(), (_b = Reflect.get(_baseTemp, _a, _classTemp), _c = _b--), _classTemp), _c)`
615616
const { classConstructor, superClassReference, facts } = currentClassLexicalEnvironment;
616617
if (facts & ClassFacts.ClassWasDecorated) {
617-
const operand = visitInvalidSuperProperty(node.operand);
618+
const expression = visitInvalidSuperProperty(operand);
618619
return isPrefixUnaryExpression(node) ?
619-
factory.updatePrefixUnaryExpression(node, operand) :
620-
factory.updatePostfixUnaryExpression(node, operand);
620+
factory.updatePrefixUnaryExpression(node, expression) :
621+
factory.updatePostfixUnaryExpression(node, expression);
621622
}
622623
if (classConstructor && superClassReference) {
623624
let setterName: Expression | undefined;
624625
let getterName: Expression | undefined;
625-
if (isPropertyAccessExpression(node.operand)) {
626-
if (isIdentifier(node.operand.name)) {
627-
getterName = setterName = factory.createStringLiteralFromNode(node.operand.name);
626+
if (isPropertyAccessExpression(operand)) {
627+
if (isIdentifier(operand.name)) {
628+
getterName = setterName = factory.createStringLiteralFromNode(operand.name);
628629
}
629630
}
630631
else {
631-
if (isSimpleInlineableExpression(node.operand.argumentExpression)) {
632-
getterName = setterName = node.operand.argumentExpression;
632+
if (isSimpleInlineableExpression(operand.argumentExpression)) {
633+
getterName = setterName = operand.argumentExpression;
633634
}
634635
else {
635636
getterName = factory.createTempVariable(hoistVariableDeclaration);
636-
setterName = factory.createAssignment(getterName, visitNode(node.operand.argumentExpression, visitor, isExpression));
637+
setterName = factory.createAssignment(getterName, visitNode(operand.argumentExpression, visitor, isExpression));
637638
}
638639
}
639640
if (setterName && getterName) {
640641
let expression: Expression = factory.createReflectGetCall(superClassReference, getterName, classConstructor);
641-
setTextRange(expression, node.operand);
642+
setTextRange(expression, operand);
642643

643644
const temp = valueIsDiscarded ? undefined : factory.createTempVariable(hoistVariableDeclaration);
644645
expression = expandPreOrPostfixIncrementOrDecrementExpression(factory, node, expression, hoistVariableDeclaration, temp);

‎tests/baselines/reference/privateNameFieldUnaryMutation.js

+44-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ class C {
1212
const d = --this.#test;
1313
for (this.#test = 0; this.#test < 10; ++this.#test) {}
1414
for (this.#test = 0; this.#test < 10; this.#test++) {}
15+
16+
(this.#test)++;
17+
(this.#test)--;
18+
++(this.#test);
19+
--(this.#test);
20+
const e = (this.#test)++;
21+
const f = (this.#test)--;
22+
const g = ++(this.#test);
23+
const h = --(this.#test);
24+
for (this.#test = 0; this.#test < 10; ++(this.#test)) {}
25+
for (this.#test = 0; this.#test < 10; (this.#test)++) {}
1526
}
1627
test() {
1728
this.getInstance().#test++;
@@ -24,6 +35,17 @@ class C {
2435
const d = --this.getInstance().#test;
2536
for (this.getInstance().#test = 0; this.getInstance().#test < 10; ++this.getInstance().#test) {}
2637
for (this.getInstance().#test = 0; this.getInstance().#test < 10; this.getInstance().#test++) {}
38+
39+
(this.getInstance().#test)++;
40+
(this.getInstance().#test)--;
41+
++(this.getInstance().#test);
42+
--(this.getInstance().#test);
43+
const e = (this.getInstance().#test)++;
44+
const f = (this.getInstance().#test)--;
45+
const g = ++(this.getInstance().#test);
46+
const h = --(this.getInstance().#test);
47+
for (this.getInstance().#test = 0; this.getInstance().#test < 10; ++(this.getInstance().#test)) {}
48+
for (this.getInstance().#test = 0; this.getInstance().#test < 10; (this.getInstance().#test)++) {}
2749
}
2850
getInstance() { return new C(); }
2951
}
@@ -44,7 +66,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
4466
var _C_test;
4567
class C {
4668
constructor() {
47-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
69+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z;
4870
_C_test.set(this, 24);
4971
__classPrivateFieldSet(this, _C_test, (_a = __classPrivateFieldGet(this, _C_test, "f"), _a++, _a), "f");
5072
__classPrivateFieldSet(this, _C_test, (_b = __classPrivateFieldGet(this, _C_test, "f"), _b--, _b), "f");
@@ -56,9 +78,19 @@ class C {
5678
const d = __classPrivateFieldSet(this, _C_test, (_k = __classPrivateFieldGet(this, _C_test, "f"), --_k), "f");
5779
for (__classPrivateFieldSet(this, _C_test, 0, "f"); __classPrivateFieldGet(this, _C_test, "f") < 10; __classPrivateFieldSet(this, _C_test, (_l = __classPrivateFieldGet(this, _C_test, "f"), ++_l), "f")) { }
5880
for (__classPrivateFieldSet(this, _C_test, 0, "f"); __classPrivateFieldGet(this, _C_test, "f") < 10; __classPrivateFieldSet(this, _C_test, (_m = __classPrivateFieldGet(this, _C_test, "f"), _m++, _m), "f")) { }
81+
__classPrivateFieldSet(this, _C_test, (_o = __classPrivateFieldGet(this, _C_test, "f"), _o++, _o), "f");
82+
__classPrivateFieldSet(this, _C_test, (_p = __classPrivateFieldGet(this, _C_test, "f"), _p--, _p), "f");
83+
__classPrivateFieldSet(this, _C_test, (_q = __classPrivateFieldGet(this, _C_test, "f"), ++_q), "f");
84+
__classPrivateFieldSet(this, _C_test, (_r = __classPrivateFieldGet(this, _C_test, "f"), --_r), "f");
85+
const e = (__classPrivateFieldSet(this, _C_test, (_t = __classPrivateFieldGet(this, _C_test, "f"), _s = _t++, _t), "f"), _s);
86+
const f = (__classPrivateFieldSet(this, _C_test, (_v = __classPrivateFieldGet(this, _C_test, "f"), _u = _v--, _v), "f"), _u);
87+
const g = __classPrivateFieldSet(this, _C_test, (_w = __classPrivateFieldGet(this, _C_test, "f"), ++_w), "f");
88+
const h = __classPrivateFieldSet(this, _C_test, (_x = __classPrivateFieldGet(this, _C_test, "f"), --_x), "f");
89+
for (__classPrivateFieldSet(this, _C_test, 0, "f"); __classPrivateFieldGet(this, _C_test, "f") < 10; __classPrivateFieldSet(this, _C_test, (_y = __classPrivateFieldGet(this, _C_test, "f"), ++_y), "f")) { }
90+
for (__classPrivateFieldSet(this, _C_test, 0, "f"); __classPrivateFieldGet(this, _C_test, "f") < 10; __classPrivateFieldSet(this, _C_test, (_z = __classPrivateFieldGet(this, _C_test, "f"), _z++, _z), "f")) { }
5991
}
6092
test() {
61-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
93+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19;
6294
__classPrivateFieldSet(_a = this.getInstance(), _C_test, (_b = __classPrivateFieldGet(_a, _C_test, "f"), _b++, _b), "f");
6395
__classPrivateFieldSet(_c = this.getInstance(), _C_test, (_d = __classPrivateFieldGet(_c, _C_test, "f"), _d--, _d), "f");
6496
__classPrivateFieldSet(_e = this.getInstance(), _C_test, (_f = __classPrivateFieldGet(_e, _C_test, "f"), ++_f), "f");
@@ -69,6 +101,16 @@ class C {
69101
const d = __classPrivateFieldSet(_s = this.getInstance(), _C_test, (_t = __classPrivateFieldGet(_s, _C_test, "f"), --_t), "f");
70102
for (__classPrivateFieldSet(this.getInstance(), _C_test, 0, "f"); __classPrivateFieldGet(this.getInstance(), _C_test, "f") < 10; __classPrivateFieldSet(_u = this.getInstance(), _C_test, (_v = __classPrivateFieldGet(_u, _C_test, "f"), ++_v), "f")) { }
71103
for (__classPrivateFieldSet(this.getInstance(), _C_test, 0, "f"); __classPrivateFieldGet(this.getInstance(), _C_test, "f") < 10; __classPrivateFieldSet(_w = this.getInstance(), _C_test, (_x = __classPrivateFieldGet(_w, _C_test, "f"), _x++, _x), "f")) { }
104+
__classPrivateFieldSet(_y = this.getInstance(), _C_test, (_z = __classPrivateFieldGet(_y, _C_test, "f"), _z++, _z), "f");
105+
__classPrivateFieldSet(_0 = this.getInstance(), _C_test, (_1 = __classPrivateFieldGet(_0, _C_test, "f"), _1--, _1), "f");
106+
__classPrivateFieldSet(_2 = this.getInstance(), _C_test, (_3 = __classPrivateFieldGet(_2, _C_test, "f"), ++_3), "f");
107+
__classPrivateFieldSet(_4 = this.getInstance(), _C_test, (_5 = __classPrivateFieldGet(_4, _C_test, "f"), --_5), "f");
108+
const e = (__classPrivateFieldSet(_6 = this.getInstance(), _C_test, (_8 = __classPrivateFieldGet(_6, _C_test, "f"), _7 = _8++, _8), "f"), _7);
109+
const f = (__classPrivateFieldSet(_9 = this.getInstance(), _C_test, (_11 = __classPrivateFieldGet(_9, _C_test, "f"), _10 = _11--, _11), "f"), _10);
110+
const g = __classPrivateFieldSet(_12 = this.getInstance(), _C_test, (_13 = __classPrivateFieldGet(_12, _C_test, "f"), ++_13), "f");
111+
const h = __classPrivateFieldSet(_14 = this.getInstance(), _C_test, (_15 = __classPrivateFieldGet(_14, _C_test, "f"), --_15), "f");
112+
for (__classPrivateFieldSet(this.getInstance(), _C_test, 0, "f"); __classPrivateFieldGet(this.getInstance(), _C_test, "f") < 10; __classPrivateFieldSet(_16 = this.getInstance(), _C_test, (_17 = __classPrivateFieldGet(_16, _C_test, "f"), ++_17), "f")) { }
113+
for (__classPrivateFieldSet(this.getInstance(), _C_test, 0, "f"); __classPrivateFieldGet(this.getInstance(), _C_test, "f") < 10; __classPrivateFieldSet(_18 = this.getInstance(), _C_test, (_19 = __classPrivateFieldGet(_18, _C_test, "f"), _19++, _19), "f")) { }
72114
}
73115
getInstance() { return new C(); }
74116
}

0 commit comments

Comments
 (0)
Please sign in to comment.