Skip to content

Commit

Permalink
[[FIX]] Correct interpretation of commas
Browse files Browse the repository at this point in the history
Re-implement the code reverted by the previous commit. Use distinct
logic to differentiate the contribution, including a correction to a
previously-unreported bug in the interpretation of `continue`
statements.
  • Loading branch information
jugglinmike authored and rwaldron committed Aug 21, 2019
1 parent e63dbc7 commit 691dbdc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
43 changes: 32 additions & 11 deletions src/jshint.js
Expand Up @@ -3172,10 +3172,9 @@ var JSHINT = (function() {
return;
}

exprs[exprs.length - 1].parent = true;

if (exprs.length > 1) {
ret = Object.create(state.syntax[","], { exprs: { value: exprs } });
ret = Object.create(state.syntax[","]);
ret.exprs = exprs;

first = exprs[0];
last = exprs[exprs.length - 1];
Expand Down Expand Up @@ -3219,6 +3218,8 @@ var JSHINT = (function() {
}

if (ret) {
ret.paren = true;

// The operator may be necessary to override the default binding power of
// neighboring operators (whenever there is an operator in use within the
// first expression *or* the current group contains multiple expressions)
Expand Down Expand Up @@ -3862,15 +3863,37 @@ var JSHINT = (function() {
state.funct["(metrics)"].ComplexityCount += 1;
}

// Parse assignments that were found instead of conditionals.
// For example: if (a = 1) { ... }

/**
* Detect and warn about assignment that occurs within conditional clauses,
* e.g.
*
* if (a = 1) { ... }
*
* This check is disabled in response to parenthesized expressions so that
* users may opt out of the warning on a case-by-case bases, e.g.
*
* if ((a = 1)) { ... }
*
* @param {object} expr - the parsed expression within the conditional clause
*/
function checkCondAssignment(expr) {
if (!expr || expr.paren || state.option.boss) {
return;
}

var id = expr.id;

// If the expression is composed of multiple sub-expressions via a comma
// operator, then check the final sub-expression.
if (id === ",") {
expr = expr.exprs[expr.exprs.length - 1];
id = expr.id;

if (expr.paren) {
return;
}
}

switch (id) {
case "=":
case "+=":
Expand All @@ -3881,9 +3904,7 @@ var JSHINT = (function() {
case "|=":
case "^=":
case "/=":
if (!expr.paren && !state.option.boss) {
warning("W084");
}
warning("W084");
}
}

Expand Down Expand Up @@ -5222,7 +5243,7 @@ var JSHINT = (function() {
if (!state.option.asi)
nolinebreak(this);

if (state.tokens.next.id !== ";" &&
if (state.tokens.next.identifier &&
state.tokens.curr.line === startLine(state.tokens.next)) {
if (!state.funct["(scope)"].funct.hasLabel(v)) {
warning("W090", state.tokens.next, v);
Expand Down Expand Up @@ -5250,7 +5271,7 @@ var JSHINT = (function() {
if (!state.option.asi)
nolinebreak(this);

if (state.tokens.next.id !== ";") {
if (state.tokens.next.identifier) {
if (state.tokens.curr.line === startLine(state.tokens.next)) {
if (!state.funct["(scope)"].funct.hasLabel(v)) {
warning("W090", state.tokens.next, v);
Expand Down
16 changes: 13 additions & 3 deletions tests/unit/parser.js
Expand Up @@ -7508,10 +7508,20 @@ exports["test for GH-1105"] = function (test) {
"}"
];

var run = TestRun(test)
.addError(2, 22, "Missing semicolon.");
TestRun(test)
.addError(2, 22, "Missing semicolon.")
.test(code);

code = [
"while (true) {",
" if (true) { continue }",
"}"
];

TestRun(test)
.addError(2, 25, "Missing semicolon.")
.test(code);

run.test(code);
test.done();
};

Expand Down

0 comments on commit 691dbdc

Please sign in to comment.