# core/adapters/tool.shellcheck.adapter.ts

> 35 lines of code and 9 definitions.

Tree: GovLab Quality
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/quality#file-quality-core-adapters-tool-shellcheck-adapter-ts
Source text: https://banes-lab.com/source/quality/core/adapters/tool.shellcheck.adapter.ts.txt

Listed in [core/adapters](https://banes-lab.com/api/source/quality/core/adapters.md), after [core/adapters/tool.semgrep.adapter.ts](https://banes-lab.com/source/quality/core/adapters/tool.semgrep.adapter.ts.md) and before [core/adapters/tool.slither.adapter.ts](https://banes-lab.com/source/quality/core/adapters/tool.slither.adapter.ts.md).

## Definitions

- `shellcheckFlags` (lexical_declaration, line 16, exported)
- `runShellcheck` (lexical_declaration, line 26)
- `TOOL` (lexical_declaration, line 14)
- `severity` (lexical_declaration, line 17, exported)
- `exclude` (lexical_declaration, line 18, exported)
- `files` (lexical_declaration, line 27)
- `flags` (lexical_declaration, line 31)
- `advisory` (lexical_declaration, line 32)
- `spec` (lexical_declaration, line 33)

## Contained in

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

## Uses

- [core/adapters/tool.adapter.ts](https://banes-lab.com/source/quality/core/adapters/tool.adapter.ts.md)
- [core/selectors/record.selector.ts](https://banes-lab.com/source/quality/core/selectors/record.selector.ts.md)

## Linked from

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

## Source

```typescript
import { boolField, stringArrayField, stringField } from "#core/selectors/record.selector";
import { scanTool, standardScan } from "#core/adapters/tool.adapter";
import { EMPTY_RESULT } from "#core/factories/finding.factory";
import { FINDING_STATUSES } from "#configuration/constants/tool.constants";
import type { RunResult } from "#types/finding.types";
import type { RunnerContext } from "#types/tool.types";
import { defineTool } from "#core/registries/tool.registry";
import { gatingAdvisory } from "#core/factories/tool.factory";
import { installHint } from "#configuration/strings/tool.strings";
import { parseShellcheckOutput } from "#core/parsers/tool.shellcheck.parser";
import { scopedFiles } from "#core/resolvers/scope.resolver";
import { toolSection } from "#core/resolvers/tool.resolver";

const TOOL = "shellcheck";

export const shellcheckFlags = function shellcheckFlags(section: Record<string, unknown>): string[] {
    const severity = stringField(section, "severity");
    const exclude = stringArrayField(section, "exclude");
    return [
        ...(boolField(section, "enableAll") ? ["--enable=all"] : []),
        ...(severity.length > 0 ? [`--severity=${severity}`] : []),
        ...(exclude.length > 0 ? [`--exclude=${exclude.join(",")}`] : []),
    ];
};

const runShellcheck = async function runShellcheck(context: RunnerContext): Promise<RunResult> {
    const files = await scopedFiles(context.root, context.paths);
    if (files.length === 0) {
        return EMPTY_RESULT;
    }
    const flags = shellcheckFlags(await toolSection(context.root, TOOL));
    const advisory = gatingAdvisory(context, TOOL, installHint(TOOL));
    const spec = standardScan(advisory, FINDING_STATUSES, (result) =>
        parseShellcheckOutput(result.stdout, context.ecosystem),
    );
    return scanTool(advisory, { args: ["-f", "json", ...flags, ...files], bin: TOOL, cwd: context.root }, spec);
};

defineTool({ ecosystems: ["shell"], run: runShellcheck, tool: TOOL });
```
