Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

包含作用域的变量如何简单的替换,而不漏网或误替换呢? #54

Open
zhangdaren opened this issue Jul 29, 2021 · 0 comments

Comments

@zhangdaren
Copy link
Contributor

比如代码:

var n = {};
n.n = 10;

function ajax(){
  var obj = {a:1, n:2}
    var {a, n} = obj;
    console.log("没想到吧,我又来了", n)

  var fun = function(n){
    console.log(n)
  }
    
}

function test(){
	n = 5;
    var fun = function(){
    console.log(n)
  }
}

function test2(n){
	n = 5;
}
ajax()

需要顶级n为abc,替换后代码为:

var abc = {};
abc.n = 10;

function ajax() {
  var obj = {
    a: 1,
    n: 2
  };
  var {
    a,
    n
  } = obj;
  console.log("没想到吧,我又来了", n);

  var fun = function (n) {
    console.log(n);
  };
}

function test() {
  abc = 5;

  var fun = function () {
    console.log(abc);
  };
}

function test2(n) {
  n = 5;
}

ajax();

使用babel,仅三行解决:

  if(t.isObjectExpression(path.node.init) && t.isProgram(path.parentPath.parentPath)){
    path.scope.rename("n", "abc")    
  }

使用gogocode,我还没法像这么简洁的弄出来,可以预想到,会有一堆代码。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant