@@ -70,6 +70,7 @@ import { mkdtempSync, rmSync, readFileSync, existsSync } from 'node:fs';
7070import { tmpdir } from 'node:os' ;
7171import { join , resolve } from 'node:path' ;
7272import { fileURLToPath } from 'node:url' ;
73+ import { maskComments } from '../../../scripts/js-comment-mask.mjs' ;
7374import { childEnv } from './helpers/serve-process.js' ;
7475
7576const HERE = resolve ( fileURLToPath ( import . meta. url ) , '..' ) ;
@@ -440,25 +441,81 @@ describe('os cloud login --json — the declared NDJSON stream (#6730)', () => {
440441 } ) ;
441442} ) ;
442443
444+ /**
445+ * The `{ … }` body of a declaration, brace-matched from its own `(`, as a span
446+ * in the MASKED source — so a `{` inside a comment cannot close it early and
447+ * the offsets are still the file's own line numbers.
448+ */
449+ function bodySpan ( src : string , declaration : RegExp ) : { start : number ; end : number } | null {
450+ const m = declaration . exec ( src ) ;
451+ if ( ! m ) return null ;
452+ let i = src . indexOf ( '(' , m . index ) ;
453+ let depth = 0 ;
454+ for ( ; i < src . length ; i ++ ) {
455+ if ( src [ i ] === '(' ) depth ++ ;
456+ else if ( src [ i ] === ')' ) {
457+ depth -- ;
458+ if ( depth === 0 ) break ;
459+ }
460+ }
461+ const open = src . indexOf ( '{' , i ) ;
462+ if ( open === - 1 ) return null ;
463+ depth = 0 ;
464+ for ( let j = open ; j < src . length ; j ++ ) {
465+ if ( src [ j ] === '{' ) depth ++ ;
466+ else if ( src [ j ] === '}' ) {
467+ depth -- ;
468+ if ( depth === 0 ) return { start : open , end : j } ;
469+ }
470+ }
471+ return null ;
472+ }
473+
443474describe ( 'the exception stays declared, not just implemented (#6730 ruling)' , ( ) => {
444- const cloudLoginSrc = ( ) => readFileSync ( CLOUD_LOGIN_SRC , 'utf-8' ) ;
475+ // Masked before anything is read: every case in this describe decides from
476+ // the TEXT of `cloud/login.ts`, and a docblock that quotes `emitJson(` — the natural
477+ // way to explain why one emitter exists — is indistinguishable from a call to
478+ // it in a raw read (#18520).
479+ const cloudLoginSrc = ( ) => maskComments ( readFileSync ( CLOUD_LOGIN_SRC , 'utf-8' ) ) ;
445480
446481 it ( 'routes every --json write through the single compact emitter' , ( ) => {
447482 // The contract is "one document per line" for the WHOLE command, so a new
448483 // write that called `emitJson` directly could reintroduce a multi-line
449484 // record on a path the e2e above does not drive. One emitter is what makes
450485 // that structurally impossible; this is the guard on the emitter.
451486 const src = cloudLoginSrc ( ) ;
452- const direct = src
453- . split ( '\n' )
454- . map ( ( line , i ) => ( { line, n : i + 1 } ) )
455- . filter ( ( { line } ) => / \b e m i t J s o n \s * \( / . test ( line ) )
456- . filter ( ( { line } ) => ! / ^ \s * a w a i t e m i t J s o n \( p a y l o a d , e x i t C o d e , \{ c o m p a c t : t r u e \} \) ; $ / . test ( line ) ) ;
487+
488+ // ⛔ This used to subtract ONE BYTE-EXACT LINE — `await emitJson(payload,
489+ // exitCode, { compact: true });` — from the `emitJson(` line hits, and call
490+ // anything left an offender. That binds the argument LIST, and binding an
491+ // argument list is the defect this file's own tier cannot survive: a pull
492+ // request that adds a parameter, renames `payload`, or simply wraps the call
493+ // over two lines moves the spelling, the per-PR run never collects this file
494+ // to say so, and the red arrives on `main` days later under whatever card
495+ // happens to be open. Bind the PROPERTY the ruling actually made instead —
496+ // ONE emitter — by partitioning the call sites against the emitter's own
497+ // brace-matched body: outside must be empty, inside must not be, so neither
498+ // half can pass by finding nothing (#18520).
499+ const emitter = bodySpan ( src , / a s y n c f u n c t i o n e m i t R e c o r d \s * \( / ) ;
500+ expect ( emitter , '`cloud/login.ts` no longer declares the single `emitRecord` emitter' ) . not . toBeNull ( ) ;
501+
502+ const sites = [ ...src . matchAll ( / \b e m i t J s o n \s * \( / g) ] ;
503+ const lineOf = ( at : number ) : number => src . slice ( 0 , at ) . split ( '\n' ) . length ;
504+ const outside = sites
505+ . filter ( ( m ) => m . index < emitter ! . start || m . index > emitter ! . end )
506+ . map ( ( m ) => {
507+ const eol = src . indexOf ( '\n' , m . index ) ;
508+ return `${ lineOf ( m . index ) } : ${ src . slice ( m . index , eol === - 1 ? undefined : eol ) . trim ( ) } ` ;
509+ } ) ;
510+
457511 expect (
458- direct . map ( ( { n , line } ) => ` ${ n } : ${ line . trim ( ) } ` ) ,
512+ outside ,
459513 'every --json write in cloud/login.ts must go through emitRecord()' ,
460514 ) . toEqual ( [ ] ) ;
461- expect ( / a s y n c f u n c t i o n e m i t R e c o r d \( / . test ( src ) ) . toBe ( true ) ;
515+ expect (
516+ sites . length - outside . length ,
517+ 'the emitter itself no longer calls `emitJson`, so the partition above is vacuous' ,
518+ ) . toBeGreaterThanOrEqual ( 1 ) ;
462519 } ) ;
463520
464521 it ( 'declares NDJSON in the --json flag help text' , ( ) => {
0 commit comments