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

path.scope not accurate for const and let #154

Open
robz opened this issue Jun 7, 2016 · 4 comments · May be fixed by #455
Open

path.scope not accurate for const and let #154

robz opened this issue Jun 7, 2016 · 4 comments · May be fixed by #455
Assignees
Labels

Comments

@robz
Copy link

robz commented Jun 7, 2016

Variables declared with let and const are block-scoped, not function-scoped. Yet path.scope is the same for both blocked-scoped variables and function-scoped variables declared with var. The following

var recast = require("recast");                                                   
var types = require("ast-types");                                                 

var ast = recast.parse(`                                                          
  function f() {                                                               
    if (true) {                                                                
      const x = 3;                                                             
    }                                                                          
  }                                                                            
`);                                                                            

types.visit(ast, {                                                                
  visitVariableDeclaration: function(path) {                                      
    console.log(path.scope.node.type);                                            
    return false;                                                                 
  }                                                                               
});

Outputs "FunctionDeclaration", but I think it should be "BlockStatement".

@benjamn benjamn added the bug label Jun 7, 2016
@benjamn benjamn self-assigned this Jun 7, 2016
@benjamn
Copy link
Owner

benjamn commented Jun 7, 2016

The Scope code was written before let and const were so universally supported, but that's no excuse. I should definitely fix this. Thanks for bringing it to my attention.

@benjamn
Copy link
Owner

benjamn commented Jun 27, 2016

@bmeck is working on this as part of #157

@sigriston
Copy link

I hit this as well, while working on a project of my own.
Glad to know you guys are aware & working on it.

@jedwards1211
Copy link
Contributor

jedwards1211 commented Apr 8, 2020

@benjamn is there a way to use @babel/traverse with a recastified ast-types AST? Even if I use jscodeshift.withParser('babel'), the nodes don't seem to work with @babel/traverse. I'm implementing a promise chain unwinding transform, and I need to be able to guarantee that I can correctly rename identifiers I move between scopes to prevent conflicts, so I can't use ast-types' Path right now.

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

Successfully merging a pull request may close this issue.

4 participants