@@ -97,10 +97,27 @@ function findLocalFn(source: ts.SourceFile, name: string): Fn | undefined {
9797 return found
9898}
9999
100- /** Identifier uses of `name` inside `scope`, excluding declarations and property-name positions. */
100+ /** Names a binding pattern or identifier declares. */
101+ function boundNames ( name : ts . BindingName , into : Set < string > = new Set ( ) ) : Set < string > {
102+ if ( ts . isIdentifier ( name ) ) into . add ( name . text )
103+ else
104+ for ( const element of name . elements )
105+ if ( ! ts . isOmittedExpression ( element ) ) boundNames ( element . name , into )
106+ return into
107+ }
108+
109+ /** Names a nested function's own parameters declare, which shadow the outer ones in its body. */
110+ function shadowedBy ( node : ts . Node ) : Set < string > {
111+ const names = new Set < string > ( )
112+ if ( isFn ( node ) ) for ( const parameter of node . parameters ) boundNames ( parameter . name , names )
113+ return names
114+ }
115+
116+ /** Identifier uses of `name` inside `scope`, excluding declarations, property names, and shadowed scopes. */
101117function referencesOf ( scope : ts . Node , name : string , declaration : ts . Node ) : number {
102118 let count = 0
103119 const visit = ( node : ts . Node ) => {
120+ if ( node !== scope && shadowedBy ( node ) . has ( name ) ) return
104121 if ( ts . isIdentifier ( node ) && node . text === name && node !== declaration ) {
105122 const parent = node . parent
106123 const isPropertyName =
@@ -143,62 +160,95 @@ function restAlias(pattern: ts.ObjectBindingPattern): string | undefined {
143160 return rest && ts . isIdentifier ( rest . name ) ? rest . name . text : undefined
144161}
145162
146- function operationReads ( source : ts . SourceFile , fn : Fn , seen : Set < Fn > ) : number {
147- if ( seen . has ( fn ) ) return 0
148- seen . add ( fn )
149- const first = fn . parameters [ 0 ]
150- if ( ! first ) return 0
151- if ( ts . isObjectBindingPattern ( first . name ) ) return usedOperationBindings ( first . name )
152- if ( ! ts . isIdentifier ( first . name ) || ! fn . body ) return 0
163+ /**
164+ * Counts operation reads from parameter `index` of `fn`: through aliases of it, and through
165+ * same-file helpers it is handed to (at whichever argument position), whose returned value is
166+ * treated as an alias too. Names a nested function rebinds are not aliases inside it.
167+ */
168+ function operationReads ( source : ts . SourceFile , fn : Fn , index : number , seen : Set < string > ) : number {
169+ const key = `${ fn . pos } :${ index } `
170+ if ( seen . has ( key ) ) return 0
171+ seen . add ( key )
172+ const parameter = fn . parameters [ index ]
173+ if ( ! parameter || ! fn . body ) return 0
153174
154- const aliases = new Set ( [ first . name . text ] )
155175 let reads = 0
156- const visit = ( node : ts . Node ) => {
176+ const initial = new Set < string > ( )
177+ if ( ts . isIdentifier ( parameter . name ) ) initial . add ( parameter . name . text )
178+ else if ( ts . isObjectBindingPattern ( parameter . name ) ) {
179+ reads += usedOperationBindings ( parameter . name )
180+ const rest = restAlias ( parameter . name )
181+ if ( rest ) initial . add ( rest )
182+ }
183+ if ( initial . size === 0 ) return reads
184+
185+ const isAlias = ( expression : ts . Expression , aliases : Set < string > ) => {
186+ const value = unwrap ( expression )
187+ return ts . isIdentifier ( value ) && aliases . has ( value . text )
188+ }
189+
190+ /** Same-file helper calls that receive an alias; reads inside them are counted once per position. */
191+ const helperCall = ( node : ts . Expression , aliases : Set < string > ) : boolean => {
192+ const call = unwrap ( node )
193+ if ( ! ts . isCallExpression ( call ) || ! ts . isIdentifier ( call . expression ) ) return false
194+ const helper = findLocalFn ( source , call . expression . text )
195+ if ( ! helper ) return false
196+ let passed = false
197+ call . arguments . forEach ( ( argument , position ) => {
198+ if ( ! isAlias ( argument , aliases ) ) return
199+ passed = true
200+ reads += operationReads ( source , helper , position , seen )
201+ } )
202+ return passed
203+ }
204+
205+ const visit = ( node : ts . Node , inherited : Set < string > ) => {
206+ let aliases = inherited
207+ if ( node !== fn ) {
208+ const shadowed = shadowedBy ( node )
209+ if ( shadowed . size ) aliases = new Set ( [ ...inherited ] . filter ( ( name ) => ! shadowed . has ( name ) ) )
210+ }
157211 if ( ts . isVariableDeclaration ( node ) && node . initializer ) {
158212 const init = unwrap ( node . initializer )
159- const fromAlias = ts . isIdentifier ( init ) && aliases . has ( init . text )
213+ const fromAlias = isAlias ( init , aliases )
160214 const spreadsAlias =
161215 ts . isObjectLiteralExpression ( init ) &&
162- init . properties . some ( ( property ) => {
163- if ( ! ts . isSpreadAssignment ( property ) ) return false
164- const spread = unwrap ( property . expression )
165- return ts . isIdentifier ( spread ) && aliases . has ( spread . text )
166- } )
167- if ( fromAlias && ts . isObjectBindingPattern ( node . name ) ) {
216+ init . properties . some (
217+ ( property ) => ts . isSpreadAssignment ( property ) && isAlias ( property . expression , aliases )
218+ )
219+ const fromHelper = helperCall ( init , aliases )
220+ if ( ( fromAlias || fromHelper ) && ts . isObjectBindingPattern ( node . name ) ) {
168221 reads += usedOperationBindings ( node . name )
169222 const rest = restAlias ( node . name )
170- if ( rest ) aliases . add ( rest )
171- } else if ( ( fromAlias || spreadsAlias ) && ts . isIdentifier ( node . name ) ) {
172- aliases . add ( node . name . text )
223+ if ( rest ) inherited . add ( rest )
224+ } else if ( ( fromAlias || spreadsAlias || fromHelper ) && ts . isIdentifier ( node . name ) ) {
225+ inherited . add ( node . name . text )
226+ }
227+ if ( fromHelper ) {
228+ visit ( node . name , aliases )
229+ return
173230 }
174231 }
175232 if ( ts . isPropertyAccessExpression ( node ) && node . name . text === 'operation' ) {
176- const target = unwrap ( node . expression )
177- if ( ts . isIdentifier ( target ) && aliases . has ( target . text ) ) reads ++
233+ if ( isAlias ( node . expression , aliases ) ) reads ++
178234 }
179235 if ( ts . isElementAccessExpression ( node ) ) {
180- const target = unwrap ( node . expression )
181236 const argument = node . argumentExpression
182237 if (
183- ts . isIdentifier ( target ) &&
184- aliases . has ( target . text ) &&
238+ isAlias ( node . expression , aliases ) &&
185239 ts . isStringLiteralLike ( argument ) &&
186240 argument . text === 'operation'
187241 ) {
188242 reads ++
189243 }
190244 }
191- if ( ts . isCallExpression ( node ) && ts . isIdentifier ( node . expression ) ) {
192- const passesParams = node . arguments . some ( ( argument ) => {
193- const value = unwrap ( argument )
194- return ts . isIdentifier ( value ) && aliases . has ( value . text )
195- } )
196- const helper = passesParams ? findLocalFn ( source , node . expression . text ) : undefined
197- if ( helper ) reads += operationReads ( source , helper , seen )
245+ if ( ts . isCallExpression ( node ) && helperCall ( node , aliases ) ) {
246+ for ( const argument of node . arguments ) visit ( argument , aliases )
247+ return
198248 }
199- ts . forEachChild ( node , visit )
249+ ts . forEachChild ( node , ( child ) => visit ( child , aliases ) )
200250 }
201- visit ( fn . body )
251+ visit ( fn . body , initial )
202252 return reads
203253}
204254
@@ -228,7 +278,7 @@ export function countMapperOperationReads(fileName: string, text: string): numbe
228278 } else if ( ts . isShorthandPropertyAssignment ( property ) ) {
229279 mapper = findLocalFn ( source , property . name . text )
230280 }
231- if ( mapper ) reads += operationReads ( source , mapper , new Set ( ) )
281+ if ( mapper ) reads += operationReads ( source , mapper , 0 , new Set ( ) )
232282 }
233283 }
234284 ts . forEachChild ( node , visit )
0 commit comments