Skip to content

Commit ac076f1

Browse files
thymikeeclaude
andcommitted
refactor(layering): read the entry's star exports as syntax
The pin matched `^[ \t]*export\s*\*` against the entry's text, so it refused a comment that mentions a star and accepted `export type * from`, which publishes a module's whole type surface just the same. Read the parsed AST instead: `ExportAllDeclaration` is the one node every star shape builds — `export *`, `export * as ns`, and both `export type` forms — and prose builds none. facade-exports.ts records the same failure mode from #1555: a hand-enumerated export-form scanner misses exactly the form that widens the surface. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8d652e3 commit ac076f1

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

‎scripts/layering/package-entry-surface.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ test('the entry names its exports: export * is refused', () => {
103103
for (const body of [
104104
"export * from './a.ts';",
105105
"export { a } from './a.ts';\nexport * as ns from './b.ts';",
106+
"export type * from './a.ts';",
107+
"export { a } from './a.ts';\nexport type * as ns from './b.ts';",
106108
]) {
107109
const found = check({}, new Map([['packages/test-kit/src/index.ts', body]]));
108110
assert.equal(found.length, 1, body);
@@ -114,6 +116,13 @@ test('the entry names its exports: export * is refused', () => {
114116
);
115117
});
116118

119+
test('text that only mentions a star is not an export', () => {
120+
const body =
121+
"// export * from './a.ts' is forbidden here\n" +
122+
"const note = `export * from './b.ts'`;\nexport { note };\nexport { a } from './a.ts';";
123+
assert.deepEqual(check({}, new Map([['packages/test-kit/src/index.ts', body]])), []);
124+
});
125+
117126
test('the daemon entry names its exports: export * is refused', () => {
118127
const sources = new Map(SOURCE);
119128
sources.set('packages/daemon/src/index.ts', "export * from './server/http/daemon-runtime.ts';");

‎scripts/layering/package-entry-surface.ts‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// Part of R83 package-export-surface. The dependency, import and reference-cycle half is the
1414
// shared walk in package-isolation.ts under TEST_KIT_ISOLATION.
1515

16+
import { parseSync } from 'oxc-parser';
17+
import { visitAst } from './layering-ast.ts';
1618
import type { LayeringViolation } from './model.ts';
1719
import { PACKAGE_EXPORT_SURFACE_RULE, TEST_KIT_PACKAGE } from './package-export-surface.ts';
1820
import {
@@ -87,16 +89,33 @@ function checkEntrySurface(
8789

8890
const entryFile = `${entrySurface.dir}/src/index.ts`;
8991
const entrySource = sources.get(entryFile);
90-
const starExport = entrySource === undefined ? null : /^[ \t]*export\s*\*/m.exec(entrySource);
91-
if (entrySource !== undefined && starExport) {
92-
const line = entrySource.slice(0, starExport.index).split('\n').length;
92+
const starLine = entrySource === undefined ? null : findStarExportLine(entryFile, entrySource);
93+
if (starLine !== null) {
9394
violations.push(
9495
violation(
9596
entryFile,
9697
`${entrySurface.packageName} names every export explicitly: \`export *\` is forbidden.`,
97-
line,
98+
starLine,
9899
),
99100
);
100101
}
101102
return violations;
102103
}
104+
105+
/**
106+
* The line of the first star re-export in an entry's source, or null when it names everything.
107+
* Read as syntax rather than text, for the reason `facade-exports.ts` records from #1555: a form
108+
* scanner has to enumerate every export FORM by hand and the one it forgets slips through.
109+
* `ExportAllDeclaration` is the one node every star shape builds — `export *`, `export * as ns`,
110+
* and their `export type` forms, which publish a module's surface under one or zero names — while a
111+
* comment or template literal that merely mentions a star declares no node at all.
112+
*/
113+
function findStarExportLine(entryFile: string, source: string): number | null {
114+
let start: number | null = null;
115+
visitAst(parseSync(entryFile, source).program, (node) => {
116+
if (start === null && node.type === 'ExportAllDeclaration' && typeof node.start === 'number') {
117+
start = node.start;
118+
}
119+
});
120+
return start === null ? null : source.slice(0, start).split('\n').length;
121+
}

0 commit comments

Comments
 (0)