facebook / facebook/jscodeshift

Problems with `renameTo` and block scope

Open
#263 2 comments 3 reactions 0 assignees View on GitHub
bug
Dominant language
JavaScript
Stars
10k
Forks
498
PR merge metrics
No merged PRs in 30d

Description

ES6 Block scoping seems to be broken in `renameTo`.

Repro:
```
jscodeshift: 0.5.0
- babel: 6.26.3
- babylon: 7.0.0-beta.47
- flow: 0.73.0
- recast: 0.14.7
```

Transform:
```js
module.exports = function transform(file, api, options) {
const j = api.jscodeshift
const root = j(file.source)

let first = true
root.find(j.VariableDeclarator)
.filter((path) => {let res = first; first = false; return res}) // select only first declarator
.forEach((path) => {
console.log('have path') // check we have exactly one
})
.renameTo('baz')

return root.toSource()
}
```

Input:
```js
const x = 42
{
const x = 47
console.log(x)
}
console.log(x)
```

Output:
```js
const baz = 42
{
const baz = 47
console.log(baz)
}
console.log(baz)
```

Expected output:
```js
const baz = 42
{
const x = 47
console.log(x)
}
console.log(baz)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.