Skip to content

Commit 131a39c

Browse files
authored
Use stable path-based WPT registry aliases (#60)
## What does this PR do? Adding a WPT source currently renumbers every later generated import alias, creating a ton of churn noise just to add a single file. This `register` and `input` aliases from each source path so additions change only their own import and registry entry. In other words, a single line change takes up a single line. ## Type of change - [ ] Bug fix - [ ] Feature - [x] Refactor (no behavior change) - [ ] Documentation - [ ] Performance improvement - [ ] Tests - [ ] Chore (dependencies, CI, tooling) ## Checklist - [x] All tests pass (`pnpm test`) - [x] Files are formatted (`pnpm format`) - [ ] I have added/updated tests for my changes (if applicable) - [ ] I have added a changeset Existing harness tests and isolated generator checks validate the change; no test files were added or updated. A changeset is not needed for this private test package. ## AI-generated code disclosure - [x] This PR includes AI-generated code
1 parent baa93e3 commit 131a39c

2 files changed

Lines changed: 783 additions & 767 deletions

File tree

‎packages/web-platform-tests/scripts/generate-registry.mjs‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,40 @@ function files(directory, prefix = '') {
1313

1414
const tests = files(root)
1515
.toSorted()
16-
.map((path) => ({ path, content: readFileSync(new URL(path, root), 'utf8') }))
16+
.map((path) => ({
17+
path,
18+
alias: path.slice(0, -3).replace(/[^A-Za-z0-9_$]/g, '_'),
19+
content: readFileSync(new URL(path, root), 'utf8'),
20+
}))
1721
.filter(({ content }) => /^export function register\(/m.test(content));
22+
23+
const pathsByAlias = new Map();
24+
for (const { path, alias } of tests) {
25+
if (pathsByAlias.has(alias)) {
26+
process.stderr.write(
27+
`WPT registry alias collision: "${pathsByAlias.get(alias)}" and "${path}" both map to "${alias}".\n`,
28+
);
29+
process.exit(1);
30+
}
31+
pathsByAlias.set(alias, path);
32+
}
33+
1834
const output = [
1935
'// Generated from src/wpt filenames by scripts/generate-registry.mjs.',
2036
"import type { CompatibilityModule } from './context.ts';",
21-
...tests.map(({ path, content }, index) => {
22-
const imports = [`register as register${index}`];
23-
if (/^export const input\b/m.test(content)) imports.push(`input as input${index}`);
37+
...tests.map(({ path, content, alias }) => {
38+
const imports = [`register as register_${alias}`];
39+
if (/^export const input\b/m.test(content)) imports.push(`input as input_${alias}`);
2440
return imports.length === 1
2541
? `import { ${imports[0]} } from '../wpt/${path}';`
2642
: `import {\n${imports.map((name) => `\t${name},`).join('\n')}\n} from '../wpt/${path}';`;
2743
}),
2844
'',
2945
'// prettier-ignore',
3046
'export const registry = {',
31-
...tests.map(({ path, content }, index) => {
32-
const members = [`register: register${index}`];
33-
if (/^export const input\b/m.test(content)) members.push(`input: input${index}`);
47+
...tests.map(({ path, content, alias }) => {
48+
const members = [`register: register_${alias}`];
49+
if (/^export const input\b/m.test(content)) members.push(`input: input_${alias}`);
3450
return `\t'${path.slice(0, -3)}': { ${members.join(', ')} },`;
3551
}),
3652
'} satisfies Record<string, CompatibilityModule>;',

0 commit comments

Comments
 (0)