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
9 changes: 3 additions & 6 deletions src/transformation/visitors/class/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { isAmbientNode } from "../../utils/typescript";
import { transformIdentifier } from "../identifier";
import { transformPropertyName } from "../literal";
import { createDecoratingExpression, transformDecoratorExpression } from "./decorators";
import { isGetAccessorOverride, transformAccessorDeclarations } from "./members/accessors";
import { transformAccessorDeclarations } from "./members/accessors";
import { createConstructorName, transformConstructorDeclaration } from "./members/constructor";
import {
createPropertyDecoratingExpression,
Expand Down Expand Up @@ -238,15 +238,12 @@ function transformClassLikeDeclaration(
);

if (constructorResult) result.push(constructorResult);
} else if (
instanceFields.length > 0 ||
classDeclaration.members.some(m => isGetAccessorOverride(context, m, classDeclaration))
) {
} else if (instanceFields.length > 0) {
// Generate a constructor if none was defined in a class with instance fields that need initialization
// localClassName.prototype.____constructor = function(self, ...)
// baseClassName.prototype.____constructor(self, ...)
// ...
const constructorBody = transformClassInstanceFields(context, classDeclaration, instanceFields);
const constructorBody = transformClassInstanceFields(context, instanceFields);
const superCall = lua.createExpressionStatement(
lua.createCallExpression(
lua.createTableIndexExpression(
Expand Down
69 changes: 2 additions & 67 deletions src/transformation/visitors/class/members/accessors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import * as ts from "typescript";
import * as lua from "../../../../LuaAST";
import { AllAccessorDeclarations, TransformationContext } from "../../../context";
import { createSelfIdentifier } from "../../../utils/lua-ast";
import { transformLuaLibFunction, LuaLibFeature } from "../../../utils/lualib";
import { LuaLibFeature, transformLuaLibFunction } from "../../../utils/lualib";
import { transformFunctionBody, transformParameters } from "../../function";
import { transformPropertyName } from "../../literal";
import { getExtendedType, isStaticNode } from "../utils";
import { isStaticNode } from "../utils";
import { createPrototypeName } from "./constructor";

function transformAccessor(context: TransformationContext, node: ts.AccessorDeclaration): lua.FunctionExpression {
Expand Down Expand Up @@ -40,68 +40,3 @@ export function transformAccessorDeclarations(
const call = transformLuaLibFunction(context, feature, undefined, ...parameters);
return lua.createExpressionStatement(call);
}

function* classWithAncestors(
context: TransformationContext,
classDeclaration: ts.ClassLikeDeclarationBase
): Generator<ts.ClassLikeDeclarationBase> {
yield classDeclaration;

const extendsType = getExtendedType(context, classDeclaration);
if (!extendsType) {
return false;
}

const symbol = extendsType.getSymbol();
if (symbol === undefined) {
return false;
}

const symbolDeclarations = symbol.getDeclarations();
if (symbolDeclarations === undefined) {
return false;
}

const declaration = symbolDeclarations.find(ts.isClassLike);
if (!declaration) {
return false;
}

yield* classWithAncestors(context, declaration);
}

export const hasMemberInClassOrAncestor = (
context: TransformationContext,
classDeclaration: ts.ClassLikeDeclarationBase,
callback: (m: ts.ClassElement) => boolean
) => [...classWithAncestors(context, classDeclaration)].some(c => c.members.some(callback));

function getPropertyName(propertyName: ts.PropertyName): string | number | undefined {
if (ts.isIdentifier(propertyName) || ts.isStringLiteral(propertyName) || ts.isNumericLiteral(propertyName)) {
return propertyName.text;
} else {
return undefined; // TODO: how to handle computed property names?
}
}

function isSamePropertyName(a: ts.PropertyName, b: ts.PropertyName): boolean {
const aName = getPropertyName(a);
const bName = getPropertyName(b);
return aName !== undefined && aName === bName;
}

export function isGetAccessorOverride(
context: TransformationContext,
element: ts.ClassElement,
classDeclaration: ts.ClassLikeDeclarationBase
): element is ts.GetAccessorDeclaration {
if (!ts.isGetAccessor(element) || isStaticNode(element)) {
return false;
}

return hasMemberInClassOrAncestor(
context,
classDeclaration,
m => ts.isPropertyDeclaration(m) && m.initializer !== undefined && isSamePropertyName(m.name, element.name)
);
}
4 changes: 2 additions & 2 deletions src/transformation/visitors/class/members/constructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as lua from "../../../../LuaAST";
import { TransformationContext } from "../../../context";
import { createSelfIdentifier } from "../../../utils/lua-ast";
import { popScope, pushScope, ScopeType } from "../../../utils/scope";
import { transformFunctionBodyHeader, transformFunctionBodyContent, transformParameters } from "../../function";
import { transformFunctionBodyContent, transformFunctionBodyHeader, transformParameters } from "../../function";
import { transformIdentifier } from "../../identifier";
import { transformClassInstanceFields } from "./fields";

Expand Down Expand Up @@ -43,7 +43,7 @@ export function transformConstructorDeclaration(
// Check for field declarations in constructor
const constructorFieldsDeclarations = statement.parameters.filter(p => p.modifiers !== undefined);

const classInstanceFields = transformClassInstanceFields(context, classDeclaration, 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
22 changes: 0 additions & 22 deletions src/transformation/visitors/class/members/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as lua from "../../../../LuaAST";
import { TransformationContext } from "../../../context";
import { createSelfIdentifier } from "../../../utils/lua-ast";
import { transformPropertyName } from "../../literal";
import { isGetAccessorOverride } from "./accessors";
import { createDecoratingExpression, transformDecoratorExpression } from "../decorators";
import { transformMemberExpressionOwnerName } from "./method";

Expand All @@ -27,7 +26,6 @@ export function createPropertyDecoratingExpression(

export function transformClassInstanceFields(
context: TransformationContext,
classDeclaration: ts.ClassLikeDeclaration,
instanceFields: ts.PropertyDeclaration[]
): lua.Statement[] {
const statements: lua.Statement[] = [];
Expand All @@ -47,26 +45,6 @@ export function transformClassInstanceFields(
statements.push(assignClassField);
}

// TODO: Remove when `useDefineForClassFields` would be `true` by default

const getOverrides = classDeclaration.members.filter((m): m is ts.GetAccessorDeclaration =>
isGetAccessorOverride(context, m, classDeclaration)
);

for (const getter of getOverrides) {
const getterName = transformPropertyName(context, getter.name);

const resetGetter = lua.createExpressionStatement(
lua.createCallExpression(lua.createIdentifier("rawset"), [
createSelfIdentifier(),
getterName,
lua.createNilLiteral(),
]),
classDeclaration.members.find(ts.isConstructorDeclaration) ?? classDeclaration
);
statements.push(resetGetter);
}

return statements;
}

Expand Down