@@ -112,6 +112,10 @@ function isBlock(node: ts.Node): node is ts.Block {
112
112
return node . kind === ts . SyntaxKind . Block ;
113
113
}
114
114
115
+ function isClassDeclaration ( node : ts . Node ) : node is ts . ClassDeclaration {
116
+ return node . kind === ts . SyntaxKind . ClassDeclaration ;
117
+ }
118
+
115
119
// Determine if a node matched the structure of a downleveled TS class.
116
120
function isDownleveledClass ( node : ts . Node ) : boolean {
117
121
@@ -169,17 +173,28 @@ function isDownleveledClass(node: ts.Node): boolean {
169
173
const className = variableDeclaration . name . text ;
170
174
171
175
const firstStatement = functionStatements [ 0 ] ;
172
- const lastStatement = functionStatements [ functionStatements . length - 1 ] ;
176
+
177
+ // find return statement - may not be last statement
178
+ let returnStatement : ts . ReturnStatement | undefined ;
179
+ for ( let i = functionStatements . length - 1 ; i > 0 ; i -- ) {
180
+ if ( isReturnStatement ( functionStatements [ i ] ) ) {
181
+ returnStatement = functionStatements [ i ] as ts . ReturnStatement ;
182
+ break ;
183
+ }
184
+ }
185
+
186
+ if ( returnStatement == undefined
187
+ || returnStatement . expression == undefined
188
+ || ! isIdentifier ( returnStatement . expression ) ) {
189
+ return false ;
190
+ }
173
191
174
192
if ( functionExpression . parameters . length === 0 ) {
175
- // potential non-extended class
176
- return isFunctionDeclaration ( firstStatement )
193
+ // potential non-extended class or wrapped es2015 class
194
+ return ( isFunctionDeclaration ( firstStatement ) || isClassDeclaration ( firstStatement ) )
177
195
&& firstStatement . name !== undefined
178
196
&& firstStatement . name . text === className
179
- && isReturnStatement ( lastStatement )
180
- && lastStatement . expression != undefined
181
- && isIdentifier ( lastStatement . expression )
182
- && lastStatement . expression . text === firstStatement . name . text ;
197
+ && returnStatement . expression . text === firstStatement . name . text ;
183
198
} else if ( functionExpression . parameters . length !== 1 ) {
184
199
return false ;
185
200
}
@@ -222,8 +237,5 @@ function isDownleveledClass(node: ts.Node): boolean {
222
237
return isFunctionDeclaration ( secondStatement )
223
238
&& secondStatement . name !== undefined
224
239
&& secondStatement . name . text === className
225
- && isReturnStatement ( lastStatement )
226
- && lastStatement . expression !== undefined
227
- && isIdentifier ( lastStatement . expression )
228
- && lastStatement . expression . text === secondStatement . name . text ;
240
+ && returnStatement . expression . text === secondStatement . name . text ;
229
241
}
0 commit comments