4545 */
4646import { readdirSync , readFileSync } from 'node:fs' ;
4747import { fileURLToPath } from 'node:url' ;
48- import ts from 'typescript' ;
4948import { describe , expect , it } from 'vitest' ;
49+ // [#25467 E1] Comments blanked (offsets and line numbers kept), so a docblock that
50+ // names the callee is not read as a call.
51+ import { maskComments } from '../../../scripts/js-comment-mask.mjs' ;
5052
5153const SOURCE = readFileSync ( fileURLToPath ( new URL ( './protocol.ts' , import . meta. url ) ) , 'utf8' ) ;
5254/** [#25467 cut 15] The seam module the lookup's body moved to; scanned with its own header shape. */
@@ -73,34 +75,13 @@ const MEMBER_HEADER = /^ {4}(?:(?:private|public|protected|static|async|override
7375/** A module function's header in a split module: optional `export` and `async`, `function`, its name, its parameter list. */
7476const FUNCTION_HEADER = / ^ (?: e x p o r t ) ? (?: a s y n c ) ? f u n c t i o n ( [ A - Z a - z _ $ ] [ \w $ ] * ) \s * (?: < [ ^ > ] * > ) ? \( / ;
7577
76- /**
77- * [#25467 E1] The source with every comment blanked to spaces (newlines kept), so
78- * offsets and line numbers are the file's own and a docblock that names the
79- * callee is not read as a call. Comment ranges come from the TypeScript parser,
80- * so a `//` inside a string or a template is not a comment.
81- */
82- function withoutComments ( source : string ) : string {
83- const sf = ts . createSourceFile ( 'x.ts' , source , ts . ScriptTarget . Latest , true , ts . ScriptKind . TS ) ;
84- const chars = source . split ( '' ) ;
85- const blank = ( ranges : ts . CommentRange [ ] | undefined ) => {
86- for ( const r of ranges ?? [ ] ) for ( let i = r . pos ; i < r . end ; i ++ ) if ( chars [ i ] !== '\n' ) chars [ i ] = ' ' ;
87- } ;
88- const visit = ( node : ts . Node ) => {
89- blank ( ts . getLeadingCommentRanges ( source , node . getFullStart ( ) ) ) ;
90- blank ( ts . getTrailingCommentRanges ( source , node . getEnd ( ) ) ) ;
91- for ( const child of node . getChildren ( sf ) ) visit ( child ) ;
92- } ;
93- visit ( sf ) ;
94- return chars . join ( '' ) ;
95- }
96-
9778interface Site { readonly file ?: string ; readonly member : string ; readonly args : string }
9879
9980/** Every `lookupArtifactItem(` in `protocol.ts` and every split module: the file, the member it sits in and its argument text, whitespace-normalised. */
10081function sitesIn ( ) : Site [ ] {
10182 return [
102- ...sitesOf ( withoutComments ( SOURCE ) , MEMBER_HEADER ) ,
103- ...SPLIT_MODULES . flatMap ( ( file ) => sitesOf ( withoutComments ( MODULE_SOURCES . get ( file ) ! ) , FUNCTION_HEADER ) . map ( ( site ) => ( { ...site , file } ) ) ) ,
83+ ...sitesOf ( maskComments ( SOURCE ) , MEMBER_HEADER ) ,
84+ ...SPLIT_MODULES . flatMap ( ( file ) => sitesOf ( maskComments ( MODULE_SOURCES . get ( file ) ! ) , FUNCTION_HEADER ) . map ( ( site ) => ( { ...site , file } ) ) ) ,
10485 ] ;
10586}
10687
0 commit comments