Skip to content

Commit 560d172

Browse files
authored
chore(scripts): prevent new wrapper shadowing (#121456)
* chore(scripts): prevent wrapper shadowing * chore(scripts): refresh wrapper baseline
1 parent 83dfd44 commit 560d172

8 files changed

Lines changed: 1034 additions & 10 deletions

‎package.json‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@
15261526
"channels:catalog:gen": "node scripts/write-official-channel-catalog.mjs --write",
15271527
"changed:lanes": "node scripts/changed-lanes.mjs",
15281528
"check": "node --import tsx scripts/check.mts",
1529-
"check:architecture": "pnpm check:import-cycles && pnpm check:madge-import-cycles && pnpm check:deprecated-api-usage && pnpm check:deprecated-jsdoc && pnpm db:kysely:check && pnpm lint:kysely && pnpm check:database-first-legacy-stores",
1529+
"check:architecture": "pnpm check:import-cycles && pnpm check:madge-import-cycles && pnpm check:deprecated-api-usage && pnpm check:wrapper-shadowing && pnpm check:deprecated-jsdoc && pnpm db:kysely:check && pnpm lint:kysely && pnpm check:database-first-legacy-stores",
15301530
"check:base-config-schema": "node --import tsx scripts/generate-base-config-schema.ts --check",
15311531
"check:bundled-channel-config-metadata": "node --import tsx scripts/generate-bundled-channel-config-metadata.ts --check",
15321532
"check:changed": "node scripts/check-changed.mjs",
@@ -1549,6 +1549,8 @@
15491549
"check:runtime-sidecar-loaders": "node --import tsx scripts/check-runtime-sidecar-loaders.mts",
15501550
"check:static-import-sccs": "pnpm check:madge-import-cycles",
15511551
"check:temp-path-guardrails": "node --import tsx scripts/check-temp-path-guardrails.ts",
1552+
"check:wrapper-shadowing": "node --import tsx scripts/check-wrapper-shadowing.mts",
1553+
"check:wrapper-shadowing:gen": "node --import tsx scripts/check-wrapper-shadowing.mts --update-debt-baseline",
15521554
"check:test-types": "pnpm tsgo:test",
15531555
"check:timed": "node --import tsx scripts/check-timed.mts",
15541556
"check:timed:all-types": "node --import tsx scripts/check-timed.mts --include-test-types",

‎scripts/check-changed.mts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ const PLUGIN_SDK_SURFACE_PATH_RE =
101101
/^(?:package\.json$|src\/plugin-sdk\/|packages\/plugin-sdk\/|scripts\/(?:plugin-sdk-surface-report\.mts|sync-plugin-sdk-exports\.mts|lib\/plugin-sdk-(?:declaration-budget\.mts|deprecated-barrel-subpaths\.json|deprecated-public-subpaths\.json|entries\.mts|entrypoints\.json|private-local-only-subpaths\.json)))/u;
102102
const DEPRECATION_HYGIENE_PATH_RE =
103103
/^(?:package\.json$|src\/|extensions\/|packages\/|scripts\/(?:check-deprecated-api-usage\.mts$|plugin-boundary-report\.ts$|lib\/plugin-sdk))/u;
104+
const WRAPPER_SHADOWING_PATH_RE =
105+
/^(?:package\.json$|src\/|scripts\/(?:check-(?:export-name-collisions|wrapper-shadowing)\.mts$|lib\/(?:export-name-collision-baseline\.json$|ts-guard-utils\.mts$|wrapper-shadowing-baseline\.json$)))/u;
104106
const CANVAS_A2UI_NATIVE_RESOURCE_PATH_RE =
105107
/^(?:pnpm-lock\.yaml$|apps\/(?:android\/app\/build\.gradle\.kts$|ios\/project\.yml$|linux\/src-tauri\/(?:build\.rs$|src\/canvas\.rs$)|shared\/OpenClawKit\/Sources\/OpenClawKit\/Resources\/CanvasA2UI\/)|extensions\/canvas\/(?:package\.json$|scripts\/bundle-a2ui\.mjs$|src\/host\/a2ui(?:\/(?:index\.html|a2ui\.bundle\.js|\.bundle\.hash)$|-app\/))|scripts\/(?:bundle-a2ui|sync-native-a2ui)\.mts$)/u;
106108
const CONTROL_UI_I18N_VERIFY_PATH_RE =
@@ -382,6 +384,13 @@ export function shouldRunDeprecationHygieneChecks(paths: string[]) {
382384
);
383385
}
384386

387+
/** Returns whether changed files can alter wrapper-shadowing results. */
388+
export function shouldRunWrapperShadowingCheck(paths: string[]) {
389+
return paths.some((changedPath) =>
390+
WRAPPER_SHADOWING_PATH_RE.test(normalizeChangedPath(changedPath)),
391+
);
392+
}
393+
385394
export function shouldRunCanvasA2uiNativeResourceCheck(paths: string[]) {
386395
return paths.some((changedPath) =>
387396
CANVAS_A2UI_NATIVE_RESOURCE_PATH_RE.test(normalizeChangedPath(changedPath)),
@@ -677,6 +686,9 @@ export function createChangedCheckPlan(
677686
// until their scheduled deletion PRs land.
678687
add("plugin boundaries", ["plugins:boundary-report:ci"]);
679688
}
689+
if (result.lanes.all || shouldRunWrapperShadowingCheck(result.paths)) {
690+
add("wrapper shadowing", ["check:wrapper-shadowing"]);
691+
}
680692
if (shouldRunCanvasA2uiNativeResourceCheck(result.paths)) {
681693
addCommand(
682694
"Canvas A2UI native resource generation",

‎scripts/check-export-name-collisions.mts‎

Lines changed: 194 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,35 @@ export type ExportNameCollision = {
1919
sdk?: true;
2020
};
2121

22-
type SourceModule = {
22+
export type SourceModule = {
2323
content: string;
2424
includeDefinitions?: boolean;
2525
path: string;
2626
};
2727

28-
type ModuleExports = {
28+
export type ImportedSymbolReference = {
29+
importedName: string;
30+
localName: string;
31+
moduleSpecifier: string;
32+
};
33+
34+
export type NamedReExport = {
35+
exportedName: string;
36+
importedName: string;
37+
moduleSpecifier: string;
38+
};
39+
40+
export type ExportedValueDefinition = {
41+
importedReferences: ImportedSymbolReference[];
42+
name: string;
43+
};
44+
45+
export type ModuleExports = {
2946
definitions: Set<string>;
3047
exportedNames: Set<string>;
48+
namedReExports: NamedReExport[];
3149
starExportSpecifiers: string[];
50+
valueDefinitions: Map<string, ExportedValueDefinition>;
3251
};
3352

3453
const exportNameCollisionSchema = z
@@ -75,6 +94,72 @@ function collectBindingNames(name: ts.BindingName, names: Set<string>) {
7594
}
7695
}
7796

97+
function resolveImportedReference(
98+
expression: ts.Expression,
99+
importedSymbolsByLocalName: ReadonlyMap<string, ImportedSymbolReference>,
100+
namespaceImportsByLocalName: ReadonlyMap<string, string>,
101+
) {
102+
const target = unwrapExpression(expression);
103+
if (ts.isIdentifier(target)) {
104+
return importedSymbolsByLocalName.get(target.text);
105+
}
106+
if (!ts.isPropertyAccessExpression(target) && !ts.isElementAccessExpression(target)) {
107+
return undefined;
108+
}
109+
const namespaceName = ts.isPropertyAccessExpression(target)
110+
? target.name.text
111+
: ts.isElementAccessExpression(target) &&
112+
target.argumentExpression &&
113+
ts.isStringLiteral(target.argumentExpression)
114+
? target.argumentExpression.text
115+
: null;
116+
if (!namespaceName || !ts.isIdentifier(target.expression)) {
117+
return undefined;
118+
}
119+
const moduleSpecifier = namespaceImportsByLocalName.get(target.expression.text);
120+
return moduleSpecifier
121+
? {
122+
importedName: namespaceName,
123+
localName: `${target.expression.text}.${namespaceName}`,
124+
moduleSpecifier,
125+
}
126+
: undefined;
127+
}
128+
129+
function collectImportedReferences(
130+
node: ts.Node,
131+
importedSymbolsByLocalName: ReadonlyMap<string, ImportedSymbolReference>,
132+
namespaceImportsByLocalName: ReadonlyMap<string, string>,
133+
) {
134+
const references = new Map<string, ImportedSymbolReference>();
135+
const addReference = (reference: ImportedSymbolReference | undefined) => {
136+
if (reference) {
137+
references.set(
138+
`${reference.moduleSpecifier}\0${reference.importedName}\0${reference.localName}`,
139+
reference,
140+
);
141+
}
142+
};
143+
const visit = (current: ts.Node): void => {
144+
if (ts.isCallExpression(current)) {
145+
addReference(
146+
resolveImportedReference(
147+
current.expression,
148+
importedSymbolsByLocalName,
149+
namespaceImportsByLocalName,
150+
),
151+
);
152+
}
153+
ts.forEachChild(current, visit);
154+
};
155+
visit(node);
156+
return [...references.values()].toSorted((left, right) =>
157+
`${left.moduleSpecifier}\0${left.importedName}\0${left.localName}`.localeCompare(
158+
`${right.moduleSpecifier}\0${right.importedName}\0${right.localName}`,
159+
),
160+
);
161+
}
162+
78163
function parametersAreForwarded(
79164
parameters: ts.NodeArray<ts.ParameterDeclaration>,
80165
args: ts.NodeArray<ts.Expression>,
@@ -238,11 +323,15 @@ function isForwardingOnlyConst(
238323
export function collectModuleExportNames(content: string, fileName = "source.ts"): ModuleExports {
239324
const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true);
240325
const importedNamesByLocalName = new Map<string, string>();
326+
const importedSymbolsByLocalName = new Map<string, ImportedSymbolReference>();
327+
const namespaceImportsByLocalName = new Map<string, string>();
241328
const localConstDeclarations = new Map<string, ts.VariableDeclaration[]>();
242329
const localFunctions = new Map<string, ts.FunctionDeclaration[]>();
243330
const directlyExportedNames = new Set<string>();
244331
const locallyExportedNames = new Set<string>();
245332
const exportedNames = new Set<string>();
333+
const namedReExports: NamedReExport[] = [];
334+
const pendingLocalReExports: Array<{ exportedName: string; localName: string }> = [];
246335
const starExportSpecifiers: string[] = [];
247336

248337
for (const statement of sourceFile.statements) {
@@ -251,16 +340,43 @@ export function collectModuleExportNames(content: string, fileName = "source.ts"
251340
if (bindings && ts.isNamedImports(bindings)) {
252341
for (const specifier of bindings.elements) {
253342
if (!statement.importClause?.isTypeOnly && !specifier.isTypeOnly) {
254-
importedNamesByLocalName.set(
255-
specifier.name.text,
256-
specifier.propertyName?.text ?? specifier.name.text,
257-
);
343+
const importedName = specifier.propertyName?.text ?? specifier.name.text;
344+
const moduleSpecifier = ts.isStringLiteral(statement.moduleSpecifier)
345+
? statement.moduleSpecifier.text
346+
: "";
347+
importedNamesByLocalName.set(specifier.name.text, importedName);
348+
importedSymbolsByLocalName.set(specifier.name.text, {
349+
importedName,
350+
localName: specifier.name.text,
351+
moduleSpecifier,
352+
});
258353
}
259354
}
355+
} else if (
356+
bindings &&
357+
ts.isNamespaceImport(bindings) &&
358+
!statement.importClause?.isTypeOnly &&
359+
ts.isStringLiteral(statement.moduleSpecifier)
360+
) {
361+
namespaceImportsByLocalName.set(bindings.name.text, statement.moduleSpecifier.text);
260362
}
261363
continue;
262364
}
263365

366+
if (
367+
ts.isImportEqualsDeclaration(statement) &&
368+
!statement.isTypeOnly &&
369+
ts.isExternalModuleReference(statement.moduleReference) &&
370+
statement.moduleReference.expression &&
371+
ts.isStringLiteral(statement.moduleReference.expression)
372+
) {
373+
namespaceImportsByLocalName.set(
374+
statement.name.text,
375+
statement.moduleReference.expression.text,
376+
);
377+
continue;
378+
}
379+
264380
if (ts.isFunctionDeclaration(statement) && statement.name) {
265381
const name = statement.name.text;
266382
const declarations = localFunctions.get(name) ?? [];
@@ -325,6 +441,15 @@ export function collectModuleExportNames(content: string, fileName = "source.ts"
325441
continue;
326442
}
327443
const localName = specifier.propertyName?.text ?? specifier.name.text;
444+
if (statement.moduleSpecifier && ts.isStringLiteral(statement.moduleSpecifier)) {
445+
namedReExports.push({
446+
exportedName: specifier.name.text,
447+
importedName: localName,
448+
moduleSpecifier: statement.moduleSpecifier.text,
449+
});
450+
} else if (!statement.moduleSpecifier) {
451+
pendingLocalReExports.push({ exportedName: specifier.name.text, localName });
452+
}
328453
// Renamed re-exports are deliberately outside this guard's first slice.
329454
if (specifier.name.text !== localName) {
330455
continue;
@@ -337,11 +462,51 @@ export function collectModuleExportNames(content: string, fileName = "source.ts"
337462
}
338463
}
339464

465+
for (const reExport of pendingLocalReExports) {
466+
const imported = importedSymbolsByLocalName.get(reExport.localName);
467+
if (imported) {
468+
namedReExports.push({
469+
exportedName: reExport.exportedName,
470+
importedName: imported.importedName,
471+
moduleSpecifier: imported.moduleSpecifier,
472+
});
473+
}
474+
}
475+
340476
const definitions = new Set<string>();
477+
const valueDefinitions = new Map<string, ExportedValueDefinition>();
341478
for (const name of new Set([...directlyExportedNames, ...locallyExportedNames])) {
342479
const constDeclarations = localConstDeclarations.get(name);
343480
if (constDeclarations) {
344481
const [constDeclaration] = constDeclarations;
482+
if (constDeclarations.length === 1 && constDeclaration) {
483+
const importedReferences = collectImportedReferences(
484+
constDeclaration,
485+
importedSymbolsByLocalName,
486+
namespaceImportsByLocalName,
487+
);
488+
const initializer = constDeclaration.initializer
489+
? unwrapExpression(constDeclaration.initializer)
490+
: undefined;
491+
if (initializer) {
492+
const aliasSource = resolveImportedReference(
493+
initializer,
494+
importedSymbolsByLocalName,
495+
namespaceImportsByLocalName,
496+
);
497+
if (aliasSource) {
498+
importedReferences.push(aliasSource);
499+
}
500+
}
501+
valueDefinitions.set(name, {
502+
importedReferences: importedReferences.toSorted((left, right) =>
503+
`${left.moduleSpecifier}\0${left.importedName}\0${left.localName}`.localeCompare(
504+
`${right.moduleSpecifier}\0${right.importedName}\0${right.localName}`,
505+
),
506+
),
507+
name,
508+
});
509+
}
345510
if (
346511
constDeclarations.length === 1 &&
347512
constDeclaration &&
@@ -357,6 +522,16 @@ export function collectModuleExportNames(content: string, fileName = "source.ts"
357522
continue;
358523
}
359524
const implementation = functionDeclarations.find((declaration) => declaration.body);
525+
if (implementation?.body) {
526+
valueDefinitions.set(name, {
527+
importedReferences: collectImportedReferences(
528+
implementation.body,
529+
importedSymbolsByLocalName,
530+
namespaceImportsByLocalName,
531+
),
532+
name,
533+
});
534+
}
360535
// Lazy runtime facades are mandated by AGENTS.md. Exempt only exact same-name
361536
// argument forwarding so those boundaries do not become duplicate behavior.
362537
if (
@@ -368,10 +543,20 @@ export function collectModuleExportNames(content: string, fileName = "source.ts"
368543
definitions.add(name);
369544
}
370545

371-
return { definitions, exportedNames, starExportSpecifiers };
546+
return {
547+
definitions,
548+
exportedNames,
549+
namedReExports: namedReExports.toSorted((left, right) =>
550+
`${left.exportedName}\0${left.importedName}\0${left.moduleSpecifier}`.localeCompare(
551+
`${right.exportedName}\0${right.importedName}\0${right.moduleSpecifier}`,
552+
),
553+
),
554+
starExportSpecifiers,
555+
valueDefinitions,
556+
};
372557
}
373558

374-
function resolveStarExportPath(
559+
export function resolveExportModulePath(
375560
sourcePath: string,
376561
specifier: string,
377562
modulesByPath: ReadonlyMap<string, ModuleExports>,
@@ -411,7 +596,7 @@ function collectTransitiveExportNames(
411596
const nextVisiting = new Set(visiting).add(modulePath);
412597
const names = new Set(moduleExports.exportedNames);
413598
for (const specifier of moduleExports.starExportSpecifiers) {
414-
const targetPath = resolveStarExportPath(modulePath, specifier, modulesByPath);
599+
const targetPath = resolveExportModulePath(modulePath, specifier, modulesByPath);
415600
if (!targetPath) {
416601
continue;
417602
}

0 commit comments

Comments
 (0)