IF Commons GitHub

extensions/unist/mod

The syntax tree as a unist tree (commons lang-sql 0003-sql-ast-tools FR-009 to FR-013): toUnist projects a parse, its comments included, to nodes that carry a type (the kind), children or a value, a position of unist points and a data of the fields that are not nodes, so that unist-util-visit, unist-util-select and the rest of the unist utilities read SQL as they read Markdown. An extension of the SQL language core: it imports the core and nothing else, and the types are structural, so that nothing here depends on @types/unist.

A node with children has children, in the order of the text with the comments among them; an identifier, a number, a string, a boolean, NULL and a parameter have a value. A child says in its data which field of its parent it is (field) and its place in a list (index), so nothing of the structure is lost. The children's types are the same for every kind (spec OQ-1).

import { visit } from "unist-util-visit";
const tree = toUnist(parse("SELECT a FROM t"));
visit(tree, "table", (t) => console.log(t.position?.start.line)); // 1

Spec: 0003-sql-ast-tools FR-009, FR-010, FR-011, FR-012, FR-013

Source: commons/lang-sql/src/extensions/unist/mod.ts

3 of 8 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

UnistPointinterfaceno test imports it
interface UnistPoint

A point of a unist position.

MemberTypeWhat it is
linenumber
columnnumber
offsetnumber
UnistPositioninterfaceno test imports it
interface UnistPosition

The position of a unist node.

MemberTypeWhat it is
startUnistPoint
endUnistPoint
SqlParentinterface
interface SqlParent

A node with children.

MemberTypeWhat it is
type"add-column" | "add-constraint" | "alias" | "alter-table" | "array" | "assignment" | "between" | "binary" | "boolean" | "call" | "case" | "cast" | "check" | "collate" | "column-def" | "column-option" | "command" | "create-index" | "create-routine" | "create-table" | "create-view" | "cte" | "data-type" | "default" | "delete" | "drop" | "drop-column" | "drop-constraint" | "exists" | "extract" | "field" | "foreign-key" | "frame" | "frame-bound" | "ident" | "in" | "in-query" | "insert" | "interval" | "is" | "join" | "keyword-arg" | "lambda" | "like" | "name" | "named-arg" | "not-null" | "null" | "number" | "on-conflict" | "operator" | "order-item" | "param" | "primary-key" | "reference" | "rename-column" | "rename-table" | "routine-body" | "routine-param" | "scalar-query" | "script" | "select" | "select-item" | "set-op" | "star" | "star-modifier" | "string" | "struct" | "struct-entry" | "subquery" | "subscript" | "table" | "table-fn" | "table-like" | "table-op" | "tuple" | "type-field" | "typed-string" | "unary" | "unique" | "unparsed" | "update" | "values" | "when" | "window-def" | "window-spec" | "with"
childrenSqlChild[]
dataRecord<string, unknown>
position?UnistPosition

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

SqlLiteralinterfaceno test imports it
interface SqlLiteral

A node with a value.

MemberTypeWhat it is
type"add-column" | "add-constraint" | "alias" | "alter-table" | "array" | "assignment" | "between" | "binary" | "boolean" | "call" | "case" | "cast" | "check" | "collate" | "column-def" | "column-option" | "command" | "create-index" | "create-routine" | "create-table" | "create-view" | "cte" | "data-type" | "default" | "delete" | "drop" | "drop-column" | "drop-constraint" | "exists" | "extract" | "field" | "foreign-key" | "frame" | "frame-bound" | "ident" | "in" | "in-query" | "insert" | "interval" | "is" | "join" | "keyword-arg" | "lambda" | "like" | "name" | "named-arg" | "not-null" | "null" | "number" | "on-conflict" | "operator" | "order-item" | "param" | "primary-key" | "reference" | "rename-column" | "rename-table" | "routine-body" | "routine-param" | "scalar-query" | "script" | "select" | "select-item" | "set-op" | "star" | "star-modifier" | "string" | "struct" | "struct-entry" | "subquery" | "subscript" | "table" | "table-fn" | "table-like" | "table-op" | "tuple" | "type-field" | "typed-string" | "unary" | "unique" | "unparsed" | "update" | "values" | "when" | "window-def" | "window-spec" | "with"
valuestring | boolean | null
dataRecord<string, unknown>
position?UnistPosition
SqlCommentinterfaceno test imports it
interface SqlComment

A comment.

MemberTypeWhat it is
type"comment"
valuestring
dataRecord<string, unknown>
position?UnistPosition
SqlChildtype
type SqlChild = SqlParent | SqlLiteral | SqlComment;

A child of a node.

Imported by 5 tests of the run (5 passed, 0 failed): the library's results

UnistOptionsinterfaceno test imports it
interface UnistOptions

What toUnist may be asked.

MemberTypeWhat it is
comments?booleanWhether the comments are nodes of the tree (the default).
positions?booleanWhether each node has a position (the default).
toUnistfunction
function toUnist(parsed: Parsed, options?: UnistOptions): SqlParent

Projects a parse to a unist tree.

ParameterWhat it is
parsedwhat parse gave
optionswhether to include comments and positions

Returns: the tree, whose root is the script node

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

Titles, headings, exports, tables, columns and requirements. Esc closes.