# shared/resolvers/anchor.resolver.ts

> 108 lines of code and 46 definitions.

Tree: Governance tree
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/governance#file-governance-shared-resolvers-anchor-resolver-ts
Source text: https://banes-lab.com/source/governance/shared/resolvers/anchor.resolver.ts.txt

Listed in [shared/resolvers](https://banes-lab.com/api/source/governance/shared/resolvers.md), before [shared/resolvers/container.resolver.ts](https://banes-lab.com/source/governance/shared/resolvers/container.resolver.ts.md).

## Definitions

- `collapsePath` (lexical_declaration, line 72, exported)
- `normalizePath` (lexical_declaration, line 48, exported)
- `belowRoot` (lexical_declaration, line 116)
- `isInContainer` (lexical_declaration, line 101, exported)
- `presentRoots` (lexical_declaration, line 135)
- `projectDirs` (lexical_declaration, line 141, exported)
- `projectFiles` (lexical_declaration, line 145, exported)
- `isSourceFile` (lexical_declaration, line 108, exported)
- `collectDirs` (lexical_declaration, line 121)
- `collectFiles` (lexical_declaration, line 128)
- `labelOf` (lexical_declaration, line 44, exported)
- `isGovernedFile` (lexical_declaration, line 88, exported)
- `rootRelative` (lexical_declaration, line 112)
- `containerPathOf` (lexical_declaration, line 93, exported)
- `MEMBER_KEY` (lexical_declaration, line 7)
- `WORKSPACE_ROOT` (lexical_declaration, line 9, exported)
- `PROJECT_ROOT` (lexical_declaration, line 11, exported)
- `MEMBER_ROOT` (lexical_declaration, line 13, exported)
- `BUILD_CONFIG_FILES` (lexical_declaration, line 15, exported)
- `TEST_ROOT` (lexical_declaration, line 24, exported)
- `SCRIPT_ROOT` (lexical_declaration, line 26, exported)
- `BUILD_SCRIPT_ROOT` (lexical_declaration, line 28, exported)
- `RULE_HOST` (lexical_declaration, line 30, exported)
- `GRAPH_PATH` (lexical_declaration, line 32, exported)
- `TEST_ROOT_SEGMENT` (lexical_declaration, line 34, exported)
- `SCRIPT_ROOT_SEGMENT` (lexical_declaration, line 36, exported)
- `BUILD_SCRIPT_ROOT_SEGMENT` (lexical_declaration, line 38, exported)
- `GOVERNED_ROOT` (lexical_declaration, line 40, exported)
- `RULE_TAG` (lexical_declaration, line 42)
- `FOUNDATION_SUBJECT` (lexical_declaration, line 52, exported)
- `TAXONOMY_SUBJECT` (lexical_declaration, line 54, exported)
- `LAYER_SUBJECT` (lexical_declaration, line 56, exported)
- `ANCHOR_SUBJECT` (lexical_declaration, line 58, exported)
- `LOCATION_SUBJECT` (lexical_declaration, line 60, exported)
- `FOUNDATION_FOLDER` (lexical_declaration, line 62, exported)
- `FOUNDATION_PREFIX` (lexical_declaration, line 64, exported)
- `basenameOf` (lexical_declaration, line 66, exported)
- `idx` (lexical_declaration, line 68, exported)
- `out` (lexical_declaration, line 73, exported)
- `GOVERNED_SEGMENTS` (lexical_declaration, line 86)
- `norm` (lexical_declaration, line 89, exported)
- `root` (lexical_declaration, line 94, exported)
- `fragment` (lexical_declaration, line 102, exported)
- `SOURCE_EXTENSIONS` (lexical_declaration, line 106, exported)
- `absRoot` (lexical_declaration, line 117)
- `p` (lexical_declaration, line 130)

## Uses

- [shared/manifests/taxonomy.manifest.ts](https://banes-lab.com/source/governance/shared/manifests/taxonomy.manifest.ts.md)
- [shared/resolvers/container.resolver.ts](https://banes-lab.com/source/governance/shared/resolvers/container.resolver.ts.md)

## Used by

- [rules/eslint/closure-event-system-has-trajectory.eslint.rule.ts](https://banes-lab.com/source/governance/rules/eslint/closure-event-system-has-trajectory.eslint.rule.ts.md)
- [rules/eslint/closure-no-cross-subsystem-import.eslint.rule.ts](https://banes-lab.com/source/governance/rules/eslint/closure-no-cross-subsystem-import.eslint.rule.ts.md)
- [rules/eslint/closure-no-inline-exported-types.eslint.rule.ts](https://banes-lab.com/source/governance/rules/eslint/closure-no-inline-exported-types.eslint.rule.ts.md)
- [rules/eslint/closure-no-unreachable-file.eslint.rule.ts](https://banes-lab.com/source/governance/rules/eslint/closure-no-unreachable-file.eslint.rule.ts.md)
- [rules/eslint/closure-types-centralized.eslint.rule.ts](https://banes-lab.com/source/governance/rules/eslint/closure-types-centralized.eslint.rule.ts.md)
- [rules/eslint/require-pool-registration.eslint.rule.ts](https://banes-lab.com/source/governance/rules/eslint/require-pool-registration.eslint.rule.ts.md)
- [rules/plugins/rule.plugin.ts](https://banes-lab.com/source/governance/rules/plugins/rule.plugin.ts.md)
- [shared/analyzers/specifier.analyzer.ts](https://banes-lab.com/source/governance/shared/analyzers/specifier.analyzer.ts.md)
- [shared/loaders/index.loader.ts](https://banes-lab.com/source/governance/shared/loaders/index.loader.ts.md)
- [shared/manifests/layer.manifest.ts](https://banes-lab.com/source/governance/shared/manifests/layer.manifest.ts.md)

## Source

```typescript
import { ROOT, absolutePath, relativePath } from "@ssot/paths";
import { existsSync, readdirSync } from "node:fs";
import { governedRoots, isDeclaredContainer, rootFor } from "../manifests/taxonomy.manifest.ts";
import { containerPath } from "./container.resolver.ts";
import { join } from "node:path";

const MEMBER_KEY = "app.member";

export const WORKSPACE_ROOT: string = ROOT;

export const PROJECT_ROOT: string = absolutePath("app.root");

export const MEMBER_ROOT: string = absolutePath(MEMBER_KEY);

export const BUILD_CONFIG_FILES: readonly string[] = [
    "vite.config.ts",
    "rollup.config.ts",
    "webpack.config.ts",
    "esbuild.config.ts",
    "tsup.config.ts",
    "build.config.ts",
];

export const TEST_ROOT: string = absolutePath("codebase.testing.app");

export const SCRIPT_ROOT: string = absolutePath("project.scripts");

export const BUILD_SCRIPT_ROOT: string = absolutePath("app.build");

export const RULE_HOST: string = absolutePath("govlabHost.rules");

export const GRAPH_PATH: string = absolutePath("govlabHost.reports.lint.closureGraph");

export const TEST_ROOT_SEGMENT = `${relativePath("codebase.testing")}/`;

export const SCRIPT_ROOT_SEGMENT = `/${relativePath("project.scripts")}/`;

export const BUILD_SCRIPT_ROOT_SEGMENT = `/${relativePath("app.build")}/`;

export const GOVERNED_ROOT = relativePath(MEMBER_KEY);

const RULE_TAG = ".eslint.rule";

export const labelOf = function labelOf(stem: string): string {
    return stem.endsWith(RULE_TAG) ? stem.slice(0, -RULE_TAG.length) : stem;
};

export const normalizePath = function normalizePath(path: string): string {
    return path.split("\\").join("/");
};

export const FOUNDATION_SUBJECT = "base";

export const TAXONOMY_SUBJECT = "taxonomy";

export const LAYER_SUBJECT = "layer";

export const ANCHOR_SUBJECT = "anchor";

export const LOCATION_SUBJECT = "location";

export const FOUNDATION_FOLDER = `/${FOUNDATION_SUBJECT}/`;

export const FOUNDATION_PREFIX = FOUNDATION_SUBJECT.slice(0, 1).toUpperCase() + FOUNDATION_SUBJECT.slice(1);

export const basenameOf = function basenameOf(path: string): string {
    const norm = normalizePath(path);
    const idx = norm.lastIndexOf("/");
    return idx === -1 ? norm : norm.slice(idx + 1);
};

export const collapsePath = function collapsePath(value: string): string {
    const out: string[] = [];
    for (const segment of value.split("/")) {
        if (segment === "..") {
            out.pop();
            continue;
        }
        if (segment !== "." && segment !== "") {
            out.push(segment);
        }
    }
    return out.join("/");
};

const GOVERNED_SEGMENTS: readonly string[] = governedRoots().map((root) => `/${root}/`);

export const isGovernedFile = function isGovernedFile(filename: string): boolean {
    const norm = normalizePath(filename);
    return GOVERNED_SEGMENTS.some((segment) => norm.includes(segment));
};

export const containerPathOf = function containerPathOf(filename: string, container: string): string | null {
    const root = rootFor(normalizePath(filename));
    if (root === undefined || !isDeclaredContainer(root, container)) {
        return null;
    }
    return `/${containerPath(container, root)}`;
};

export const isInContainer = function isInContainer(filename: string, container: string): boolean {
    const fragment = containerPathOf(filename, container);
    return fragment !== null && normalizePath(filename).includes(fragment);
};

export const SOURCE_EXTENSIONS: readonly string[] = [".ts", ".tsx", ".mts", ".cts"];

export const isSourceFile = function isSourceFile(filename: string): boolean {
    return SOURCE_EXTENSIONS.some((ext) => normalizePath(filename).endsWith(ext));
};

const rootRelative = function rootRelative(root: string): string {
    return root.slice(root.indexOf("/") + 1);
};

const belowRoot = function belowRoot(root: string, absPath: string): string {
    const absRoot = normalizePath(join(ROOT, root));
    return `${rootRelative(root)}/${normalizePath(absPath).slice(absRoot.length + 1)}`;
};

const collectDirs = function collectDirs(root: string, absDir: string): string[] {
    return readdirSync(absDir, { withFileTypes: true })
        .filter((entry) => entry.isDirectory())
        .map((entry) => join(absDir, entry.name))
        .flatMap((p) => [`${belowRoot(root, p)}/`, ...collectDirs(root, p)]);
};

const collectFiles = function collectFiles(root: string, absDir: string): string[] {
    return readdirSync(absDir, { withFileTypes: true }).flatMap((entry) => {
        const p = join(absDir, entry.name);
        return entry.isDirectory() ? collectFiles(root, p) : [belowRoot(root, p)];
    });
};

const presentRoots = function presentRoots(): { abs: string; root: string }[] {
    return governedRoots()
        .map((root) => ({ abs: join(ROOT, root), root }))
        .filter((entry) => existsSync(entry.abs));
};

export const projectDirs = function projectDirs(): string[] {
    return presentRoots().flatMap((entry) => collectDirs(entry.root, entry.abs));
};

export const projectFiles = function projectFiles(): string[] {
    return presentRoots().flatMap((entry) => collectFiles(entry.root, entry.abs));
};
```
