refactor: dedupe boundary-path canonical checks

This commit is contained in:
Peter Steinberger
2026-02-26 14:04:40 +01:00
parent b402770f63
commit 7b5153f214

View File

@@ -56,19 +56,19 @@ export async function resolveBoundaryPath(
const outsideLexicalCanonicalPath = lexicalInside const outsideLexicalCanonicalPath = lexicalInside
? undefined ? undefined
: await resolvePathViaExistingAncestor(absolutePath); : await resolvePathViaExistingAncestor(absolutePath);
const canonicalOutsideLexicalPath = outsideLexicalCanonicalPath ?? absolutePath; const canonicalOutsideLexicalPath = resolveCanonicalOutsideLexicalPath({
absolutePath,
if ( outsideLexicalCanonicalPath,
!params.skipLexicalRootCheck && });
!lexicalInside && assertLexicalBoundaryOrCanonicalAlias({
!isPathInside(rootCanonicalPath, canonicalOutsideLexicalPath) skipLexicalRootCheck: params.skipLexicalRootCheck,
) { lexicalInside,
throw pathEscapeError({ canonicalOutsideLexicalPath,
boundaryLabel: params.boundaryLabel, rootCanonicalPath,
rootPath, boundaryLabel: params.boundaryLabel,
absolutePath, rootPath,
}); absolutePath,
} });
if (!lexicalInside) { if (!lexicalInside) {
const canonicalPath = canonicalOutsideLexicalPath; const canonicalPath = canonicalOutsideLexicalPath;
@@ -79,15 +79,13 @@ export async function resolveBoundaryPath(
absolutePath, absolutePath,
}); });
const kind = await getPathKind(absolutePath, false); const kind = await getPathKind(absolutePath, false);
return { return buildResolvedBoundaryPath({
absolutePath, absolutePath,
canonicalPath, canonicalPath,
rootPath, rootPath,
rootCanonicalPath, rootCanonicalPath,
relativePath: relativeInsideRoot(rootCanonicalPath, canonicalPath), kind,
exists: kind.exists, });
kind: kind.kind,
};
} }
return resolveBoundaryPathLexicalAsync({ return resolveBoundaryPathLexicalAsync({
@@ -108,19 +106,19 @@ export function resolveBoundaryPathSync(params: ResolveBoundaryPathParams): Reso
const outsideLexicalCanonicalPath = lexicalInside const outsideLexicalCanonicalPath = lexicalInside
? undefined ? undefined
: resolvePathViaExistingAncestorSync(absolutePath); : resolvePathViaExistingAncestorSync(absolutePath);
const canonicalOutsideLexicalPath = outsideLexicalCanonicalPath ?? absolutePath; const canonicalOutsideLexicalPath = resolveCanonicalOutsideLexicalPath({
absolutePath,
if ( outsideLexicalCanonicalPath,
!params.skipLexicalRootCheck && });
!lexicalInside && assertLexicalBoundaryOrCanonicalAlias({
!isPathInside(rootCanonicalPath, canonicalOutsideLexicalPath) skipLexicalRootCheck: params.skipLexicalRootCheck,
) { lexicalInside,
throw pathEscapeError({ canonicalOutsideLexicalPath,
boundaryLabel: params.boundaryLabel, rootCanonicalPath,
rootPath, boundaryLabel: params.boundaryLabel,
absolutePath, rootPath,
}); absolutePath,
} });
if (!lexicalInside) { if (!lexicalInside) {
const canonicalPath = canonicalOutsideLexicalPath; const canonicalPath = canonicalOutsideLexicalPath;
@@ -131,15 +129,13 @@ export function resolveBoundaryPathSync(params: ResolveBoundaryPathParams): Reso
absolutePath, absolutePath,
}); });
const kind = getPathKindSync(absolutePath, false); const kind = getPathKindSync(absolutePath, false);
return { return buildResolvedBoundaryPath({
absolutePath, absolutePath,
canonicalPath, canonicalPath,
rootPath, rootPath,
rootCanonicalPath, rootCanonicalPath,
relativePath: relativeInsideRoot(rootCanonicalPath, canonicalPath), kind,
exists: kind.exists, });
kind: kind.kind,
};
} }
return resolveBoundaryPathLexicalSync({ return resolveBoundaryPathLexicalSync({
@@ -228,15 +224,13 @@ async function resolveBoundaryPathLexicalAsync(params: {
absolutePath: params.absolutePath, absolutePath: params.absolutePath,
}); });
const kind = await getPathKind(params.absolutePath, preserveFinalSymlink); const kind = await getPathKind(params.absolutePath, preserveFinalSymlink);
return { return buildResolvedBoundaryPath({
absolutePath: params.absolutePath, absolutePath: params.absolutePath,
canonicalPath: canonicalCursor, canonicalPath: canonicalCursor,
rootPath: params.rootPath, rootPath: params.rootPath,
rootCanonicalPath: params.rootCanonicalPath, rootCanonicalPath: params.rootCanonicalPath,
relativePath: relativeInsideRoot(params.rootCanonicalPath, canonicalCursor), kind,
exists: kind.exists, });
kind: kind.kind,
};
} }
function resolveBoundaryPathLexicalSync(params: { function resolveBoundaryPathLexicalSync(params: {
@@ -317,14 +311,59 @@ function resolveBoundaryPathLexicalSync(params: {
absolutePath: params.absolutePath, absolutePath: params.absolutePath,
}); });
const kind = getPathKindSync(params.absolutePath, preserveFinalSymlink); const kind = getPathKindSync(params.absolutePath, preserveFinalSymlink);
return { return buildResolvedBoundaryPath({
absolutePath: params.absolutePath, absolutePath: params.absolutePath,
canonicalPath: canonicalCursor, canonicalPath: canonicalCursor,
rootPath: params.rootPath, rootPath: params.rootPath,
rootCanonicalPath: params.rootCanonicalPath, rootCanonicalPath: params.rootCanonicalPath,
relativePath: relativeInsideRoot(params.rootCanonicalPath, canonicalCursor), kind,
exists: kind.exists, });
kind: kind.kind, }
function resolveCanonicalOutsideLexicalPath(params: {
absolutePath: string;
outsideLexicalCanonicalPath?: string;
}): string {
return params.outsideLexicalCanonicalPath ?? params.absolutePath;
}
function assertLexicalBoundaryOrCanonicalAlias(params: {
skipLexicalRootCheck?: boolean;
lexicalInside: boolean;
canonicalOutsideLexicalPath: string;
rootCanonicalPath: string;
boundaryLabel: string;
rootPath: string;
absolutePath: string;
}): void {
if (params.skipLexicalRootCheck || params.lexicalInside) {
return;
}
if (isPathInside(params.rootCanonicalPath, params.canonicalOutsideLexicalPath)) {
return;
}
throw pathEscapeError({
boundaryLabel: params.boundaryLabel,
rootPath: params.rootPath,
absolutePath: params.absolutePath,
});
}
function buildResolvedBoundaryPath(params: {
absolutePath: string;
canonicalPath: string;
rootPath: string;
rootCanonicalPath: string;
kind: { exists: boolean; kind: ResolvedBoundaryPathKind };
}): ResolvedBoundaryPath {
return {
absolutePath: params.absolutePath,
canonicalPath: params.canonicalPath,
rootPath: params.rootPath,
rootCanonicalPath: params.rootCanonicalPath,
relativePath: relativeInsideRoot(params.rootCanonicalPath, params.canonicalPath),
exists: params.kind.exists,
kind: params.kind.kind,
}; };
} }