Skip to content

Commit 86cc1b9

Browse files
authored
Remove class field/accessor compatibility code (#929)
1 parent dae7aba commit 86cc1b9

File tree

4 files changed

+7
-97
lines changed

4 files changed

+7
-97
lines changed

src/transformation/visitors/class/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { isAmbientNode } from "../../utils/typescript";
2929
import { transformIdentifier } from "../identifier";
3030
import { transformPropertyName } from "../literal";
3131
import { createDecoratingExpression, transformDecoratorExpression } from "./decorators";
32-
import { isGetAccessorOverride, transformAccessorDeclarations } from "./members/accessors";
32+
import { transformAccessorDeclarations } from "./members/accessors";
3333
import { createConstructorName, transformConstructorDeclaration } from "./members/constructor";
3434
import {
3535
createPropertyDecoratingExpression,
@@ -238,15 +238,12 @@ function transformClassLikeDeclaration(
238238
);
239239

240240
if (constructorResult) result.push(constructorResult);
241-
} else if (
242-
instanceFields.length > 0 ||
243-
classDeclaration.members.some(m => isGetAccessorOverride(context, m, classDeclaration))
244-
) {
241+
} else if (instanceFields.length > 0) {
245242
// Generate a constructor if none was defined in a class with instance fields that need initialization
246243
// localClassName.prototype.____constructor = function(self, ...)
247244
// baseClassName.prototype.____constructor(self, ...)
248245
// ...
249-
const constructorBody = transformClassInstanceFields(context, classDeclaration, instanceFields);
246+
const constructorBody = transformClassInstanceFields(context, instanceFields);
250247
const superCall = lua.createExpressionStatement(
251248
lua.createCallExpression(
252249
lua.createTableIndexExpression(

src/transformation/visitors/class/members/accessors.ts

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import * as ts from "typescript";
22
import * as lua from "../../../../LuaAST";
33
import { AllAccessorDeclarations, TransformationContext } from "../../../context";
44
import { createSelfIdentifier } from "../../../utils/lua-ast";
5-
import { transformLuaLibFunction, LuaLibFeature } from "../../../utils/lualib";
5+
import { LuaLibFeature, transformLuaLibFunction } from "../../../utils/lualib";
66
import { transformFunctionBody, transformParameters } from "../../function";
77
import { transformPropertyName } from "../../literal";
8-
import { getExtendedType, isStaticNode } from "../utils";
8+
import { isStaticNode } from "../utils";
99
import { createPrototypeName } from "./constructor";
1010

1111
function transformAccessor(context: TransformationContext, node: ts.AccessorDeclaration): lua.FunctionExpression {
@@ -40,68 +40,3 @@ export function transformAccessorDeclarations(
4040
const call = transformLuaLibFunction(context, feature, undefined, ...parameters);
4141
return lua.createExpressionStatement(call);
4242
}
43-
44-
function* classWithAncestors(
45-
context: TransformationContext,
46-
classDeclaration: ts.ClassLikeDeclarationBase
47-
): Generator<ts.ClassLikeDeclarationBase> {
48-
yield classDeclaration;
49-
50-
const extendsType = getExtendedType(context, classDeclaration);
51-
if (!extendsType) {
52-
return false;
53-
}
54-
55-
const symbol = extendsType.getSymbol();
56-
if (symbol === undefined) {
57-
return false;
58-
}
59-
60-
const symbolDeclarations = symbol.getDeclarations();
61-
if (symbolDeclarations === undefined) {
62-
return false;
63-
}
64-
65-
const declaration = symbolDeclarations.find(ts.isClassLike);
66-
if (!declaration) {
67-
return false;
68-
}
69-
70-
yield* classWithAncestors(context, declaration);
71-
}
72-
73-
export const hasMemberInClassOrAncestor = (
74-
context: TransformationContext,
75-
classDeclaration: ts.ClassLikeDeclarationBase,
76-
callback: (m: ts.ClassElement) => boolean
77-
) => [...classWithAncestors(context, classDeclaration)].some(c => c.members.some(callback));
78-
79-
function getPropertyName(propertyName: ts.PropertyName): string | number | undefined {
80-
if (ts.isIdentifier(propertyName) || ts.isStringLiteral(propertyName) || ts.isNumericLiteral(propertyName)) {
81-
return propertyName.text;
82-
} else {
83-
return undefined; // TODO: how to handle computed property names?
84-
}
85-
}
86-
87-
function isSamePropertyName(a: ts.PropertyName, b: ts.PropertyName): boolean {
88-
const aName = getPropertyName(a);
89-
const bName = getPropertyName(b);
90-
return aName !== undefined && aName === bName;
91-
}
92-
93-
export function isGetAccessorOverride(
94-
context: TransformationContext,
95-
element: ts.ClassElement,
96-
classDeclaration: ts.ClassLikeDeclarationBase
97-
): element is ts.GetAccessorDeclaration {
98-
if (!ts.isGetAccessor(element) || isStaticNode(element)) {
99-
return false;
100-
}
101-
102-
return hasMemberInClassOrAncestor(
103-
context,
104-
classDeclaration,
105-
m => ts.isPropertyDeclaration(m) && m.initializer !== undefined && isSamePropertyName(m.name, element.name)
106-
);
107-
}

src/transformation/visitors/class/members/constructor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as lua from "../../../../LuaAST";
33
import { TransformationContext } from "../../../context";
44
import { createSelfIdentifier } from "../../../utils/lua-ast";
55
import { popScope, pushScope, ScopeType } from "../../../utils/scope";
6-
import { transformFunctionBodyHeader, transformFunctionBodyContent, transformParameters } from "../../function";
6+
import { transformFunctionBodyContent, transformFunctionBodyHeader, transformParameters } from "../../function";
77
import { transformIdentifier } from "../../identifier";
88
import { transformClassInstanceFields } from "./fields";
99

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

46-
const classInstanceFields = transformClassInstanceFields(context, classDeclaration, instanceFields);
46+
const classInstanceFields = transformClassInstanceFields(context, instanceFields);
4747

4848
// If there are field initializers and the first statement is a super call,
4949
// move super call between default assignments and initializers

src/transformation/visitors/class/members/fields.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import * as lua from "../../../../LuaAST";
33
import { TransformationContext } from "../../../context";
44
import { createSelfIdentifier } from "../../../utils/lua-ast";
55
import { transformPropertyName } from "../../literal";
6-
import { isGetAccessorOverride } from "./accessors";
76
import { createDecoratingExpression, transformDecoratorExpression } from "../decorators";
87
import { transformMemberExpressionOwnerName } from "./method";
98

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

2827
export function transformClassInstanceFields(
2928
context: TransformationContext,
30-
classDeclaration: ts.ClassLikeDeclaration,
3129
instanceFields: ts.PropertyDeclaration[]
3230
): lua.Statement[] {
3331
const statements: lua.Statement[] = [];
@@ -47,26 +45,6 @@ export function transformClassInstanceFields(
4745
statements.push(assignClassField);
4846
}
4947

50-
// TODO: Remove when `useDefineForClassFields` would be `true` by default
51-
52-
const getOverrides = classDeclaration.members.filter((m): m is ts.GetAccessorDeclaration =>
53-
isGetAccessorOverride(context, m, classDeclaration)
54-
);
55-
56-
for (const getter of getOverrides) {
57-
const getterName = transformPropertyName(context, getter.name);
58-
59-
const resetGetter = lua.createExpressionStatement(
60-
lua.createCallExpression(lua.createIdentifier("rawset"), [
61-
createSelfIdentifier(),
62-
getterName,
63-
lua.createNilLiteral(),
64-
]),
65-
classDeclaration.members.find(ts.isConstructorDeclaration) ?? classDeclaration
66-
);
67-
statements.push(resetGetter);
68-
}
69-
7048
return statements;
7149
}
7250

0 commit comments

Comments
 (0)