Skip to content

Commit

Permalink
fix(49935): omit parentheses in the operand of the unary expression (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Aug 1, 2022
1 parent 55f2c0c commit c0072aa
Show file tree
Hide file tree
Showing 5 changed files with 449 additions and 50 deletions.
29 changes: 15 additions & 14 deletions src/compiler/transformers/classFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,10 +576,11 @@ namespace ts {

function visitPreOrPostfixUnaryExpression(node: PrefixUnaryExpression | PostfixUnaryExpression, valueIsDiscarded: boolean) {
if (node.operator === SyntaxKind.PlusPlusToken || node.operator === SyntaxKind.MinusMinusToken) {
if (shouldTransformPrivateElementsOrClassStaticBlocks && isPrivateIdentifierPropertyAccessExpression(node.operand)) {
const operand = skipParentheses(node.operand);
if (shouldTransformPrivateElementsOrClassStaticBlocks && isPrivateIdentifierPropertyAccessExpression(operand)) {
let info: PrivateIdentifierInfo | undefined;
if (info = accessPrivateIdentifier(node.operand.name)) {
const receiver = visitNode(node.operand.expression, visitor, isExpression);
if (info = accessPrivateIdentifier(operand.name)) {
const receiver = visitNode(operand.expression, visitor, isExpression);
const { readExpression, initializeExpression } = createCopiableReceiverExpr(receiver);

let expression: Expression = createPrivateIdentifierAccess(info, readExpression);
Expand All @@ -601,7 +602,7 @@ namespace ts {
}
}
else if (shouldTransformSuperInStaticInitializers &&
isSuperProperty(node.operand) &&
isSuperProperty(operand) &&
currentStaticPropertyDeclarationOrStaticBlock &&
currentClassLexicalEnvironment) {
// converts `++super.a` into `(Reflect.set(_baseTemp, "a", (_a = Reflect.get(_baseTemp, "a", _classTemp), _b = ++_a), _classTemp), _b)`
Expand All @@ -614,31 +615,31 @@ namespace ts {
// converts `super[f()]--` into `(Reflect.set(_baseTemp, _a = f(), (_b = Reflect.get(_baseTemp, _a, _classTemp), _c = _b--), _classTemp), _c)`
const { classConstructor, superClassReference, facts } = currentClassLexicalEnvironment;
if (facts & ClassFacts.ClassWasDecorated) {
const operand = visitInvalidSuperProperty(node.operand);
const expression = visitInvalidSuperProperty(operand);
return isPrefixUnaryExpression(node) ?
factory.updatePrefixUnaryExpression(node, operand) :
factory.updatePostfixUnaryExpression(node, operand);
factory.updatePrefixUnaryExpression(node, expression) :
factory.updatePostfixUnaryExpression(node, expression);
}
if (classConstructor && superClassReference) {
let setterName: Expression | undefined;
let getterName: Expression | undefined;
if (isPropertyAccessExpression(node.operand)) {
if (isIdentifier(node.operand.name)) {
getterName = setterName = factory.createStringLiteralFromNode(node.operand.name);
if (isPropertyAccessExpression(operand)) {
if (isIdentifier(operand.name)) {
getterName = setterName = factory.createStringLiteralFromNode(operand.name);
}
}
else {
if (isSimpleInlineableExpression(node.operand.argumentExpression)) {
getterName = setterName = node.operand.argumentExpression;
if (isSimpleInlineableExpression(operand.argumentExpression)) {
getterName = setterName = operand.argumentExpression;
}
else {
getterName = factory.createTempVariable(hoistVariableDeclaration);
setterName = factory.createAssignment(getterName, visitNode(node.operand.argumentExpression, visitor, isExpression));
setterName = factory.createAssignment(getterName, visitNode(operand.argumentExpression, visitor, isExpression));
}
}
if (setterName && getterName) {
let expression: Expression = factory.createReflectGetCall(superClassReference, getterName, classConstructor);
setTextRange(expression, node.operand);
setTextRange(expression, operand);

const temp = valueIsDiscarded ? undefined : factory.createTempVariable(hoistVariableDeclaration);
expression = expandPreOrPostfixIncrementOrDecrementExpression(factory, node, expression, hoistVariableDeclaration, temp);
Expand Down
46 changes: 44 additions & 2 deletions tests/baselines/reference/privateNameFieldUnaryMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ class C {
const d = --this.#test;
for (this.#test = 0; this.#test < 10; ++this.#test) {}
for (this.#test = 0; this.#test < 10; this.#test++) {}

(this.#test)++;
(this.#test)--;
++(this.#test);
--(this.#test);
const e = (this.#test)++;
const f = (this.#test)--;
const g = ++(this.#test);
const h = --(this.#test);
for (this.#test = 0; this.#test < 10; ++(this.#test)) {}
for (this.#test = 0; this.#test < 10; (this.#test)++) {}
}
test() {
this.getInstance().#test++;
Expand All @@ -24,6 +35,17 @@ class C {
const d = --this.getInstance().#test;
for (this.getInstance().#test = 0; this.getInstance().#test < 10; ++this.getInstance().#test) {}
for (this.getInstance().#test = 0; this.getInstance().#test < 10; this.getInstance().#test++) {}

(this.getInstance().#test)++;
(this.getInstance().#test)--;
++(this.getInstance().#test);
--(this.getInstance().#test);
const e = (this.getInstance().#test)++;
const f = (this.getInstance().#test)--;
const g = ++(this.getInstance().#test);
const h = --(this.getInstance().#test);
for (this.getInstance().#test = 0; this.getInstance().#test < 10; ++(this.getInstance().#test)) {}
for (this.getInstance().#test = 0; this.getInstance().#test < 10; (this.getInstance().#test)++) {}
}
getInstance() { return new C(); }
}
Expand All @@ -44,7 +66,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
var _C_test;
class C {
constructor() {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
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;
_C_test.set(this, 24);
__classPrivateFieldSet(this, _C_test, (_a = __classPrivateFieldGet(this, _C_test, "f"), _a++, _a), "f");
__classPrivateFieldSet(this, _C_test, (_b = __classPrivateFieldGet(this, _C_test, "f"), _b--, _b), "f");
Expand All @@ -56,9 +78,19 @@ class C {
const d = __classPrivateFieldSet(this, _C_test, (_k = __classPrivateFieldGet(this, _C_test, "f"), --_k), "f");
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")) { }
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")) { }
__classPrivateFieldSet(this, _C_test, (_o = __classPrivateFieldGet(this, _C_test, "f"), _o++, _o), "f");
__classPrivateFieldSet(this, _C_test, (_p = __classPrivateFieldGet(this, _C_test, "f"), _p--, _p), "f");
__classPrivateFieldSet(this, _C_test, (_q = __classPrivateFieldGet(this, _C_test, "f"), ++_q), "f");
__classPrivateFieldSet(this, _C_test, (_r = __classPrivateFieldGet(this, _C_test, "f"), --_r), "f");
const e = (__classPrivateFieldSet(this, _C_test, (_t = __classPrivateFieldGet(this, _C_test, "f"), _s = _t++, _t), "f"), _s);
const f = (__classPrivateFieldSet(this, _C_test, (_v = __classPrivateFieldGet(this, _C_test, "f"), _u = _v--, _v), "f"), _u);
const g = __classPrivateFieldSet(this, _C_test, (_w = __classPrivateFieldGet(this, _C_test, "f"), ++_w), "f");
const h = __classPrivateFieldSet(this, _C_test, (_x = __classPrivateFieldGet(this, _C_test, "f"), --_x), "f");
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")) { }
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")) { }
}
test() {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
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;
__classPrivateFieldSet(_a = this.getInstance(), _C_test, (_b = __classPrivateFieldGet(_a, _C_test, "f"), _b++, _b), "f");
__classPrivateFieldSet(_c = this.getInstance(), _C_test, (_d = __classPrivateFieldGet(_c, _C_test, "f"), _d--, _d), "f");
__classPrivateFieldSet(_e = this.getInstance(), _C_test, (_f = __classPrivateFieldGet(_e, _C_test, "f"), ++_f), "f");
Expand All @@ -69,6 +101,16 @@ class C {
const d = __classPrivateFieldSet(_s = this.getInstance(), _C_test, (_t = __classPrivateFieldGet(_s, _C_test, "f"), --_t), "f");
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")) { }
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")) { }
__classPrivateFieldSet(_y = this.getInstance(), _C_test, (_z = __classPrivateFieldGet(_y, _C_test, "f"), _z++, _z), "f");
__classPrivateFieldSet(_0 = this.getInstance(), _C_test, (_1 = __classPrivateFieldGet(_0, _C_test, "f"), _1--, _1), "f");
__classPrivateFieldSet(_2 = this.getInstance(), _C_test, (_3 = __classPrivateFieldGet(_2, _C_test, "f"), ++_3), "f");
__classPrivateFieldSet(_4 = this.getInstance(), _C_test, (_5 = __classPrivateFieldGet(_4, _C_test, "f"), --_5), "f");
const e = (__classPrivateFieldSet(_6 = this.getInstance(), _C_test, (_8 = __classPrivateFieldGet(_6, _C_test, "f"), _7 = _8++, _8), "f"), _7);
const f = (__classPrivateFieldSet(_9 = this.getInstance(), _C_test, (_11 = __classPrivateFieldGet(_9, _C_test, "f"), _10 = _11--, _11), "f"), _10);
const g = __classPrivateFieldSet(_12 = this.getInstance(), _C_test, (_13 = __classPrivateFieldGet(_12, _C_test, "f"), ++_13), "f");
const h = __classPrivateFieldSet(_14 = this.getInstance(), _C_test, (_15 = __classPrivateFieldGet(_14, _C_test, "f"), --_15), "f");
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")) { }
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")) { }
}
getInstance() { return new C(); }
}
Expand Down

0 comments on commit c0072aa

Please sign in to comment.