@@ -3,10 +3,9 @@ import { LuaTarget } from "../../CompilerOptions";
33import * as lua from "../../LuaAST" ;
44import { TransformationContext } from "../context" ;
55import { getCurrentNamespace } from "../visitors/namespace" ;
6- import { UndefinedScope } from "./errors" ;
76import { createExportedIdentifier , getIdentifierExportScope } from "./export" ;
87import { findScope , peekScope , ScopeType } from "./scope" ;
9- import { isFirstDeclaration , isFunctionType } from "./typescript" ;
8+ import { isFunctionType } from "./typescript" ;
109
1110export type OneToManyVisitorResult < T extends lua . Node > = T | T [ ] | undefined ;
1211export function unwrapVisitorResult < T extends lua . Node > ( result : OneToManyVisitorResult < T > ) : T [ ] {
@@ -27,22 +26,6 @@ export function createExportsIdentifier(): lua.Identifier {
2726 return lua . createIdentifier ( "____exports" ) ;
2827}
2928
30- export function replaceStatementInParent ( oldNode : lua . Statement , newNode ?: lua . Statement ) : void {
31- if ( ! oldNode . parent ) {
32- throw new Error ( "node has not yet been assigned a parent" ) ;
33- }
34-
35- if ( lua . isBlock ( oldNode . parent ) || lua . isDoStatement ( oldNode . parent ) ) {
36- if ( newNode ) {
37- oldNode . parent . statements . splice ( oldNode . parent . statements . indexOf ( oldNode ) , 1 , newNode ) ;
38- } else {
39- oldNode . parent . statements . splice ( oldNode . parent . statements . indexOf ( oldNode ) , 1 ) ;
40- }
41- } else {
42- throw new Error ( "unexpected parent type" ) ;
43- }
44- }
45-
4629export function createExpressionPlusOne ( expression : lua . Expression ) : lua . Expression {
4730 if ( lua . isNumericLiteral ( expression ) ) {
4831 const newNode = lua . cloneNode ( expression ) ;
@@ -111,10 +94,9 @@ export function createHoistableVariableDeclarationStatement(
11194 context : TransformationContext ,
11295 identifier : lua . Identifier ,
11396 initializer ?: lua . Expression ,
114- tsOriginal ?: ts . Node ,
115- parent ?: lua . Node
97+ tsOriginal ?: ts . Node
11698) : lua . AssignmentStatement | lua . VariableDeclarationStatement {
117- const declaration = lua . createVariableDeclarationStatement ( identifier , initializer , tsOriginal , parent ) ;
99+ const declaration = lua . createVariableDeclarationStatement ( identifier , initializer , tsOriginal ) ;
118100 if ( ! context . options . noHoisting && identifier . symbolId ) {
119101 const scope = peekScope ( context ) ;
120102 if ( ! scope . variableDeclarations ) {
@@ -132,13 +114,13 @@ export function createLocalOrExportedOrGlobalDeclaration(
132114 lhs : lua . Identifier | lua . Identifier [ ] ,
133115 rhs ?: lua . Expression | lua . Expression [ ] ,
134116 tsOriginal ?: ts . Node ,
135- parent ?: lua . Node ,
136117 overrideExportScope ?: ts . SourceFile | ts . ModuleDeclaration
137118) : lua . Statement [ ] {
138119 let declaration : lua . VariableDeclarationStatement | undefined ;
139120 let assignment : lua . AssignmentStatement | undefined ;
140121
141- const functionDeclaration = tsOriginal && ts . isFunctionDeclaration ( tsOriginal ) ? tsOriginal : undefined ;
122+ const isVariableDeclaration = tsOriginal !== undefined && ts . isVariableDeclaration ( tsOriginal ) ;
123+ const isFunctionDeclaration = tsOriginal !== undefined && ts . isFunctionDeclaration ( tsOriginal ) ;
142124
143125 const identifiers = Array . isArray ( lhs ) ? lhs : [ lhs ] ;
144126 if ( identifiers . length === 0 ) {
@@ -154,48 +136,31 @@ export function createLocalOrExportedOrGlobalDeclaration(
154136 assignment = lua . createAssignmentStatement (
155137 identifiers . map ( identifier => createExportedIdentifier ( context , identifier , exportScope ) ) ,
156138 rhs ,
157- tsOriginal ,
158- parent
139+ tsOriginal
159140 ) ;
160141 }
161142 } else {
162143 const insideFunction = findScope ( context , ScopeType . Function ) !== undefined ;
163- let isLetOrConst = false ;
164- let isVariableFirstDeclaration = true ; // var can have multiple declarations for the same variable :/
165- if ( tsOriginal && ts . isVariableDeclaration ( tsOriginal ) && tsOriginal . parent ) {
166- isLetOrConst = ( tsOriginal . parent . flags & ( ts . NodeFlags . Let | ts . NodeFlags . Const ) ) !== 0 ;
167- isVariableFirstDeclaration = isLetOrConst || isFirstDeclaration ( context , tsOriginal ) ;
168- }
169144
170- if (
171- ( context . isModule || getCurrentNamespace ( context ) || insideFunction || isLetOrConst ) &&
172- isVariableFirstDeclaration
173- ) {
145+ if ( context . isModule || getCurrentNamespace ( context ) || insideFunction || isVariableDeclaration ) {
174146 // local
175147 const isPossibleWrappedFunction =
176- ! functionDeclaration &&
148+ ! isFunctionDeclaration &&
177149 tsOriginal &&
178150 ts . isVariableDeclaration ( tsOriginal ) &&
179151 tsOriginal . initializer &&
180152 isFunctionType ( context , context . checker . getTypeAtLocation ( tsOriginal . initializer ) ) ;
181153 if ( isPossibleWrappedFunction ) {
182154 // Split declaration and assignment for wrapped function types to allow recursion
183- declaration = lua . createVariableDeclarationStatement ( lhs , undefined , tsOriginal , parent ) ;
184- assignment = lua . createAssignmentStatement ( lhs , rhs , tsOriginal , parent ) ;
155+ declaration = lua . createVariableDeclarationStatement ( lhs , undefined , tsOriginal ) ;
156+ assignment = lua . createAssignmentStatement ( lhs , rhs , tsOriginal ) ;
185157 } else {
186- declaration = lua . createVariableDeclarationStatement ( lhs , rhs , tsOriginal , parent ) ;
158+ declaration = lua . createVariableDeclarationStatement ( lhs , rhs , tsOriginal ) ;
187159 }
188160
189161 if ( ! context . options . noHoisting ) {
190162 // Remember local variable declarations for hoisting later
191- const scope =
192- isLetOrConst || functionDeclaration
193- ? peekScope ( context )
194- : findScope ( context , ScopeType . Function | ScopeType . File ) ;
195-
196- if ( scope === undefined ) {
197- throw UndefinedScope ( ) ;
198- }
163+ const scope = peekScope ( context ) ;
199164
200165 if ( ! scope . variableDeclarations ) {
201166 scope . variableDeclarations = [ ] ;
@@ -205,13 +170,13 @@ export function createLocalOrExportedOrGlobalDeclaration(
205170 }
206171 } else if ( rhs ) {
207172 // global
208- assignment = lua . createAssignmentStatement ( lhs , rhs , tsOriginal , parent ) ;
173+ assignment = lua . createAssignmentStatement ( lhs , rhs , tsOriginal ) ;
209174 } else {
210175 return [ ] ;
211176 }
212177 }
213178
214- if ( ! context . options . noHoisting && functionDeclaration ) {
179+ if ( ! context . options . noHoisting && isFunctionDeclaration ) {
215180 // Remember function definitions for hoisting later
216181 const functionSymbolId = ( lhs as lua . Identifier ) . symbolId ;
217182 const scope = peekScope ( context ) ;
0 commit comments