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
6 changes: 1 addition & 5 deletions src/transformation/visitors/class/members/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as ts from "typescript";
import * as lua from "../../../../LuaAST";
import { TransformationContext } from "../../../context";
import { createSelfIdentifier } from "../../../utils/lua-ast";
import { transformInPrecedingStatementScope } from "../../../utils/preceding-statements";
import { popScope, pushScope, ScopeType } from "../../../utils/scope";
import { transformFunctionBodyContent, transformFunctionBodyHeader, transformParameters } from "../../function";
import { transformIdentifier } from "../../identifier";
Expand Down Expand Up @@ -44,9 +43,7 @@ export function transformConstructorDeclaration(
// Check for field declarations in constructor
const constructorFieldsDeclarations = statement.parameters.filter(p => p.modifiers !== undefined);

const [fieldsPrecedingStatements, classInstanceFields] = transformInPrecedingStatementScope(context, () =>
transformClassInstanceFields(context, instanceFields)
);
const classInstanceFields = transformClassInstanceFields(context, instanceFields);

// If there are field initializers and the first statement is a super call,
// move super call between default assignments and initializers
Expand Down Expand Up @@ -81,7 +78,6 @@ export function transformConstructorDeclaration(
// else { TypeScript error: A parameter property may not be declared using a binding pattern }
}

bodyWithFieldInitializers.push(...fieldsPrecedingStatements);
bodyWithFieldInitializers.push(...classInstanceFields);

bodyWithFieldInitializers.push(...body);
Expand Down
21 changes: 13 additions & 8 deletions src/transformation/visitors/class/members/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as ts from "typescript";
import * as lua from "../../../../LuaAST";
import { TransformationContext } from "../../../context";
import { createSelfIdentifier } from "../../../utils/lua-ast";
import { transformInPrecedingStatementScope } from "../../../utils/preceding-statements";
import { transformPropertyName } from "../../literal";
import { createDecoratingExpression, transformDecoratorExpression } from "../decorators";
import { transformMemberExpressionOwnerName } from "./method";
Expand Down Expand Up @@ -30,18 +31,22 @@ export function transformClassInstanceFields(
const statements: lua.Statement[] = [];

for (const f of instanceFields) {
// Get identifier
const fieldName = transformPropertyName(context, f.name);
const [precedingStatements, statement] = transformInPrecedingStatementScope(context, () => {
// Get identifier
const fieldName = transformPropertyName(context, f.name);

const value = f.initializer ? context.transformExpression(f.initializer) : undefined;
const value = f.initializer ? context.transformExpression(f.initializer) : undefined;

// self[fieldName]
const selfIndex = lua.createTableIndexExpression(createSelfIdentifier(), fieldName);
// self[fieldName]
const selfIndex = lua.createTableIndexExpression(createSelfIdentifier(), fieldName);

// self[fieldName] = value
const assignClassField = lua.createAssignmentStatement(selfIndex, value, f);
// self[fieldName] = value
const assignClassField = lua.createAssignmentStatement(selfIndex, value, f);

statements.push(assignClassField);
return assignClassField;
});

statements.push(...precedingStatements, statement);
}

return statements;
Expand Down
11 changes: 11 additions & 0 deletions test/unit/precedingStatements.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,14 @@ test("class member initializers", () => {
return [inst.myField, inst.foo];
`.expectToMatchJsResult();
});

test("class member initializers in extended class", () => {
util.testFunction`
class A {}
class MyClass extends A {
myField = false ?? true;
}
const inst = new MyClass();
return inst.myField;
`.expectToMatchJsResult();
});