Skip to content

Commit

Permalink
Fix: max-lines-per-function flagging arrow IIFEs (fixes #13332) (#13336)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherryblossom000 committed May 23, 2020
1 parent 25462b2 commit bd3f092
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/rules/max-lines-per-function.md
Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/max-lines-per-function.js
Expand Up @@ -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;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/rules/max-lines-per-function.js
Expand Up @@ -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 }]
}
],

Expand Down Expand Up @@ -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 } }
]
}
]
});

0 comments on commit bd3f092

Please sign in to comment.