mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 10:50:44 +00:00
refactor: finish dynamic import cleanup
This commit is contained in:
57
test/scripts/check-dynamic-import-warts.test.ts
Normal file
57
test/scripts/check-dynamic-import-warts.test.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { findDynamicImportAdvisories } from "../../scripts/check-dynamic-import-warts.mjs";
|
||||
|
||||
describe("check-dynamic-import-warts", () => {
|
||||
it("flags runtime static plus dynamic imports of the same module", () => {
|
||||
const source = `
|
||||
import { run } from "./runtime.js";
|
||||
export async function start() {
|
||||
return await import("./runtime.js");
|
||||
}
|
||||
`;
|
||||
expect(findDynamicImportAdvisories(source)).toEqual([
|
||||
{
|
||||
line: 4,
|
||||
reason: 'runtime static + dynamic import of "./runtime.js" (static line 2)',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("ignores type-only static imports", () => {
|
||||
const source = `
|
||||
import { type Runtime } from "./runtime.js";
|
||||
export async function start(): Promise<Runtime> {
|
||||
return (await import("./runtime.js")).createRuntime();
|
||||
}
|
||||
`;
|
||||
expect(findDynamicImportAdvisories(source)).toEqual([]);
|
||||
});
|
||||
|
||||
it("flags repeated direct dynamic imports", () => {
|
||||
const source = `
|
||||
export async function one() {
|
||||
return await import("./runtime.js");
|
||||
}
|
||||
export async function two() {
|
||||
return await import("./runtime.js");
|
||||
}
|
||||
`;
|
||||
expect(findDynamicImportAdvisories(source)).toEqual([
|
||||
{
|
||||
line: 3,
|
||||
reason: 'repeated direct dynamic import of "./runtime.js" (2 callsites: 3, 6)',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("ignores cached loader patterns", () => {
|
||||
const source = `
|
||||
let runtimePromise: Promise<typeof import("./runtime.js")> | undefined;
|
||||
function loadRuntime() {
|
||||
runtimePromise ??= import("./runtime.js");
|
||||
return runtimePromise;
|
||||
}
|
||||
`;
|
||||
expect(findDynamicImportAdvisories(source)).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user