Repo boundary breach detection on import normalise

This commit is contained in:
vorotamoroz
2026-06-19 06:03:53 +01:00
parent 463c0c0bc8
commit 2ee6a2c09f
5 changed files with 33 additions and 27 deletions
+12 -6
View File
@@ -40,6 +40,7 @@ function toPosixPath(filePath: string): string {
const posixProjectRoot = toPosixPath(projectRoot);
const posixSrc = `${posixProjectRoot}/src`;
const posixLibSrc = `${posixProjectRoot}/src/lib/src`;
const posixSubrepo = `${posixProjectRoot}/src/lib`;
console.log(`Project Root: ${posixProjectRoot}`);
console.log(`Source Directory: ${posixSrc}`);
@@ -93,12 +94,6 @@ for (const sourceFile of project.getSourceFiles()) {
continue;
}
// Keep relative sibling/child imports unchanged (e.g. ./utils) unless --all-alias is set.
const isSibling = isRelative && !moduleSpecifier.startsWith("..");
if (isSibling && !allAlias) {
continue;
}
// Resolve path to an absolute POSIX path.
let resolvedPath = "";
if (moduleSpecifier.startsWith("@lib/")) {
@@ -112,6 +107,17 @@ for (const sourceFile of project.getSourceFiles()) {
resolvedPath = toPosixPath(path.normalize(resolvedPath));
// Keep relative sibling/child imports unchanged (e.g. ./utils) unless:
// 1. --all-alias is set, OR
// 2. the import crosses the subrepository boundary (src/lib/)
const isSibling = isRelative && !moduleSpecifier.startsWith("..");
const importerInsideSubrepo = posixFilePath.startsWith(posixSubrepo + "/");
const targetInsideSubrepo = resolvedPath.startsWith(posixSubrepo + "/");
const crossesSubrepo = importerInsideSubrepo !== targetInsideSubrepo;
if (isSibling && !allAlias && !crossesSubrepo) {
continue;
}
// Determine correct normalised specifier.
let newSpecifier = "";
const hasExtension =