fix for-head duplication testing and replacement - fixes #162
This commit is contained in:
parent
58fac2e2be
commit
85c2de57e4
@ -145,11 +145,25 @@ LetScoping.prototype.noClosure = function () {
|
|||||||
|
|
||||||
if (_.isEmpty(replacements)) return;
|
if (_.isEmpty(replacements)) return;
|
||||||
|
|
||||||
traverse(block, function (node, parent) {
|
var replace = function (node, parent) {
|
||||||
if (!t.isIdentifier(node)) return;
|
if (!t.isIdentifier(node)) return;
|
||||||
if (!t.isReferenced(node, parent)) return;
|
if (!t.isReferenced(node, parent)) return;
|
||||||
node.name = replacements[node.name] || node.name;
|
node.name = replacements[node.name] || node.name;
|
||||||
});
|
};
|
||||||
|
|
||||||
|
var traverseReplace = function (node, parent) {
|
||||||
|
replace(node, parent);
|
||||||
|
traverse(node, replace);
|
||||||
|
};
|
||||||
|
|
||||||
|
var forParent = this.forParent;
|
||||||
|
if (forParent) {
|
||||||
|
traverseReplace(forParent.right, forParent);
|
||||||
|
traverseReplace(forParent.test, forParent);
|
||||||
|
traverseReplace(forParent.update, forParent);
|
||||||
|
}
|
||||||
|
|
||||||
|
traverse(block, replace);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -181,10 +195,23 @@ LetScoping.prototype.getInfo = function () {
|
|||||||
keys: []
|
keys: []
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var duplicates = function (id, key) {
|
||||||
|
var has = scope.parentGet(key);
|
||||||
|
|
||||||
|
if (has && has !== id) {
|
||||||
|
// there's a variable with this exact name in an upper scope so we need
|
||||||
|
// to generate a new name
|
||||||
|
opts.duplicates[key] = id.name = file.generateUid(key, scope);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
_.each(opts.declarators, function (declar) {
|
_.each(opts.declarators, function (declar) {
|
||||||
opts.declarators.push(declar);
|
opts.declarators.push(declar);
|
||||||
|
|
||||||
var keys = t.getIds(declar);
|
var keys = t.getIds(declar, true);
|
||||||
|
_.each(keys, duplicates);
|
||||||
|
keys = _.keys(keys);
|
||||||
|
|
||||||
opts.outsideKeys = opts.outsideKeys.concat(keys);
|
opts.outsideKeys = opts.outsideKeys.concat(keys);
|
||||||
opts.keys = opts.keys.concat(keys);
|
opts.keys = opts.keys.concat(keys);
|
||||||
});
|
});
|
||||||
@ -193,14 +220,7 @@ LetScoping.prototype.getInfo = function () {
|
|||||||
if (!isLet(declar)) return;
|
if (!isLet(declar)) return;
|
||||||
|
|
||||||
_.each(t.getIds(declar, true), function (id, key) {
|
_.each(t.getIds(declar, true), function (id, key) {
|
||||||
var has = scope.parentGet(key);
|
duplicates(id, key);
|
||||||
|
|
||||||
if (has && has !== id) {
|
|
||||||
// there's a variable with this exact name in an upper scope so we need
|
|
||||||
// to generate a new name
|
|
||||||
opts.duplicates[key] = id.name = file.generateUid(key, scope);
|
|
||||||
}
|
|
||||||
|
|
||||||
opts.keys.push(key);
|
opts.keys.push(key);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
6
test/fixtures/transformation/let-scoping/exec-block-scoped/exec.js
vendored
Normal file
6
test/fixtures/transformation/let-scoping/exec-block-scoped/exec.js
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
let x = 1;
|
||||||
|
{
|
||||||
|
let x = 2;
|
||||||
|
assert.equal(x, 2);
|
||||||
|
}
|
||||||
|
assert.equal(x, 1);
|
||||||
5
test/fixtures/transformation/let-scoping/exec-for-loop-head/exec.js
vendored
Normal file
5
test/fixtures/transformation/let-scoping/exec-for-loop-head/exec.js
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
assert.equal((function(){
|
||||||
|
let a = 1;
|
||||||
|
for (let a = 0; a < 8; a++) {}
|
||||||
|
return a;
|
||||||
|
}()), 1);
|
||||||
Loading…
x
Reference in New Issue
Block a user