8000 Fix: max-lines-per-function flagging arrow IIFEs (fixes #13332) (#13336) · eslint/eslint@bd3f092 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd3f092

Browse files
Fix: max-lines-per-function flagging arrow IIFEs (fixes #13332) (#13336)
1 parent 25462b2 commit bd3f092
Copy full SHA for bd3f092

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

docs/rules/max-lines-per-function.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ Examples of **incorrect** code for this rule with the `{ "IIFEs": true }` option
165165
(function(){
166166
var x = 0;
167167
}());
168+
169+
(() => {
170+
var x = 0;
171+
})();
168172
```
169173

170174
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:
174178
(function(){
175179
var x = 0;
176180
}());
181+
182+
(() => {
183+
var x = 0;
184+
})();
177185
```
178186

179187
## When Not To Use It

lib/rules/max-lines-per-function.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ module.exports = {
134134
* @returns {boolean} True if it's an IIFE
135135
*/
136136
function isIIFE(node) {
137-
return node.type === "FunctionExpression" && node.parent && node.parent.type === "CallExpression" && node.parent.callee === node;
137+
return (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") && node.parent && node.parent.type === "CallExpression" && node.parent.callee === node;
138138
}
139139

140140
/**

tests/lib/rules/max-lines-per-function.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,30 @@ if ( x === y ) {
164164
return bar;
165165
}());`,
166166
options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: false }]
167+
},
168+
169+
// Arrow IIFEs should be recognised if IIFEs: true
170+
{
171+
code: `(() => {
172+
let x = 0;
173+
let y = 0;
174+
let z = x + y;
175+
let foo = {};
176+
return bar;
177+
})();`,
178+
options: [{ max: 7, skipComments: true, skipBlankLines: false, IIFEs: true }]
179+
},
180+
181+
// Arrow IIFEs should not be recognised if IIFEs: false
182+
{
183+
code: `(() => {
184+
let x = 0;
185+
let y = 0;
186+
let z = x + y;
187+
let foo = {};
188+
return bar;
189+
})();`,
190+
options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: false }]
167191
}
168192
],
169193

@@ -435,6 +459,21 @@ if ( x === y ) {
435459
errors: [
436460
{ messageId: "exceed", data: { name: "Function", lineCount: 7, maxLines: 2 } }
437461
]
462+
},
463+
464+
// Test the IIFEs option includes arrow IIFEs
465+
{
466+
code: `(() => {
467+
let x = 0;
468+
let y = 0;
469+
let z = x + y;
470+
let foo = {};
471+
return bar;
472+
})();`,
473+
options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }],
474+
errors: [
475+
{ messageId: "exceed", data: { name: "Arrow function", lineCount: 7, maxLines: 2 } }
476+
]
438477
}
439478
]
440479
});

0 commit comments

Comments
 (0)
0