Skip to content

Commit

Permalink
Add nocomma option to forbid use of a comma operator
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-sokolov committed Nov 24, 2014
1 parent 493d03d commit 4d5194d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ var JSHINT = (function () {
// statements inside of a one-line blocks.
newcap : true, // if constructor names must be capitalized
noarg : true, // if arguments.caller and arguments.callee should be
nocomma : true, // if comma operator should be disallowed
noempty : true, // if empty blocks should be disallowed
nonbsp : true, // if non-breaking spaces should be disallowed
nonew : true, // if using `new` for side-effects should be disallowed
Expand Down Expand Up @@ -2237,6 +2238,10 @@ var JSHINT = (function () {
bitwiseassignop(">>=");
bitwiseassignop(">>>=");
infix(",", function (left, that) {
if (state.option.nocomma) {
warning("W127");
}

var expr;
that.exprs = [left];
if (!comma({peek: true})) {
Expand Down
3 changes: 2 additions & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ var warnings = {
W123: "'{a}' is already defined in outer scope.",
W124: "A generator function shall contain a yield statement.",
W125: "This line contains non-breaking spaces: http://jshint.com/doc/options/#nonbsp",
W126: "Grouping operator is unnecessary for lone expressions."
W126: "Grouping operator is unnecessary for lone expressions.",
W127: "Unexpected use of a comma operator."
};

var info = {
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,19 @@ exports.nonbsp = function (test) {
test.done();
};

/** Option `nocomma` disallows the use of comma operator. */
exports.nocomma = function (test) {
// By default allow comma operator
TestRun(test)
.test('return 2, 5;', {});

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

test.done();
};

exports.enforceall = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/enforceall.js", "utf8");

Expand Down

0 comments on commit 4d5194d

Please sign in to comment.