fix(javascript): preserve import-equals declaration and export evidence (#2062)
* fix(javascript): retain TypeScript import-equals dependencies * test(mcp): align completion checks with warm provider state * fix(javascript): propagate require origins through exported aliases * fix(javascript): normalize equivalent CTS binding origins --------- Co-authored-by: hhang <169027046+xu91102@users.noreply.github.com>
This commit is contained in:
@@ -52,6 +52,36 @@ describe("TypeScript import-equals links", () => {
|
||||
});
|
||||
|
||||
describe("CommonJS dependency links", () => {
|
||||
it("retains an import-equals binding and its module origin", () => {
|
||||
const ir = analyzeJavaScriptSemantics(`
|
||||
import typed = require("dependency");
|
||||
import type TypeOnly = require("type-only");
|
||||
namespace Local { export class Marker {} }
|
||||
import Alias = Local.Marker;
|
||||
void typed;
|
||||
`);
|
||||
expect(topLevelBinding(ir, "typed")).toMatchObject({
|
||||
kind: "import",
|
||||
mutable: false,
|
||||
provenance: {
|
||||
origins: [{ specifier: "dependency", importedPath: [] }],
|
||||
},
|
||||
});
|
||||
expect(
|
||||
ir.moduleLinks.filter(({ specifier }) => specifier !== null),
|
||||
).toEqual([
|
||||
expect.objectContaining({
|
||||
kind: "require",
|
||||
specifier: "dependency",
|
||||
importedName: null,
|
||||
localName: "typed",
|
||||
}),
|
||||
]);
|
||||
expect(ir.references.filter(({ name }) => name === "typed")).toHaveLength(
|
||||
1,
|
||||
);
|
||||
});
|
||||
|
||||
it.each([
|
||||
'require("dependency");',
|
||||
'void require("dependency");',
|
||||
@@ -148,7 +178,9 @@ describe("CommonJS dependency links", () => {
|
||||
]);
|
||||
expect(
|
||||
ir.bindings.find(({ name }) => name === "bound")?.provenance.origins,
|
||||
).toEqual([{ specifier: "bound", importedPath: [] }]);
|
||||
).toEqual([
|
||||
{ specifier: "bound", importedPath: [], resolutionMode: "require" },
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
@@ -179,6 +211,7 @@ it("preserves unshadowed require origins through aliases and assignments", () =>
|
||||
expect(origin(topLevelBinding(ir, name))).toEqual({
|
||||
specifier: "electron",
|
||||
importedPath: ["ipcMain"],
|
||||
resolutionMode: "require",
|
||||
});
|
||||
expect(topLevelBinding(ir, "assigned").provenance).toMatchObject({
|
||||
status: "ambiguous",
|
||||
|
||||
@@ -38,10 +38,12 @@ describe("JavaScript semantic analysis: structure 1", () => {
|
||||
expect(origin(topLevelBinding(ir, "bus"))).toEqual({
|
||||
specifier: "electron",
|
||||
importedPath: ["ipcMain"],
|
||||
resolutionMode: "require",
|
||||
});
|
||||
expect(origin(topLevelBinding(ir, "forwarded"))).toEqual({
|
||||
specifier: "electron",
|
||||
importedPath: ["ipcMain"],
|
||||
resolutionMode: "require",
|
||||
});
|
||||
expect(topLevelBinding(ir, "assigned").provenance).toMatchObject({
|
||||
status: "ambiguous",
|
||||
@@ -66,6 +68,7 @@ describe("JavaScript semantic analysis: structure 1", () => {
|
||||
expect(origin(innerBus)).toEqual({
|
||||
specifier: "./local-bus.js",
|
||||
importedPath: [],
|
||||
resolutionMode: "require",
|
||||
});
|
||||
const shadow = onlyBinding(ir, "ipcRenderer");
|
||||
expect(shadow.provenance).toEqual({
|
||||
|
||||
@@ -134,8 +134,11 @@ export function* indexJavaScriptBindingsSteps(
|
||||
/** Recover semantics from an already parsed JavaScript artifact. */
|
||||
export const analyzeParsedJavaScriptSemantics = (
|
||||
file: ParsedJavaScriptSource,
|
||||
staticImportResolutionMode: "import" | "require" = "import",
|
||||
): JavaScriptSemanticIr =>
|
||||
completeSemanticSteps(analyzeParsedJavaScriptSemanticsSteps(file));
|
||||
completeSemanticSteps(
|
||||
analyzeParsedJavaScriptSemanticsSteps(file, staticImportResolutionMode),
|
||||
);
|
||||
|
||||
/**
|
||||
* Recover semantics from a parsed artifact with a yield between analysis
|
||||
@@ -145,8 +148,9 @@ export const analyzeParsedJavaScriptSemantics = (
|
||||
*/
|
||||
export function* analyzeParsedJavaScriptSemanticsSteps(
|
||||
file: ParsedJavaScriptSource,
|
||||
staticImportResolutionMode: "import" | "require" = "import",
|
||||
): Generator<void, JavaScriptSemanticIr> {
|
||||
const state = createState(file.program);
|
||||
const state = createState(file.program, staticImportResolutionMode);
|
||||
yield* collectDefinitionsSteps(file.program, state);
|
||||
yield* collectSemanticMemberMutationsSteps(file.program, state);
|
||||
yield* traverseJavaScriptAstSteps(file.program, {
|
||||
@@ -159,7 +163,11 @@ export function* analyzeParsedJavaScriptSemanticsSteps(
|
||||
yield;
|
||||
const callables = collectSemanticReturns(state, parserPartial);
|
||||
yield;
|
||||
const moduleLinks = resolveSemanticModuleCallables(state, callables);
|
||||
const moduleLinks = resolveSemanticModuleCallables(
|
||||
state,
|
||||
callables,
|
||||
bindings,
|
||||
);
|
||||
const derived = yield* collectJavaScriptDerivedSemanticsSteps(
|
||||
file.program,
|
||||
state,
|
||||
@@ -517,7 +525,10 @@ const bindingOrigin = (
|
||||
: { ...origin, importedPath: [...origin.importedPath, ...projection] };
|
||||
};
|
||||
|
||||
const createState = (program: t.Program): JavaScriptSemanticAnalysisState => {
|
||||
const createState = (
|
||||
program: t.Program,
|
||||
staticImportResolutionMode: "import" | "require" = "import",
|
||||
): JavaScriptSemanticAnalysisState => {
|
||||
const root: JavaScriptSemanticScopeState = {
|
||||
scopeId: semanticScopeId("program", program),
|
||||
parentScopeId: null,
|
||||
@@ -527,6 +538,7 @@ const createState = (program: t.Program): JavaScriptSemanticAnalysisState => {
|
||||
bindings: new Map(),
|
||||
};
|
||||
return {
|
||||
staticImportResolutionMode,
|
||||
parentsByNode: new WeakMap(),
|
||||
scopes: [root],
|
||||
scopesById: new Map([[root.scopeId, root]]),
|
||||
@@ -803,7 +815,7 @@ const bindImportEquals = (
|
||||
mutable: false,
|
||||
definitionNode: node.id,
|
||||
initializer: null,
|
||||
directOrigin: { specifier, importedPath: [] },
|
||||
directOrigin: { specifier, importedPath: [], resolutionMode: "require" },
|
||||
});
|
||||
};
|
||||
|
||||
@@ -1181,7 +1193,13 @@ const addBinding = (input: AddBindingInput): void => {
|
||||
binding.definitions.push({ kind, location: range(definitionNode) });
|
||||
if (initializer !== null)
|
||||
binding.initializers.push({ node: initializer, projection });
|
||||
if (directOrigin !== undefined) binding.directOrigins.push(directOrigin);
|
||||
if (directOrigin !== undefined)
|
||||
binding.directOrigins.push(
|
||||
directOrigin.resolutionMode === undefined &&
|
||||
state.staticImportResolutionMode === "require"
|
||||
? { ...directOrigin, resolutionMode: "require" }
|
||||
: directOrigin,
|
||||
);
|
||||
};
|
||||
|
||||
const createBinding = (
|
||||
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
|
||||
/** One exact module origin followed through imports, requires, or aliases. */
|
||||
export interface JavaScriptModuleOrigin {
|
||||
readonly resolutionMode?: "import" | "require";
|
||||
readonly specifier: string;
|
||||
readonly importedPath: readonly string[];
|
||||
}
|
||||
@@ -392,8 +393,10 @@ export interface JavaScriptSemanticFrontier {
|
||||
|
||||
/** Static import/export relationship retained for cross-file composition. */
|
||||
export interface JavaScriptSemanticModuleLink {
|
||||
readonly resolutionMode?: "import" | "require";
|
||||
readonly kind:
|
||||
| "import"
|
||||
| "type-import"
|
||||
| "require"
|
||||
| "export"
|
||||
| "re-export"
|
||||
|
||||
@@ -669,19 +669,26 @@ export const semanticReferenceRole = (
|
||||
return "read";
|
||||
};
|
||||
|
||||
const isDeclarationIdentifier = (node: t.Identifier, parent: t.Node): boolean =>
|
||||
(t.isVariableDeclarator(parent) && patternContains(parent.id, node)) ||
|
||||
((t.isFunctionDeclaration(parent) || t.isFunctionExpression(parent)) &&
|
||||
parent.id === node) ||
|
||||
(t.isFunction(parent) &&
|
||||
parent.params.some((value) => patternContains(value, node))) ||
|
||||
((t.isClassDeclaration(parent) || t.isClassExpression(parent)) &&
|
||||
parent.id === node) ||
|
||||
(t.isImportSpecifier(parent) &&
|
||||
(parent.local === node || parent.imported === node)) ||
|
||||
(t.isImportDefaultSpecifier(parent) && parent.local === node) ||
|
||||
(t.isImportNamespaceSpecifier(parent) && parent.local === node) ||
|
||||
(t.isCatchClause(parent) && parent.param === node);
|
||||
const isDeclarationIdentifier = (
|
||||
node: t.Identifier,
|
||||
parent: t.Node,
|
||||
): boolean => {
|
||||
if (t.isTSImportEqualsDeclaration(parent)) return parent.id === node;
|
||||
return (
|
||||
(t.isVariableDeclarator(parent) && patternContains(parent.id, node)) ||
|
||||
((t.isFunctionDeclaration(parent) || t.isFunctionExpression(parent)) &&
|
||||
parent.id === node) ||
|
||||
(t.isFunction(parent) &&
|
||||
parent.params.some((value) => patternContains(value, node))) ||
|
||||
((t.isClassDeclaration(parent) || t.isClassExpression(parent)) &&
|
||||
parent.id === node) ||
|
||||
(t.isImportSpecifier(parent) &&
|
||||
(parent.local === node || parent.imported === node)) ||
|
||||
(t.isImportDefaultSpecifier(parent) && parent.local === node) ||
|
||||
(t.isImportNamespaceSpecifier(parent) && parent.local === node) ||
|
||||
(t.isCatchClause(parent) && parent.param === node)
|
||||
);
|
||||
};
|
||||
|
||||
const isNonReferenceKey = (node: t.Identifier, parent: t.Node): boolean =>
|
||||
((t.isMemberExpression(parent) || t.isOptionalMemberExpression(parent)) &&
|
||||
|
||||
@@ -48,7 +48,11 @@ export const uniqueSemanticOrigins = (
|
||||
);
|
||||
|
||||
const semanticOriginKey = (origin: JavaScriptModuleOrigin): string =>
|
||||
JSON.stringify([origin.specifier, origin.importedPath]);
|
||||
JSON.stringify([
|
||||
origin.specifier,
|
||||
origin.importedPath,
|
||||
origin.resolutionMode ?? "import",
|
||||
]);
|
||||
|
||||
/** Canonically distinguish primitive values across type boundaries. */
|
||||
export const semanticPrimitiveKey = (
|
||||
|
||||
@@ -54,7 +54,11 @@ export const semanticRequireOrigin = (
|
||||
return specifier === undefined
|
||||
? undefined
|
||||
: {
|
||||
origin: { specifier, importedPath: members.reverse() },
|
||||
origin: {
|
||||
specifier,
|
||||
importedPath: members.reverse(),
|
||||
resolutionMode: "require",
|
||||
},
|
||||
call: current,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from "./javascriptSemanticSlots.js";
|
||||
|
||||
import type {
|
||||
JavaScriptSemanticBinding,
|
||||
JavaScriptSemanticCallable,
|
||||
JavaScriptSemanticModuleLink,
|
||||
JavaScriptSemanticReturnSite,
|
||||
@@ -89,12 +90,16 @@ const semanticReturnSiteId = (
|
||||
String(location.end.column),
|
||||
].join(":");
|
||||
|
||||
/** Link exports to callables only when lexical resolution is unique and exact. */
|
||||
/** Finalize exported bindings only when lexical resolution is unique and exact. */
|
||||
export const resolveSemanticModuleCallables = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
callables: readonly JavaScriptSemanticCallable[],
|
||||
bindings: readonly JavaScriptSemanticBinding[],
|
||||
): JavaScriptSemanticModuleLink[] => {
|
||||
const callableIds = new Set(callables.map(({ callableId }) => callableId));
|
||||
const provenanceByBinding = new Map(
|
||||
bindings.map(({ bindingId, provenance }) => [bindingId, provenance]),
|
||||
);
|
||||
return state.moduleLinks.map((link) => {
|
||||
// Named exports follow their lexical binding, including every assignment.
|
||||
// A declaration's ID or a callable's display name cannot prove that link.
|
||||
@@ -107,8 +112,19 @@ export const resolveSemanticModuleCallables = (
|
||||
binding === undefined
|
||||
? []
|
||||
: callableIdsForBinding(binding, state, callableIds, new Set());
|
||||
const provenance =
|
||||
binding === undefined || link.specifier !== null
|
||||
? undefined
|
||||
: provenanceByBinding.get(binding.bindingId);
|
||||
const resolutionMode =
|
||||
provenance?.status === "module"
|
||||
? provenance.origins[0].resolutionMode
|
||||
: undefined;
|
||||
return {
|
||||
...link,
|
||||
...(resolutionMode === "import" || resolutionMode === "require"
|
||||
? { resolutionMode }
|
||||
: {}),
|
||||
callableId: resolved.length === 1 ? (resolved[0] ?? null) : null,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -57,6 +57,8 @@ export interface JavaScriptSemanticScopeState {
|
||||
|
||||
/** Shared state for semantic collection and evaluation. */
|
||||
export interface JavaScriptSemanticAnalysisState {
|
||||
/** Effective loader for static imports in this source artifact. */
|
||||
readonly staticImportResolutionMode: "import" | "require";
|
||||
readonly parentsByNode: WeakMap<t.Node, t.Node>;
|
||||
readonly scopes: JavaScriptSemanticScopeState[];
|
||||
readonly scopesById: Map<string, JavaScriptSemanticScopeState>;
|
||||
|
||||
@@ -52,6 +52,7 @@ import {
|
||||
} from "./javascriptSourceParser.js";
|
||||
|
||||
import { createJavaScriptRpcScan } from "./javascriptRpcEndpoints.js";
|
||||
import { semanticImportEqualsSpecifier } from "./javascriptSemanticProjection.js";
|
||||
|
||||
/** Parse one JavaScript artifact and recover static structure only. */
|
||||
export const analyzeJavaScriptStaticSource = (
|
||||
@@ -238,7 +239,11 @@ const inspectNode = (
|
||||
} else if (t.isOptionalCallExpression(node)) {
|
||||
inspectCall(source, node, findings);
|
||||
} else if (t.isNewExpression(node)) inspectCall(source, node, findings);
|
||||
if (
|
||||
if (t.isTSImportEqualsDeclaration(node)) {
|
||||
const specifier = semanticImportEqualsSpecifier(node);
|
||||
if (specifier !== null)
|
||||
addReference(findings, { node, kind: "require", specifier });
|
||||
} else if (
|
||||
(t.isImportDeclaration(node) || t.isExportAllDeclaration(node)) &&
|
||||
node.source !== undefined
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user