fix let scoping not working with while loops - fixes #254
This commit is contained in:
parent
7c1a924ef6
commit
9c0a8e22d2
@ -27,7 +27,7 @@ exports.VariableDeclaration = function (node) {
|
|||||||
isLet(node);
|
isLet(node);
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.For = function (node, parent, file, scope) {
|
exports.Loop = function (node, parent, file, scope) {
|
||||||
var init = node.left || node.init;
|
var init = node.left || node.init;
|
||||||
if (isLet(init)) {
|
if (isLet(init)) {
|
||||||
t.ensureBlock(node);
|
t.ensureBlock(node);
|
||||||
@ -49,7 +49,7 @@ exports.For = function (node, parent, file, scope) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.BlockStatement = function (block, parent, file, scope) {
|
exports.BlockStatement = function (block, parent, file, scope) {
|
||||||
if (!t.isFor(parent)) {
|
if (!t.isLoop(parent)) {
|
||||||
var letScoping = new LetScoping(false, block, parent, file, scope);
|
var letScoping = new LetScoping(false, block, parent, file, scope);
|
||||||
letScoping.run();
|
letScoping.run();
|
||||||
}
|
}
|
||||||
@ -58,15 +58,15 @@ exports.BlockStatement = function (block, parent, file, scope) {
|
|||||||
/**
|
/**
|
||||||
* Description
|
* Description
|
||||||
*
|
*
|
||||||
* @param {Boolean|Node} forParent
|
* @param {Boolean|Node} loopParent
|
||||||
* @param {Node} block
|
* @param {Node} block
|
||||||
* @param {Node} parent
|
* @param {Node} parent
|
||||||
* @param {File} file
|
* @param {File} file
|
||||||
* @param {Scope} scope
|
* @param {Scope} scope
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function LetScoping(forParent, block, parent, file, scope) {
|
function LetScoping(loopParent, block, parent, file, scope) {
|
||||||
this.forParent = forParent;
|
this.loopParent = loopParent;
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.scope = scope;
|
this.scope = scope;
|
||||||
this.block = block;
|
this.block = block;
|
||||||
@ -172,11 +172,11 @@ LetScoping.prototype.remap = function () {
|
|||||||
traverse(node, replace);
|
traverse(node, replace);
|
||||||
};
|
};
|
||||||
|
|
||||||
var forParent = this.forParent;
|
var loopParent = this.loopParent;
|
||||||
if (forParent) {
|
if (loopParent) {
|
||||||
traverseReplace(forParent.right, forParent);
|
traverseReplace(loopParent.right, loopParent);
|
||||||
traverseReplace(forParent.test, forParent);
|
traverseReplace(loopParent.test, loopParent);
|
||||||
traverseReplace(forParent.update, forParent);
|
traverseReplace(loopParent.update, loopParent);
|
||||||
}
|
}
|
||||||
|
|
||||||
traverse(block, replace);
|
traverse(block, replace);
|
||||||
@ -365,7 +365,7 @@ LetScoping.prototype.getLetReferences = function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
} else if (t.isFor(node)) {
|
} else if (t.isLoop(node)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -428,7 +428,7 @@ LetScoping.prototype.buildHas = function (ret, call) {
|
|||||||
t.variableDeclarator(ret, call)
|
t.variableDeclarator(ret, call)
|
||||||
]));
|
]));
|
||||||
|
|
||||||
var forParent = this.forParent;
|
var loopParent = this.loopParent;
|
||||||
var retCheck;
|
var retCheck;
|
||||||
var has = this.has;
|
var has = this.has;
|
||||||
var cases = [];
|
var cases = [];
|
||||||
@ -443,7 +443,7 @@ LetScoping.prototype.buildHas = function (ret, call) {
|
|||||||
if (has.hasBreak || has.hasContinue) {
|
if (has.hasBreak || has.hasContinue) {
|
||||||
// ensure that the parent has a label as we're building a switch and we
|
// ensure that the parent has a label as we're building a switch and we
|
||||||
// need to be able to access it
|
// need to be able to access it
|
||||||
var label = forParent.label = forParent.label || this.file.generateUidIdentifier("loop", this.scope);
|
var label = loopParent.label = loopParent.label || this.file.generateUidIdentifier("loop", this.scope);
|
||||||
|
|
||||||
if (has.hasBreak) {
|
if (has.hasBreak) {
|
||||||
cases.push(t.switchCase(t.literal("break"), [t.breakStatement(label)]));
|
cases.push(t.switchCase(t.literal("break"), [t.breakStatement(label)]));
|
||||||
|
|||||||
@ -3,14 +3,14 @@
|
|||||||
"BreakStatement": ["Statement"],
|
"BreakStatement": ["Statement"],
|
||||||
"ContinueStatement": ["Statement"],
|
"ContinueStatement": ["Statement"],
|
||||||
"DebuggerStatement": ["Statement"],
|
"DebuggerStatement": ["Statement"],
|
||||||
"DoWhileStatement": ["Statement", "Loop", "With"],
|
"DoWhileStatement": ["Statement", "Loop", "While"],
|
||||||
"IfStatement": ["Statement"],
|
"IfStatement": ["Statement"],
|
||||||
"ReturnStatement": ["Statement"],
|
"ReturnStatement": ["Statement"],
|
||||||
"SwitchStatement": ["Statement"],
|
"SwitchStatement": ["Statement"],
|
||||||
"ThrowStatement": ["Statement"],
|
"ThrowStatement": ["Statement"],
|
||||||
"TryStatement": ["Statement"],
|
"TryStatement": ["Statement"],
|
||||||
"WhileStatement": ["Statement"],
|
"WhileStatement": ["Statement", "Loop", "While"],
|
||||||
"WithStatement": ["Statement", "Loop", "With"],
|
"WithStatement": ["Statement"],
|
||||||
"EmptyStatement": ["Statement"],
|
"EmptyStatement": ["Statement"],
|
||||||
"LabeledStatement": ["Statement"],
|
"LabeledStatement": ["Statement"],
|
||||||
"VariableDeclaration": ["Statement", "Declaration"],
|
"VariableDeclaration": ["Statement", "Declaration"],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user