mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 21:23:45 +00:00
* fix(javascript): honor loader semantics for BOM hashbangs * fix(javascript): retain conditional BOM hashbang imports * fix(javascript): share conditional hashbang recovery * fix(javascript): recover suffix-only source imports * fix(javascript): retain conditional CommonJS hashbang imports * fix(javascript): preserve conditional hashbang dependency evidence * fix(javascript): preserve ambiguous dotfile and worker evidence * fix(javascript): qualify semantic evidence with source loader conditions * fix(javascript): qualify all source-backed application evidence * test(javascript): identify imports by their projected kind --------- Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
144 lines
4.3 KiB
TypeScript
144 lines
4.3 KiB
TypeScript
import { expect, it } from "vitest";
|
|
|
|
import { javaScriptMemberMutationRefusal } from "./javascript/javascriptSemanticWorkAllowance.js";
|
|
import { parseReferenceSourceImports } from "./referenceSourceImportParsing.js";
|
|
|
|
/** Conditional self-selections before one member write (#8294). */
|
|
const SOURCE = [
|
|
"function probe(input, env) { var m; if (env.first) m = env.first.info;",
|
|
...Array.from(
|
|
{ length: 4 },
|
|
(_, index) =>
|
|
`if (/case${String(index)}/.test(input)) m = m[${String(index % 2)}];`,
|
|
),
|
|
"m[1] = input.length; return m; }",
|
|
"module.exports = probe;",
|
|
'const other = require.call(null, "./other");',
|
|
'import("./lazy");',
|
|
].join("\n");
|
|
|
|
it("keeps declaration-level imports when the member-mutation phase is refused", () => {
|
|
const admitted = parseReferenceSourceImports(
|
|
"probe.js",
|
|
Buffer.from(SOURCE),
|
|
"JavaScript",
|
|
"commonjs",
|
|
);
|
|
expect(admitted.relationships.map(({ to, kind }) => [kind, to])).toEqual(
|
|
expect.arrayContaining([
|
|
["requires", "./other"],
|
|
["imports", "./lazy"],
|
|
]),
|
|
);
|
|
const refused = parseReferenceSourceImports(
|
|
"probe.js",
|
|
Buffer.from(SOURCE),
|
|
"JavaScript",
|
|
"commonjs",
|
|
{ aliasStates: 1, source: "setting" },
|
|
);
|
|
expect(refused.relationships.map(({ to, kind }) => [kind, to])).toEqual([
|
|
["imports", "./lazy"],
|
|
]);
|
|
expect(refused.parse_failures).toEqual([]);
|
|
// The limitation carries the owning formatter's recovery for this file.
|
|
const owner = javaScriptMemberMutationRefusal(
|
|
"analyze_javascript_semantics",
|
|
{
|
|
aliasStates: 1,
|
|
source: "setting",
|
|
},
|
|
);
|
|
expect(
|
|
refused.limitations.filter(
|
|
(limitation) =>
|
|
limitation.includes("probe.js") &&
|
|
limitation.includes(owner.remediationAction ?? "<missing>"),
|
|
),
|
|
).toHaveLength(1);
|
|
});
|
|
|
|
it.each([
|
|
["main.mjs", "commonjs", true],
|
|
["main.js", "module", true],
|
|
["main.cjs", "module", true],
|
|
["main.js", "commonjs", true],
|
|
["main.mts", "commonjs", true],
|
|
["main.cts", "module", true],
|
|
["main.ts", "commonjs", true],
|
|
["main.tsx", "module", true],
|
|
["main.TSX", "module", true],
|
|
["main.jsx", "commonjs", true],
|
|
[".js", "module", true],
|
|
[".mjs", "module", true],
|
|
[".mjs", "commonjs", true],
|
|
[".js", "commonjs", true],
|
|
[".cjs", "module", true],
|
|
[".CJS", "module", true],
|
|
[".JS", "module", true],
|
|
[".MJS", "module", true],
|
|
[".cjs", "commonjs", true],
|
|
] as const)(
|
|
"retains the loader's BOM/hashbang semantics for %s (%s)",
|
|
(path, packageType, valid) => {
|
|
const esm =
|
|
(path.endsWith(".mjs") && path !== ".mjs") ||
|
|
path.endsWith(".mts") ||
|
|
path.endsWith(".jsx") ||
|
|
(path.startsWith(".") && packageType === "module") ||
|
|
(!path.endsWith(".cjs") &&
|
|
!path.endsWith(".cts") &&
|
|
packageType === "module");
|
|
const statement = esm ? 'import "./dep.mjs";' : 'require("./dep.cjs");';
|
|
const parsed = parseReferenceSourceImports(
|
|
path,
|
|
Buffer.from(`\uFEFF#!/usr/bin/env node\n${statement}`),
|
|
path.endsWith("js") ? "JavaScript" : "TypeScript",
|
|
packageType,
|
|
);
|
|
if (valid) {
|
|
expect(parsed.parse_failures).toEqual([]);
|
|
expect(parsed.relationships).toMatchObject([
|
|
{
|
|
to: esm ? "./dep.mjs" : "./dep.cjs",
|
|
parse_state:
|
|
path.startsWith(".") ||
|
|
path === "main.cjs" ||
|
|
(path === "main.js" && packageType === "commonjs")
|
|
? "partial"
|
|
: "parsed",
|
|
},
|
|
]);
|
|
} else {
|
|
expect(parsed.parse_failures).toHaveLength(1);
|
|
expect(parsed.relationships).toEqual([]);
|
|
}
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
["main.js", "unknown"],
|
|
["main.js", "unspecified"],
|
|
[".cjs", "unknown"],
|
|
[".js", "unspecified"],
|
|
] as const)(
|
|
"retains a conditional dependency without a strict-mode error for %s in a %s package",
|
|
(path, mode) => {
|
|
const parsed = parseReferenceSourceImports(
|
|
path,
|
|
Buffer.from(
|
|
'\uFEFF#!/usr/bin/env node\nwith ({}) {}\nrequire("./dep.cjs");',
|
|
),
|
|
"JavaScript",
|
|
mode,
|
|
);
|
|
expect(parsed.parse_failures).toEqual([]);
|
|
expect(parsed.relationships).toMatchObject([
|
|
{ to: "./dep.cjs", parse_state: "partial" },
|
|
]);
|
|
expect(parsed.limitations).toContainEqual(
|
|
expect.stringContaining("package loader mode is unknown"),
|
|
);
|
|
},
|
|
);
|