1

TL;DR: i want to do same thing as there https://github.com/nolanlawson/optimize-js but with esprima when i traverse through AST tree with estraverse.

ESPrima gives same output nodes for following code:

!function (){}()

and

!(function (){})()

http://esprima.org/demo/parse.html?code=!function%20()%7B%7D()%0A%0A!(function%20()%7B%7D)()

For example - i will traverse through AST tree. On Function Expression ExpressionStatement node i want to check - if that node doesn't have parens around function - i want to add it.

So, how i can detect function parens, how i can add them? I look at tokens, but i have no idea how i can associate flat tokens object with object with specified AST node.

  • On esprima homepage it says: "Sensible syntax tree format as standardized by EStree project" and that only seems to include AST Nodes from f.e. [ESTree (ES5) Spec](https://github.com/estree/estree/blob/f32646033ed12e6aaa7f5cd496d936a8f8de194e/es5.md). There is no typical *Subexpression* for Parenthesis, this is only needed by the Parser (would be contained in a parse tree). You probably need your own extension or create the parens on rewriting. – makadev Oct 10 '16 at 15:39
  • Seems it task not for `esprima` but for `escodegen` – Качалов Тимофей Oct 11 '16 at 12:00
  • Maybe, but as far as I can see, there is no option to do so easily. You could override the `CallExpression` part of [the Code Generator](https://github.com/estools/escodegen/blob/719de3033c4a9a0df27f698c47b48d2af7241835/escodegen.js#L1874) and maybe add your parenthesis around it, if it's `callee` is a `FunctionExpression` (f.e. by modification of `parenthesize` precedence). Not sure if that may wreck something else, f.e. methods. Documentation mostly covers some formatting settings and it seems they had some - fruitless - Discussions about adding a Parenthesis Expression. Good Luck with that. – makadev Oct 11 '16 at 12:59

1 Answers1

1

Seems it task not for esprima but for escodegen https://github.com/estools/escodegen/issues/315