Skip to content

Commit

Permalink
[[FIX]] It is not un-necessary to assign undefined in a loop
Browse files Browse the repository at this point in the history
Fixes #1191
  • Loading branch information
lukeapage committed Aug 8, 2015
1 parent 1bbcc64 commit e8ce9bf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/jshint.js
Expand Up @@ -3555,7 +3555,8 @@ var JSHINT = (function() {
state.nameStack.set(state.tokens.curr);

advance("=");
if (!prefix && report && state.tokens.next.id === "undefined") {
if (!prefix && report && !state.funct["(loopage)"] &&
state.tokens.next.id === "undefined") {
warning("W080", state.tokens.prev, state.tokens.prev.value);
}
if (peek(0).id === "=" && state.tokens.next.identifier) {
Expand Down Expand Up @@ -4116,8 +4117,6 @@ var JSHINT = (function() {
}
}

state.funct["(breakage)"] += 1;
state.funct["(loopage)"] += 1;
increaseComplexityCount();
advance("(");

Expand Down Expand Up @@ -4191,6 +4190,9 @@ var JSHINT = (function() {
});
}

state.funct["(breakage)"] += 1;
state.funct["(loopage)"] += 1;

s = block(true, true);

if (nextop.value === "in" && state.option.forin) {
Expand Down Expand Up @@ -4239,6 +4241,10 @@ var JSHINT = (function() {
}
nolinebreak(state.tokens.curr);
advance(";");

// start loopage after the first ; as the next two expressions are executed
// on every loop
state.funct["(loopage)"] += 1;
if (state.tokens.next.id !== ";") {
checkCondAssignment(expression(0));
}
Expand All @@ -4257,6 +4263,7 @@ var JSHINT = (function() {
}
}
advance(")", t);
state.funct["(breakage)"] += 1;
block(true, true);
state.funct["(breakage)"] -= 1;
state.funct["(loopage)"] -= 1;
Expand Down
22 changes: 21 additions & 1 deletion tests/unit/core.js
Expand Up @@ -755,13 +755,33 @@ exports.testUndefinedAssignment = function (test) {
var src = [
"var x = undefined;",
"const y = undefined;",
"let z = undefined;"
"let z = undefined;",
"for(var a = undefined; a < 9; a++) {",
" var b = undefined;", // necessary - see gh-1191
" const c = undefined;",
" let d = undefined;",
" var e = function() {",
" var f = undefined;",
" const g = undefined;",
" let h = undefined;",
" };",
"}",
"// jshint -W080",
"var i = undefined;",
"const j = undefined;",
"let k = undefined;",
];

TestRun(test)
.addError(1, "It's not necessary to initialize 'x' to 'undefined'.")
.addError(2, "It's not necessary to initialize 'y' to 'undefined'.")
.addError(3, "It's not necessary to initialize 'z' to 'undefined'.")
.addError(4, "It's not necessary to initialize 'a' to 'undefined'.")
.addError(6, "It's not necessary to initialize 'c' to 'undefined'.")
.addError(7, "It's not necessary to initialize 'd' to 'undefined'.")
.addError(9, "It's not necessary to initialize 'f' to 'undefined'.")
.addError(10, "It's not necessary to initialize 'g' to 'undefined'.")
.addError(11, "It's not necessary to initialize 'h' to 'undefined'.")
.test(src, {esnext: true});

test.done();
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/options.js
Expand Up @@ -1238,6 +1238,18 @@ exports.loopfunc = function (test) {
.addError(17, "Don't make functions within a loop.")
.test(es6LoopFuncSrc, {esnext: true});

// functions declared in the expressions that loop should warn
var src2 = [
"for(var i = 0; function a(){return i;}; i++) { break; }",
"var j;",
"while(function b(){return j;}){}",
"for(var c = function(){return j;};;){c();}"];

TestRun(test)
.addError(1, "Don't make functions within a loop.")
.addError(3, "Don't make functions within a loop.")
.test(src2, { es3: true, loopfunc: false, boss: true });

test.done();
};

Expand Down

0 comments on commit e8ce9bf

Please sign in to comment.