Skip to content

Commit

Permalink
[[FIX]] Relax singleGroups restrictions: arrow fns
Browse files Browse the repository at this point in the history
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 4a4f522
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === "=>");
}
Expand Down
22 changes: 19 additions & 3 deletions tests/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4a4f522

Please sign in to comment.