@@ -18,6 +18,10 @@ import {
1818 BlockStatement ,
1919 FunctionParameter ,
2020 SourceLocation ,
21+ BinaryNode ,
22+ ReturnStatement ,
23+ IfStatement ,
24+ Expression ,
2125} from "../../ast/types.js" ;
2226import {
2327 ClosureAnalyzer ,
@@ -272,7 +276,7 @@ export class ArrowFunctionExpressionGenerator extends BaseGenerator {
272276 return "string" ;
273277 }
274278 if ( bodyTyped . type === "binary" ) {
275- const binExpr = body as { type : string ; op : string ; left : unknown ; right : unknown } ;
279+ const binExpr = body as BinaryNode ;
276280 if ( binExpr . op === "+" ) {
277281 const leftType = this . inferReturnTypeFromBody ( binExpr . left as ArrowFunctionNode [ "body" ] ) ;
278282 const rightType = this . inferReturnTypeFromBody ( binExpr . right as ArrowFunctionNode [ "body" ] ) ;
@@ -310,42 +314,38 @@ export class ArrowFunctionExpressionGenerator extends BaseGenerator {
310314 }
311315 }
312316 if ( bodyTyped . type === "block" ) {
313- const blockTyped = body as { type : string ; statements : unknown [ ] } ;
317+ const blockTyped = body as BlockStatement ;
314318 const blockStatements = blockTyped . statements ;
315319 if ( blockStatements ) {
316320 for ( let i = 0 ; i < blockStatements . length ; i ++ ) {
317321 const stmt = blockStatements [ i ] ;
318- const stmtTyped = stmt as { type : string ; value : unknown } ;
319- if ( stmtTyped . type === "return" && stmtTyped . value ) {
320- const returnValue = stmtTyped . value ;
321- const returnValueTyped = returnValue as { type : string } ;
322- if ( returnValueTyped . type === "object" ) {
323- return "object" ;
324- }
325- if (
326- returnValueTyped . type === "string" ||
327- returnValueTyped . type === "string_literal" ||
328- returnValueTyped . type === "template_literal"
329- ) {
330- return "string" ;
322+ const stmtBase = stmt as { type : string } ;
323+ if ( stmtBase . type === "return" ) {
324+ const stmtTyped = stmt as ReturnStatement ;
325+ if ( stmtTyped . value ) {
326+ const returnValueTyped = stmtTyped . value as { type : string } ;
327+ if ( returnValueTyped . type === "object" ) {
328+ return "object" ;
329+ }
330+ if (
331+ returnValueTyped . type === "string" ||
332+ returnValueTyped . type === "string_literal" ||
333+ returnValueTyped . type === "template_literal"
334+ ) {
335+ return "string" ;
336+ }
331337 }
332338 }
333- if ( stmtTyped . type === "if" ) {
334- const ifStmt = stmt as {
335- type : string ;
336- condition : unknown ;
337- thenBlock : unknown ;
338- elseBlock : unknown ;
339- } ;
340- const thenBlock = ifStmt . thenBlock as { type : string ; statements : unknown [ ] } ;
339+ if ( stmtBase . type === "if" ) {
340+ const ifStmt = stmt as IfStatement ;
341+ const thenBlock = ifStmt . thenBlock ;
341342 const thenBlockStatements = thenBlock ? thenBlock . statements : null ;
342343 if ( thenBlockStatements ) {
343344 for ( let j = 0 ; j < thenBlockStatements . length ; j ++ ) {
344345 const innerStmt = thenBlockStatements [ j ] ;
345- const innerStmtTyped = innerStmt as { type : string ; value : unknown } ;
346+ const innerStmtTyped = innerStmt as ReturnStatement ;
346347 if ( innerStmtTyped . type === "return" && innerStmtTyped . value ) {
347- const innerReturnValue = innerStmtTyped . value ;
348- const innerReturnValueTyped = innerReturnValue as { type : string } ;
348+ const innerReturnValueTyped = innerStmtTyped . value as { type : string } ;
349349 if ( innerReturnValueTyped . type === "object" ) {
350350 return "object" ;
351351 }
0 commit comments