将所有变量声明包装在功能中
我有一个JavaScript函数声明为字符串(从function.tostring
),我想用函数包装所有变量声明(也在JavaScript中),例如 const value = 42
to const value = wrapper(42)
。
首先,我想到使用正直值来获取原始值和位置,然后用包装值替换它们,但是由于需要考虑多行字符串和对象之类的事物,因此正则是非常复杂的。使用Regex也会影响其他人为该项目做出贡献的便利性。
之后,我研究了为此使用一个模块,我找到了橡子(由Babel,Svelte。将JavaScript解析为estree,javascript抽象语法树的规格): https://github.com/acornjs/acorn ,但是我找不到将Estree解析回Javascript函数声明的方法,在进行修改后声明。
是否有一种方法可以将estree解析回功能或其他更好的解决方案?
I have a Javascript function declartions as a string (gotten from Function.toString
), and I want to wrap all variable declarations with a function (also in Javascript), E.g.const value = 42
to const value = wrapper(42)
.
First I thought of using RegEx to get the original values and location and then replace them with the wrapped value, but the RegEx got too complex very fast because of needing to think about things like multiline strings and objects. Using RegEx would also impact the ease of other people contributing to the project.
After that I looked into using a module for this, I found Acorn (used by Babel, Svelte. Parses the Javascript into an ESTree, the spec for Javascript Abstract Syntax Trees): https://github.com/acornjs/acorn, but I couldn't find a way of parsing the ESTree back to a Javascript function declaration after making the modifications.
Is there a way of parsing the ESTree back to a function, or another better solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上不需要将树将树重新串起来的函数。相反,请注意应该发生更改的位置 offsets ,然后不要将更改应用于原始字符串中。
这是带有橡子API的演示:
You don't really need a function to stringify the tree back into code. Instead, take note of the offsets where the change should occur, and then don't apply the change in the tree, but to the original string.
Here is a demo with the acorn API:
是:
您可以使用
recast
要打印结果,它将 支持babel
,acorn
,甚至typescript
解析器。但是最简单的方法是使用
You can use
recast
to print results, it would be something like:It supports
babel
,acorn
, and eventypescript
parsers.But the simplest possible way would be to use ????Putout code transformer.
Here is how it can look like:
????Putout has both parser and printer inside of itself, so you can just use function call to do the thing:
Check out in ????Putout Editor.