Skip to content

Commit

Permalink
Merge pull request ampproject#3 from jridgewell/cleanup-transform-saf…
Browse files Browse the repository at this point in the history
…ari-for-shadowing

Only visit For statements
  • Loading branch information
developit committed Aug 9, 2019
2 parents 0f94cfa + ca24a6d commit ba514e2
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/plugins/transform-safari-for-shadowing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@
* e => { for (let _e of []) _e } // works
*/

function handle(declaration) {
if (!declaration.isVariableDeclaration()) return;

const fn = declaration.getFunctionParent();
const { name } = declaration.node.declarations[0].id;

// check if there is a shadowed binding coming from a parameter
if (fn && fn.scope.hasOwnBinding(name) && fn.scope.getOwnBinding(name).kind === 'param') {
declaration.scope.rename(name);
}
}

export default ({ types: t }) => ({
name: 'transform-safari-for-shadowing',
visitor: {
VariableDeclarator(path) {
if (!path.parentPath) return;
const name = path.node.id.name;
const parent = path.parentPath.parentPath;
const key = path.parentPath.parentKey;
if (
// for (let e of ..) & for (let e in ..)
(t.isForXStatement(parent) && key==='left') ||
// for (let e=..; ;)
(t.isForStatement(parent) && key==='init')
) {
// check if there is a shadowed binding coming from a parameter
const fn = path.getFunctionParent();
if (fn && fn.scope.hasOwnBinding(name) && fn.scope.getOwnBinding(name).kind === 'param') {
path.scope.rename(name);
}
}
ForXStatement(path) {
handle(path.get('left'));
},

ForStatement(path) {
handle(path.get('init'));
}
}
});

0 comments on commit ba514e2

Please sign in to comment.