Skip to content

Commit b697e8e

Browse files
authored
Fix compound assignment not assigning in expressions (#1287)
1 parent 60f11dd commit b697e8e

File tree

2 files changed

+28
-34
lines changed

2 files changed

+28
-34
lines changed

src/transformation/visitors/binary-expression/compound.ts

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function transformCompoundAssignment(
7676
context.transformExpression(rhs)
7777
);
7878

79-
if (lua.isTableIndexExpression(left) && shouldCacheTableIndexExpressions(left, rightPrecedingStatements)) {
79+
if (lua.isTableIndexExpression(left)) {
8080
// Complex property/element accesses need to cache object/index expressions to avoid repeating side-effects
8181
// local __obj, __index = ${objExpression}, ${indexExpression};
8282
const obj = context.createTempNameForLuaExpression(left.table);
@@ -105,6 +105,20 @@ export function transformCompoundAssignment(
105105
result: tmp,
106106
};
107107
} else {
108+
if (isSetterSkippingCompoundAssignmentOperator(operator)) {
109+
return {
110+
statements: [
111+
objAndIndexDeclaration,
112+
...transformSetterSkippingCompoundAssignment(
113+
accessExpression,
114+
operator,
115+
right,
116+
rightPrecedingStatements
117+
),
118+
],
119+
result: left,
120+
};
121+
}
108122
// local ____tmp = ____obj[____index] ${replacementOperator} ${right};
109123
// ____obj[____index] = ____tmp;
110124
// return ____tmp
@@ -145,37 +159,6 @@ export function transformCompoundAssignment(
145159
rightPrecedingStatements
146160
);
147161
return { statements: [tmpDeclaration, ...precedingStatements, ...assignStatements], result: tmpIdentifier };
148-
} else if (ts.isPropertyAccessExpression(lhs) || ts.isElementAccessExpression(lhs)) {
149-
// Simple property/element access expressions need to cache in temp to avoid double-evaluation
150-
// local ____tmp = ${left} ${replacementOperator} ${right};
151-
// ${left} = ____tmp;
152-
// return ____tmp
153-
const tmpIdentifier = context.createTempNameForLuaExpression(left);
154-
const [precedingStatements, operatorExpression] = transformBinaryOperation(
155-
context,
156-
left,
157-
right,
158-
rightPrecedingStatements,
159-
operator,
160-
expression
161-
);
162-
const tmpDeclaration = lua.createVariableDeclarationStatement(tmpIdentifier, operatorExpression);
163-
164-
if (isSetterSkippingCompoundAssignmentOperator(operator)) {
165-
const statements = [
166-
tmpDeclaration,
167-
...transformSetterSkippingCompoundAssignment(tmpIdentifier, operator, right, precedingStatements),
168-
];
169-
return { statements, result: tmpIdentifier };
170-
}
171-
172-
const assignStatements = transformAssignmentWithRightPrecedingStatements(
173-
context,
174-
lhs,
175-
tmpIdentifier,
176-
precedingStatements
177-
);
178-
return { statements: [tmpDeclaration, ...assignStatements], result: tmpIdentifier };
179162
} else {
180163
if (rightPrecedingStatements.length > 0 && isSetterSkippingCompoundAssignmentOperator(operator)) {
181164
return {

test/unit/assignments.spec.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ test.each([
135135
`.expectToMatchJsResult();
136136
});
137137

138+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1277
139+
test("Compound assignment as expression (#1277)", () => {
140+
util.testFunction`
141+
let foo = {
142+
bar: false as any
143+
}
144+
const result = foo.bar ||= true;
145+
return { result, foo };
146+
`.expectToMatchJsResult();
147+
});
148+
138149
test.each([
139150
"++o.p",
140151
"o.p++",
@@ -417,7 +428,7 @@ test.each([
417428
* x.y ||= z is translated to x.y || (x.y = z).
418429
* x.y &&= z is translated to x.y && (x.y = z).
419430
* x.y ||= z is translated to x.y !== undefined && (x.y = z).
420-
431+
421432
Test if setter in Lua is called same nr of times as in JS.
422433
*/
423434
util.testModule`
@@ -449,7 +460,7 @@ test.each([
449460
* x.y ||= z is translated to x.y || (x.y = z).
450461
* x.y &&= z is translated to x.y && (x.y = z).
451462
* x.y ||= z is translated to x.y !== undefined && (x.y = z).
452-
463+
453464
Test if setter in Lua is called same nr of times as in JS.
454465
*/
455466
util.testModule`

0 commit comments

Comments
 (0)