# core/parsers/code.parser.ts

> 62 lines of code and 10 definitions.

Tree: GovLab Patterns
Language: typescript
Layer: processing
Canonical: https://banes-lab.com/anatomy/patterns#file-patterns-core-parsers-code-parser-ts
Source text: https://banes-lab.com/source/patterns/core/parsers/code.parser.ts.txt

Listed in [core/parsers](https://banes-lab.com/api/source/patterns/core/parsers.md), before [core/parsers/definition.parser.ts](https://banes-lab.com/source/patterns/core/parsers/definition.parser.ts.md).

## Definitions

- `ingestFile` (lexical_declaration, line 57, exported)
- `ingestCode` (lexical_declaration, line 26, exported)
- `ingestSymbols` (lexical_declaration, line 35, exported)
- `ingestImports` (lexical_declaration, line 44, exported)
- `ParsedSource` (interface_declaration, line 8)
- `parsedSource` (lexical_declaration, line 13)
- `language` (lexical_declaration, line 49, exported)
- `root` (lexical_declaration, line 53, exported)
- `parsed` (lexical_declaration, line 62, exported)
- `{ language, root }` (lexical_declaration, line 66, exported)

## Contained in

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

## Uses

- [core/parsers/definition.parser.ts](https://banes-lab.com/source/patterns/core/parsers/definition.parser.ts.md)
- [core/parsers/dependency.parser.ts](https://banes-lab.com/source/patterns/core/parsers/dependency.parser.ts.md)
- [core/parsers/syntax.parser.ts](https://banes-lab.com/source/patterns/core/parsers/syntax.parser.ts.md)

## Linked from

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

## Source

```typescript
import { type CodeParseOptions, type CstNode, detectLanguage, parseCode } from "@govlab/code-parse";
import type { CodeSymbol, ImportBinding, IngestFile } from "#types/code.types";
import { IMPORT_LANGUAGES } from "#configuration/constants/dependency.constants";
import { codeImports } from "#core/parsers/dependency.parser";
import { codeRecords } from "#core/parsers/syntax.parser";
import { codeSymbols } from "#core/parsers/definition.parser";

interface ParsedSource {
    language: string;
    root: CstNode;
}

const parsedSource = async function parsedSource(
    source: string,
    filename: string,
    options: CodeParseOptions,
): Promise<ParsedSource | null> {
    const language = detectLanguage(filename, source);
    if (language === null) {
        return null;
    }
    const root = await parseCode(source, language, options);
    return root === null ? null : { language, root };
};

export const ingestCode = async function ingestCode(
    source: string,
    filename: string,
    options: CodeParseOptions = {},
): Promise<Record<string, unknown>[]> {
    const parsed = await parsedSource(source, filename, options);
    return parsed === null ? [] : codeRecords(parsed.root, parsed.language);
};

export const ingestSymbols = async function ingestSymbols(
    source: string,
    filename: string,
    options: CodeParseOptions = {},
): Promise<CodeSymbol[]> {
    const parsed = await parsedSource(source, filename, options);
    return parsed === null ? [] : codeSymbols(parsed.root);
};

export const ingestImports = async function ingestImports(
    source: string,
    filename: string,
    options: CodeParseOptions = {},
): Promise<ImportBinding[]> {
    const language = detectLanguage(filename, source);
    if (language === null || !IMPORT_LANGUAGES.has(language)) {
        return [];
    }
    const root = await parseCode(source, language, options);
    return root === null ? [] : codeImports(root);
};

export const ingestFile = async function ingestFile(
    source: string,
    filename: string,
    options: CodeParseOptions = {},
): Promise<IngestFile> {
    const parsed = await parsedSource(source, filename, options);
    if (parsed === null) {
        return { imports: [], records: [], symbols: [] };
    }
    const { language, root } = parsed;
    return { imports: codeImports(root), records: codeRecords(root, language), symbols: codeSymbols(root) };
};
```
