Rewriting a tree (commons lang-sql 0003-sql-ast-toolsFR-001 to FR-004): from the leaves up, by rules given for sorts of node. A rule for a sort is called for each node that stands in a place of that sort, with its children already rewritten, and returns a node of the same sort or undefined to keep it, so a rule cannot put a node of another sort where one does not belong. A rewrite changes nothing it is given: a node no rule changed is the same object in the result, and the whole tree is, where no rule changed anything.
The sorts are those of ast.ts. A name is an expression where it stands as one (a column), and the expr rule is called for it; where a field is typed as a name (the name of a table, of a function, of a qualifier) the name rule is. The same holds for a statement (the statement rule is called where a script holds it) and the queries and sources inside it. The ranges of the nodes above a replaced one stay those of the text they were read from.
// lower-case every identifierconstlower=rewriteScript(parse("SELECT A FROM T").script, { ident: (i) => (i.name === i.name.toLowerCase() ?undefined: { ...i, name: i.name.toLowerCase() }) });