77 extensionAndMetaExtensionConflict ,
88 extensionCannotExport ,
99 extensionCannotExtend ,
10- metaExtensionMissingExtends ,
11- luaTableMustBeAmbient ,
1210 luaTableCannotBeExtended ,
11+ luaTableMustBeAmbient ,
12+ metaExtensionMissingExtends ,
1313} from "../../utils/diagnostics" ;
1414import {
1515 createDefaultExportIdentifier ,
@@ -21,7 +21,6 @@ import {
2121import {
2222 createImmediatelyInvokedFunctionExpression ,
2323 createSelfIdentifier ,
24- OneToManyVisitorResult ,
2524 unwrapVisitorResult ,
2625} from "../../utils/lua-ast" ;
2726import { createSafeName , isUnsafeName } from "../../utils/safe-names" ;
@@ -38,25 +37,29 @@ import { checkForLuaLibType } from "./new";
3837import { createClassSetup } from "./setup" ;
3938import { getExtendedNode , getExtendedType , isStaticNode } from "./utils" ;
4039
40+ export const transformClassDeclaration : FunctionVisitor < ts . ClassLikeDeclaration > = ( declaration , context ) => {
41+ // If declaration is a default export, transform to export variable assignment instead
42+ if ( hasDefaultExportModifier ( declaration ) ) {
43+ const left = createExportedIdentifier ( context , createDefaultExportIdentifier ( declaration ) ) ;
44+ const right = transformClassAsExpression ( declaration , context ) ;
45+ return [ lua . createAssignmentStatement ( left , right , declaration ) ] ;
46+ }
47+
48+ const { statements } = transformClassLikeDeclaration ( declaration , context ) ;
49+ return statements ;
50+ } ;
51+
52+ export const transformThisExpression : FunctionVisitor < ts . ThisExpression > = node => createSelfIdentifier ( node ) ;
53+
4154export function transformClassAsExpression (
4255 expression : ts . ClassLikeDeclaration ,
43- context : TransformationContext ,
44- isDefaultExport = false
56+ context : TransformationContext
4557) : lua . Expression {
46- let className : lua . Identifier ;
47- if ( expression . name ) {
48- className = transformIdentifier ( context , expression . name ) ;
49- } else if ( isDefaultExport ) {
50- className = createDefaultExportIdentifier ( expression ) ;
51- } else {
52- className = lua . createAnonymousIdentifier ( ) ;
53- }
54-
5558 pushScope ( context , ScopeType . Function ) ;
56- const classDeclaration = unwrapVisitorResult ( transformClassDeclaration ( expression , context , className ) ) ;
59+ const { statements , name } = transformClassLikeDeclaration ( expression , context ) ;
5760 popScope ( context ) ;
5861
59- return createImmediatelyInvokedFunctionExpression ( classDeclaration , className , expression ) ;
62+ return createImmediatelyInvokedFunctionExpression ( unwrapVisitorResult ( statements ) , name , expression ) ;
6063}
6164
6265const classSuperInfos = new WeakMap < TransformationContext , ClassSuperInfo [ ] > ( ) ;
@@ -65,28 +68,19 @@ interface ClassSuperInfo {
6568 extendedTypeNode ?: ts . ExpressionWithTypeArguments ;
6669}
6770
68- export function transformClassDeclaration (
71+ function transformClassLikeDeclaration (
6972 classDeclaration : ts . ClassLikeDeclaration ,
7073 context : TransformationContext ,
7174 nameOverride ?: lua . Identifier
72- ) : OneToManyVisitorResult < lua . Statement > {
75+ ) : { statements : lua . Statement [ ] ; name : lua . Identifier } {
7376 let className : lua . Identifier ;
74- let classNameText : string ;
7577 if ( nameOverride !== undefined ) {
7678 className = nameOverride ;
77- classNameText = nameOverride . text ;
7879 } else if ( classDeclaration . name !== undefined ) {
7980 className = transformIdentifier ( context , classDeclaration . name ) ;
80- classNameText = classDeclaration . name . text ;
81- } else if ( hasDefaultExportModifier ( classDeclaration ) ) {
82- const left = createExportedIdentifier ( context , createDefaultExportIdentifier ( classDeclaration ) ) ;
83- const right = transformClassAsExpression ( classDeclaration , context , true ) ;
84-
85- return lua . createAssignmentStatement ( left , right , classDeclaration ) ;
8681 } else {
8782 // TypeScript error
8883 className = lua . createAnonymousIdentifier ( ) ;
89- classNameText = className . text ;
9084 }
9185
9286 const annotations = getTypeAnnotations ( context . checker . getTypeAtLocation ( classDeclaration ) ) ;
@@ -196,9 +190,7 @@ export function transformClassDeclaration(
196190 }
197191
198192 if ( ! isExtension && ! isMetaExtension ) {
199- result . push (
200- ...createClassSetup ( context , classDeclaration , className , localClassName , classNameText , extendedType )
201- ) ;
193+ result . push ( ...createClassSetup ( context , classDeclaration , className , localClassName , extendedType ) ) ;
202194 } else {
203195 for ( const f of instanceFields ) {
204196 const fieldName = transformPropertyName ( context , f . name ) ;
@@ -317,7 +309,7 @@ export function transformClassDeclaration(
317309
318310 superInfo . pop ( ) ;
319311
320- return result ;
312+ return { statements : result , name : className } ;
321313}
322314
323315export const transformSuperExpression : FunctionVisitor < ts . SuperExpression > = ( expression , context ) => {
@@ -345,5 +337,3 @@ export const transformSuperExpression: FunctionVisitor<ts.SuperExpression> = (ex
345337
346338 return lua . createTableIndexExpression ( baseClassName , lua . createStringLiteral ( "prototype" ) ) ;
347339} ;
348-
349- export const transformThisExpression : FunctionVisitor < ts . ThisExpression > = node => createSelfIdentifier ( node ) ;
0 commit comments