@@ -995,7 +995,7 @@ function convertVariable(
995995 ) {
996996 if (
997997 comment ?. hasModifier ( "@function" ) ||
998- ! ( declaration && ts . isVariableDeclaration ( declaration ) && declaration . type )
998+ ( declaration && shouldAutomaticallyConvertAsFunction ( declaration ) )
999999 ) {
10001000 return convertVariableAsFunction ( context , symbol , exportSymbol ) ;
10011001 }
@@ -1418,3 +1418,48 @@ function setSymbolModifiers(symbol: ts.Symbol, reflection: Reflection) {
14181418 hasAllFlags ( symbol . flags , ts . SymbolFlags . Optional ) ,
14191419 ) ;
14201420}
1421+
1422+ function shouldAutomaticallyConvertAsFunction ( node : ts . Declaration ) : boolean {
1423+ // const fn = () => {}
1424+ if ( ts . isVariableDeclaration ( node ) ) {
1425+ if ( node . type || ! node . initializer ) return false ;
1426+
1427+ return isFunctionLikeInitializer ( node . initializer ) ;
1428+ }
1429+
1430+ // { fn: () => {} }
1431+ if ( ts . isPropertyAssignment ( node ) ) {
1432+ return isFunctionLikeInitializer ( node . initializer ) ;
1433+ }
1434+
1435+ // exports.fn = () => {}
1436+ // exports.fn ||= () => {}
1437+ // exports.fn ??= () => {}
1438+ if ( ts . isPropertyAccessExpression ( node ) ) {
1439+ if (
1440+ ts . isBinaryExpression ( node . parent ) &&
1441+ [ ts . SyntaxKind . EqualsToken , ts . SyntaxKind . BarBarEqualsToken , ts . SyntaxKind . QuestionQuestionEqualsToken ]
1442+ . includes ( node . parent . operatorToken . kind )
1443+ ) {
1444+ return isFunctionLikeInitializer ( node . parent . right ) ;
1445+ }
1446+ }
1447+
1448+ return false ;
1449+ }
1450+
1451+ function isFunctionLikeInitializer ( node : ts . Expression ) : boolean {
1452+ if ( ts . isFunctionExpression ( node ) || ts . isArrowFunction ( node ) ) {
1453+ return true ;
1454+ }
1455+
1456+ if ( ts . isSatisfiesExpression ( node ) ) {
1457+ return isFunctionLikeInitializer ( node . expression ) ;
1458+ }
1459+
1460+ if ( ts . isAsExpression ( node ) ) {
1461+ return isFunctionLikeInitializer ( node . expression ) ;
1462+ }
1463+
1464+ return false ;
1465+ }
0 commit comments