@@ -2,6 +2,7 @@ import path from 'node:path';
22
33import { type ecmaVersion , Parser , type Program } from 'acorn' ;
44import { build , type BuildOptions } from 'esbuild' ;
5+ import { isArray } from 'remeda' ;
56
67import type {
78 GeneratorMutatorParsingInfo ,
@@ -101,7 +102,9 @@ function parseFunction(
101102) : GeneratorMutatorParsingInfo | undefined {
102103 const node = ast . body . find ( ( childNode ) => {
103104 if ( childNode . type === 'VariableDeclaration' ) {
104- return childNode . declarations . find ( ( d ) => d . id . name === funcName ) ;
105+ return childNode . declarations . find (
106+ ( d ) => d . id . type === 'Identifier' && d . id . name === funcName ,
107+ ) ;
105108 }
106109 if (
107110 childNode . type === 'FunctionDeclaration' &&
@@ -121,7 +124,7 @@ function parseFunction(
121124 ) ;
122125
123126 // If the function directly returns an arrow function
124- if ( returnStatement ?. argument ?. params ) {
127+ if ( returnStatement ?. argument && ' params' in returnStatement . argument ) {
125128 return {
126129 numberOfParams : node . params . length ,
127130 returnNumberOfParams : returnStatement . argument . params . length ,
@@ -141,42 +144,59 @@ function parseFunction(
141144 } ;
142145 }
143146
144- const declaration = node . declarations . find ( ( d ) => d . id . name === funcName ) ;
147+ const declaration =
148+ 'declarations' in node
149+ ? node . declarations . find (
150+ ( d ) => d . id . type === 'Identifier' && d . id . name === funcName ,
151+ )
152+ : undefined ;
145153
146- if ( declaration . init . name ) {
147- return parseFunction ( ast , declaration . init . name ) ;
148- }
154+ if ( declaration ?. init ) {
155+ if ( 'name' in declaration . init ) {
156+ return parseFunction ( ast , declaration . init . name ) ;
157+ }
149158
150- if ( declaration . init . body . type === 'ArrowFunctionExpression' ) {
151- return {
152- numberOfParams : declaration . init . params . length ,
153- returnNumberOfParams : declaration . init . body . params . length ,
154- } ;
155- }
159+ if (
160+ 'body' in declaration . init &&
161+ 'params' in declaration . init &&
162+ declaration . init . body . type === 'ArrowFunctionExpression'
163+ ) {
164+ return {
165+ numberOfParams : declaration . init . params . length ,
166+ returnNumberOfParams : declaration . init . body . params . length ,
167+ } ;
168+ }
156169
157- const returnStatement = declaration . init . body ?. body ?. find (
158- ( b : any ) => b . type === 'ReturnStatement' ,
159- ) ;
170+ const returnStatement =
171+ 'body' in declaration . init &&
172+ 'body' in declaration . init . body &&
173+ isArray ( declaration . init . body . body )
174+ ? declaration . init . body . body . find ( ( b ) => b . type === 'ReturnStatement' )
175+ : undefined ;
160176
161- if ( returnStatement ?. argument ?. params ) {
162- return {
163- numberOfParams : declaration . init . params . length ,
164- returnNumberOfParams : returnStatement . argument . params . length ,
165- } ;
166- } else if (
167- returnStatement ?. argument ?. type === 'CallExpression' &&
168- returnStatement . argument . arguments ?. [ 0 ] ?. type === 'ArrowFunctionExpression'
169- ) {
170- const arrowFn = returnStatement . argument . arguments [ 0 ] ;
171- return {
172- numberOfParams : declaration . init . params . length ,
173- returnNumberOfParams : arrowFn . params . length ,
174- } ;
175- }
177+ if ( 'params' in declaration . init ) {
178+ if ( returnStatement ?. argument && 'params' in returnStatement . argument ) {
179+ return {
180+ numberOfParams : declaration . init . params . length ,
181+ returnNumberOfParams : returnStatement . argument . params . length ,
182+ } ;
183+ } else if (
184+ returnStatement ?. argument ?. type === 'CallExpression' &&
185+ returnStatement . argument . arguments [ 0 ] ?. type ===
186+ 'ArrowFunctionExpression'
187+ ) {
188+ const arrowFn = returnStatement . argument . arguments [ 0 ] ;
189+ return {
190+ numberOfParams : declaration . init . params . length ,
191+ returnNumberOfParams : arrowFn . params . length ,
192+ } ;
193+ }
176194
177- return {
178- numberOfParams : declaration . init . params . length ,
179- } ;
195+ return {
196+ numberOfParams : declaration . init . params . length ,
197+ } ;
198+ }
199+ }
180200}
181201
182202function getEcmaVersion ( target ?: TsConfigTarget ) : ecmaVersion | undefined {
0 commit comments