It's been reformatted to not be class-based, but the code still needs quite a bit of re-writing. When you're confused what the code is doing it's probably because it's broken - look at the relevant part of the old code to see what the intent was.
Everywhere that's currently using deparse needs to go - we've changed the signature of the visitors so they need to be used with print now instead.
Old code was like this:
['A_Indices'](node) {
// ...
}
New code generally follows this pattern:
['A_Indices'](path, options, print) {
const node = path.getValue(); // < This is the same node as in the old code
// ...
}
Here's come common patterns in the ... section above:
path.call(callback, path1, path2, ...) - will call callback passing a new FastPath object that represents node[path1][path2] - note that the path elements can be strings (for objects) or numbers (for arrays).
path.map(callback, path1, path2, ...) - as path.call except the entry identified by node[path1][path2] is expected to be an array, so the callback is called for each entry of that array.
Since prettier is happy dealing with strings it's wise to start at the bottom (i.e. the "statement" AST nodes) and then work your way up through the tree until all branches and leaves are prettier Doc elements.
print.js
was originally based on this file to give us a head start:https://github.com/pyramation/pg-query-parser/blob/8a83b18bfd3ff85d40f10ea1d679e4605a8b1022/src/deparser.js
It's been reformatted to not be class-based, but the code still needs quite a bit of re-writing. When you're confused what the code is doing it's probably because it's broken - look at the relevant part of the old code to see what the intent was.
Everywhere that's currently using
deparse
needs to go - we've changed the signature of the visitors so they need to be used withprint
now instead.Old code was like this:
New code generally follows this pattern:
Here's come common patterns in the
...
section above:Old code:
New code:
Old code:
New code:
Note the helpers:
path.call(callback, path1, path2, ...)
- will callcallback
passing a new FastPath object that representsnode[path1][path2]
- note that the path elements can be strings (for objects) or numbers (for arrays).path.map(callback, path1, path2, ...)
- aspath.call
except the entry identified bynode[path1][path2]
is expected to be an array, so the callback is called for each entry of that array.Since prettier is happy dealing with strings it's wise to start at the bottom (i.e. the "statement" AST nodes) and then work your way up through the tree until all branches and leaves are prettier
Doc
elements.Handy reference: