Skip to content

Commit

Permalink
Update: max-statements to report function name (refs #7260) (#7399)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Nov 3, 2016
1 parent 0c215fa commit 3798aea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
11 changes: 10 additions & 1 deletion lib/rules/max-statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,18 @@ module.exports = {
*/
function reportIfTooManyStatements(node, count, max) {
if (count > max) {
const messageEnd = " has too many statements ({{count}}). Maximum allowed is {{max}}.";
let name = "This function";

if (node.id) {
name = `Function '${node.id.name}'`;
} else if (node.parent.type === "MethodDefinition" || node.parent.type === "Property") {
name = `Function '${context.getSource(node.parent.key)}'`;
}

context.report(
node,
"This function has too many statements ({{count}}). Maximum allowed is {{max}}.",
name + messageEnd,
{ count, max });
}
}
Expand Down
26 changes: 16 additions & 10 deletions tests/lib/rules/max-statements.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const ruleTester = new RuleTester();

ruleTester.run("max-statements", rule, {
valid: [
{ code: "var foo = { thing: function() { var bar = 1; var baz = 2; } }", options: [2] },
{ code: "var foo = { thing: () => { var bar = 1; var baz = 2; } }", options: [2], parserOptions: { ecmaVersion: 6 } },
{ code: "function foo() { var bar = 1; function qux () { var noCount = 2; } return 3; }", options: [3] },
{ code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } else { quxx(); } return 3; }", options: [6]},
{ code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }", options: [5]},
Expand All @@ -31,23 +29,31 @@ ruleTester.run("max-statements", rule, {
{ code: "define(['foo', 'qux'], function(foo, qux) { var bar = 1; var baz = 2; })", options: [1, {ignoreTopLevelFunctions: true}] },

// object property options
{ code: "var foo = { thing: function() { var bar = 1; var baz = 2; } }", options: [2] },
{ code: "var foo = { thing() { var bar = 1; var baz = 2; } }", options: [2], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = { ['thing']() { var bar = 1; var baz = 2; } }", options: [2], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = { thing: () => { var bar = 1; var baz = 2; } }", options: [2], parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = { thing: function() { var bar = 1; var baz = 2; } }", options: [{ max: 2 }] }
],
invalid: [
{ code: "function foo() { var bar = 1; var baz = 2; var qux = 3; }", options: [2], errors: [{ message: "This function has too many statements (3). Maximum allowed is 2."}] },
{ code: "function foo() { var bar = 1; var baz = 2; var qux = 3; }", options: [2], errors: [{ message: "Function 'foo' has too many statements (3). Maximum allowed is 2."}] },
{ code: "var foo = () => { var bar = 1; var baz = 2; var qux = 3; };", options: [2], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This function has too many statements (3). Maximum allowed is 2."}] },
{ code: "var foo = function() { var bar = 1; var baz = 2; var qux = 3; };", options: [2], errors: [{ message: "This function has too many statements (3). Maximum allowed is 2."}] },
{ code: "function foo() { var bar = 1; if (true) { while (false) { var qux = null; } } return 3; }", options: [4], errors: [{ message: "This function has too many statements (5). Maximum allowed is 4."}] },
{ code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } return 3; }", options: [4], errors: [{ message: "This function has too many statements (5). Maximum allowed is 4."}] },
{ code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } else { quxx(); } return 3; }", options: [5], errors: [{ message: "This function has too many statements (6). Maximum allowed is 5."}] },
{ code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }", options: [3], errors: [{ message: "This function has too many statements (5). Maximum allowed is 3."}] },
{ code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }", options: [4], errors: [{ message: "This function has too many statements (5). Maximum allowed is 4."}] },
{ code: "function foo() { var bar = 1; if (true) { while (false) { var qux = null; } } return 3; }", options: [4], errors: [{ message: "Function 'foo' has too many statements (5). Maximum allowed is 4."}] },
{ code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } return 3; }", options: [4], errors: [{ message: "Function 'foo' has too many statements (5). Maximum allowed is 4."}] },
{ code: "function foo() { var bar = 1; if (true) { for (;;) { var qux = null; } } else { quxx(); } return 3; }", options: [5], errors: [{ message: "Function 'foo' has too many statements (6). Maximum allowed is 5."}] },
{ code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }", options: [3], errors: [{ message: "Function 'foo' has too many statements (5). Maximum allowed is 3."}] },
{ code: "function foo() { var x = 5; function bar() { var y = 6; } bar(); z = 10; baz(); }", options: [4], errors: [{ message: "Function 'foo' has too many statements (5). Maximum allowed is 4."}] },
{ code: ";(function() { var bar = 1; return function () { var z; return 42; }; })()", options: [1, {ignoreTopLevelFunctions: true}], errors: [{ message: "This function has too many statements (2). Maximum allowed is 1."}] },
{ code: ";(function() { var bar = 1; var baz = 2; })(); (function() { var bar = 1; var baz = 2; })()", options: [1, {ignoreTopLevelFunctions: true}], errors: [{ message: "This function has too many statements (2). Maximum allowed is 1."}, { message: "This function has too many statements (2). Maximum allowed is 1."}] },
{ code: "define(['foo', 'qux'], function(foo, qux) { var bar = 1; var baz = 2; return function () { var z; return 42; }; })", options: [1, {ignoreTopLevelFunctions: true}], errors: [{ message: "This function has too many statements (2). Maximum allowed is 1."}] },
{ code: "function foo() { var a; var b; var c; var x; var y; var z; bar(); baz(); qux(); quxx(); foo(); }", errors: [{ message: "This function has too many statements (11). Maximum allowed is 10."}] },
{ code: "function foo() { var a; var b; var c; var x; var y; var z; bar(); baz(); qux(); quxx(); foo(); }", errors: [{ message: "Function 'foo' has too many statements (11). Maximum allowed is 10."}] },

// object property options
{ code: "function foo() { var bar = 1; var baz = 2; var qux = 3; }", options: [{ max: 2 }], errors: [{ message: "This function has too many statements (3). Maximum allowed is 2."}] }
{ code: "var foo = { thing: function() { var bar = 1; var baz = 2; var baz2; } }", options: [2], errors: [{ message: "Function 'thing' has too many statements (3). Maximum allowed is 2."}] },
{ code: "var foo = { thing() { var bar = 1; var baz = 2; var baz2; } }", options: [2], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Function 'thing' has too many statements (3). Maximum allowed is 2."}] },
{ code: "var foo = { ['thing']() { var bar = 1; var baz = 2; var baz2; } }", options: [2], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Function ''thing'' has too many statements (3). Maximum allowed is 2."}] },
{ code: "var foo = { thing: () => { var bar = 1; var baz = 2; var baz2; } }", options: [2], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "Function 'thing' has too many statements (3). Maximum allowed is 2."}] },
{ code: "var foo = { thing: function() { var bar = 1; var baz = 2; var baz2; } }", options: [{ max: 2 }], errors: [{ message: "Function 'thing' has too many statements (3). Maximum allowed is 2."}] }
]
});

0 comments on commit 3798aea

Please sign in to comment.