Avoid `parenthesis` and `cast` nodes in ast
See original GitHub issueExample how this do babylon https://astexplorer.net/#/gist/56cf77073a7a8dcc4a785a423c26a1c7/209de623aac9c972645542acfcefdcf67a7bceb1
Why? Example from our code (print return
node):
case "return": {
const parts = [];
parts.push("return");
if (node.expr) {
const printedExpr = path.call(print, "expr");
if (node.expr.kind === "bin") {
parts.push(
group(
concat([
ifBreak(" (", " "),
indent(concat([softline, printedExpr])),
softline,
ifBreak(")")
])
)
);
} else {
parts.push(" ", printedExpr);
}
}
if (hasDanglingComments(node)) {
parts.push(
" ",
comments.printDanglingComments(path, options, /* sameIndent */ true)
);
}
return concat(parts);
}
Look on node.expr.kind === "bin"
, in some cases people can have parenthesis
and our check does not works correctly and you will get bad output (ugly). Adding check on parenthesis
and parenthesis.inner
can solve this problem, but it is require a lot of code and new checks (in some places it is really a lot of code). Better use logic as in prettier
for js
. They have ast without parenthesis
and print nodes as is and then add parens basic on own logic for parenthesis
(https://github.com/prettier/prettier/blob/master/src/language-js/needs-parens.js#L43).
Same for cast
node.
All this would allow us to work in a beautiful form with the ast.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top GitHub Comments
I understand better the problem now. I have to handle another problem in order to be sure what to do, actually the cast precedence #172 is incorect (not handled at all) and I need to address this problem first.
In both cases, you’re right, you only need the cast information as an attribute, so what you can do, is to use the parser with this configuration :
This way you can rewrite any parsing function, so I’ve made a function specialy for
cast
nodes and added it to snapshots in order to avoid breaking changes on future releases.With this kind of atomic function you can still diverge from the parser implementation and continue to integrate bugfixes on each release, the catch is to be sure that these functions are atomic enough and don’t introduce parsing logic.
@ichiriac can we do same for
silent
node?