Skip to content
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

Invalid output when indexing anonymous table #15

Closed
fnuecke opened this issue Jul 10, 2013 · 9 comments
Closed

Invalid output when indexing anonymous table #15

fnuecke opened this issue Jul 10, 2013 · 9 comments
Assignees
Labels

Comments

@fnuecke
Copy link

fnuecke commented Jul 10, 2013

I'm not sure if there's a special term for this, but I'm talking about this pattern: ({...})[key].

For example: luamin.minify("print(({})[1])") will output
print({}[1]) which is invalid. It should remain as print(({})[1]).

Tested with luaparse 0.1.4 and luamin 0.2.7.

@mathiasbynens
Copy link
Owner

Good catch!

Looks like an IndexExpression of which the base is a TableConstructorExpression must be wrapped in parentheses. I suppose the same goes for MemberExpressions.

@fnuecke
Copy link
Author

fnuecke commented Jul 10, 2013

Awesome, thanks for the quick response and fix!

@mathiasbynens
Copy link
Owner

mathiasbynens commented Jul 10, 2013

@fnuecke Thanks for the helpful bug report with a reduced test case!


Perhaps the fix was a bit naive / incomplete.

E.g. print(('a'..'b')[0])print('a'..'b'[0]), which is not the same.

After giving it some more thought, I think the base of a MemberExpression should always be wrapped in parens except when it’s an identifier.

Update: Ah, that is another oversimplification. Following that logic, e.g. print(a.b.c)print((a.b).c) which is unneeded. The best way to fix this is probably by extending PRECEDENCE.

@mathiasbynens mathiasbynens reopened this Jul 10, 2013
@fnuecke
Copy link
Author

fnuecke commented Jul 11, 2013

Good point. It probably should. Another testcase: (function()end)() becomes function()end() but should remain as is.

@yihuang
Copy link

yihuang commented May 9, 2016

What's the status of the bug with anonymous function?

@ghost
Copy link

ghost commented May 24, 2016

Well I just took a look and decided to fix this. I think this project has probably been abandoned since the fix was so easy.

Just change this

result = formatExpression(expression.base) + '(';

https://github.com/mathiasbynens/luamin/blob/master/luamin.js#L331

if(expression.base.inParens && expression.base.type == 'FunctionDeclaration')
        result = '(' + formatExpression(expression.base) + ')('
else
        result = formatExpression(expression.base) + '(';

If you have any future issues you can easily debug them by running luaparse and looking at what the AST looks like

./luaparse -b -c '(function()end)()'
{
  "type": "Chunk",
  "body": [
    {
      "type": "CallStatement",
      "expression": {
        "type": "CallExpression",
        "base": {
          "type": "FunctionDeclaration",
          "identifier": null,
          "isLocal": false,
          "parameters": [],
          "body": [],
          "inParens": true
        },
        "arguments": []
      }
    }
  ],
  "comments": []
}

@mathiasbynens
Copy link
Owner

@EdanPyx Care to submit a pull request?

@ghost
Copy link

ghost commented May 24, 2016

Done #36

@mathiasbynens
Copy link
Owner

575a4bb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants