Skip to content

Commit

Permalink
[[FIX]] singleGroups: Allow grouping for integers
Browse files Browse the repository at this point in the history
The grouping operator is necessary when accessing properties of number
literals when the literal describes an integer (the period character
would otherwise be interpreted as a decimal point). Update the
`singleGroups` enforcement logic to allow for this usage.
  • Loading branch information
jugglinmike authored and lukeapage committed Aug 8, 2015
1 parent 6d6bd8f commit 8c265ca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2523,7 +2523,11 @@ var JSHINT = (function() {
// operator.
(isFunctor(ret) && !isEndOfExpr()) ||
// Used as the return value of a single-statement arrow function
(ret.id === "{" && preceeding.id === "=>");
(ret.id === "{" && preceeding.id === "=>") ||
// Used to delineate an integer number literal from a dereferencing
// punctuator (otherwise interpreted as a decimal point)
(ret.type === "(number)" &&
checkPunctuator(pn, ".") && /^\d+$/.test(ret.value));
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2681,6 +2681,39 @@ singleGroups.unary = function (test) {
test.done();
};

singleGroups.numberLiterals = function (test) {
var code = [
"(3).toString();",
"(3.1).toString();",
"(.3).toString();",
"(3.).toString();",
"(1e3).toString();",
"(1e-3).toString();",
"(1.1e3).toString();",
"(1.1e-3).toString();",
"(3)[methodName]();",
"var x = (3) + 3;",
"('3').toString();"
];

TestRun(test)
.addError(2, "Unnecessary grouping operator.")
.addError(3, "Unnecessary grouping operator.")
.addError(3, "A leading decimal point can be confused with a dot: '.3'.")
.addError(4, "Unnecessary grouping operator.")
.addError(4, "A trailing decimal point can be confused with a dot: '3.'.")
.addError(5, "Unnecessary grouping operator.")
.addError(6, "Unnecessary grouping operator.")
.addError(7, "Unnecessary grouping operator.")
.addError(8, "Unnecessary grouping operator.")
.addError(9, "Unnecessary grouping operator.")
.addError(10, "Unnecessary grouping operator.")
.addError(11, "Unnecessary grouping operator.")
.test(code, { singleGroups: true });

test.done();
};

exports.elision = function (test) {
var code = [
"var a = [1,,2];",
Expand Down

0 comments on commit 8c265ca

Please sign in to comment.