@@ -126,17 +126,90 @@ function handBuiltStudioAddresses(fileName: string, text: string): string[] {
126126 return hits ;
127127}
128128
129+ /**
130+ * The cheap gate in front of the parser: may this source text hold anything
131+ * `handBuiltStudioAddresses` flags? Parsing every ui source takes seconds and
132+ * all but a few files hold none, so only the files that pass are parsed.
133+ *
134+ * It is a superset of the matcher by construction. The matcher flags three
135+ * node shapes, and each one needs a token whose COOKED text is one of three
136+ * words:
137+ *
138+ * - a string or template literal (or a template head) whose text starts with
139+ * `/studio`;
140+ * - a template whose first span is the identifier `STUDIO_ROOT_PATH` or
141+ * `STUDIO_LANDING_PATH`;
142+ * - `+` with one of those identifiers on its left.
143+ *
144+ * A JSX attribute string is not cooked (`/` stays `/`), so its raw text
145+ * is its text. A JS string or template is cooked, and its raw text can spell
146+ * each character as itself, an identity escape (`\/`, `\s`), `\xHH`, `\uHHHH`,
147+ * `\u{H…}` or a legacy octal escape (`\57`), with line continuations (a
148+ * backslash before a line break, which cook to nothing) between characters. An
149+ * identifier can spell a character as `\uHHHH` or `\u{H…}`. So the gate looks
150+ * for each word with every character in any of those spellings, and with
151+ * continuations allowed between them: a literal or identifier the matcher can
152+ * flag always leaves one of these in the raw text. Extra spellings
153+ * (`\t` read as `t`) only cost a parse, never a miss.
154+ */
155+ const STUDIO_ADDRESS_WORDS = [ '/studio' , ...ROOT_NAMES ] ;
156+
157+ const hexDigits = ( code : number , width : number ) : string =>
158+ [ ...code . toString ( 16 ) . padStart ( width , '0' ) ] . map ( ( d ) => ( / [ a - f ] / . test ( d ) ? `[${ d } ${ d . toUpperCase ( ) } ]` : d ) ) . join ( '' ) ;
159+
160+ const anySpellingOf = ( char : string ) : string => {
161+ const code = char . charCodeAt ( 0 ) ;
162+ const plain = char . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ / ] / g, '\\$&' ) ;
163+ return `(?:${ [
164+ plain ,
165+ `\\\\${ plain } ` ,
166+ `\\\\x${ hexDigits ( code , 2 ) } ` ,
167+ `\\\\u${ hexDigits ( code , 4 ) } ` ,
168+ `\\\\u\\{0*${ hexDigits ( code , 1 ) } \\}` ,
169+ `\\\\0*${ code . toString ( 8 ) } ` ,
170+ ] . join ( '|' ) } )`;
171+ } ;
172+
173+ const LINE_CONTINUATIONS = '(?:\\\\(?:\\r\\n|[\\n\\r\\u2028\\u2029]))*' ;
174+
175+ const MAY_SPELL_STUDIO_ADDRESS = new RegExp (
176+ STUDIO_ADDRESS_WORDS . map ( ( word ) => [ ...word ] . map ( anySpellingOf ) . join ( LINE_CONTINUATIONS ) ) . join ( '|' ) ,
177+ ) ;
178+
179+ function maySpellStudioAddress ( text : string ) : boolean {
180+ return MAY_SPELL_STUDIO_ADDRESS . test ( text ) ;
181+ }
182+
183+ /**
184+ * Every spelling the matcher flags, one hit per fixture: the first case holds
185+ * the matcher to them, the pre-filter control holds the gate to them.
186+ */
187+ const FLAGGED_SPELLINGS = [
188+ "const a = '/studio';" ,
189+ 'const b = `/studio/${pkg}/data`;' ,
190+ 'const c = "/studio?new=package";' ,
191+ 'const d = <Link to="/studio" />;' ,
192+ 'const d2 = <Tab path="/studio" />;' ,
193+ 'const d3 = <Route path={`/studio/${pkg}`} />;' ,
194+ 'const e = `${STUDIO_ROOT_PATH}/${pkg}`;' ,
195+ "const f = STUDIO_ROOT_PATH + '/' + pkg;" ,
196+ // The same addresses with escapes in them: cooked, they are the same text.
197+ "const a1 = '\\/studio';" ,
198+ "const a2 = '\\x2Fstudio';" ,
199+ "const a3 = '\\u002fstud\\u{69}o';" ,
200+ "const a4 = '\\57\\163tudio';" ,
201+ "const a5 = '/stu\\\ndio';" ,
202+ 'const b1 = `\\x2fstudio/${pkg}`;' ,
203+ 'const e1 = `${STUDIO_LANDING_PATH}/${pkg}`;' ,
204+ 'const e2 = `${STUDIO\\u005FROOT_PATH}/${pkg}`;' ,
205+ "const f1 = STUDIO_LANDING_PATH + '/' + pkg;" ,
206+ "const f2 = STUDIO_\\u{52}OOT_PATH + '/' + pkg;" ,
207+ ] ;
208+
129209describe ( 'no Studio address is built by hand under ui/ (objectstack#36895)' , ( ) => {
130210 it ( 'the matcher sees each spelling and spares prose and look-alikes' , ( ) => {
131211 const scan = ( text : string ) => handBuiltStudioAddresses ( 'x.tsx' , text ) ;
132- expect ( scan ( "const a = '/studio';" ) ) . toHaveLength ( 1 ) ;
133- expect ( scan ( 'const b = `/studio/${pkg}/data`;' ) ) . toHaveLength ( 1 ) ;
134- expect ( scan ( 'const c = "/studio?new=package";' ) ) . toHaveLength ( 1 ) ;
135- expect ( scan ( 'const d = <Link to="/studio" />;' ) ) . toHaveLength ( 1 ) ;
136- expect ( scan ( 'const d2 = <Tab path="/studio" />;' ) ) . toHaveLength ( 1 ) ;
137- expect ( scan ( 'const d3 = <Route path={`/studio/${pkg}`} />;' ) ) . toHaveLength ( 1 ) ;
138- expect ( scan ( 'const e = `${STUDIO_ROOT_PATH}/${pkg}`;' ) ) . toHaveLength ( 1 ) ;
139- expect ( scan ( "const f = STUDIO_ROOT_PATH + '/' + pkg;" ) ) . toHaveLength ( 1 ) ;
212+ for ( const spelling of FLAGGED_SPELLINGS ) expect ( scan ( spelling ) , spelling ) . toHaveLength ( 1 ) ;
140213 // Not an address.
141214 expect ( scan ( "const g = '/studios';" ) ) . toEqual ( [ ] ) ;
142215 expect ( scan ( "const h = '/api/v1/studio/runtime-config';" ) ) . toEqual ( [ ] ) ;
@@ -150,7 +223,28 @@ describe('no Studio address is built by hand under ui/ (objectstack#36895)', ()
150223 expect ( scan ( 'const l = studioHomePath(pkg);' ) ) . toEqual ( [ ] ) ;
151224 } ) ;
152225
153- it ( 'no ui source outside the builder spells one' , ( ) => {
226+ it ( 'the pre-filter keeps every source the matcher flags' , ( ) => {
227+ // Each fixture's only hit is one spelling, so a gate that drops any of
228+ // them would hide exactly that spelling from the walk below.
229+ for ( const spelling of FLAGGED_SPELLINGS ) {
230+ expect ( handBuiltStudioAddresses ( 'x.tsx' , spelling ) , spelling ) . toHaveLength ( 1 ) ;
231+ expect ( maySpellStudioAddress ( spelling ) , spelling ) . toBe ( true ) ;
232+ }
233+ // The one file allowed to spell the root fires the matcher and passes.
234+ const builder = path . join ( UI_ROOT , 'packages/app-shell/src/views/studio-design/studioAddress.ts' ) ;
235+ const builderText = readFileSync ( builder , 'utf8' ) ;
236+ expect ( handBuiltStudioAddresses ( builder , builderText ) . length ) . toBeGreaterThan ( 0 ) ;
237+ expect ( maySpellStudioAddress ( builderText ) ) . toBe ( true ) ;
238+ // And it does filter: a source naming no Studio address is skipped.
239+ expect ( maySpellStudioAddress ( "const i = 'studio:builder';\nconst p = '\\t';" ) ) . toBe ( false ) ;
240+ } ) ;
241+
242+ // The walk reads every ui source; the pre-filter keeps the parse to the few
243+ // that may spell an address (about 120 of 2,400 today). Measured locally:
244+ // 4.5 s with every file parsed, 0.9 s with the pre-filter. The explicit budget
245+ // is the second line of defence for a loaded CI shard, where the default 15 s
246+ // ran out.
247+ it ( 'no ui source outside the builder spells one' , { timeout : 60_000 } , ( ) => {
154248 const files = uiSources ( ) ;
155249 const rel = ( file : string ) => path . relative ( UI_ROOT , file ) . split ( path . sep ) . join ( '/' ) ;
156250 const names = new Set ( files . map ( rel ) ) ;
@@ -163,7 +257,11 @@ describe('no Studio address is built by hand under ui/ (objectstack#36895)', ()
163257
164258 const offenders = files
165259 . filter ( ( file ) => ! ALLOWED . has ( rel ( file ) ) )
166- . flatMap ( ( file ) => handBuiltStudioAddresses ( file , readFileSync ( file , 'utf8' ) ) . map ( ( h ) => `${ rel ( file ) } :${ h } ` ) ) ;
260+ . flatMap ( ( file ) => {
261+ const text = readFileSync ( file , 'utf8' ) ;
262+ if ( ! maySpellStudioAddress ( text ) ) return [ ] ;
263+ return handBuiltStudioAddresses ( file , text ) . map ( ( h ) => `${ rel ( file ) } :${ h } ` ) ;
264+ } ) ;
167265 expect ( offenders ) . toEqual ( [ ] ) ;
168266 } ) ;
169267} ) ;
0 commit comments