Skip to content

Commit

Permalink
[[FIX]] Dont crash when testing x == keyword if eqnull is on
Browse files Browse the repository at this point in the history
If eqnull was on jshint previously crashed if the right hand side of an
assignment was not an expression.
Fixes #2587
  • Loading branch information
lukeapage authored and jugglinmike committed Aug 8, 2015
1 parent f2a59f1 commit 6afd373
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,8 @@ var JSHINT = (function() {
bitwise("^", "bitxor", 80);
bitwise("&", "bitand", 90);
relation("==", function(left, right) {
var eqnull = state.option.eqnull && (left.value === "null" || right.value === "null");
var eqnull = state.option.eqnull &&
((left && left.value) === "null" || (right && right.value) === "null");

switch (true) {
case !eqnull && state.option.eqeqeq:
Expand Down Expand Up @@ -2064,7 +2065,7 @@ var JSHINT = (function() {
});
relation("!=", function(left, right) {
var eqnull = state.option.eqnull &&
(left.value === "null" || right.value === "null");
((left && left.value) === "null" || (right && right.value) === "null");

if (!eqnull && state.option.eqeqeq) {
this.from = this.character;
Expand Down
79 changes: 79 additions & 0 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,85 @@ exports.comma = function (test) {
test.done();
};

exports["gh-2587"] = function (test) {

TestRun(test)
.addError(1, "Expected an identifier and instead saw 'if'.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.addError(1, "Expected '===' and instead saw '=='.")
.test([
"true == if"
], {eqeqeq: true, eqnull: true});

TestRun(test)
.addError(1, "Expected an identifier and instead saw 'if'.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.addError(1, "Expected '!==' and instead saw '!='.")
.test([
"true != if"
], {eqeqeq: true, eqnull: true});

TestRun(test)
.addError(1, "Expected an identifier and instead saw 'if'.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.addError(1, "Use '===' to compare with 'true'.")
.test([
"true == if"
], {});

TestRun(test)
.addError(1, "Expected an identifier and instead saw 'if'.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.addError(1, "Use '!==' to compare with 'true'.")
.test([
"true != if"
], {});

TestRun(test)
.addError(1, "Expected an identifier and instead saw 'if'.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.test([
"true === if"
], {});

TestRun(test)
.addError(1, "Expected an identifier and instead saw 'if'.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.test([
"true !== if"
], {});

TestRun(test)
.addError(1, "Expected an identifier and instead saw 'if'.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.test([
"true > if"
], {});

TestRun(test)
.addError(1, "Expected an identifier and instead saw 'if'.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.test([
"true < if"
], {});

TestRun(test)
.addError(1, "Expected an identifier and instead saw 'if'.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.test([
"true >= if"
], {});

TestRun(test)
.addError(1, "Expected an identifier and instead saw 'if'.")
.addError(1, "Unrecoverable syntax error. (100% scanned).")
.test([
"true <= if"
], {});

test.done();
};

exports.withStatement = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/with.js", "utf8");
var run;
Expand Down

0 comments on commit 6afd373

Please sign in to comment.