# core/converters/baseline.converter.ts

> 80 lines of code and 18 definitions.

Tree: Bane's Lab Build Scripts
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/build-scripts#file-build-scripts-core-converters-baseline-converter-ts
Source text: https://banes-lab.com/source/build-scripts/core/converters/baseline.converter.ts.txt

Listed in [core/converters](https://banes-lab.com/api/source/build-scripts/core/converters.md), after [core/converters/base.converter.ts](https://banes-lab.com/source/build-scripts/core/converters/base.converter.ts.md) and before [core/converters/browser.converter.ts](https://banes-lab.com/source/build-scripts/core/converters/browser.converter.ts.md).

## Definitions

- `isExact` (lexical_declaration, line 10)
- `exactReferencesOf` (lexical_declaration, line 22, exported)
- `stillWritten` (lexical_declaration, line 36)
- `targetOf` (lexical_declaration, line 43)
- `isChannel` (lexical_declaration, line 75)
- `parseBaseline` (lexical_declaration, line 88, exported)
- `lostReferences` (lexical_declaration, line 48, exported)
- `isEntry` (lexical_declaration, line 79)
- `CHANNELS` (lexical_declaration, line 5)
- `EXACT_KINDS` (lexical_declaration, line 6)
- `STRINGS_CHANNEL` (lexical_declaration, line 7)
- `QUOTES` (lexical_declaration, line 8)
- `byEntry` (lexical_declaration, line 14)
- `targets` (lexical_declaration, line 44)
- `currentCandidates` (lexical_declaration, line 61, exported)
- `site` (lexical_declaration, line 65, exported)
- `target` (lexical_declaration, line 66, exported)
- `parsed` (lexical_declaration, line 89, exported)

## Contained in

- [core/converters](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-core-converters.md)

## Used by

- [core/coordinators/anatomy.coordinator.ts](https://banes-lab.com/source/build-scripts/core/coordinators/anatomy.coordinator.ts.md)
- [core/persistence/baseline.persistence.ts](https://banes-lab.com/source/build-scripts/core/persistence/baseline.persistence.ts.md)

## Linked from

- [core/converters](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-core-converters.md)
- [core/coordinators](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-core-coordinators.md)
- [core/persistence](https://banes-lab.com/anatomy/build-scripts/folder-build-scripts-core-persistence.md)

## Source

```typescript
import type { ExactReference, ReferenceChannel, ReferenceSite } from "#types/anatomy.types";
import type { CodeTarget } from "@banes-lab/web/types/code.types.js";
import { isRecord } from "#core/selectors/base.selector";

const CHANNELS: readonly ReferenceChannel[] = ["spans", "strings", "words"];
const EXACT_KINDS: ReadonlySet<CodeTarget["kind"]> = new Set(["definition", "file", "folder", "record"]);
const STRINGS_CHANNEL: ReferenceChannel = "strings";
const QUOTES: readonly string[] = ['"', "'", "`"];

const isExact = function isExact(target: CodeTarget | undefined): boolean {
    return target !== undefined && EXACT_KINDS.has(target.kind);
};

const byEntry = function byEntry(left: ExactReference, right: ExactReference): number {
    return (
        left.path.localeCompare(right.path) ||
        left.channel.localeCompare(right.channel) ||
        left.text.localeCompare(right.text)
    );
};

export const exactReferencesOf = function exactReferencesOf(
    sites: ReadonlyMap<string, ReferenceSite>,
): readonly ExactReference[] {
    return [...sites]
        .flatMap(([path, site]) =>
            CHANNELS.flatMap((channel) =>
                Object.entries(site.references[channel])
                    .filter(([, target]) => isExact(target))
                    .map(([text]) => ({ channel, path, text })),
            ),
        )
        .toSorted(byEntry);
};

const stillWritten = function stillWritten(source: string, entry: ExactReference): boolean {
    if (entry.channel !== STRINGS_CHANNEL) {
        return source.includes(entry.text);
    }
    return QUOTES.some((quote) => source.includes(quote + entry.text + quote));
};

const targetOf = function targetOf(entry: ExactReference, site: ReferenceSite): CodeTarget | undefined {
    const targets = site.references[entry.channel];
    return Object.hasOwn(targets, entry.text) ? targets[entry.text] : undefined;
};

export const lostReferences = function lostReferences(
    baseline: readonly ExactReference[],
    sites: ReadonlyMap<string, ReferenceSite>,
): readonly ExactReference[] {
    return baseline.filter((entry) => {
        const site = sites.get(entry.path);
        if (site === undefined || !stillWritten(site.source, entry)) {
            return false;
        }
        return !isExact(targetOf(entry, site));
    });
};

export const currentCandidates = function currentCandidates(
    entry: ExactReference,
    sites: ReadonlyMap<string, ReferenceSite>,
): readonly string[] {
    const site = sites.get(entry.path);
    const target = site === undefined ? undefined : targetOf(entry, site);
    if (target?.kind !== "candidates") {
        return [];
    }
    return target.locations.map((location) =>
        location.line === null ? location.file : `${location.file}:${String(location.line)}`,
    );
};

const isChannel = function isChannel(value: unknown): value is ReferenceChannel {
    return CHANNELS.some((channel) => channel === value);
};

const isEntry = function isEntry(value: unknown): value is ExactReference {
    return (
        isRecord(value) &&
        isChannel(value["channel"]) &&
        typeof value["path"] === "string" &&
        typeof value["text"] === "string"
    );
};

export const parseBaseline = function parseBaseline(text: string): readonly ExactReference[] {
    const parsed: unknown = JSON.parse(text);
    return Array.isArray(parsed) ? parsed.filter(isEntry) : [];
};
```
