@@ -24,19 +24,25 @@ import { transformBindingPattern } from "./variable-declaration";
2424function transformParameterDefaultValueDeclaration (
2525 context : TransformationContext ,
2626 parameterName : lua . Identifier ,
27- value ? : ts . Expression ,
27+ value : ts . Expression ,
2828 tsOriginal ?: ts . Node
29- ) : lua . Statement {
30- const parameterValue = value ? context . transformExpression ( value ) : undefined ;
31- const assignment = lua . createAssignmentStatement ( parameterName , parameterValue ) ;
29+ ) : lua . Statement | undefined {
30+ const { precedingStatements : statements , result : parameterValue } = transformInPrecedingStatementScope (
31+ context ,
32+ ( ) => context . transformExpression ( value )
33+ ) ;
34+ if ( ! lua . isNilLiteral ( parameterValue ) ) {
35+ statements . push ( lua . createAssignmentStatement ( parameterName , parameterValue ) ) ;
36+ }
37+ if ( statements . length === 0 ) return undefined ;
3238
3339 const nilCondition = lua . createBinaryExpression (
3440 parameterName ,
3541 lua . createNilLiteral ( ) ,
3642 lua . SyntaxKind . EqualityOperator
3743 ) ;
3844
39- const ifBlock = lua . createBlock ( [ assignment ] ) ;
45+ const ifBlock = lua . createBlock ( statements , tsOriginal ) ;
4046
4147 return lua . createIfStatement ( nilCondition , ifBlock , undefined , tsOriginal ) ;
4248}
@@ -106,7 +112,7 @@ export function transformFunctionBodyHeader(
106112 parameters : ts . NodeArray < ts . ParameterDeclaration > ,
107113 spreadIdentifier ?: lua . Identifier
108114) : lua . Statement [ ] {
109- const headerStatements = [ ] ;
115+ const headerStatements : lua . Statement [ ] = [ ] ;
110116
111117 // Add default parameters and object binding patterns
112118 const bindingPatternDeclarations : lua . Statement [ ] = [ ] ;
@@ -116,9 +122,12 @@ export function transformFunctionBodyHeader(
116122 const identifier = lua . createIdentifier ( `____bindingPattern${ bindPatternIndex ++ } ` ) ;
117123 if ( declaration . initializer !== undefined ) {
118124 // Default binding parameter
119- headerStatements . push (
120- transformParameterDefaultValueDeclaration ( context , identifier , declaration . initializer )
125+ const initializer = transformParameterDefaultValueDeclaration (
126+ context ,
127+ identifier ,
128+ declaration . initializer
121129 ) ;
130+ if ( initializer ) headerStatements . push ( initializer ) ;
122131 }
123132
124133 // Binding pattern
@@ -129,13 +138,12 @@ export function transformFunctionBodyHeader(
129138 bindingPatternDeclarations . push ( ...precedingStatements , ...bindings ) ;
130139 } else if ( declaration . initializer !== undefined ) {
131140 // Default parameter
132- headerStatements . push (
133- transformParameterDefaultValueDeclaration (
134- context ,
135- transformIdentifier ( context , declaration . name ) ,
136- declaration . initializer
137- )
141+ const initializer = transformParameterDefaultValueDeclaration (
142+ context ,
143+ transformIdentifier ( context , declaration . name ) ,
144+ declaration . initializer
138145 ) ;
146+ if ( initializer ) headerStatements . push ( initializer ) ;
139147 }
140148 }
141149
0 commit comments