Skip to content

Commit 5a28620

Browse files
committed
tooling(cutover): check:dts-closure expands exports subpath patterns before calling a declaration missing (objectstack#25803)
Cutover bill item 22. @object-ui/i18n declares "./locales/*": { types: "./dist/locales/*.d.ts" }, and the sweep tested that path with the star in it as one file, so it refused a package whose build had emitted 13 locale declarations. expandDeclarationPattern() expands a declared path containing one * against the package tree the way Node substitutes it (any non-empty string, / included, under the pattern's fixed directory). The family is owed as a whole: missing when nothing matches (the finding names the pattern), and each match held to the same not-empty rule as a literal path. The shared derivation in check-dts-emitted.mjs is left alone on purpose: it is a turbo build input of every package, so editing it re-keys every build cache, and no package that runs it declares a pattern. Self-test: a new 6-case battery (match, family scope, / inside the star, no match, zero-byte member, empty star and double star). Before (after the 119-task build): exit 1, @object-ui/i18n missing dist/locales/*.d.ts. After: self-test 0, scan 0 (119 built, 231/231 declared declarations present). Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CUXv64KLMfYv3ia2sWi6te
1 parent e63cef1 commit 5a28620

1 file changed

Lines changed: 121 additions & 3 deletions

File tree

‎scripts/check-dts-closure.mjs‎

Lines changed: 121 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112

113113
import { mkdirSync, mkdtempSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
114114
import { tmpdir } from 'node:os';
115-
import { join, relative, resolve } from 'node:path';
115+
import { dirname, join, relative, resolve } from 'node:path';
116116
import process from 'node:process';
117117

118118
import { declaredDeclarationPaths, missingDeclarations } from './check-dts-emitted.mjs';
@@ -158,6 +158,51 @@ export function sizeOnDisk(packageDir) {
158158
};
159159
}
160160

161+
/**
162+
* The files an `exports` SUBPATH PATTERN target names (objectstack#25803, cutover
163+
* bill item 22). `./locales/*` -> `./dist/locales/*.d.ts` promises a FAMILY of
164+
* declarations, one per locale, and the literal path with the `*` in it is no
165+
* file at all: tested as one, `@object-ui/i18n` read as missing a declaration
166+
* its build had emitted thirteen times over.
167+
*
168+
* Node substitutes the `*` with any non-empty string, `/` included, so the
169+
* family is every file under the pattern's fixed directory whose path starts
170+
* with the text before the `*` and ends with the text after it. A pattern with
171+
* a second `*` is not one Node resolves; it expands to nothing, which reports it.
172+
*
173+
* Expanded HERE and not in the shared derivation: `check-dts-emitted.mjs` is a
174+
* build input of every package (turbo.json `build.inputs`), so changing it
175+
* re-keys every build cache, and no package that runs it declares a pattern.
176+
*
177+
* @param {string} packageDir absolute package directory
178+
* @param {string} pattern package-relative declared path containing one `*`
179+
* @returns {string[]} package-relative matches, sorted
180+
*/
181+
export function expandDeclarationPattern(packageDir, pattern) {
182+
const star = pattern.indexOf('*');
183+
const prefix = pattern.slice(0, star);
184+
const suffix = pattern.slice(star + 1);
185+
if (suffix.includes('*')) return [];
186+
const matches = [];
187+
const walk = (rel) => {
188+
let entries;
189+
try {
190+
entries = readdirSync(join(packageDir, rel), { withFileTypes: true });
191+
} catch {
192+
return;
193+
}
194+
for (const entry of entries) {
195+
const child = `${rel}${entry.name}`;
196+
if (entry.isDirectory()) walk(`${child}/`);
197+
else if (child.length > prefix.length + suffix.length && child.startsWith(prefix) && child.endsWith(suffix)) {
198+
matches.push(child);
199+
}
200+
}
201+
};
202+
walk(prefix.slice(0, prefix.lastIndexOf('/') + 1));
203+
return matches.sort();
204+
}
205+
161206
/**
162207
* The sweep itself, over an already-enumerated member list.
163208
*
@@ -186,7 +231,15 @@ export function sweep(root, members) {
186231
}
187232
checked += 1;
188233
files += declared.length;
189-
const missing = missingDeclarations(declared, sizeOnDisk(join(root, dir)));
234+
// A pattern is owed as a family: missing when nothing matches it, and each
235+
// match held to the same not-empty rule as a literal path.
236+
const packageDir = join(root, dir);
237+
const missing = missingDeclarations(declared.filter((path) => !path.includes('*')), sizeOnDisk(packageDir));
238+
for (const pattern of declared.filter((path) => path.includes('*'))) {
239+
const family = expandDeclarationPattern(packageDir, pattern);
240+
if (family.length === 0) missing.push({ path: pattern, why: 'missing' });
241+
else missing.push(...missingDeclarations(family, sizeOnDisk(packageDir)));
242+
}
190243
if (missing.length > 0) findings.push({ name: typeof manifest.name === 'string' ? manifest.name : dir, dir, missing });
191244
}
192245

@@ -305,6 +358,7 @@ export function run(root) {
305358
const SELF_TEST_BATTERIES = Object.freeze({
306359
'the sweep, over real fixture workspaces': 12,
307360
'the shared derivation is the per-package guard\'s own': 2,
361+
'an `exports` subpath pattern is owed as a family (#25803)': 6,
308362
});
309363

310364
// DELETING an entry silences that battery's floor exactly as effectively as
@@ -369,7 +423,10 @@ function selfTest() {
369423
writeFileSync(join(abs, 'package.json'), JSON.stringify(spec.manifest, null, 2));
370424
if (spec.dist) {
371425
mkdirSync(join(abs, 'dist'), { recursive: true });
372-
for (const [file, contents] of Object.entries(spec.dist)) writeFileSync(join(abs, 'dist', file), contents);
426+
for (const [file, contents] of Object.entries(spec.dist)) {
427+
mkdirSync(dirname(join(abs, 'dist', file)), { recursive: true });
428+
writeFileSync(join(abs, 'dist', file), contents);
429+
}
373430
}
374431
}
375432
return root;
@@ -493,6 +550,67 @@ function selfTest() {
493550
prerequisiteNotMetText('x').includes('NOT a pass and NOT a finding'),
494551
);
495552

553+
battery('an `exports` subpath pattern is owed as a family (#25803)');
554+
// `@object-ui/i18n`'s shape: a literal entry beside a `*` pattern whose
555+
// family sits one directory down.
556+
const patternManifest = (pkgName) => ({
557+
name: pkgName,
558+
exports: {
559+
'.': { types: './dist/index.d.ts', import: './dist/index.js' },
560+
'./locales/*': { types: './dist/locales/*.d.ts', import: './dist/locales/*.js' },
561+
},
562+
});
563+
const familyRoot = fixture('family', {
564+
'packages/i18n': {
565+
manifest: patternManifest('@fixture/i18n'),
566+
dist: { 'index.js': '', 'index.d.ts': 'export {}\n', 'locales/en.js': '', 'locales/en.d.ts': 'export {}\n', 'locales/zh.d.ts': 'export {}\n' },
567+
},
568+
});
569+
eq('a pattern with matching declarations is not a finding', sweep(familyRoot, workspacePackages(familyRoot)).findings, []);
570+
eq(
571+
'the family is every match under the fixed directory, nothing else',
572+
expandDeclarationPattern(join(familyRoot, 'packages/i18n'), 'dist/locales/*.d.ts'),
573+
['dist/locales/en.d.ts', 'dist/locales/zh.d.ts'],
574+
);
575+
const nestedRoot = fixture('family-nested', {
576+
'packages/i18n': {
577+
manifest: patternManifest('@fixture/i18n'),
578+
dist: { 'index.js': '', 'index.d.ts': 'export {}\n', 'locales/zh/hans.d.ts': 'export {}\n' },
579+
},
580+
});
581+
eq(
582+
'the `*` spans `/`, the way Node substitutes it',
583+
expandDeclarationPattern(join(nestedRoot, 'packages/i18n'), 'dist/locales/*.d.ts'),
584+
['dist/locales/zh/hans.d.ts'],
585+
);
586+
const emptyFamilyRoot = fixture('family-empty', {
587+
'packages/i18n': { manifest: patternManifest('@fixture/i18n'), dist: { 'index.js': '', 'index.d.ts': 'export {}\n', 'locales/en.js': '' } },
588+
});
589+
eq(
590+
'a pattern nothing matches is a finding that names the pattern',
591+
sweep(emptyFamilyRoot, workspacePackages(emptyFamilyRoot)).findings.map((f) => f.missing.map((x) => `${x.why}:${x.path}`)),
592+
[['missing:dist/locales/*.d.ts']],
593+
);
594+
const zeroByteRoot = fixture('family-zero', {
595+
'packages/i18n': {
596+
manifest: patternManifest('@fixture/i18n'),
597+
dist: { 'index.js': '', 'index.d.ts': 'export {}\n', 'locales/en.d.ts': 'export {}\n', 'locales/zh.d.ts': '' },
598+
},
599+
});
600+
eq(
601+
'a zero-byte member of the family reds as `empty`',
602+
sweep(zeroByteRoot, workspacePackages(zeroByteRoot)).findings.map((f) => f.missing.map((x) => `${x.why}:${x.path}`)),
603+
[['empty:dist/locales/zh.d.ts']],
604+
);
605+
eq(
606+
'the `*` stands for a non-empty string, and a second `*` expands to nothing',
607+
[
608+
expandDeclarationPattern(join(familyRoot, 'packages/i18n'), 'dist/locales/en.d.ts*'),
609+
expandDeclarationPattern(join(familyRoot, 'packages/i18n'), 'dist/*/*.d.ts'),
610+
],
611+
[[], []],
612+
);
613+
496614
battery('the shared derivation is the per-package guard\'s own');
497615
// Not a re-implementation: the sweep's answer for a manifest IS
498616
// `declaredDeclarationPaths`'s answer, and that function is the per-package

0 commit comments

Comments
 (0)