fix(javascript): preserve TypeScript loader and dependency semantics
Share loader selection across static findings and module relationships: CTS static imports and re-exports use CommonJS lookup while dynamic imports retain ESM lookup. Retain runtime import-equals dependencies, bindings, and exported names; do not resolve require file-URL strings as local artifacts. Restore import-map descriptions in CLI help. Validated resolver and semantic tests, application reconstruction boundaries, and CLI help. Installed TypeScript NodeNext emission and Node require behavior were checked independently. Refs #2056, #2057, #2058, #2053.
This commit is contained in:
@@ -6,6 +6,51 @@ import {
|
||||
topLevelBinding,
|
||||
} from "./javascriptSemanticAnalysis.fixture.js";
|
||||
|
||||
describe("TypeScript import-equals links", () => {
|
||||
it("retains runtime external import-equals as a require dependency", () => {
|
||||
const links = analyzeJavaScriptSemantics(`
|
||||
import dependency = require("./dependency.cjs");
|
||||
import type types = require("./types");
|
||||
import alias = Namespace.Dependency;
|
||||
`).moduleLinks;
|
||||
|
||||
expect(links).toEqual([
|
||||
expect.objectContaining({
|
||||
kind: "require",
|
||||
specifier: "./dependency.cjs",
|
||||
importedName: null,
|
||||
localName: "dependency",
|
||||
exportedName: null,
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
it("keeps the exported binding for exported import-equals", () => {
|
||||
const source = 'export import dependency = require("./dependency");';
|
||||
const links = analyzeJavaScriptSemantics(source).moduleLinks;
|
||||
|
||||
expect(links).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
kind: "require",
|
||||
specifier: "./dependency",
|
||||
localName: "dependency",
|
||||
exportedName: null,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
kind: "export",
|
||||
specifier: null,
|
||||
localName: "dependency",
|
||||
exportedName: "dependency",
|
||||
}),
|
||||
]),
|
||||
);
|
||||
expect(
|
||||
links.find(({ kind }) => kind === "require")?.location.start,
|
||||
).toEqual({ line: 1, column: 7 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("CommonJS dependency links", () => {
|
||||
it.each([
|
||||
'require("dependency");',
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
import {
|
||||
collectSemanticModuleLink,
|
||||
collectSemanticCallable,
|
||||
semanticImportEqualsSpecifier,
|
||||
collectSemanticReferences,
|
||||
collectSemanticReferencesSteps,
|
||||
immutableSemanticBindings,
|
||||
@@ -752,6 +753,8 @@ const bindInnerDeclaration = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): void => {
|
||||
if (t.isImportDeclaration(node)) bindImports(node, scope, state);
|
||||
else if (t.isTSImportEqualsDeclaration(node))
|
||||
bindImportEquals(node, scope, state);
|
||||
else if (t.isVariableDeclarator(node))
|
||||
bindPattern({
|
||||
pattern: node.id,
|
||||
@@ -785,6 +788,25 @@ const bindInnerDeclaration = (
|
||||
});
|
||||
};
|
||||
|
||||
const bindImportEquals = (
|
||||
node: t.TSImportEqualsDeclaration,
|
||||
scope: JavaScriptSemanticScopeState,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): void => {
|
||||
const specifier = semanticImportEqualsSpecifier(node);
|
||||
if (specifier === null) return;
|
||||
addBinding({
|
||||
state,
|
||||
scope,
|
||||
name: node.id.name,
|
||||
kind: "import",
|
||||
mutable: false,
|
||||
definitionNode: node.id,
|
||||
initializer: null,
|
||||
directOrigin: { specifier, importedPath: [] },
|
||||
});
|
||||
};
|
||||
|
||||
const bindAssignment = (
|
||||
node: t.Node,
|
||||
scope: JavaScriptSemanticScopeState,
|
||||
|
||||
@@ -69,7 +69,8 @@ export const collectSemanticModuleLink = (
|
||||
node: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): void => {
|
||||
if (t.isImportDeclaration(node)) collectImports(node, state);
|
||||
if (t.isTSImportEqualsDeclaration(node)) collectImportEquals(node, state);
|
||||
else if (t.isImportDeclaration(node)) collectImports(node, state);
|
||||
else if (t.isExportAllDeclaration(node))
|
||||
addModuleLink(
|
||||
state,
|
||||
@@ -103,6 +104,36 @@ export const collectSemanticModuleLink = (
|
||||
else if (t.isCallExpression(node)) collectUnboundRequireLink(node, state);
|
||||
};
|
||||
|
||||
const collectImportEquals = (
|
||||
node: t.TSImportEqualsDeclaration,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): void => {
|
||||
const specifier = semanticImportEqualsSpecifier(node);
|
||||
if (specifier === null) return;
|
||||
addModuleLink(
|
||||
state,
|
||||
{
|
||||
kind: "require",
|
||||
specifier,
|
||||
importedName: null,
|
||||
localName: node.id.name,
|
||||
exportedName: null,
|
||||
location: range(node),
|
||||
},
|
||||
node,
|
||||
);
|
||||
};
|
||||
|
||||
/** Runtime external-module target for a value import-equals declaration. */
|
||||
export const semanticImportEqualsSpecifier = (
|
||||
node: t.TSImportEqualsDeclaration,
|
||||
): string | null =>
|
||||
node.importKind !== "type" &&
|
||||
t.isTSExternalModuleReference(node.moduleReference) &&
|
||||
t.isStringLiteral(node.moduleReference.expression)
|
||||
? node.moduleReference.expression.value
|
||||
: null;
|
||||
|
||||
/** Collect lexical references after definitions exist, optionally for one name. */
|
||||
export const collectSemanticReferences = (
|
||||
program: t.Program,
|
||||
@@ -707,6 +738,11 @@ const declarationNames = (declaration: t.Declaration): string[] => {
|
||||
t.isIdentifier(declaration.id)
|
||||
)
|
||||
return [declaration.id.name];
|
||||
if (
|
||||
t.isTSImportEqualsDeclaration(declaration) &&
|
||||
semanticImportEqualsSpecifier(declaration) !== null
|
||||
)
|
||||
return [declaration.id.name];
|
||||
return [];
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user