# core/adapters/syntax.adapter.ts

> 31 lines of code and 9 definitions.

Tree: Code Parse
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/code-parse#file-code-parse-core-adapters-syntax-adapter-ts
Source text: https://banes-lab.com/source/code-parse/core/adapters/syntax.adapter.ts.txt

Listed in [core/adapters](https://banes-lab.com/api/source/code-parse/core/adapters.md), after [core/adapters/shell.adapter.ts](https://banes-lab.com/source/code-parse/core/adapters/shell.adapter.ts.md).

## Definitions

- `isNamedOf` (lexical_declaration, line 3)
- `setField` (lexical_declaration, line 7)
- `adaptNode` (lexical_declaration, line 13, exported)
- `kids` (lexical_declaration, line 14, exported)
- `fields` (lexical_declaration, line 15, exported)
- `count` (lexical_declaration, line 16, exported)
- `index` (lexical_declaration, line 17, exported)
- `raw` (lexical_declaration, line 18, exported)
- `adapted` (lexical_declaration, line 19, exported)

## Contained in

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

## Linked from

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

## Source

```typescript
import type { CstNode, RawNode } from "#types/syntax.types";

const isNamedOf = function isNamedOf(node: RawNode): boolean {
    return typeof node.isNamed === "function" ? node.isNamed() : node.isNamed;
};

const setField = function setField(fields: Map<string, CstNode>, adapted: CstNode | null, field: string | null): void {
    if (adapted !== null && field !== null && field.length > 0) {
        fields.set(field, adapted);
    }
};

export const adaptNode = function adaptNode(node: RawNode): CstNode {
    const kids: (CstNode | null)[] = [];
    const fields = new Map<string, CstNode>();
    const count = node.childCount;
    for (let index = 0; index < count; index += 1) {
        const raw = node.child(index);
        const adapted = raw === null ? null : adaptNode(raw);
        kids.push(adapted);
        setField(fields, adapted, node.fieldNameForChild?.(index) ?? null);
    }
    return {
        child: (index: number): CstNode | null => kids.at(index) ?? null,
        childCount: count,
        childForFieldName: (field: string): CstNode | null => fields.get(field) ?? null,
        endIndex: node.endIndex,
        isNamed: isNamedOf(node),
        startIndex: node.startIndex,
        startPosition: node.startPosition ?? { column: 0, row: 0 },
        text: node.text ?? "",
        type: node.type,
    };
};
```
