2626 * - A file whose count rises, or a file that gains its first read, fails.
2727 * - A file whose count drops fails until the baseline is rewritten, so the baseline only shrinks.
2828 *
29- * Regenerate after fixing mappers: `bun run scripts/check-mapper-operation.ts --update`.
29+ * Regenerate after fixing mappers: `bun run scripts/check-mapper-operation.ts --update` (it refuses to
30+ * raise any file's count).
3031 *
3132 * Run: `bun run check:mapper-operation`
3233 */
@@ -160,6 +161,35 @@ function restAlias(pattern: ts.ObjectBindingPattern): string | undefined {
160161 return rest && ts . isIdentifier ( rest . name ) ? rest . name . text : undefined
161162}
162163
164+ /**
165+ * Whether `fn` returns its parameter `index` itself, or an object literal spreading it, so the
166+ * caller's result still carries the input's `operation`.
167+ */
168+ function returnsInput ( fn : Fn , index : number ) : boolean {
169+ const parameter = fn . parameters [ index ]
170+ if ( ! parameter || ! ts . isIdentifier ( parameter . name ) || ! fn . body ) return false
171+ const name = parameter . name . text
172+ const carries = ( expression : ts . Expression ) : boolean => {
173+ const value = unwrap ( expression )
174+ if ( ts . isIdentifier ( value ) ) return value . text === name
175+ return (
176+ ts . isObjectLiteralExpression ( value ) &&
177+ value . properties . some (
178+ ( property ) => ts . isSpreadAssignment ( property ) && carries ( property . expression )
179+ )
180+ )
181+ }
182+ if ( ! ts . isBlock ( fn . body ) ) return carries ( fn . body )
183+ let found = false
184+ const visit = ( node : ts . Node ) => {
185+ if ( found || ( node !== fn . body && isFn ( node ) ) ) return
186+ if ( ts . isReturnStatement ( node ) && node . expression && carries ( node . expression ) ) found = true
187+ ts . forEachChild ( node , visit )
188+ }
189+ visit ( fn . body )
190+ return found
191+ }
192+
163193/**
164194 * Counts operation reads from parameter `index` of `fn`: through aliases of it, and through
165195 * same-file helpers it is handed to (at whichever argument position), whose returned value is
@@ -187,19 +217,23 @@ function operationReads(source: ts.SourceFile, fn: Fn, index: number, seen: Set<
187217 return ts . isIdentifier ( value ) && aliases . has ( value . text )
188218 }
189219
190- /** Same-file helper calls that receive an alias; reads inside them are counted once per position. */
220+ /**
221+ * Counts reads inside a same-file helper at each argument position that receives an alias.
222+ * Returns whether the helper hands one of those inputs back (see {@link returnsInput}), which
223+ * makes its result an alias too.
224+ */
191225 const helperCall = ( node : ts . Expression , aliases : Set < string > ) : boolean => {
192226 const call = unwrap ( node )
193227 if ( ! ts . isCallExpression ( call ) || ! ts . isIdentifier ( call . expression ) ) return false
194228 const helper = findLocalFn ( source , call . expression . text )
195229 if ( ! helper ) return false
196- let passed = false
230+ let carriesInput = false
197231 call . arguments . forEach ( ( argument , position ) => {
198232 if ( ! isAlias ( argument , aliases ) ) return
199- passed = true
200233 reads += operationReads ( source , helper , position , seen )
234+ if ( returnsInput ( helper , position ) ) carriesInput = true
201235 } )
202- return passed
236+ return carriesInput
203237 }
204238
205239 const visit = ( node : ts . Node , inherited : Set < string > ) => {
@@ -217,15 +251,19 @@ function operationReads(source: ts.SourceFile, fn: Fn, index: number, seen: Set<
217251 ( property ) => ts . isSpreadAssignment ( property ) && isAlias ( property . expression , aliases )
218252 )
219253 const fromHelper = helperCall ( init , aliases )
220- if ( ( fromAlias || fromHelper ) && ts . isObjectBindingPattern ( node . name ) ) {
254+ if ( ( fromAlias || spreadsAlias || fromHelper ) && ts . isObjectBindingPattern ( node . name ) ) {
221255 reads += usedOperationBindings ( node . name )
222256 const rest = restAlias ( node . name )
223257 if ( rest ) inherited . add ( rest )
224258 } else if ( ( fromAlias || spreadsAlias || fromHelper ) && ts . isIdentifier ( node . name ) ) {
225259 inherited . add ( node . name . text )
226260 }
227- if ( fromHelper ) {
228- visit ( node . name , aliases )
261+ if (
262+ ts . isCallExpression ( init ) &&
263+ ts . isIdentifier ( init . expression ) &&
264+ findLocalFn ( source , init . expression . text )
265+ ) {
266+ for ( const argument of init . arguments ) visit ( argument , aliases )
229267 return
230268 }
231269 }
@@ -242,9 +280,12 @@ function operationReads(source: ts.SourceFile, fn: Fn, index: number, seen: Set<
242280 reads ++
243281 }
244282 }
245- if ( ts . isCallExpression ( node ) && helperCall ( node , aliases ) ) {
246- for ( const argument of node . arguments ) visit ( argument , aliases )
247- return
283+ if ( ts . isCallExpression ( node ) && ts . isIdentifier ( node . expression ) ) {
284+ if ( findLocalFn ( source , node . expression . text ) ) {
285+ helperCall ( node , aliases )
286+ for ( const argument of node . arguments ) visit ( argument , aliases )
287+ return
288+ }
248289 }
249290 ts . forEachChild ( node , ( child ) => visit ( child , aliases ) )
250291 }
@@ -300,16 +341,33 @@ function collect(): Record<string, number> {
300341
301342if ( import . meta. main ) {
302343 const current = collect ( )
344+ const init = process . argv . includes ( '--init' )
345+ if ( ! existsSync ( BASELINE ) && ! init ) {
346+ console . error (
347+ `✗ ${ path . relative ( ROOT , BASELINE ) } is missing. Restore it from git; create one only with --update --init.`
348+ )
349+ process . exit ( 1 )
350+ }
351+ const baseline : Record < string , number > = existsSync ( BASELINE )
352+ ? JSON . parse ( readFileSync ( BASELINE , 'utf8' ) )
353+ : current
354+
303355 if ( process . argv . includes ( '--update' ) ) {
356+ const raised = Object . entries ( current ) . filter ( ( [ file , count ] ) => count > ( baseline [ file ] ?? 0 ) )
357+ if ( raised . length ) {
358+ console . error (
359+ `✗ Refused to raise the baseline for ${ raised . length } file(s); fix them instead:`
360+ )
361+ for ( const [ file , count ] of raised )
362+ console . error ( ` ${ file } : ${ count } (baseline ${ baseline [ file ] ?? 0 } )` )
363+ process . exit ( 1 )
364+ }
304365 writeFileSync ( BASELINE , `${ JSON . stringify ( current , null , 2 ) } \n` )
305366 const total = Object . values ( current ) . reduce ( ( sum , count ) => sum + count , 0 )
306367 console . log ( `Wrote ${ Object . keys ( current ) . length } files (${ total } reads) to ${ BASELINE } ` )
307368 process . exit ( 0 )
308369 }
309370
310- const baseline : Record < string , number > = existsSync ( BASELINE )
311- ? JSON . parse ( readFileSync ( BASELINE , 'utf8' ) )
312- : { }
313371 const rose : string [ ] = [ ]
314372 const dropped : string [ ] = [ ]
315373 for ( const file of new Set ( [ ...Object . keys ( baseline ) , ...Object . keys ( current ) ] ) ) {
0 commit comments