-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reflect parentheses that discard extra values in the AST #79
Comments
There are two kinds of expressions which may evaluate to a value tuple: function calls and the vararg literal,
In the last of these, parenthesising the only expression also has the effect of preventing tail-call optimisation. This suggests a representation as a new type of node, so that tail calls can be detected simply by checking whether a function-call node appears as a direct child of the Alternatively, I might treat parentheses as a circumfix unary operator, and piggyback on the |
Im currently updating a fork of lua-fmt that uses on old version of this library. needsParens() {
const parent = this.getParent() as luaparse.Node
const value = this.getValue() as luaparse.Node
let inParens = false
switch (value.type) {
case 'FunctionDeclaration':
case 'Chunk':
case 'Identifier':
case 'BooleanLiteral':
case 'NilLiteral':
case 'NumericLiteral':
case 'StringLiteral':
case 'VarargLiteral':
case 'TableConstructorExpression':
case 'BinaryExpression':
case 'LogicalExpression':
case 'UnaryExpression':
case 'MemberExpression':
case 'IndexExpression':
case 'CallExpression':
case 'TableCallExpression':
case 'StringCallExpression':
inParens = value.inParens || false
}
if (parent) {
/*
If this UnaryExpression is nested below another UnaryExpression, wrap the nested expression in
parens. This not only improves readability of complex expressions, but also prevents `- -1` from
becoming `--1`, which would result in a comment.
*/
if (value.type === 'UnaryExpression' && parent.type === 'UnaryExpression') {
inParens = true
}
}
return inParens
} |
I found a solution (mixing prettier's lua and php plugin approaches). The only thing that is missing for now is distinguishing |
Any update on this? I was working on a custom Lua minifier with a somewhat better compression ratio than luamin, and am currently stuck with a luaparse version from 2019 (before .inParens was removed). |
This is a MAJOR BUG giving a totally useless and INCORRECT parse. Why hasn't anyone fixed this yet? |
Currently, the latest released version does not distinguish e.g.
f()
from(f())
in the AST. These expressions are not equivalent; the former evaluates to all the return values off()
, and the latter evaluates to only the first of them (the rest are discarded). Current git master distinguishes them by theinParens
field, but that is just a workaround for #24 and I expect to remove it when that issue is fixed properly.A more principled approach to distinguishing these expressions is desired. I may keep the
inParens
field, or I may introduce a new kind of node that expresses the operation of discarding extra values.The text was updated successfully, but these errors were encountered: