# core/loaders/eslint.loader.ts

> 50 lines of code and 13 definitions.

Tree: GovLab Quality
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/quality#file-quality-core-loaders-eslint-loader-ts
Source text: https://banes-lab.com/source/quality/core/loaders/eslint.loader.ts.txt

Listed in [core/loaders](https://banes-lab.com/api/source/quality/core/loaders.md), after [core/loaders/emitter.loader.ts](https://banes-lab.com/source/quality/core/loaders/emitter.loader.ts.md) and before [core/loaders/folder.loader.ts](https://banes-lab.com/source/quality/core/loaders/folder.loader.ts.md).

## Definitions

- `assertCorePluginsLoaded` (lexical_declaration, line 39, exported)
- `unwrap` (lexical_declaration, line 10)
- `loadModule` (lexical_declaration, line 12, exported)
- `installedEslintPlugins` (lexical_declaration, line 47, exported)
- `ESLINT_EMITTER` (lexical_declaration, line 8)
- `resolveGlobals` (lexical_declaration, line 23, exported)
- `table` (lexical_declaration, line 28, exported)
- `merged` (lexical_declaration, line 29, exported)
- `entry` (lexical_declaration, line 31, exported)
- `missing` (lexical_declaration, line 40, exported)
- `names` (lexical_declaration, line 42, exported)
- `candidates` (lexical_declaration, line 48, exported)
- `loaded` (lexical_declaration, line 51, exported)

## Contained in

- [core/loaders](https://banes-lab.com/anatomy/quality/folder-quality-core-loaders.md)

## Uses

- [configuration/strings/plugin.strings.ts](https://banes-lab.com/source/quality/configuration/strings/plugin.strings.ts.md)
- [core/predicates/failure.predicate.ts](https://banes-lab.com/source/quality/core/predicates/failure.predicate.ts.md)

## Linked from

- [configuration/strings](https://banes-lab.com/anatomy/quality/folder-quality-configuration-strings.md)
- [core/loaders](https://banes-lab.com/anatomy/quality/folder-quality-core-loaders.md)
- [core/predicates](https://banes-lab.com/anatomy/quality/folder-quality-core-predicates.md)

## Source

```typescript
import type { InstalledPlugin, LoadedPlugin } from "#types/eslint.types";
import { corePluginsMissing } from "#configuration/strings/plugin.strings";
import { isCorePlugin } from "#core/predicates/eslint.predicate";
import { isModuleMissing } from "#core/predicates/failure.predicate";
import { isRecord } from "#core/selectors/record.selector";
import { loadInstallRegistry } from "#core/loaders/dependency.loader";

const ESLINT_EMITTER = "eslint";

const unwrap = (mod: unknown): unknown => (isRecord(mod) ? (mod["default"] ?? mod) : mod);

export const loadModule = async (npm: string): Promise<unknown> => {
    try {
        return unwrap(await import(npm));
    } catch (error) {
        if (isModuleMissing(error)) {
            return null;
        }
        throw error;
    }
};

export const resolveGlobals = async (
    env: string[],
    custom?: Record<string, string>,
): Promise<Record<string, string>> => {
    const loaded = unwrap(await import("globals"));
    const table = isRecord(loaded) ? loaded : {};
    const merged: Record<string, string> = {};
    for (const name of env) {
        const entry = table[name];
        if (isRecord(entry)) {
            Object.assign(merged, entry);
        }
    }
    return { ...merged, ...custom };
};

export const assertCorePluginsLoaded = (loaded: readonly LoadedPlugin[]): void => {
    const missing = loaded.filter((entry) => entry.plugin === null && isCorePlugin(String(entry.record.npm)));
    if (missing.length > 0) {
        const names = missing.map((entry) => String(entry.record.npm)).join(", ");
        throw new Error(corePluginsMissing(names));
    }
};

export const installedEslintPlugins = async (): Promise<InstalledPlugin[]> => {
    const candidates = loadInstallRegistry().records.filter(
        (record) => record.emitter === ESLINT_EMITTER && record.isPlugin && Boolean(record.npm),
    );
    const loaded = await Promise.all(
        candidates.map(async (record) => ({ plugin: await loadModule(record.npm ?? ""), record })),
    );
    assertCorePluginsLoaded(loaded);
    return loaded.filter((entry): entry is InstalledPlugin => entry.plugin !== null);
};
```
