fix(javascript): respect lexical shadowing of require (#509)
Signed-off-by: RudyCelekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
@@ -98,6 +98,41 @@ describe("JavaScript semantic analysis: structure 1", () => {
|
||||
).toEqual(shadowReference);
|
||||
});
|
||||
|
||||
it.each([
|
||||
`function run(require) { const bus = require("electron").ipcMain; return bus; }`,
|
||||
`function run() { const { ipcMain: bus } = require("electron"); function require() {} return bus; }`,
|
||||
`{ const bus = require("electron").ipcMain; const require = localLoader; }`,
|
||||
`import require from "./loader.js"; const bus = require("electron").ipcMain;`,
|
||||
])(
|
||||
"does not assign CommonJS origins to lexically shadowed require",
|
||||
(source) => {
|
||||
const ir = analyzeJavaScriptSemantics(source);
|
||||
const bus = onlyBinding(ir, "bus");
|
||||
expect(bus.provenance.origins).toEqual([]);
|
||||
expect(ir.moduleLinks.filter(({ kind }) => kind === "require")).toEqual(
|
||||
[],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it("preserves unshadowed require origins through aliases and assignments", () => {
|
||||
const ir = analyzeJavaScriptSemantics(`
|
||||
const { ipcMain: bus } = require("electron");
|
||||
const forwarded = bus;
|
||||
let assigned;
|
||||
assigned = require("electron").ipcMain;
|
||||
{ const require = localLoader; require("./local.js"); }
|
||||
`);
|
||||
for (const name of ["bus", "forwarded", "assigned"])
|
||||
expect(origin(topLevelBinding(ir, name))).toEqual({
|
||||
specifier: "electron",
|
||||
importedPath: ["ipcMain"],
|
||||
});
|
||||
expect(
|
||||
ir.moduleLinks.filter(({ kind }) => kind === "require"),
|
||||
).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("propagates literal, template, object, conditional, and destructured values", () => {
|
||||
const ir = analyzeJavaScriptSemantics(`
|
||||
const prefix = "rea";
|
||||
|
||||
@@ -12,7 +12,6 @@ import {
|
||||
collectSemanticReferences,
|
||||
immutableSemanticBindings,
|
||||
immutableSemanticScopes,
|
||||
semanticModuleOrigin,
|
||||
semanticStaticPropertyName,
|
||||
} from "./javascriptSemanticProjection.js";
|
||||
import type {
|
||||
@@ -77,6 +76,9 @@ export const analyzeParsedJavaScriptSemantics = (
|
||||
): JavaScriptSemanticIr => {
|
||||
const state = createState(file.program);
|
||||
collectDefinitions(file.program, state);
|
||||
traverseJavaScriptAst(file.program, {
|
||||
enter: (node) => collectSemanticModuleLink(node, state),
|
||||
});
|
||||
const references = collectSemanticReferences(file.program, state);
|
||||
const parserPartial = file.errors.length > 0;
|
||||
const bindings = immutableSemanticBindings(state);
|
||||
@@ -159,7 +161,6 @@ const collectDefinitions = (
|
||||
state,
|
||||
});
|
||||
bindInnerDeclaration(node, parent, scope, state);
|
||||
collectSemanticModuleLink(node, state);
|
||||
},
|
||||
exit: (node) => {
|
||||
if (openedScopes.has(node)) stack.pop();
|
||||
@@ -329,17 +330,6 @@ const bindPattern = (input: BindPatternInput): void => {
|
||||
return;
|
||||
}
|
||||
if (t.isIdentifier(pattern)) {
|
||||
const baseOrigin = semanticModuleOrigin(initializer, []);
|
||||
const directOrigin =
|
||||
baseOrigin === undefined || projection.includes(null)
|
||||
? undefined
|
||||
: {
|
||||
...baseOrigin,
|
||||
importedPath: [
|
||||
...baseOrigin.importedPath,
|
||||
...projection.map((segment) => String(segment)),
|
||||
],
|
||||
};
|
||||
addBinding({
|
||||
state,
|
||||
scope,
|
||||
@@ -348,7 +338,6 @@ const bindPattern = (input: BindPatternInput): void => {
|
||||
mutable,
|
||||
definitionNode: pattern,
|
||||
initializer,
|
||||
...(directOrigin === undefined ? {} : { directOrigin }),
|
||||
projection,
|
||||
});
|
||||
return;
|
||||
|
||||
@@ -155,13 +155,14 @@ export const immutableSemanticBindings = (
|
||||
.sort((left, right) => compareCodePoints(left.bindingId, right.bindingId));
|
||||
|
||||
/** Recover a literal require origin plus an optional member chain. */
|
||||
export const semanticModuleOrigin = (
|
||||
const semanticModuleOrigin = (
|
||||
node: t.Node | null | undefined,
|
||||
projection: readonly (string | number | null)[],
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): JavaScriptModuleOrigin | undefined => {
|
||||
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
|
||||
const nested = t.isNode(node.object)
|
||||
? semanticModuleOrigin(node.object, projection)
|
||||
? semanticModuleOrigin(node.object, projection, state)
|
||||
: undefined;
|
||||
const member = semanticStaticPropertyName(node.property, node.computed);
|
||||
return nested === undefined || member === ""
|
||||
@@ -173,6 +174,11 @@ export const semanticModuleOrigin = (
|
||||
!t.isIdentifier(node.callee, { name: "require" })
|
||||
)
|
||||
return undefined;
|
||||
if (
|
||||
resolveSemanticBindingState(state, node.callee, "require") !== undefined ||
|
||||
semanticResolutionBlocked(state, node.callee, "require")
|
||||
)
|
||||
return undefined;
|
||||
const specifier = stringValue(node.arguments[0]);
|
||||
return specifier === undefined || projection.includes(null)
|
||||
? undefined
|
||||
@@ -290,7 +296,7 @@ const collectRequireLink = (
|
||||
node: t.VariableDeclarator,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): void => {
|
||||
const origin = semanticModuleOrigin(node.init, []);
|
||||
const origin = semanticModuleOrigin(node.init, [], state);
|
||||
if (origin === undefined) return;
|
||||
for (const binding of requirePatternBindings(node.id, origin.importedPath))
|
||||
addModuleLink(state, {
|
||||
@@ -336,7 +342,7 @@ const collectCommonJsExport = (
|
||||
): void => {
|
||||
const exportedName = commonJsExportName(node.left);
|
||||
if (exportedName === undefined) return;
|
||||
const origin = semanticModuleOrigin(node.right, []);
|
||||
const origin = semanticModuleOrigin(node.right, [], state);
|
||||
addModuleLink(state, {
|
||||
kind: "commonjs-export",
|
||||
specifier: origin?.specifier ?? null,
|
||||
|
||||
@@ -8,6 +8,7 @@ import type {
|
||||
} from "./javascriptSemanticIr.js";
|
||||
import {
|
||||
resolveSemanticBindingState,
|
||||
semanticResolutionBlocked,
|
||||
type JavaScriptSemanticAnalysisState,
|
||||
type JavaScriptSemanticBindingState,
|
||||
} from "./javascriptSemanticState.js";
|
||||
@@ -356,7 +357,7 @@ const provenanceForExpression = (
|
||||
node: t.Node,
|
||||
context: EvaluationContext,
|
||||
): JavaScriptBindingProvenance => {
|
||||
const required = requireOrigin(node);
|
||||
const required = requireOrigin(node, context.state);
|
||||
if (required !== undefined) return semanticOriginsProvenance([required]);
|
||||
if (t.isIdentifier(node)) {
|
||||
const binding = resolveSemanticBindingState(context.state, node, node.name);
|
||||
@@ -410,10 +411,13 @@ const provenanceForExpression = (
|
||||
return semanticLocalProvenance();
|
||||
};
|
||||
|
||||
const requireOrigin = (node: t.Node): JavaScriptModuleOrigin | undefined => {
|
||||
const requireOrigin = (
|
||||
node: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): JavaScriptModuleOrigin | undefined => {
|
||||
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
|
||||
if (!t.isNode(node.object)) return undefined;
|
||||
const nested = requireOrigin(node.object);
|
||||
const nested = requireOrigin(node.object, state);
|
||||
const member = memberKey(node);
|
||||
return nested === undefined || typeof member !== "string"
|
||||
? undefined
|
||||
@@ -424,6 +428,11 @@ const requireOrigin = (node: t.Node): JavaScriptModuleOrigin | undefined => {
|
||||
!t.isIdentifier(node.callee, { name: "require" })
|
||||
)
|
||||
return undefined;
|
||||
if (
|
||||
resolveSemanticBindingState(state, node.callee, "require") !== undefined ||
|
||||
semanticResolutionBlocked(state, node.callee, "require")
|
||||
)
|
||||
return undefined;
|
||||
const specifier = stringValue(node.arguments[0]);
|
||||
return specifier === undefined ? undefined : { specifier, importedPath: [] };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user