//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
+
const rule = require("../../../lib/rules/max-lines-per-function");
const { RuleTester } = require("../../../lib/rule-tester");
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 }]
}
],
code: "function name() {\n}",
options: [1],
errors: [
- { messageId: "exceed", data: { name: "Function 'name'", lineCount: 2, maxLines: 1 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function 'name'", linesExceed: 1, maxLines: 1 },
+ line: 2,
+ column: 1,
+ endLine: 2,
+ endColumn: 2
+ }
]
},
code: "var func = function() {\n}",
options: [1],
errors: [
- { messageId: "exceed", data: { name: "Function", lineCount: 2, maxLines: 1 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function", linesExceed: 1, maxLines: 1 },
+ line: 2,
+ column: 1,
+ endLine: 2,
+ endColumn: 2
+ }
]
},
code: "const bar = () => {\nconst x = 2 + 1;\nreturn x;\n}",
options: [3],
errors: [
- { messageId: "exceed", data: { name: "Arrow function", lineCount: 4, maxLines: 3 } }
+ {
+ messageId: "exceed",
+ data: { name: "Arrow function", linesExceed: 1, maxLines: 3 },
+ line: 4,
+ column: 1,
+ endLine: 4,
+ endColumn: 2
+ }
]
},
code: "const bar = () =>\n 2",
options: [1],
errors: [
- { messageId: "exceed", data: { name: "Arrow function", lineCount: 2, maxLines: 1 } }
+ {
+ messageId: "exceed",
+ data: { name: "Arrow function", linesExceed: 1, maxLines: 1 },
+ line: 2,
+ column: 1,
+ endLine: 2,
+ endColumn: 3
+ }
]
},
code: `() => {${"foo\n".repeat(60)}}`,
options: [{}],
errors: [
- { messageId: "exceed", data: { name: "Arrow function", lineCount: 61, maxLines: 50 } }
+ {
+ messageId: "exceed",
+ data: { name: "Arrow function", linesExceed: 11, maxLines: 50 },
+ line: 51,
+ column: 1,
+ endLine: 61,
+ endColumn: 2
+ }
]
},
code: "function name() {\nvar x = 5;\n\t\n \n\nvar x = 2;\n}",
options: [{ max: 6, skipComments: false, skipBlankLines: false }],
errors: [
- { messageId: "exceed", data: { name: "Function 'name'", lineCount: 7, maxLines: 6 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 },
+ line: 7,
+ column: 1,
+ endLine: 7,
+ endColumn: 2
+ }
]
},
code: "function name() {\r\nvar x = 5;\r\n\t\r\n \r\n\r\nvar x = 2;\r\n}",
options: [{ max: 6, skipComments: true, skipBlankLines: false }],
errors: [
- { messageId: "exceed", data: { name: "Function 'name'", lineCount: 7, maxLines: 6 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 },
+ line: 7,
+ column: 1,
+ endLine: 7,
+ endColumn: 2
+ }
]
},
code: "function name() {\nvar x = 5;\n\t\n \n\nvar x = 2;\n}",
options: [{ max: 2, skipComments: true, skipBlankLines: true }],
errors: [
- { messageId: "exceed", data: { name: "Function 'name'", lineCount: 4, maxLines: 2 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 },
+ line: 6,
+ column: 1,
+ endLine: 7,
+ endColumn: 2
+ }
]
},
code: "function name() {\r\nvar x = 5;\r\n\t\r\n \r\n\r\nvar x = 2;\r\n}",
options: [{ max: 2, skipComments: true, skipBlankLines: true }],
errors: [
- { messageId: "exceed", data: { name: "Function 'name'", lineCount: 4, maxLines: 2 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 },
+ line: 6,
+ column: 1,
+ endLine: 7,
+ endColumn: 2
+ }
]
},
code: "function name() { // end of line comment\nvar x = 5; /* mid line comment */\n\t// single line comment taking up whole line\n\t\n \n\nvar x = 2;\n}",
options: [{ max: 6, skipComments: true, skipBlankLines: false }],
errors: [
- { messageId: "exceed", data: { name: "Function 'name'", lineCount: 7, maxLines: 6 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function 'name'", linesExceed: 1, maxLines: 6 },
+ line: 8,
+ column: 1,
+ endLine: 8,
+ endColumn: 2
+ }
]
},
code: "function name() { // end of line comment\nvar x = 5; /* mid line comment */\n\t// single line comment taking up whole line\n\t\n \n\nvar x = 2;\n}",
options: [{ max: 1, skipComments: true, skipBlankLines: true }],
errors: [
- { messageId: "exceed", data: { name: "Function 'name'", lineCount: 4, maxLines: 1 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function 'name'", linesExceed: 3, maxLines: 1 },
+ line: 2,
+ column: 1,
+ endLine: 8,
+ endColumn: 2
+ }
]
},
code: "function name() { // end of line comment\nvar x = 5; /* mid line comment */\n\t// single line comment taking up whole line\n\t\n \n\nvar x = 2;\n}",
options: [{ max: 1, skipComments: false, skipBlankLines: true }],
errors: [
- { messageId: "exceed", data: { name: "Function 'name'", lineCount: 5, maxLines: 1 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function 'name'", linesExceed: 4, maxLines: 1 },
+ line: 2,
+ column: 1,
+ endLine: 8,
+ endColumn: 2
+ }
]
},
}`,
options: [{ max: 2, skipComments: true, skipBlankLines: false }],
errors: [
- { messageId: "exceed", data: { name: "Function 'foo'", lineCount: 7, maxLines: 2 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function 'foo'", linesExceed: 5, maxLines: 2 },
+ line: 3,
+ column: 1,
+ endLine: 7,
+ endColumn: 2
+ }
]
},
()`,
options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }],
errors: [
- { messageId: "exceed", data: { name: "Function", lineCount: 4, maxLines: 2 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function", linesExceed: 2, maxLines: 2 },
+ line: 4,
+ column: 1,
+ endLine: 5,
+ endColumn: 2
+ }
]
},
}`,
options: [{ max: 9, skipComments: true, skipBlankLines: false }],
errors: [
- { messageId: "exceed", data: { name: "Function 'parent'", lineCount: 10, maxLines: 9 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function 'parent'", linesExceed: 1, maxLines: 9 },
+ line: 10,
+ column: 1,
+ endLine: 10,
+ endColumn: 2
+ }
]
},
}`,
options: [{ max: 2, skipComments: true, skipBlankLines: false }],
errors: [
- { messageId: "exceed", data: { name: "Function 'parent'", lineCount: 10, maxLines: 2 } },
- { messageId: "exceed", data: { name: "Function 'nested'", lineCount: 4, maxLines: 2 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function 'parent'", linesExceed: 8, maxLines: 2 },
+ line: 3,
+ column: 1,
+ endLine: 10,
+ endColumn: 2
+ },
+ {
+ messageId: "exceed",
+ data: { name: "Function 'nested'", linesExceed: 2, maxLines: 2 },
+ line: 5,
+ column: 1,
+ endLine: 6,
+ endColumn: 2
+ }
]
},
}`,
options: [{ max: 2, skipComments: true, skipBlankLines: false }],
errors: [
- { messageId: "exceed", data: { name: "Method 'method'", lineCount: 5, maxLines: 2 } }
+ {
+ messageId: "exceed",
+ data: { name: "Method 'method'", linesExceed: 3, maxLines: 2 },
+ line: 4,
+ column: 1,
+ endLine: 6,
+ endColumn: 6
+ }
]
},
}`,
options: [{ max: 2, skipComments: true, skipBlankLines: false }],
errors: [
- { messageId: "exceed", data: { name: "Static method 'foo'", lineCount: 5, maxLines: 2 } }
+ {
+ messageId: "exceed",
+ data: { name: "Static method 'foo'", linesExceed: 3, maxLines: 2 },
+ line: 4,
+ column: 1,
+ endLine: 6,
+ endColumn: 6
+ }
]
},
return 1
}
}`,
- options: [{ max: 2, skipComments: true, skipBlankLines: false }],
+ options: [{ max: 4, skipComments: true, skipBlankLines: false }],
errors: [
- { messageId: "exceed", data: { name: "Getter 'foo'", lineCount: 5, maxLines: 2 } }
+ {
+ messageId: "exceed",
+ data: { name: "Getter 'foo'", linesExceed: 1, maxLines: 4 },
+ line: 6,
+ column: 1,
+ endLine: 6,
+ endColumn: 6
+ }
]
},
}`,
options: [{ max: 2, skipComments: true, skipBlankLines: false }],
errors: [
- { messageId: "exceed", data: { name: "Setter 'foo'", lineCount: 5, maxLines: 2 } }
+ {
+ messageId: "exceed",
+ data: { name: "Setter 'foo'", linesExceed: 3, maxLines: 2 },
+ line: 4,
+ column: 1,
+ endLine: 6,
+ endColumn: 6
+ }
]
},
}`,
options: [{ max: 2, skipComments: true, skipBlankLines: false }],
errors: [
- { messageId: "exceed", data: { name: "Static method", lineCount: 8, maxLines: 2 } }
+ {
+ messageId: "exceed",
+ data: { name: "Static method", linesExceed: 6, maxLines: 2 },
+ line: 4,
+ column: 1,
+ endLine: 9,
+ endColumn: 6
+ }
]
},
}());`,
options: [{ max: 2, skipComments: true, skipBlankLines: false, IIFEs: true }],
errors: [
- { messageId: "exceed", data: { name: "Function", lineCount: 7, maxLines: 2 } }
+ {
+ messageId: "exceed",
+ data: { name: "Function", linesExceed: 5, maxLines: 2 },
+ line: 3,
+ column: 1,
+ endLine: 7,
+ endColumn: 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", linesExceed: 5, maxLines: 2 },
+ line: 3,
+ column: 1,
+ endLine: 7,
+ endColumn: 2
+ }
+ ]
+ },
+ {
+ code: "\nfoo();\nbar();\nbaz();\nfunction name() {\nvar x = 5;\n/* comment 1 */\n/* comment 2 */\n\t\n \n\nvar x = 2;\n}\nquz();",
+ options: [{ max: 2, skipComments: true, skipBlankLines: true }],
+ errors: [
+ {
+ messageId: "exceed",
+ data: { name: "Function 'name'", linesExceed: 2, maxLines: 2 },
+ line: 12,
+ column: 1,
+ endLine: 13,
+ endColumn: 2
+ }
]
}
]