Skip to content

Commit 0c9691c

Browse files
committed
Add isExternalModuleNameRelative AST predicate
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 9980615 commit 0c9691c

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎packages/typescript/src/ast/utils.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ModifierFlags } from "../../enums/modifierFlags.js";
44
import { NodeFlags } from "../../enums/nodeFlags.js";
55
import { OuterExpressionKinds } from "../../enums/outerExpressionKinds.js";
66
import { SyntaxKind } from "../../enums/syntaxKind.js";
7+
import { isRootedDiskPath } from "../api/path.ts";
78
import type {
89
__String,
910
AccessExpression,
@@ -116,6 +117,10 @@ export function isExternalModule(file: SourceFile): boolean {
116117
return file.externalModuleIndicator !== undefined;
117118
}
118119

120+
export function isExternalModuleNameRelative(moduleName: string): boolean {
121+
return /^\.\.?(?:$|[\\/])/.test(moduleName) || isRootedDiskPath(moduleName);
122+
}
123+
119124
function getRootDeclaration(node: Node): Node {
120125
while (node.kind === SyntaxKind.BindingElement) {
121126
node = node.parent.parent;

‎packages/typescript/test/astPredicates.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,12 @@ f<TypeArgument>();
458458
const argument = nodes.find(node => ast.isIdentifier(node) && node.text === "TypeArgument");
459459
assert.ok(argument && ast.isPartOfTypeNode(argument));
460460
});
461+
462+
test("isExternalModuleNameRelative includes rooted disk paths but excludes URLs and bare names", () => {
463+
for (const name of [".", "..", "./module", "../module", ".\\module", "..\\module", "/module", "C:/module", "C:\\module", "\\\\server\\share\\module"]) {
464+
assert.ok(ast.isExternalModuleNameRelative(name), name);
465+
}
466+
for (const name of ["", "module", "@scope/module", ".module", ".../module", "https://example.com/module", "file:///module", "C:module"]) {
467+
assert.equal(ast.isExternalModuleNameRelative(name), false, name);
468+
}
469+
});

0 commit comments

Comments
 (0)