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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
56 changes: 23 additions & 33 deletions src/transformation/visitors/class/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
extensionAndMetaExtensionConflict,
extensionCannotExport,
extensionCannotExtend,
metaExtensionMissingExtends,
luaTableMustBeAmbient,
luaTableCannotBeExtended,
luaTableMustBeAmbient,
metaExtensionMissingExtends,
} from "../../utils/diagnostics";
import {
createDefaultExportIdentifier,
Expand All @@ -21,7 +21,6 @@ import {
import {
createImmediatelyInvokedFunctionExpression,
createSelfIdentifier,
OneToManyVisitorResult,
unwrapVisitorResult,
} from "../../utils/lua-ast";
import { createSafeName, isUnsafeName } from "../../utils/safe-names";
Expand All @@ -38,25 +37,29 @@ import { checkForLuaLibType } from "./new";
import { createClassSetup } from "./setup";
import { getExtendedNode, getExtendedType, isStaticNode } from "./utils";

export const transformClassDeclaration: FunctionVisitor<ts.ClassLikeDeclaration> = (declaration, context) => {
// If declaration is a default export, transform to export variable assignment instead
if (hasDefaultExportModifier(declaration)) {
const left = createExportedIdentifier(context, createDefaultExportIdentifier(declaration));
const right = transformClassAsExpression(declaration, context);
return [lua.createAssignmentStatement(left, right, declaration)];
}

const { statements } = transformClassLikeDeclaration(declaration, context);
return statements;
};

export const transformThisExpression: FunctionVisitor<ts.ThisExpression> = node => createSelfIdentifier(node);

export function transformClassAsExpression(
expression: ts.ClassLikeDeclaration,
context: TransformationContext,
isDefaultExport = false
context: TransformationContext
): lua.Expression {
let className: lua.Identifier;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this? Test this.

if (expression.name) {
className = transformIdentifier(context, expression.name);
} else if (isDefaultExport) {
className = createDefaultExportIdentifier(expression);
} else {
className = lua.createAnonymousIdentifier();
}

pushScope(context, ScopeType.Function);
const classDeclaration = unwrapVisitorResult(transformClassDeclaration(expression, context, className));
const { statements, name } = transformClassLikeDeclaration(expression, context);
popScope(context);

return createImmediatelyInvokedFunctionExpression(classDeclaration, className, expression);
return createImmediatelyInvokedFunctionExpression(unwrapVisitorResult(statements), name, expression);
}

const classSuperInfos = new WeakMap<TransformationContext, ClassSuperInfo[]>();
Expand All @@ -65,28 +68,19 @@ interface ClassSuperInfo {
extendedTypeNode?: ts.ExpressionWithTypeArguments;
}

export function transformClassDeclaration(
function transformClassLikeDeclaration(
classDeclaration: ts.ClassLikeDeclaration,
context: TransformationContext,
Comment on lines 72 to 73
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency:

Suggested change
classDeclaration: ts.ClassLikeDeclaration,
context: TransformationContext,
context: TransformationContext,
classDeclaration: ts.ClassLikeDeclaration,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it is consistent right now with transformClassDeclaration and transformClassAsExpression

nameOverride?: lua.Identifier
): OneToManyVisitorResult<lua.Statement> {
): { statements: lua.Statement[]; name: lua.Identifier } {
let className: lua.Identifier;
let classNameText: string;
if (nameOverride !== undefined) {
className = nameOverride;
classNameText = nameOverride.text;
} else if (classDeclaration.name !== undefined) {
className = transformIdentifier(context, classDeclaration.name);
classNameText = classDeclaration.name.text;
} else if (hasDefaultExportModifier(classDeclaration)) {
const left = createExportedIdentifier(context, createDefaultExportIdentifier(classDeclaration));
const right = transformClassAsExpression(classDeclaration, context, true);

return lua.createAssignmentStatement(left, right, classDeclaration);
} else {
// TypeScript error
className = lua.createAnonymousIdentifier();
classNameText = className.text;
}

const annotations = getTypeAnnotations(context.checker.getTypeAtLocation(classDeclaration));
Expand Down Expand Up @@ -196,9 +190,7 @@ export function transformClassDeclaration(
}

if (!isExtension && !isMetaExtension) {
result.push(
...createClassSetup(context, classDeclaration, className, localClassName, classNameText, extendedType)
);
result.push(...createClassSetup(context, classDeclaration, className, localClassName, extendedType));
} else {
for (const f of instanceFields) {
const fieldName = transformPropertyName(context, f.name);
Expand Down Expand Up @@ -317,7 +309,7 @@ export function transformClassDeclaration(

superInfo.pop();

return result;
return { statements: result, name: className };
}

export const transformSuperExpression: FunctionVisitor<ts.SuperExpression> = (expression, context) => {
Expand Down Expand Up @@ -345,5 +337,3 @@ export const transformSuperExpression: FunctionVisitor<ts.SuperExpression> = (ex

return lua.createTableIndexExpression(baseClassName, lua.createStringLiteral("prototype"));
};

export const transformThisExpression: FunctionVisitor<ts.ThisExpression> = node => createSelfIdentifier(node);
23 changes: 21 additions & 2 deletions src/transformation/visitors/class/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export function createClassSetup(
statement: ts.ClassLikeDeclarationBase,
className: lua.Identifier,
localClassName: lua.Identifier,
classNameText: string,
extendsType?: ts.Type
): lua.Statement[] {
const result: lua.Statement[] = [];
Expand Down Expand Up @@ -56,7 +55,7 @@ export function createClassSetup(
result.push(
lua.createAssignmentStatement(
lua.createTableIndexExpression(lua.cloneIdentifier(localClassName), lua.createStringLiteral("name")),
lua.createStringLiteral(classNameText),
getReflectionClassName(context, statement, className),
statement
)
);
Expand All @@ -79,3 +78,23 @@ export function createClassSetup(

return result;
}

export function getReflectionClassName(
context: TransformationContext,
declaration: ts.ClassLikeDeclarationBase,
className: lua.Identifier
): lua.Expression {
if (declaration.name) {
return lua.createStringLiteral(declaration.name.text);
} else if (ts.isVariableDeclaration(declaration.parent) && ts.isIdentifier(declaration.parent.name)) {
return lua.createStringLiteral(declaration.parent.name.text);
} else if (hasDefaultExportModifier(declaration)) {
return lua.createStringLiteral("default");
}

if (getExtendedNode(context, declaration)) {
return lua.createTableIndexExpression(className, lua.createStringLiteral("name"));
}

return lua.createStringLiteral("");
}
2 changes: 1 addition & 1 deletion test/unit/classes/__snapshots__/classes.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`missing declaration name: code 1`] = `
"require(\\"lualib_bundle\\");
____ = __TS__Class()
____.name = \\"____\\"
____.name = \\"\\"
function ____.prototype.____constructor(self)
end"
`;
Expand Down
Loading