简体   繁体   English

`esprima` AST Tree:如何轻松检测和添加函数括号?

[英]`esprima` AST Tree: How to easily detect and add function parens?

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 . TL; DR:我想做与https://github.com/nolanlawson/optimize-js相同的事情,但是当我使用esprima遍历 AST 树时使用estraverse

ESPrima gives same output nodes for following code: ESPrima 为以下代码提供相同的输出节点:

!function (){}()

and

!(function (){})()

http://esprima.org/demo/parse.html?code=!function%20()%7B%7D()%0A%0A!(function%20()%7B%7D)() 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.例如 - 我将遍历 AST 树。 On Function Expression ExpressionStatement node i want to check - if that node doesn't have parens around function - i want to add it.在 Function Expression ExpressionStatement 节点上,我想检查 - 如果该节点在函数周围没有括号 - 我想添加它。

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.我查看令牌,但我不知道如何将平面令牌对象与具有指定 AST 节点的对象相关联。

似乎它的任务不是针对 esprima,而是针对 escodegen https://github.com/estools/escodegen/issues/315

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测是否使用分号终止由Esprima生成的Mozilla Parser AST中的表达式? - How does one detect whether a semicolon is used to terminate an expression in the Mozilla Parser AST as generated by Esprima? 如何将关键字添加到 acorn 或 esprima 解析器 - How to add keyword to acorn or esprima parser 如何通过javascript(esprima)将一个节点插入到抽象语法树中 - How to insert one node into an abstract syntax tree by javascript (esprima) 如何在esprima解析器上添加新的语义? - How can I add a new semantics on esprima parser? 如何使用acorn.js 或类似库向ast 树添加新节点? - How to use acorn.js or similar library to add a new node to the ast tree? Esprima get 函数和变量声明 - Esprima get function and variable declarations Javascript解析器本身是用ESPRIMA这样的javascript编写的,它是如何工作的? 然后谁来解析ESPRIMA的javascript - How it works that Javascript parser is itself written in javascript like ESPRIMA ? Then who parses the javascript of ESPRIMA 无法使用Esprima / Acorn解析函数:意外令牌'(' - Unable to parse function with Esprima/Acorn: unexpected token '(' 如何使用recast / esprima创建await表达式 - How do i create an await expression using recast/esprima 如何使用 esprima 从 Java 解析 javascript 文件? - How to use esprima to parse a javascript file, from Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM