Permalink
Browse files

[[FIX]] Relax singleGroups restrictions: arrow fns

Because arrow functions are AssignmentExpressions but *not* primary
expressions, they cannot appear directly as the left-hand side of an
operator. Extend the behavior of the singleGroups option to allow the
grouping operator to wrap arrow functions, provided they do not end the
current statement.
  • Loading branch information...
jugglinmike committed Apr 29, 2015
1 parent 9f55160 commit 4a4f522f1e112a2d9be0c4df5653d085b0c68a86
Showing with 22 additions and 3 deletions.
  1. +3 −0 src/jshint.js
  2. +19 −3 tests/unit/options.js
@@ -2634,6 +2634,9 @@ var JSHINT = (function() {
// some other operator--either within the parenthesis or directly
// following them.
(!isEndOfExpr() || state.tokens.prev.id !== "}")) ||
// Used to demarcate an arrow function as the left-hand side of some
// operator.
(isFunctor(ret) && !isEndOfExpr()) ||
// Used as the return value of a single-statement arrow function
(ret.id === "{" && preceeding.id === "=>");
}
@@ -2087,14 +2087,30 @@ singleGroups.arrowFunctions = function (test) {
var code = [
"var a = () => ({});",
"var b = (c) => {};",
"var g = (() => 3)();",
"var h = (() => ({}))();",
"var i = (() => 3).length;",
"var j = (() => ({})).length;",
"var k = (() => 3)[prop];",
"var l = (() => ({}))[prop];",
"var m = (() => 3) || 3;",
"var n = (() => ({})) || 3;",
"var o = (() => {})();",
"var p = (() => {})[prop];",
"var q = (() => {}) || 3;",
"(() => {})();",
// Invalid forms:
"var d = () => (e);",
"var f = () => (3);"
"var f = () => (3);",
"var r = (() => 3);",
"var s = (() => {});"
];

TestRun(test)
.addError(4, "Unnecessary grouping operator.")
.addError(5, "Unnecessary grouping operator.")
.addError(15, "Unnecessary grouping operator.")
.addError(16, "Unnecessary grouping operator.")
.addError(17, "Unnecessary grouping operator.")
.addError(18, "Unnecessary grouping operator.")
.test(code, { singleGroups: true, esnext: true });

test.done();

0 comments on commit 4a4f522

Please sign in to comment.