# core/selectors/syntax.selector.ts

> 31 lines of code and 9 definitions.

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

Listed in [core/selectors](https://banes-lab.com/api/source/code-parse/core/selectors.md).

## Definitions

- `walk` (lexical_declaration, line 15, exported)
- `childrenOf` (lexical_declaration, line 4)
- `commentNodes` (lexical_declaration, line 26, exported)
- `children` (lexical_declaration, line 5)
- `index` (lexical_declaration, line 6)
- `child` (lexical_declaration, line 7)
- `stack` (lexical_declaration, line 16, exported)
- `node` (lexical_declaration, line 18, exported)
- `found` (lexical_declaration, line 27, exported)

## Contained in

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

## Uses

- [core/predicates/syntax.predicate.ts](https://banes-lab.com/source/code-parse/core/predicates/syntax.predicate.ts.md)

## Linked from

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

## Source

```typescript
import type { CstNode } from "#types/syntax.types";
import { isCommentType } from "#core/predicates/syntax.predicate";

const childrenOf = function childrenOf(node: CstNode): CstNode[] {
    const children: CstNode[] = [];
    for (let index = node.childCount - 1; index >= 0; index -= 1) {
        const child = node.child(index);
        if (child !== null) {
            children.push(child);
        }
    }
    return children;
};

export const walk = function walk(root: CstNode, visit: (node: CstNode) => void): void {
    const stack: CstNode[] = [root];
    while (stack.length > 0) {
        const node = stack.pop();
        if (node) {
            visit(node);
            stack.push(...childrenOf(node));
        }
    }
};

export const commentNodes = function commentNodes(root: CstNode): CstNode[] {
    const found: CstNode[] = [];
    walk(root, (node) => {
        if (isCommentType(node.type)) {
            found.push(node);
        }
    });
    return found;
};
```
