# tools/core/inspectors/taxonomy.inspector.ts

> 73 lines of code and 16 definitions.

Tree: Coordination tree
Language: typescript
Canonical: https://banes-lab.com/anatomy/coordination#file-coordination-tools-core-inspectors-taxonomy-inspector-ts
Source text: https://banes-lab.com/assets/sources/source.9fc83a40f1b84ada96de4212e65b3a40eca5212a85a8eacc9773005fc544ce07.generated.txt

## Definitions

- `isGroupingFolder` (lexical_declaration, line 31)
- `wraps` (lexical_declaration, line 27)
- `markerOf` (lexical_declaration, line 43)
- `ForeignMarker` (interface_declaration, line 7, exported)
- `ForeignScope` (type_alias_declaration, line 12)
- `FolderScan` (interface_declaration, line 14)
- `isDirectory` (lexical_declaration, line 19, exported)
- `entriesOf` (lexical_declaration, line 35)
- `scanFolder` (lexical_declaration, line 58)
- `entries` (lexical_declaration, line 59)
- `marker` (lexical_declaration, line 60)
- `children` (lexical_declaration, line 63)
- `foreignMarkerIn` (lexical_declaration, line 70, exported)
- `pending` (lexical_declaration, line 75, exported)
- `current` (lexical_declaration, line 76, exported)
- `scan` (lexical_declaration, line 79, exported)

## Source

```typescript
import { readdirSync, statSync } from "node:fs";
import type { Dirent } from "node:fs";

import type { TaxonomyData } from "../types/taxonomy.types.ts";
import { resolve } from "node:path";

export interface ForeignMarker {
    readonly evidence: string;
    readonly why: string;
}

type ForeignScope = Pick<TaxonomyData, "foreignGrammar" | "ignored">;

interface FolderScan {
    readonly marker: ForeignMarker | undefined;
    readonly children: readonly string[];
}

export const isDirectory = function isDirectory(repoRoot: string, relPath: string): boolean {
    try {
        return statSync(resolve(repoRoot, relPath), { throwIfNoEntry: false })?.isDirectory() ?? false;
    } catch {
        return false;
    }
};

const wraps = function wraps(name: string, open: string, close: string): boolean {
    return name.length > open.length + close.length && name.startsWith(open) && name.endsWith(close);
};

const isGroupingFolder = function isGroupingFolder(name: string, pairs: readonly (readonly string[])[]): boolean {
    return pairs.some(([open = "", close = ""]) => open.length > 0 && close.length > 0 && wraps(name, open, close));
};

const entriesOf = function entriesOf(repoRoot: string, folder: string): Dirent[] {
    try {
        return readdirSync(resolve(repoRoot, folder), { withFileTypes: true });
    } catch {
        return [];
    }
};

const markerOf = function markerOf(entry: Dirent, evidence: string, scope: ForeignScope): ForeignMarker | null {
    if (!entry.isDirectory()) {
        return scope.foreignGrammar.ownershipManifests.includes(entry.name)
            ? {
                  evidence,
                  why: `"${entry.name}" makes its directory a runtime-identified unit, so its name is an identifier another system resolves.`,
              }
            : null;
    }

    return !scope.ignored.includes(entry.name) && isGroupingFolder(entry.name, scope.foreignGrammar.groupingDelimiters)
        ? { evidence, why: `"${entry.name}" is another grammar's grouping construct, which this one forbids outright.` }
        : null;
};

const scanFolder = function scanFolder(repoRoot: string, folder: string, scope: ForeignScope): FolderScan {
    const entries = entriesOf(repoRoot, folder);
    const marker = entries
        .map((entry) => markerOf(entry, `${folder}/${entry.name}`, scope))
        .find((found): found is ForeignMarker => found !== null);
    const children = entries
        .filter((entry) => entry.isDirectory() && !scope.ignored.includes(entry.name))
        .map((entry) => `${folder}/${entry.name}`);

    return { children, marker };
};

export const foreignMarkerIn = function foreignMarkerIn(
    repoRoot: string,
    root: string,
    scope: ForeignScope,
): ForeignMarker | null {
    const pending: string[] = [root];
    let current = pending.pop();

    while (current !== undefined) {
        const scan = scanFolder(repoRoot, current, scope);
        if (scan.marker !== undefined) {
            return scan.marker;
        }
        pending.push(...scan.children);
        current = pending.pop();
    }

    return null;
};
```
