216216// is transpiled with the same-file declarations it references (resolved from
217217// its enclosing scopes, transitively) and evaluated. Only the matcher and the
218218// declarations it names are executed -- never the test file, never a suite.
219+ // "Names" means its CODE names them: identifiers are read off the parsed tree,
220+ // so the words of a string, a template literal's text or a comment never pull
221+ // a sibling binding into the lift (#29860).
219222// Anything that cannot be lifted this way is UNJUDGED and must be declared;
220223// it is never treated as passing.
221224
@@ -316,7 +319,11 @@ function declaredName(node) {
316319 return null ;
317320}
318321
319- /** Same-file declarations visible from `node`, innermost scope first. */
322+ /**
323+ * Same-file declarations visible from `node`, innermost scope first. Each entry
324+ * carries the declaration's TEXT (what the lift executes) and the identifiers
325+ * its CODE names (what the lift follows), read off the tree rather than the text.
326+ */
320327function visibleDeclarations ( node , sf ) {
321328 const decls = new Map ( ) ;
322329 for ( let cur = node . parent ; cur ; cur = cur . parent ) {
@@ -328,22 +335,44 @@ function visibleDeclarations(node, sf) {
328335 if ( ts . isVariableStatement ( s ) ) {
329336 for ( const d of s . declarationList . declarations ) {
330337 if ( ts . isIdentifier ( d . name ) && d . initializer && ! decls . has ( d . name . text ) ) {
331- decls . set ( d . name . text , `const ${ d . name . text } = ${ d . initializer . getText ( sf ) } ;` ) ;
338+ decls . set ( d . name . text , {
339+ text : `const ${ d . name . text } = ${ d . initializer . getText ( sf ) } ;` ,
340+ identifiers : identifiersIn ( d . initializer ) ,
341+ } ) ;
332342 }
333343 }
334344 } else if ( ts . isFunctionDeclaration ( s ) && s . name && ! decls . has ( s . name . text ) ) {
335- decls . set ( s . name . text , s . getText ( sf ) ) ;
345+ decls . set ( s . name . text , { text : s . getText ( sf ) , identifiers : identifiersIn ( s ) } ) ;
336346 }
337347 }
338348 }
339349 return decls ;
340350}
341351
342- const identifiersIn = ( src ) => {
352+ /**
353+ * Every identifier the CODE under `node` names — and nothing its TEXT says
354+ * (#29860). Walked off the parsed tree, so a string literal, a template
355+ * literal's text between its `${…}` holes, a regular expression and a comment
356+ * contribute nothing, while an identifier inside a `${…}` hole is code and is
357+ * collected. Over-inclusive on purpose in one direction only: a property name
358+ * (`row.name`) is still an identifier node, and a name that resolves to no
359+ * declaration is skipped by the lift anyway.
360+ *
361+ * It used to be a word regex over the source text. A refusal message reading
362+ * `sparse driver: …` then pulled the sibling `const driver = { name, … }` into
363+ * the lift, `name` was a factory parameter the lift cannot supply, and a
364+ * CONFORMING matcher read UNJUDGED (`ReferenceError: name is not defined`) —
365+ * the gate's verdict hung on which English words a string contained.
366+ */
367+ function identifiersIn ( node ) {
343368 const out = new Set ( ) ;
344- for ( const m of src . matchAll ( / \b ( [ A - Z a - z _ $ ] [ A - Z a - z 0 - 9 _ $ ] * ) \b / g) ) out . add ( m [ 1 ] ) ;
369+ const visit = ( n ) => {
370+ if ( ts . isIdentifier ( n ) ) out . add ( n . text ) ;
371+ ts . forEachChild ( n , visit ) ;
372+ } ;
373+ visit ( node ) ;
345374 return out ;
346- } ;
375+ }
347376
348377/**
349378 * Does `body` read `id` as a filter object? The two spellings are exactly the
@@ -474,6 +503,7 @@ export function discoverInSource(text, label) {
474503 line : line + 1 ,
475504 name : declaredName ( node ) ?? '(anonymous)' ,
476505 source : node . getText ( sf ) ,
506+ sourceIdentifiers : identifiersIn ( node ) ,
477507 declarations : declarations ?? visibleDeclarations ( node , sf ) ,
478508 capturedPath : captured ? w : null ,
479509 } ) ;
@@ -498,15 +528,15 @@ function buildCallable(candidate, dropped = new Set()) {
498528 // cannot supply anyway (`query?.where ?? {}`).
499529 const skip = captured ? new Set ( [ ...dropped , captured [ 0 ] ] ) : dropped ;
500530 const included = new Map ( ) ;
501- let frontier = identifiersIn ( candidate . source ) ;
531+ let frontier = candidate . sourceIdentifiers ;
502532 for ( let depth = 0 ; depth < 6 ; depth ++ ) {
503533 const next = new Set ( ) ;
504534 for ( const id of frontier ) {
505535 if ( included . has ( id ) || id === self || skip . has ( id ) ) continue ;
506536 const decl = candidate . declarations . get ( id ) ;
507537 if ( ! decl ) continue ;
508- included . set ( id , decl ) ;
509- for ( const x of identifiersIn ( decl ) ) next . add ( x ) ;
538+ included . set ( id , decl . text ) ;
539+ for ( const x of decl . identifiers ) next . add ( x ) ;
510540 }
511541 if ( next . size === 0 ) break ;
512542 frontier = next ;
@@ -649,7 +679,7 @@ export function measure() {
649679 const bucket = measured . get ( rel ) ;
650680 if ( result . verdict === 'SILENT' ) bucket . silent ++ ;
651681 else bucket . unjudged ++ ;
652- bucket . details . push ( { ...candidate , declarations : undefined , result } ) ;
682+ bucket . details . push ( { ...candidate , declarations : undefined , sourceIdentifiers : undefined , result } ) ;
653683 }
654684 }
655685 return { measured, discovered, conforming, refusing } ;
@@ -844,6 +874,39 @@ const FIXTURE_CAPTURED_NEGATED = `
844874const run = (rows: any[], opts: any) =>
845875 rows.filter((r) => !Object.entries(opts.where).every(([k, v]) => r[k] === v));` ;
846876
877+ // --- #29860: the lift reads identifiers from code, never from text ---------
878+ // A conforming, refusing matcher inside a factory, beside a sibling binding the
879+ // lift cannot run (`driver` closes over the factory's `name` parameter). The
880+ // matcher's refusal MESSAGE says "sparse driver", so a lift that reads words
881+ // out of template-literal text pulls `driver` in and throws `name is not
882+ // defined`. The control word is in a plain string literal too, so neither
883+ // spelling of a string may name a binding into the lift.
884+ const FIXTURE_TEXT_NAMES_SIBLING = `
885+ function makeSparseDriver(name: string) {
886+ const matches = (row: any, where: any): boolean => {
887+ for (const [k, v] of Object.entries(where ?? {})) {
888+ if (k.startsWith('$')) throw new Error(\`sparse driver: filter operator \${k} is not implemented\`);
889+ if (v !== null && typeof v === 'object') throw new Error('sparse driver: operator is not implemented');
890+ if (row[k] !== v) return false;
891+ }
892+ return true;
893+ };
894+ const driver = { name, version: '0.0.0' };
895+ return { driver, matches };
896+ }` ;
897+
898+ // The other direction: an identifier inside a `\${…}` hole IS code. Here the
899+ // hole calls a same-file helper on the matcher's answering path, so a lift that
900+ // stopped reading holes would leave `label` undefined and the control probe
901+ // would throw — UNJUDGED, not CONFORMING.
902+ const FIXTURE_HOLE_NAMES_HELPER = `
903+ const label = (k: string) => k.toUpperCase();
904+ const matches = (row: any, where: any): boolean => {
905+ if (Array.isArray(where.$or) && !where.$or.some((w: any) => matches(row, w))) return false;
906+ if (Array.isArray(where.$and) && !where.$and.every((w: any) => matches(row, w))) return false;
907+ return Object.entries(where).every(([k, v]) => k.startsWith('$') || \`\${label(k)}\` === k.toUpperCase() && row[k] === v);
908+ };` ;
909+
847910function judgeFixture ( src ) {
848911 const found = discoverInSource ( src , 'fixture.test.ts' ) ;
849912 return { found, results : found . map ( judge ) } ;
@@ -876,11 +939,12 @@ const SELF_TEST_BATTERIES = Object.freeze({
876939 '#8615: the captured-filter arm' : 17 ,
877940 'the dispatch-gates declaration (#13163\'s landing obligation)' : 6 ,
878941 'the ratchet is ONE-SIDED: a decrease passes and is reported (#26444)' : 5 ,
942+ '#29860: the lift reads identifiers from code, never from text' : 7 ,
879943} ) ;
880944
881945// DELETING an entry silences that battery's floor exactly as effectively as
882946// zeroing it, so the roster's own size is pinned too.
883- const SELF_TEST_BATTERY_FLOOR = 4 ;
947+ const SELF_TEST_BATTERY_FLOOR = 5 ;
884948
885949// The key an assertion is filed under when no battery is open. It is not a
886950// declared battery, so it reds by the same set difference rather than silently
@@ -991,6 +1055,33 @@ function selfTest() {
9911055 expect ( 'a fallen count PASSES (one-sided, #26444)' , reconcile ( fakeMeasured , { 'a.test.ts' : { silent : 2 } } ) . length === 0 ) ;
9921056 expect ( 'a clean/gone entry PASSES (one-sided, #26444)' , reconcile ( new Map ( ) , { 'gone.test.ts' : { silent : 1 } } ) . length === 0 ) ;
9931057
1058+ // --- #29860: the lift reads identifiers from code, never from text -------
1059+ battery ( '#29860: the lift reads identifiers from code, never from text' ) ;
1060+ const textNames = judgeFixture ( FIXTURE_TEXT_NAMES_SIBLING ) ;
1061+ expect ( 'a matcher beside text naming a sibling binding is discovered' , textNames . found . length === 1 ) ;
1062+ expect (
1063+ 'template-literal TEXT names no identifier: "driver" in the message is not collected' ,
1064+ textNames . found [ 0 ] ?. sourceIdentifiers ?. has ( 'driver' ) === false ,
1065+ ) ;
1066+ expect (
1067+ 'nor does a plain string literal: its "implemented" is not collected either' ,
1068+ textNames . found [ 0 ] ?. sourceIdentifiers ?. has ( 'implemented' ) === false ,
1069+ ) ;
1070+ expect (
1071+ 'a matcher beside text naming a sibling binding lifts and is CONFORMING, not UNJUDGED' ,
1072+ textNames . results [ 0 ] ?. verdict === 'CONFORMING' && textNames . results [ 0 ] ?. refused === true ,
1073+ ) ;
1074+ const holeNames = judgeFixture ( FIXTURE_HOLE_NAMES_HELPER ) ;
1075+ expect ( 'a matcher calling a helper inside a `${…}` hole is discovered' , holeNames . found . length === 1 ) ;
1076+ expect (
1077+ 'the helper named only inside the hole is collected' ,
1078+ holeNames . found [ 0 ] ?. sourceIdentifiers ?. has ( 'label' ) === true ,
1079+ ) ;
1080+ expect (
1081+ 'so the hole-reading matcher lifts with its helper and is CONFORMING' ,
1082+ holeNames . results [ 0 ] ?. verdict === 'CONFORMING' ,
1083+ ) ;
1084+
9941085 // ── The ratchet is ONE-SIDED: a decrease passes and is reported (#26444) ──
9951086 battery ( 'the ratchet is ONE-SIDED: a decrease passes and is reported (#26444)' ) ;
9961087 expect ( 'a fallen count is reported once' , reconcileDown ( fakeMeasured , { 'a.test.ts' : { silent : 2 } } ) . length === 1 ) ;
@@ -1110,6 +1201,7 @@ function selfTest() {
11101201 ' parameter callbacks capturing their filter — by name and by access path — are\n' +
11111202 ' discovered and judged, while an unresolvable root, a computed filter and an\n' +
11121203 ' inverted survivor filter are each declined for their own recorded reason; the\n' +
1204+ ' lift follows identifiers its code names, never the words of its text; the\n' +
11131205 ' ledger reconciles one-sided (a decrease passes and is reported).' ,
11141206 ) ;
11151207
0 commit comments