Skip resolveImports for files without subclasses
Add a cheap AST probe in file-pipeline to detect if a file contains extending classes (using fileHasExtendingClass) and return early to avoid the expensive resolveImports recursive descent when not needed. Update subclass-wrapper tests to import beforeAll and cache the plugin transform output and its stripped JS once in a beforeAll hook, replacing repeated transform calls with reused transformedOutput/strippedJs to reduce test runtime.
This commit is contained in:
1 parent
2c565fbd8d
commit
a434da9e85
2 files changed
+19
-7
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import ts from 'typescript';
|
||||
import { renderSubclassOverride } from '../codegen/subclass-override.js';
|
||||
import { fileHasExtendingClass } from './ast-helpers.js';
|
||||
import { resolveImports, type ParsedFileEntry } from './import-resolver.js';
|
||||
import {
|
||||
resolveSubclassWrappers,
|
||||
@@ -90,6 +91,10 @@ export function runSubclassWrapperPipelineFromPath(filePath: string): SubclassWr
|
||||
} catch {
|
||||
return EMPTY;
|
||||
}
|
||||
// Cheap AST scan first — most files have no extending classes and we can
|
||||
// skip the expensive `resolveImports` recursive descent entirely.
|
||||
const probeSf = ts.createSourceFile(filePath, code, ts.ScriptTarget.Latest, true);
|
||||
if (!fileHasExtendingClass(probeSf)) return EMPTY;
|
||||
const fileCache = new Map<string, ParsedFileEntry>();
|
||||
const imported = resolveImports(code, filePath, fileCache);
|
||||
return runSubclassWrapperPipeline(imported.sourceFile, fileCache);
|
||||
|
||||
@@ -2,7 +2,7 @@ import { readFileSync } from 'fs';
|
||||
import { dirname, resolve } from 'path';
|
||||
import ts from 'typescript';
|
||||
import { fileURLToPath } from 'url';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { beforeAll, describe, expect, it } from 'vitest';
|
||||
import { runSubclassWrapperPipelineFromPath } from '../../src/ast/file-pipeline';
|
||||
import { resolveImports, type ParsedFileEntry } from '../../src/ast/import-resolver';
|
||||
import { detectClassWrappersInFile } from '../../src/ast/static-wrapper';
|
||||
@@ -164,17 +164,24 @@ describe('cross-file inheritance', () => {
|
||||
});
|
||||
|
||||
describe('plugin transform integration', () => {
|
||||
function runPluginOnFixture(): string {
|
||||
// Transform output is deterministic, so cache it once for all tests in this
|
||||
// describe block; the alternative — transforming the fixture three times —
|
||||
// dominates this file's wall-clock cost.
|
||||
let transformedOutput: string;
|
||||
let strippedJs: string;
|
||||
|
||||
beforeAll(() => {
|
||||
const plugin = protobufVitePlugin();
|
||||
const code = loadFixture();
|
||||
const transformHook = plugin.transform!;
|
||||
const transformFn = typeof transformHook === 'function' ? transformHook : transformHook.handler;
|
||||
const result = transformFn.call({} as never, code, fixturePath);
|
||||
return (result as { code: string }).code;
|
||||
}
|
||||
transformedOutput = (result as { code: string }).code;
|
||||
strippedJs = toPlainJs(transformedOutput);
|
||||
});
|
||||
|
||||
it('appends concrete codecs + per-subclass overrides to the fixture output', () => {
|
||||
const out = runPluginOnFixture();
|
||||
const out = transformedOutput;
|
||||
|
||||
// Concrete codecs got generated for the types extracted from
|
||||
// serialize/deserialize annotations.
|
||||
@@ -211,7 +218,7 @@ describe('plugin transform integration', () => {
|
||||
}
|
||||
|
||||
it('end-to-end: DemoTransformer.encode(undefined, { param: 42 }) produces the expected wire bytes', () => {
|
||||
const stripped = toPlainJs(runPluginOnFixture());
|
||||
const stripped = strippedJs;
|
||||
// Silence the `console.log` that the fixture's deserialize() performs;
|
||||
// we only verify the encode path in this test, but the fixture's
|
||||
// top-level call exercises encode, not decode, so the stub is just a
|
||||
@@ -237,7 +244,7 @@ describe('plugin transform integration', () => {
|
||||
});
|
||||
|
||||
it('end-to-end: decode override forwards the bytes through deserialize', () => {
|
||||
const stripped = toPlainJs(runPluginOnFixture());
|
||||
const stripped = strippedJs;
|
||||
|
||||
const harness = `
|
||||
const console = { log: () => {} };
|
||||
|
||||
Reference in new issue
Block a user