从 JSLINT 解析树生成 JavaScript
使用 Crockford 的 JSLINT,在调用 JSLINT()
解析一些 JavaScript 源代码后,它提供了通过 JSLINT.tree
访问解析树
如果能够分析树,根据分析对其进行结构更改,然后从修改后的树生成新的 JavaScript,这将非常强大。
是否有一个开源项目基于 JSLINT 构建,提供从解析树到 JavaScript 的转换?
(这是否可能,即 JSLINT 是否保留了从解析到往返所有重要内容的完整信息?)
With Crockford's JSLINT, after calling JSLINT()
to parse some JavaScript source, it provides access to the parse tree via JSLINT.tree
It would be really powerful to be able to analyse the tree, make structural changes to it based on the analysis and then generate new JavaScript from the modified tree.
Is there an open source project that builds on JSLINT by providing a conversion from a parse tree back to JavaScript?
(Is this even possible, i.e. does JSLINT keep complete enough information from the parse to round-trip everything significant?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道 JSLint 是否可以做到这一点(查看一些论坛帖子,看起来维护树不是他们的目标,而是执行 linting 过程的副产品)。
uglifyjs 项目可能有助于从 javascript 生成 AST 并将 AST 重新转换为代码。请参阅 此 AST 到 JavaScript 的方法。
I dont know if JSLint can do it (looking at some of the forum postings, it doesnt look like maintaining the tree is their goal but its a by-product for doing the linting process).
The uglifyjs project may help with generating an AST from javascript and re-converting a AST to code. See this method for AST to javascript.
不是开源的,但完全符合 OP 的要求:我们的 JavaScript 前端。
该前端基于我们的 DMS 软件重新工程工具包,该工具包是通用可定制的编译器技术可用于构建任意代码分析器和转换器。 DMS 有许多前端可用于多种语言(COBOL、Java、C++...)
特别是基于 DMS 的解析器(包括其 JavaScript 解析器),收集用于重新生成源树的完整信息,包括列开始和结束信息对于标记、数字文字的基数、字符串引用约定的差异等,使其能够在未应用转换的情况下进行保真打印。 DMS 还为树处理提供许多其他有用的服务,例如访问者、符号表构建支持、流分析支持、模式匹配以及源到源转换。
它可以提供所有这些服务,因为它的工程成本已分摊到许多语言和应用程序中。
我们这样做是因为正如丹尼尔所说,“可能性是无限的”。他给出了一个通过检测代码来监视运行时故障的示例;这种检测是一个非常有用的想法,我们正是基于这种想法构建测试覆盖率工具。
Not open source, but does exactly what OP requests: our JavaScript Front End.
This front end is based on our DMS Software Reengineering Toolkit, which is general purpose customizable compiler technology useful for building arbitrary code analyzers and transformers. DMS has many front ends available for many languages (COBOL, Java, C++, ...)
In particular, DMS-based parsers (including its JavaScript one), collect complete information for regenerating the source tree, including column-start and end information for tokens, radix of numeric literals, differences in string quoting conventions, etc. to enable it to do fidelity printing where no transformations have been applied. DMS also provides many other useful services for tree processing such as visitors, symbol table construction support, flow analysis support, pattern matching, as well as source-to-source transformations.
It can provide all these services because the cost of engineering it has been amortized across many, many languages and applications.
We did it because as Daniel said, "the possibilities are limitless". He gives an example of instrumenting the code to watch for runtime failures; this kind of instrumentation is a very useful idea and we build test coverage tools based on exactly this thought.