JS:重命名变量以进行重构(使用AST,而不是文本) [英] JS: rename variables for refactor (using an AST, not text)

查看:296
本文介绍了JS:重命名变量以进行重构(使用AST,而不是文本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重构代码时,我经常需要重命名变量,目前我正使用正则表达式以某种有点怪异的方式进行操作-由于缺乏实际的结构,我最终不得不采用愚蠢的文本解决方法,例如,将'req'重命名为请求"并避免具有类似请求"之类的名称的副作用.

I often need to rename variables when refactoring code, which I currently do in a somewhat hacky way using regexs - I end up having to come with silly text workaround workarounds for the lack of actual structure, eg, rename 'req' to 'request' and avoid side effects with similar names like 'require'.

考虑这些东西:有点像用正则表达式修改DOM:只是不起作用.

Thinking about this stuff: it's kind of like modifying the DOM with regexs: it just doesn't work.

我已经了解了AST和诸如Esprima之类的代码结构修改工具. 是否有一种工具可以重命名变量(基于Esprima还是其他方式)?

I've learnt about ASTs and code structure modification tools like Esprima. Is there a tool to rename variables, Esprima based or otherwise?

推荐答案

1. handle.js

看起来 http://graspjs.com/确实做到了这一点.

1. grasp.js

It looks like http://graspjs.com/ does exactly this.

grasp selector --replace replacement file.js

例如,将"el"重命名为"element":

For example, to rename 'el' to 'element':

grasp '#el' --replace 'element' index.js

正式掌握.替换文档.

Visual Studio代码还包括一个真正的替换工具.只需右键单击令牌,然后选择重命名符号.

Visual Studio Code also includes a real replacement tool. Just right click a token and choose rename symbol.

这篇关于JS:重命名变量以进行重构(使用AST,而不是文本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆