Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: max-lines-per-function flagging arrow IIFEs (fixes #13332) #13336

Merged
merged 1 commit into from May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 } }
]
}
]
});