Skip to content

Commit

Permalink
[[FIX]] Fix false positives in 'nocomma' option
Browse files Browse the repository at this point in the history
- Fix false positives in nocomma in literals and for loops
- Fix false positives in nocomma in arrow function expression
- Add tests for false positives in destructuring with nocomma option
  • Loading branch information
denis-sokolov authored and jugglinmike committed Jan 31, 2015
1 parent 2dc713a commit 33612f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,10 +981,6 @@ var JSHINT = (function() {
function comma(opts) {
opts = opts || {};

if (state.option.nocomma) {
warning("W127");
}

if (!opts.peek) {
nobreakcomma(state.tokens.curr, state.tokens.next);
advance(",");
Expand Down Expand Up @@ -2063,6 +2059,11 @@ var JSHINT = (function() {
infix(",", function(left, that) {
var expr;
that.exprs = [left];

if (state.option.nocomma) {
warning("W127");
}

if (!comma({ peek: true })) {
return that;
}
Expand Down Expand Up @@ -2476,6 +2477,11 @@ var JSHINT = (function() {
if (state.tokens.next.id !== ",") {
break;
}

if (pn1.value !== "=>" && state.option.nocomma) {
warning("W127");
}

comma();
}
}
Expand Down
24 changes: 21 additions & 3 deletions tests/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -1680,17 +1680,35 @@ exports.nonbsp = function (test) {
/** Option `nocomma` disallows the use of comma operator. */
exports.nocomma = function (test) {
// By default allow comma operator
TestRun(test)
TestRun(test, "nocomma off by default")
.test("return 2, 5;", {});

TestRun(test)
TestRun(test, "nocomma main case")
.addError(1, "Unexpected use of a comma operator.")
.test("return 2, 5;", { nocomma: true });

TestRun(test)
TestRun(test, "nocomma in an expression")
.addError(1, "Unexpected use of a comma operator.")
.test("(2, 5);", { expr: true, nocomma: true });

TestRun(test, "avoid nocomma false positives in value literals")
.test("return { a: 2, b: [1, 2] };", { nocomma: true });

TestRun(test, "avoid nocomma false positives in for statements")
.test("for(;;) { return; }", { nocomma: true });

TestRun(test, "avoid nocomma false positives in function expressions")
.test("return function(a, b) {};", { nocomma: true });

TestRun(test, "avoid nocomma false positives in arrow function expressions")
.test("return (a, b) => a;", { esnext: true, nocomma: true });

TestRun(test, "avoid nocomma false positives in destructuring arrays")
.test("var [a, b] = [1, 2];", { esnext: true, nocomma: true });

TestRun(test, "avoid nocomma false positives in destructuring objects")
.test("var {a, b} = {a:1, b:2};", { esnext: true, nocomma: true });

test.done();
};

Expand Down

0 comments on commit 33612f8

Please sign in to comment.