1414 * node scripts/typePerf.mjs check against the budget
1515 * node scripts/typePerf.mjs --update rewrite the budget from this run
1616 * node scripts/typePerf.mjs --count 500
17+ * node scripts/typePerf.mjs --augment measure the `data-*`-augmented path
18+ *
19+ * `--augment` injects the `data-${string}` template-literal index signature onto
20+ * `HTMLAttributes` that large apps add for arbitrary data attributes. That routes
21+ * every intrinsic element through a distinct `style` widening (#5796), a cost the
22+ * default fixture never exercises. It checks against its own budget file, so the
23+ * two paths are guarded independently.
1724 *
1825 * Requires `pnpm build` first.
1926 */
2027import { execFileSync } from 'node:child_process' ;
21- import { existsSync , mkdirSync , readFileSync , readdirSync , rmSync , statSync , writeFileSync } from 'node:fs' ;
28+ import {
29+ existsSync ,
30+ mkdirSync ,
31+ readFileSync ,
32+ readdirSync ,
33+ rmSync ,
34+ statSync ,
35+ writeFileSync ,
36+ } from 'node:fs' ;
2237import { createRequire } from 'node:module' ;
2338import { dirname , join , resolve } from 'node:path' ;
2439import { fileURLToPath } from 'node:url' ;
@@ -27,7 +42,6 @@ import { parseArgs } from 'node:util';
2742const require = createRequire ( import . meta. url ) ;
2843const pkgRoot = resolve ( dirname ( fileURLToPath ( import . meta. url ) ) , '..' ) ;
2944const workDir = join ( pkgRoot , '.type-perf' ) ;
30- const budgetFile = join ( pkgRoot , 'type-perf.budget.json' ) ;
3145
3246// parseArgs over hand-rolled indexOf: it accepts `--count=500` as well as
3347// `--count 500`, and throws on an unknown flag or a valueless one instead of
@@ -36,7 +50,11 @@ const budgetFile = join(pkgRoot, 'type-perf.budget.json');
3650let parsed ;
3751try {
3852 parsed = parseArgs ( {
39- options : { count : { type : 'string' , default : '100' } , update : { type : 'boolean' } } ,
53+ options : {
54+ augment : { type : 'boolean' } ,
55+ count : { type : 'string' , default : '100' } ,
56+ update : { type : 'boolean' } ,
57+ } ,
4058 strict : true ,
4159 } ) ;
4260} catch ( error ) {
@@ -45,12 +63,21 @@ try {
4563}
4664
4765const update = parsed . values . update === true ;
66+ const augment = parsed . values . augment === true ;
4867const count = Number ( parsed . values . count ) ;
4968if ( ! Number . isInteger ( count ) || count < 1 ) {
5069 console . error ( `type-perf: --count needs a positive integer, got ${ parsed . values . count } ` ) ;
5170 process . exit ( 1 ) ;
5271}
5372
73+ // The augmented and default paths have different costs and different baselines,
74+ // so they keep separate budget files; one run never clobbers the other.
75+ const budgetFile = join (
76+ pkgRoot ,
77+ augment ? 'type-perf.augment.budget.json' : 'type-perf.budget.json'
78+ ) ;
79+ const updateHint = augment ? '--augment --update' : '--update' ;
80+
5481const distEntry = join ( pkgRoot , 'dist' , 'index.d.ts' ) ;
5582if ( ! existsSync ( distEntry ) ) {
5683 console . error ( 'type-perf: dist/index.d.ts missing -- run `pnpm build` first.' ) ;
@@ -63,7 +90,10 @@ if (!existsSync(distEntry)) {
6390// `test/` never reaches dist, and `utils/errors.ts` is generated -- the test run
6491// rewrites it, so counting either would report a stale dist after every
6592// `pnpm test` and train everyone to ignore the warning.
66- const IGNORED_SOURCES = new Set ( [ join ( pkgRoot , 'src' , 'test' ) , join ( pkgRoot , 'src' , 'utils' , 'errors.ts' ) ] ) ;
93+ const IGNORED_SOURCES = new Set ( [
94+ join ( pkgRoot , 'src' , 'test' ) ,
95+ join ( pkgRoot , 'src' , 'utils' , 'errors.ts' ) ,
96+ ] ) ;
6797
6898const newestSource = ( function newest ( dir ) {
6999 let latest = 0 ;
@@ -387,6 +417,18 @@ export const CanaryNative = <NativeCanary notAPropOfView={1} />;
387417export const NativeOk = <NativeCanary testID="ok" />;
388418` ;
389419
420+ // The `data-*` template-literal augmentation from #5796. A separate module (an
421+ // `import` makes it one) so the `declare module` merges into every fixture file
422+ // without editing the shared HEADER. Only written under `--augment`.
423+ const AUGMENT = `import 'react';
424+
425+ declare module 'react' {
426+ interface HTMLAttributes<T> {
427+ [dataAttribute: \`data-\${string}\`]: string | undefined;
428+ }
429+ }
430+ ` ;
431+
390432rmSync ( workDir , { recursive : true , force : true } ) ;
391433mkdirSync ( join ( workDir , 'src' ) , { recursive : true } ) ;
392434
@@ -396,6 +438,7 @@ for (let start = 0, f = 0; start < count; start += PER_FILE, f++) {
396438 writeFileSync ( join ( workDir , 'src' , `mod${ f } .tsx` ) , `${ HEADER } \n${ body . join ( '\n' ) } ` ) ;
397439}
398440writeFileSync ( join ( workDir , 'src' , 'canary.tsx' ) , CANARY ) ;
441+ if ( augment ) writeFileSync ( join ( workDir , 'src' , '_augment.d.ts' ) , AUGMENT ) ;
399442
400443writeFileSync (
401444 join ( workDir , 'tsconfig.json' ) ,
@@ -484,7 +527,10 @@ const versions = {
484527 typesReact : require ( '@types/react/package.json' ) . version ,
485528} ;
486529
487- console . log ( `type-perf: ${ count } components, TypeScript ${ versions . typescript } , @types/react ${ versions . typesReact } ` ) ;
530+ console . log (
531+ `type-perf${ augment ? ' (augmented)' : '' } : ${ count } components, ` +
532+ `TypeScript ${ versions . typescript } , @types/react ${ versions . typesReact } `
533+ ) ;
488534console . log ( ` types: ${ measured . types . toLocaleString ( ) } ` ) ;
489535console . log ( ` instantiations: ${ measured . instantiations . toLocaleString ( ) } ` ) ;
490536console . log ( ` memory: ${ Math . round ( measured . memoryKb / 1024 ) . toLocaleString ( ) } MB` ) ;
@@ -505,9 +551,14 @@ if (update) {
505551 process . exit ( 0 ) ;
506552}
507553
554+ if ( ! existsSync ( budgetFile ) ) {
555+ console . error ( `type-perf: ${ budgetFile } does not exist -- run with ${ updateHint } to create it.` ) ;
556+ process . exit ( 1 ) ;
557+ }
558+
508559const budget = JSON . parse ( readFileSync ( budgetFile , 'utf8' ) ) ;
509560if ( ! budget . measured || ! budget . tolerancePct ) {
510- console . error ( `type-perf: ${ budgetFile } is not in the expected shape -- run with --update .` ) ;
561+ console . error ( `type-perf: ${ budgetFile } is not in the expected shape -- run with ${ updateHint } .` ) ;
511562 process . exit ( 1 ) ;
512563}
513564
@@ -517,7 +568,9 @@ if (!budget.measured || !budget.tolerancePct) {
517568// to enforce. Any budget written before a metric existed has exactly this shape.
518569for ( const metric of Object . keys ( TOLERANCE_PCT ) ) {
519570 if ( ! Number . isFinite ( budget . measured [ metric ] ) || ! Number . isFinite ( budget . tolerancePct [ metric ] ) ) {
520- console . error ( `type-perf: budget is missing '${ metric } ' -- run with --update to re-measure.` ) ;
571+ console . error (
572+ `type-perf: budget is missing '${ metric } ' -- run with ${ updateHint } to re-measure.`
573+ ) ;
521574 process . exit ( 1 ) ;
522575 }
523576}
@@ -558,15 +611,15 @@ if (over.length > 0) {
558611 }
559612 console . error (
560613 'Consumer type-check cost grew. Find the cause before raising the budget; ' +
561- ' run with --update only when the increase is understood and intended.'
614+ ` run with ${ updateHint } only when the increase is understood and intended.`
562615 ) ;
563616 process . exit ( 1 ) ;
564617}
565618
566619for ( const [ label , actual , baseline ] of under ) {
567620 console . log (
568621 `type-perf: ${ label } improved ${ Math . round ( ( 1 - actual / baseline ) * 100 ) } % ` +
569- `(${ baseline . toLocaleString ( ) } -> ${ actual . toLocaleString ( ) } ); run --update to bank it`
622+ `(${ baseline . toLocaleString ( ) } -> ${ actual . toLocaleString ( ) } ); run ${ updateHint } to bank it`
570623 ) ;
571624}
572625
0 commit comments