From bd3f092efa579944f75bfbc277b35f85e6d966ed Mon Sep 17 00:00:00 2001 From: cherryblossom000 <31467609+cherryblossom000@users.noreply.github.com> Date: Sat, 23 May 2020 10:33:43 +1000 Subject: [PATCH] Fix: max-lines-per-function flagging arrow IIFEs (fixes #13332) (#13336) --- docs/rules/max-lines-per-function.md | 8 +++++ lib/rules/max-lines-per-function.js | 2 +- tests/lib/rules/max-lines-per-function.js | 39 +++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/docs/rules/max-lines-per-function.md b/docs/rules/max-lines-per-function.md index 22ffea79f41..d3b13615b42 100644 --- a/docs/rules/max-lines-per-function.md +++ b/docs/rules/max-lines-per-function.md @@ -165,6 +165,10 @@ Examples of **incorrect** code for this rule with the `{ "IIFEs": true }` option (function(){ var x = 0; }()); + +(() => { + var x = 0; +})(); ``` Examples of **correct** code for this rule with the `{ "IIFEs": true }` option: @@ -174,6 +178,10 @@ Examples of **correct** code for this rule with the `{ "IIFEs": true }` option: (function(){ var x = 0; }()); + +(() => { + var x = 0; +})(); ``` ## When Not To Use It diff --git a/lib/rules/max-lines-per-function.js b/lib/rules/max-lines-per-function.js index 03539fae470..aa423a60a1f 100644 --- a/lib/rules/max-lines-per-function.js +++ b/lib/rules/max-lines-per-function.js @@ -134,7 +134,7 @@ module.exports = { * @returns {boolean} True if it's an IIFE */ function isIIFE(node) { - return node.type === "FunctionExpression" && node.parent && node.parent.type === "CallExpression" && node.parent.callee === node; + return (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") && node.parent && node.parent.type === "CallExpression" && node.parent.callee === node; } /** diff --git a/tests/lib/rules/max-lines-per-function.js b/tests/lib/rules/max-lines-per-function.js index 9107cd69f0f..008f2b01acb 100644 --- a/tests/lib/rules/max-lines-per-function.js +++ b/tests/lib/rules/max-lines-per-function.js @@ -164,6 +164,30 @@ if ( x === y ) { return bar; }());`, options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: false }] + }, + + // Arrow IIFEs should be recognised if IIFEs: true + { + code: `(() => { + let x = 0; + let y = 0; + let z = x + y; + let foo = {}; + return bar; +})();`, + options: [{ max: 7, skipComments: true, skipBlankLines: false, IIFEs: true }] + }, + + // Arrow IIFEs should not be recognised if IIFEs: false + { + code: `(() => { + let x = 0; + let y = 0; + let z = x + y; + let foo = {}; + return bar; +})();`, + options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: false }] } ], @@ -435,6 +459,21 @@ if ( x === y ) { errors: [ { messageId: "exceed", data: { name: "Function", lineCount: 7, maxLines: 2 } } ] + }, + + // Test the IIFEs option includes arrow IIFEs + { + code: `(() => { + let x = 0; + let y = 0; + let z = x + y; + let foo = {}; + return bar; +})();`, + options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }], + errors: [ + { messageId: "exceed", data: { name: "Arrow function", lineCount: 7, maxLines: 2 } } + ] } ] });