# core/parsers/dependency.parser.ts

> 28 lines of code and 16 definitions.

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

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

## Definitions

- `codeImports` (lexical_declaration, line 29, exported)
- `specifierNames` (lexical_declaration, line 17)
- `IMPORT_STATEMENT` (lexical_declaration, line 5)
- `IMPORT_SPECIFIER` (lexical_declaration, line 6)
- `SOURCE_FIELD` (lexical_declaration, line 7)
- `NAME_FIELD` (lexical_declaration, line 8)
- `MIN_QUOTED` (lexical_declaration, line 9)
- `QUOTES` (lexical_declaration, line 10)
- `stripQuotes` (lexical_declaration, line 12)
- `quoted` (lexical_declaration, line 13)
- `name` (lexical_declaration, line 18)
- `bindingOf` (lexical_declaration, line 23)
- `source` (lexical_declaration, line 24)
- `importedNames` (lexical_declaration, line 25)
- `binding` (lexical_declaration, line 30, exported)
- `here` (lexical_declaration, line 31, exported)

## Contained in

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

## Uses

- [core/selectors/syntax.selector.ts](https://banes-lab.com/source/patterns/core/selectors/syntax.selector.ts.md)

## Used by

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

## Linked from

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

## Source

```typescript
import { childNodes, fieldText } from "#core/selectors/syntax.selector";
import type { CstNode } from "@govlab/code-parse";
import type { ImportBinding } from "#types/code.types";

const IMPORT_STATEMENT = "import_statement";
const IMPORT_SPECIFIER = "import_specifier";
const SOURCE_FIELD = "source";
const NAME_FIELD = "name";
const MIN_QUOTED = 2;
const QUOTES: readonly string[] = ['"', "'"];

const stripQuotes = function stripQuotes(text: string): string {
    const quoted = QUOTES.some((quote) => text.startsWith(quote) && text.endsWith(quote));
    return quoted && text.length >= MIN_QUOTED ? text.slice(1, -1) : text;
};

const specifierNames = function specifierNames(node: CstNode): string[] {
    const name = node.type === IMPORT_SPECIFIER ? fieldText(node, NAME_FIELD) : "";
    const here = name.length > 0 ? [name] : [];
    return [...here, ...childNodes(node).flatMap(specifierNames)];
};

const bindingOf = function bindingOf(node: CstNode): ImportBinding | null {
    const source = stripQuotes(fieldText(node, SOURCE_FIELD));
    const importedNames = specifierNames(node);
    return source.length > 0 && importedNames.length > 0 ? { importedNames, source } : null;
};

export const codeImports = function codeImports(node: CstNode): ImportBinding[] {
    const binding = node.type === IMPORT_STATEMENT ? bindingOf(node) : null;
    const here = binding === null ? [] : [binding];
    return [...here, ...childNodes(node).flatMap(codeImports)];
};
```
