Skip to content

Commit ba9de23

Browse files
committed
Address review: non-mutating test helper, drop as-cast, split stranded comment
1 parent ffc0a88 commit ba9de23

2 files changed

Lines changed: 48 additions & 42 deletions

File tree

packages/plugins/apps/src/vite/backend-module-graph-collector.test.ts

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@ import { parseAst } from 'rollup/parseAst';
66

77
import { createBackendModuleGraphCollector } from './backend-module-graph-collector';
88

9-
type FakeModuleInfo = { id: string; code?: string | null };
9+
type FakeModuleInfo = { id: string; code: string | null };
1010

1111
/**
12-
* Invokes the hook with a plugin context exposing `parse`, which is where it gets
13-
* its parser. `rollup/parseAst` is what Rollup's real context uses.
12+
* Calls the `moduleParsed` hook with a plugin context exposing `parse`, which is
13+
* where it takes its parser from. `rollup/parseAst` is what Rollup's real
14+
* context supplies. The hook reads only the few `ModuleInfo` fields these fakes
15+
* model, so building a complete one would be noise.
1416
*/
17+
const getModuleParsedHook = (collector: ReturnType<typeof createBackendModuleGraphCollector>) => {
18+
const hook = collector.plugin.moduleParsed;
19+
if (typeof hook !== 'function') {
20+
throw new Error('Expected "moduleParsed" to be a function hook.');
21+
}
22+
23+
return (moduleInfo: object) => Reflect.apply(hook, { parse: parseAst }, [moduleInfo]);
24+
};
25+
1526
const getEmit = (collector: ReturnType<typeof createBackendModuleGraphCollector>) => {
16-
const moduleParsed = collector.plugin.moduleParsed as (
17-
this: { parse: typeof parseAst },
18-
moduleInfo: unknown,
19-
) => void;
27+
const callHook = getModuleParsedHook(collector);
2028

21-
// Fills the remaining fields in place rather than spreading into a new
22-
// object: a spread would drop (or trigger) an `ast` getter, and one case
23-
// below depends on that getter surviving intact.
24-
return (moduleInfo: FakeModuleInfo, importedIds: string[] = []) =>
25-
moduleParsed.call(
26-
{ parse: parseAst },
27-
Object.assign(moduleInfo, {
28-
importedIds,
29-
importedIdResolutions: importedIds.map((id) => ({ id })),
30-
}),
31-
);
29+
return (moduleInfo: FakeModuleInfo, importedIds: string[] = []) => {
30+
const importedIdResolutions = importedIds.map((id) => ({ id }));
31+
callHook({ ...moduleInfo, importedIds, importedIdResolutions });
32+
};
3233
};
3334

3435
describe('Backend Functions - backend module graph collector', () => {
@@ -70,23 +71,28 @@ describe('Backend Functions - backend module graph collector', () => {
7071

7172
test('Should collect records under a bundler that does not support ModuleInfo#ast', () => {
7273
const collector = createBackendModuleGraphCollector('/project');
73-
const emit = getEmit(collector);
74+
const callHook = getModuleParsedHook(collector);
7475

75-
// Rolldown, the bundler Vite 8 uses by default, keeps `ast` on its
76-
// Rollup-compat object but stubs the getter to throw. Reading the
77-
// property at all is the failure, so it must throw rather than be absent.
78-
const moduleInfo: FakeModuleInfo = Object.defineProperty(
79-
{ id: '/project/src/backend/actions.backend.ts', code: 'export const id = "conn-1";' },
80-
'ast',
81-
{
82-
get() {
83-
throw new Error('UNSUPPORTED: ModuleInfo#ast');
84-
},
85-
enumerable: true,
76+
// Rolldown, Vite 8's default bundler, keeps `ast` on its Rollup-compat
77+
// object but stubs the getter to throw. Reading the property at all is
78+
// the failure, so it has to throw rather than be absent — which is also
79+
// why this is assembled in place instead of going through `getEmit`,
80+
// whose spread would trigger the getter during setup.
81+
const moduleInfo = {
82+
id: '/project/src/backend/actions.backend.ts',
83+
code: 'export const id = "conn-1";',
84+
importedIds: [],
85+
importedIdResolutions: [],
86+
};
87+
Object.defineProperty(moduleInfo, 'ast', {
88+
get() {
89+
throw new Error('UNSUPPORTED: ModuleInfo#ast');
8690
},
87-
);
91+
enumerable: true,
92+
});
93+
94+
callHook(moduleInfo);
8895

89-
expect(() => emit(moduleInfo)).not.toThrow();
9096
expect([...collector.getModuleRecords().keys()]).toEqual([
9197
'/project/src/backend/actions.backend.ts',
9298
]);

packages/plugins/apps/src/vite/backend-module-graph-collector.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,27 @@ export function createBackendModuleGraphCollector(buildRoot: string): BackendMod
3737
return;
3838
}
3939

40-
// Parse the source instead of reading `moduleInfo.ast`: Rolldown,
41-
// the bundler Vite 8 uses by default, stubs that getter to throw
42-
// `UNSUPPORTED: ModuleInfo#ast`. `code` is null for external and
43-
// synthetic modules.
44-
//
45-
// `this.parse` is the bundler's own parser, which already parsed
46-
// this exact source to compute `importedIds` — so whatever the
47-
// bundler accepted parses here too, and no TypeScript-capable
48-
// parser is needed (`moduleParsed` runs after `transform`, so
49-
// types and JSX are already gone).
40+
// External and synthetic modules have no source to parse.
5041
if (typeof moduleInfo.code !== 'string') {
5142
return;
5243
}
5344

45+
// Parse the source instead of reading `moduleInfo.ast`: Rolldown,
46+
// the bundler Vite 8 uses by default, stubs that getter to throw
47+
// `UNSUPPORTED: ModuleInfo#ast`. Using the context's own parser
48+
// is also correct by construction — it already accepted this
49+
// exact source to compute `importedIds` — and needs no
50+
// TypeScript support, since `moduleParsed` runs after
51+
// `transform`, so types and JSX are already gone.
5452
const parsed = this.parse(moduleInfo.code);
5553
const record = createParsedModuleRecord(
5654
moduleId,
5755
buildRoot,
5856
parsed,
5957
getStaticDependencyIds(moduleInfo).map(normalizeViteModuleId),
6058
);
59+
// Only null when the traversal predicate rejects, which the guard
60+
// above already covered; kept to narrow the nullable return type.
6161
if (!record) {
6262
return;
6363
}

0 commit comments

Comments
 (0)