fix(javascript): parse explicit CommonJS wrapper syntax (#2475)
This commit is contained in:
@@ -37,10 +37,19 @@ export const parserPluginsForPath = (sourcePath?: string): ParserPlugin[] => {
|
|||||||
: ["decorators-legacy", typescript];
|
: ["decorators-legacy", typescript];
|
||||||
};
|
};
|
||||||
|
|
||||||
/** `.mjs` and `.mts` are modules by extension; other paths leave the mode to syntax. */
|
/** `.mjs` and `.mts` explicitly select module mode. */
|
||||||
export const isExplicitModuleSourcePath = (path?: string): boolean =>
|
export const isExplicitModuleSourcePath = (path?: string): boolean =>
|
||||||
/\.m[jt]s$/iu.test(path ?? "");
|
/\.m[jt]s$/iu.test(path ?? "");
|
||||||
|
|
||||||
|
const runtimeSourceTypeForPath = (
|
||||||
|
path?: string,
|
||||||
|
): "commonjs" | "module" | "unambiguous" =>
|
||||||
|
/\.cjs$/iu.test(path ?? "")
|
||||||
|
? "commonjs"
|
||||||
|
: isExplicitModuleSourcePath(path)
|
||||||
|
? "module"
|
||||||
|
: "unambiguous";
|
||||||
|
|
||||||
export interface RecoveredJavaScriptParse {
|
export interface RecoveredJavaScriptParse {
|
||||||
readonly file: ParsedJavaScriptSource;
|
readonly file: ParsedJavaScriptSource;
|
||||||
/** Errors that apply only if a source of unknown mode is an ES module. */
|
/** Errors that apply only if a source of unknown mode is an ES module. */
|
||||||
@@ -58,25 +67,25 @@ export const parseRecoveredJavaScript = (
|
|||||||
sourcePath: string | undefined,
|
sourcePath: string | undefined,
|
||||||
options: Omit<ParserOptions, "sourceType" | "errorRecovery">,
|
options: Omit<ParserOptions, "sourceType" | "errorRecovery">,
|
||||||
): RecoveredJavaScriptParse => {
|
): RecoveredJavaScriptParse => {
|
||||||
// `.cjs` is CommonJS by extension, so module-only errors cannot apply.
|
const sourceType = runtimeSourceTypeForPath(sourcePath);
|
||||||
if (/\.cjs$/iu.test(sourcePath ?? ""))
|
if (sourceType !== "unambiguous") {
|
||||||
|
const normalizedOptions = { ...options };
|
||||||
|
if (sourceType === "commonjs") {
|
||||||
|
// Babel owns these wrapper semantics in CommonJS mode and rejects
|
||||||
|
// explicit script overrides, including the reference-import options.
|
||||||
|
delete normalizedOptions.allowAwaitOutsideFunction;
|
||||||
|
delete normalizedOptions.allowReturnOutsideFunction;
|
||||||
|
delete normalizedOptions.allowNewTargetOutsideFunction;
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
file: parse(source, {
|
file: parse(source, {
|
||||||
...options,
|
...normalizedOptions,
|
||||||
sourceType: "script",
|
sourceType,
|
||||||
errorRecovery: true,
|
|
||||||
}),
|
|
||||||
moduleOnlyErrors: [],
|
|
||||||
};
|
|
||||||
if (isExplicitModuleSourcePath(sourcePath))
|
|
||||||
return {
|
|
||||||
file: parse(source, {
|
|
||||||
...options,
|
|
||||||
sourceType: "module",
|
|
||||||
errorRecovery: true,
|
errorRecovery: true,
|
||||||
}),
|
}),
|
||||||
moduleOnlyErrors: [],
|
moduleOnlyErrors: [],
|
||||||
};
|
};
|
||||||
|
}
|
||||||
const file = parse(source, {
|
const file = parse(source, {
|
||||||
...options,
|
...options,
|
||||||
sourceType: "unambiguous",
|
sourceType: "unambiguous",
|
||||||
@@ -98,18 +107,34 @@ export const parseRecoveredJavaScript = (
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Parse JavaScript or TypeScript once without attaching comments to AST nodes. */
|
/** Parse inert JavaScript or TypeScript facts without attaching comments to AST nodes. */
|
||||||
export const parseJavaScriptSource = (
|
export const parseJavaScriptSource = (
|
||||||
source: string,
|
source: string,
|
||||||
sourcePath?: string,
|
sourcePath?: string,
|
||||||
): ParsedJavaScriptSource | null => {
|
): ParsedJavaScriptSource | null => {
|
||||||
try {
|
try {
|
||||||
return parse(source, {
|
const runtimeSourceType = runtimeSourceTypeForPath(sourcePath);
|
||||||
sourceType: "unambiguous",
|
const commonjs = runtimeSourceType === "commonjs";
|
||||||
|
const options = {
|
||||||
errorRecovery: true,
|
errorRecovery: true,
|
||||||
attachComment: false,
|
attachComment: false,
|
||||||
plugins: parserPluginsForPath(sourcePath),
|
plugins: parserPluginsForPath(sourcePath),
|
||||||
|
};
|
||||||
|
const file = parse(source, {
|
||||||
|
...options,
|
||||||
|
// Inert artifact analysis retains module declarations even in .cjs assets.
|
||||||
|
// Infer their syntax here; reference imports keep filename-mode diagnostics.
|
||||||
|
sourceType: commonjs ? "unambiguous" : runtimeSourceType,
|
||||||
|
allowReturnOutsideFunction: commonjs,
|
||||||
|
allowNewTargetOutsideFunction: commonjs,
|
||||||
});
|
});
|
||||||
|
// Unambiguous mode retains module-only diagnostics after choosing a script.
|
||||||
|
// The .cjs extension establishes wrapper mode, so obtain its own diagnostics.
|
||||||
|
return commonjs &&
|
||||||
|
file.program.sourceType === "script" &&
|
||||||
|
file.errors.length > 0
|
||||||
|
? parse(source, { ...options, sourceType: "commonjs" })
|
||||||
|
: file;
|
||||||
} catch (cause: unknown) {
|
} catch (cause: unknown) {
|
||||||
// Unparseable source is represented by the null return.
|
// Unparseable source is represented by the null return.
|
||||||
void cause;
|
void cause;
|
||||||
|
|||||||
@@ -6,6 +6,31 @@ import { expect, it } from "vitest";
|
|||||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||||
import { importReferenceSource } from "../../support/referenceSourceResourceScope.js";
|
import { importReferenceSource } from "../../support/referenceSourceResourceScope.js";
|
||||||
|
|
||||||
|
it("imports valid CommonJS wrapper syntax without inventing a parse failure", async () => {
|
||||||
|
const root = await createTestTempDirectory("rea-reference-commonjs-mode-");
|
||||||
|
await Promise.all([
|
||||||
|
writeFile(
|
||||||
|
join(root, "main.cjs"),
|
||||||
|
'void new.target; require("./dep.cjs"); return;\n',
|
||||||
|
),
|
||||||
|
writeFile(join(root, "dep.cjs"), "module.exports = 1;\n"),
|
||||||
|
]);
|
||||||
|
const imported = await importReferenceSource({
|
||||||
|
root,
|
||||||
|
caller: "native-commonjs-source",
|
||||||
|
policy: { secretPatterns: [] },
|
||||||
|
});
|
||||||
|
if (!imported.ok) throw imported.error;
|
||||||
|
expect(imported.value.parse_failures).toEqual([]);
|
||||||
|
expect(imported.value.relationships).toContainEqual({
|
||||||
|
from_path: "main.cjs",
|
||||||
|
to: "dep.cjs",
|
||||||
|
kind: "requires",
|
||||||
|
resolution: "internal",
|
||||||
|
parse_state: "parsed",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it.each([
|
it.each([
|
||||||
["main.ts", "dep.js", "dep.ts"],
|
["main.ts", "dep.js", "dep.ts"],
|
||||||
["main.mts", "dep.mjs", "dep.mts"],
|
["main.mts", "dep.mjs", "dep.mts"],
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { expect } from "vitest";
|
||||||
|
|
||||||
|
import { parseJavaScriptSource } from "../../../src/domain/javascript/javascriptSourceParser.js";
|
||||||
|
import { processTest } from "../../support/process/processFixture.js";
|
||||||
|
|
||||||
|
for (const source of [
|
||||||
|
"return;",
|
||||||
|
"void new.target;",
|
||||||
|
"var await = 1; return;",
|
||||||
|
"with ({ value: 1 }) { void value; } void new.target;",
|
||||||
|
]) {
|
||||||
|
processTest(
|
||||||
|
`accepts Node CommonJS syntax while retaining module errors: ${source}`,
|
||||||
|
async ({ processes }) => {
|
||||||
|
const commonjs = await processes.run(
|
||||||
|
process.execPath,
|
||||||
|
["--check", "--input-type=commonjs"],
|
||||||
|
{ input: source, timeoutMs: 5_000 },
|
||||||
|
);
|
||||||
|
expect(commonjs.exitCode).toBe(0);
|
||||||
|
const module = await processes.run(
|
||||||
|
process.execPath,
|
||||||
|
["--check", "--input-type=module"],
|
||||||
|
{ input: source, timeoutMs: 5_000 },
|
||||||
|
);
|
||||||
|
expect(module.exitCode).not.toBe(0);
|
||||||
|
|
||||||
|
const parsed = parseJavaScriptSource(source, "main.cjs");
|
||||||
|
expect(parsed).not.toBeNull();
|
||||||
|
expect(parsed?.errors).toEqual([]);
|
||||||
|
expect(
|
||||||
|
parseJavaScriptSource(source, "main.mjs")?.errors.length,
|
||||||
|
).toBeGreaterThan(0);
|
||||||
|
expect(
|
||||||
|
parseJavaScriptSource(source, "main.mts")?.errors.length,
|
||||||
|
).toBeGreaterThan(0);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user