Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/transformation/visitors/binary-expression/compound.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as ts from "typescript";
import * as lua from "../../../LuaAST";
import { cast, assertNever } from "../../../utils";
import { assertNever } from "../../../utils";
import { TransformationContext } from "../../context";
import { transformInPrecedingStatementScope, WithPrecedingStatements } from "../../utils/preceding-statements";
import { transformBinaryOperation } from "./index";
import { transformAssignmentWithRightPrecedingStatements } from "./assignments";
import { isArrayLength } from "./destructuring-assignments";
import { LuaLibFeature, transformLuaLibFunction } from "../../utils/lualib";
import { cannotAssignToNodeOfKind } from "../../utils/diagnostics";

function isLuaExpressionWithSideEffect(expression: lua.Expression) {
return !(lua.isLiteral(expression) || lua.isIdentifier(expression));
Expand Down Expand Up @@ -85,7 +86,12 @@ function transformCompoundAssignment(
return { precedingStatements, result: lengthSetterStatement.expression };
}

const left = cast(context.transformExpression(lhs), lua.isAssignmentLeftHandSideExpression);
const left = context.transformExpression(lhs);
if (!lua.isAssignmentLeftHandSideExpression(left)) {
context.diagnostics.push(cannotAssignToNodeOfKind(expression, left.kind));
return { precedingStatements: [], result: left };
}

const { precedingStatements: rightPrecedingStatements, result: right } = transformInPrecedingStatementScope(
context,
() => context.transformExpression(rhs)
Expand Down Expand Up @@ -250,7 +256,12 @@ export function transformCompoundAssignmentStatement(
return [...precedingStatements, lengthSetterStatement];
}

const left = cast(context.transformExpression(lhs), lua.isAssignmentLeftHandSideExpression);
const left = context.transformExpression(lhs);
if (!lua.isAssignmentLeftHandSideExpression(left)) {
context.diagnostics.push(cannotAssignToNodeOfKind(node, left.kind));
return [];
}

const { precedingStatements: rightPrecedingStatements, result: right } = transformInPrecedingStatementScope(
context,
() => context.transformExpression(rhs)
Expand Down
10 changes: 10 additions & 0 deletions test/unit/assignments.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,13 @@ test.each([
return [obj, objGot];
`.expectToMatchJsResult();
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1412
test("invalid assignment to call expression (#1412)", () => {
util.testFunction`
function foo(): number {
return 3;
}
foo() += 4;
`.expectNoTranspileException();
});
13 changes: 12 additions & 1 deletion test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,23 @@ export abstract class TestBuilder {
// Actions

public debug(includeLualib = false): this {
const transpiledFiles = this.getLuaResult().transpiledFiles;
const { transpiledFiles, diagnostics } = this.getLuaResult();
const luaCode = transpiledFiles
.filter(f => includeLualib || f.outPath !== "lualib_bundle.lua")
.map(f => `[${f.outPath}]:\n${f.lua?.replace(/^/gm, " ")}`);
const value = prettyFormat.format(this.getLuaExecutionResult()).replace(/^/gm, " ");
console.log(`Lua Code:\n${luaCode.join("\n")}\n\nValue:\n${value}`);

if (diagnostics.length > 0) {
console.log(
ts.formatDiagnostics(diagnostics.map(tstl.prepareDiagnosticForFormatting), {
getCurrentDirectory: () => "",
getCanonicalFileName: fileName => fileName,
getNewLine: () => "\n",
})
);
}

return this;
}

Expand Down