feat(javascript): resolve exact package import aliases (#1862)
* feat(javascript): resolve exact package import aliases Resolve exact #alias specifiers against the importer's enclosing package.json imports map using the shared condition, array, and null target selection. Nested packages keep their own maps; missing keys, explicit nulls, and out-of-scope aliases report a precise reason. Wildcard, recursive, and external-package import targets remain unresolved. * fix(javascript): keep nested import aliases in CLI graphs Bound nested #alias requires produce application-graph require edges from the enclosing package. Node reports MODULE_NOT_FOUND when a nested package.json has no imports field, without inheriting the ancestor map. * fix(javascript): resolve literal package import keys before path admission * fix(javascript): preserve import keys and invalid-target fallback * fix(javascript): preserve unmatched package target arrays * fix(javascript): retain package target refusal evidence * test(browser): match real verifier startup budget --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { createRequire } from "node:module";
|
||||
import { dirname, join, relative } from "node:path";
|
||||
|
||||
import { expect } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js";
|
||||
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
const literalAliases = ["#/x", "#x%2Fy", "#x%2Ey", "#x\\y", "#x\0y"];
|
||||
const fallbackAliases = {
|
||||
"#inactive-array": {
|
||||
node: [{ browser: "./browser.cjs" }],
|
||||
default: "./config.cjs",
|
||||
},
|
||||
"#nested-inactive-array": {
|
||||
node: [[{ browser: "./browser.cjs" }]],
|
||||
default: "./config.cjs",
|
||||
},
|
||||
"#parent": ["../outside.cjs", "./config.cjs"],
|
||||
"#absolute": { node: [["/outside.cjs"], "./config.cjs"] },
|
||||
"#url": {
|
||||
import: ["https://example.invalid/outside.cjs", "./config.cjs"],
|
||||
require: ["file:///outside.cjs", "./config.cjs"],
|
||||
},
|
||||
};
|
||||
const resolvedAliases = [...literalAliases, ...Object.keys(fallbackAliases)];
|
||||
const refusedAliases = {
|
||||
"#empty": { target: [], limitation: "an empty imports target array" },
|
||||
"#empty-active": {
|
||||
target: { node: [] },
|
||||
limitation: "an empty imports target array",
|
||||
},
|
||||
"#null-last": {
|
||||
target: [[], null],
|
||||
limitation: "an explicit null imports target",
|
||||
},
|
||||
"#empty-last": {
|
||||
target: [null, []],
|
||||
limitation: "an empty imports target array",
|
||||
},
|
||||
"#inactive": {
|
||||
target: [{ browser: "./browser.cjs" }],
|
||||
limitation: "it declares browser.",
|
||||
},
|
||||
"#nested-inactive": {
|
||||
target: [
|
||||
[{ browser: "./browser.cjs" }, { "rea-inactive": "./inactive.cjs" }],
|
||||
],
|
||||
limitation: "it declares browser, rea-inactive.",
|
||||
},
|
||||
};
|
||||
const allAliases = [...resolvedAliases, ...Object.keys(refusedAliases)];
|
||||
|
||||
const createPackageImportAliasFixture = async () => {
|
||||
const root = await createTestTempDirectory("rea-package-import-aliases-");
|
||||
const mainSource = [
|
||||
'const config = require("#config");',
|
||||
'const nested = require("./node_modules/fixture/index.cjs");',
|
||||
'const missing = require("#missing");',
|
||||
...allAliases.map((specifier) => `require(${JSON.stringify(specifier)});`),
|
||||
].join("\n");
|
||||
const moduleSource = allAliases
|
||||
.map((specifier) => `import ${JSON.stringify(specifier)};`)
|
||||
.join("\n");
|
||||
const nestedSource = [
|
||||
'const config = require("#config");',
|
||||
"module.exports = config;",
|
||||
].join("\n");
|
||||
const files = {
|
||||
"package.json": JSON.stringify({
|
||||
name: "app",
|
||||
imports: {
|
||||
"#config": "./config.cjs",
|
||||
...Object.fromEntries(
|
||||
literalAliases.map((specifier) => [specifier, "./config.cjs"]),
|
||||
),
|
||||
...fallbackAliases,
|
||||
...Object.fromEntries(
|
||||
Object.entries(refusedAliases).map(([specifier, { target }]) => [
|
||||
specifier,
|
||||
target,
|
||||
]),
|
||||
),
|
||||
},
|
||||
}),
|
||||
"main.cjs": mainSource,
|
||||
"main.mjs": moduleSource,
|
||||
"config.cjs": "module.exports = 'app-config';",
|
||||
"node_modules/fixture/package.json": JSON.stringify({
|
||||
name: "fixture",
|
||||
imports: { "#config": "./local.cjs" },
|
||||
}),
|
||||
"node_modules/fixture/index.cjs": nestedSource,
|
||||
"node_modules/fixture/local.cjs": "module.exports = 'fixture-config';",
|
||||
};
|
||||
await Promise.all(
|
||||
Object.entries(files).map(async ([path, content]) => {
|
||||
const absolute = join(root, path);
|
||||
await mkdir(dirname(absolute), { recursive: true });
|
||||
await writeFile(absolute, content);
|
||||
}),
|
||||
);
|
||||
return { root, mainSource, moduleSource, nestedSource };
|
||||
};
|
||||
|
||||
cliTest.for(["CLI", "stdio MCP"] as const)(
|
||||
"resolves exact package import aliases with package scope through %s",
|
||||
async (surface, { cli }) => {
|
||||
const { root, mainSource, moduleSource, nestedSource } =
|
||||
await createPackageImportAliasFixture();
|
||||
const nativeRequire = createRequire(join(root, "main.cjs"));
|
||||
expect(
|
||||
relative(root, nativeRequire.resolve("#config")).replaceAll("\\", "/"),
|
||||
).toBe("config.cjs");
|
||||
expect(
|
||||
relative(
|
||||
root,
|
||||
createRequire(join(root, "node_modules/fixture/index.cjs")).resolve(
|
||||
"#config",
|
||||
),
|
||||
).replaceAll("\\", "/"),
|
||||
).toBe("node_modules/fixture/local.cjs");
|
||||
expect(() => nativeRequire.resolve("#missing")).toThrow(
|
||||
expect.objectContaining({ code: "ERR_PACKAGE_IMPORT_NOT_DEFINED" }),
|
||||
);
|
||||
|
||||
let document: unknown;
|
||||
if (surface === "CLI") {
|
||||
const response = await cli.run({
|
||||
arguments: ["analyze-javascript-application", root, "--format", "json"],
|
||||
environment: { REA_LOG_LEVEL: "silent" },
|
||||
});
|
||||
expect(response.exitCode, response.stderr).toBe(0);
|
||||
document = response.json;
|
||||
} else {
|
||||
const { call } = await connectLocalToolsMcp();
|
||||
const response = await call("analyze_javascript_application", {
|
||||
input_path: root,
|
||||
format: "directory",
|
||||
});
|
||||
expect(response.isError, JSON.stringify(response)).not.toBe(true);
|
||||
document = response.structuredContent;
|
||||
}
|
||||
const { graph } = z
|
||||
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
|
||||
.parse(document).normalized_result;
|
||||
for (const specifier of resolvedAliases)
|
||||
for (const [moduleKind, sourcePath, source] of [
|
||||
["require", "main.cjs", mainSource],
|
||||
["import", "main.mjs", moduleSource],
|
||||
] as const)
|
||||
expect(graph.edges).toContainEqual(
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
module_link_kind: moduleKind,
|
||||
specifier,
|
||||
resolved_path: "config.cjs",
|
||||
resolution_status: "resolved",
|
||||
}),
|
||||
evidence: sourceEvidence(sourcePath, source),
|
||||
}),
|
||||
);
|
||||
const imports = graph.edges.filter(
|
||||
({ properties }) => properties.module_link_kind === "require",
|
||||
);
|
||||
for (const [specifier, { limitation }] of Object.entries(refusedAliases))
|
||||
for (const [moduleKind, sourcePath, source] of [
|
||||
["require", "main.cjs", mainSource],
|
||||
["import", "main.mjs", moduleSource],
|
||||
] as const)
|
||||
expect(graph.edges).toContainEqual(
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
module_link_kind: moduleKind,
|
||||
specifier,
|
||||
resolved_path: null,
|
||||
resolution_status: "external",
|
||||
}),
|
||||
evidence: sourceEvidence(sourcePath, source, limitation),
|
||||
}),
|
||||
);
|
||||
expect(imports).toContainEqual(
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
specifier: "#config",
|
||||
resolved_path: "config.cjs",
|
||||
resolution_status: "resolved",
|
||||
}),
|
||||
evidence: sourceEvidence("main.cjs", mainSource),
|
||||
}),
|
||||
);
|
||||
expect(imports).toContainEqual(
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
specifier: "#config",
|
||||
resolved_path: "node_modules/fixture/local.cjs",
|
||||
resolution_status: "resolved",
|
||||
}),
|
||||
evidence: sourceEvidence(
|
||||
"node_modules/fixture/index.cjs",
|
||||
nestedSource,
|
||||
),
|
||||
}),
|
||||
);
|
||||
expect(imports).toContainEqual(
|
||||
expect.objectContaining({
|
||||
properties: expect.objectContaining({
|
||||
specifier: "#missing",
|
||||
resolved_path: null,
|
||||
resolution_status: "external",
|
||||
}),
|
||||
evidence: expect.objectContaining({
|
||||
limitations: expect.arrayContaining([
|
||||
expect.stringContaining("does not define package import alias"),
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
const sourceEvidence = (
|
||||
sourcePath: string,
|
||||
source: string,
|
||||
limitation?: string,
|
||||
): unknown =>
|
||||
expect.objectContaining({
|
||||
artifact: expect.objectContaining({
|
||||
available: true,
|
||||
sha256: createHash("sha256").update(source).digest("hex"),
|
||||
}),
|
||||
location: expect.objectContaining({
|
||||
available: true,
|
||||
value: expect.objectContaining({
|
||||
kind: "source-range",
|
||||
source: sourcePath,
|
||||
}),
|
||||
}),
|
||||
...(limitation === undefined
|
||||
? {}
|
||||
: {
|
||||
limitations: expect.arrayContaining([
|
||||
expect.stringContaining(limitation),
|
||||
]),
|
||||
}),
|
||||
});
|
||||
Reference in New Issue
Block a user