Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jun 16, 2016
1 parent 3f13325 commit 8f45228
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
6 changes: 4 additions & 2 deletions lib/rules/max-statements-per-line.js
Expand Up @@ -4,6 +4,8 @@
*/
"use strict";

var format = require("util").format;

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -35,7 +37,7 @@ module.exports = {
const sourceCode = context.getSourceCode(),
options = context.options[0] || {},
maxStatementsPerLine = typeof options.max !== "undefined" ? options.max : 1,
message = "This line has too many statements. Maximum allowed is " + maxStatementsPerLine + ".";
message = "This line has %s statements. Maximum allowed is " + maxStatementsPerLine + ".";
let lastStatementLine = 0,
numberOfStatementsOnThisLine = 0;

Expand Down Expand Up @@ -89,7 +91,7 @@ module.exports = {

// Reports if the node violated this rule.
if (numberOfStatementsOnThisLine === maxStatementsPerLine + 1) {
context.report({node, message});
context.report({node, message: format(message, numberOfStatementsOnThisLine)});

This comment has been minimized.

Copy link
@btmills

btmills Sep 2, 2016

@ljharb what if:

  • Create another variable, let firstExtraStatement = null; right below line 42.
  • Instead of reporting here, save firstExtraStatement = node;.
  • Report when the traversal goes to the next line, report before lines 88 and 109, passing firstExtraStatement as the node.
  • After reporting, clear firstExtraStatement.
  • Add a Program:exit handler that would report one more time if firstExtraStatement is truthy. This would handle the case where the last line has too many statements, and there aren't any statements later on to trigger the report.
  • Perhaps extract reporting and clearing firstExtraStatement into a helper to consolidate that behavior.

This would delay reporting until all statements on a line have been counted, but it would still report the location of the first extra node.

Would that work?

This comment has been minimized.

Copy link
@ljharb

ljharb Sep 2, 2016

Author Owner

I'll give that a shot!

This comment has been minimized.

Copy link
@ljharb

ljharb Sep 2, 2016

Author Owner
}
}

Expand Down
84 changes: 42 additions & 42 deletions tests/lib/rules/max-statements-per-line.js
Expand Up @@ -117,47 +117,47 @@ ruleTester.run("max-statements-per-line", rule, {
}
],
invalid: [
{ code: "{ }", options: [{ max: 0 }], errors: [{ message: "This line has too many statements. Maximum allowed is 0." }] },
{ code: "var bar = 1;", options: [{ max: 0 }], errors: [{ message: "This line has too many statements. Maximum allowed is 0." }] },
{ code: "var bar = 1; var baz = 2;", errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "var bar = 1; var baz = 2;", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "if (condition) var bar = 1; if (condition) var baz = 2;", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "if (condition) var bar = 1; else var baz = 1;", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "if (condition) { } if (condition) { }", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "if (condition) { var bar = 1; } else { }", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "if (condition) { } else { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "if (condition) { var bar = 1; } else { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "for (var i = 0; i < length; ++i) { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "switch (discriminant) { default: break; }", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "function foo() { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "function foo() { if (condition) var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "function foo() { if (condition) { var bar = 1; } }", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "(function() { var bar = 1; })();", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "var foo = function foo() { var bar = 1; };", options: [{ max: 1 }], errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "var foo = { prop: () => { var bar = 1; } };", options: [{ max: 1 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "var bar = 1; var baz = 2; var qux = 3;", options: [{ max: 2 }], errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "if (condition) { var bar = 1; } else { var bar = 1; }", options: [{ max: 2 }], errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; } else { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "for (var i = 0; i < length; ++i) { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "switch (discriminant) { case 'test': break; default: break; }", options: [{ max: 2 }], errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "function foo() { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "function foo() { if (condition) { var bar = 1; } }", options: [{ max: 2 }], errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "(function() { var bar = 1; var baz = 2; })();", options: [{ max: 2 }], errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "var foo = function foo() { var bar = 1; var baz = 2; };", options: [{ max: 2 }], errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "var foo = { prop: () => { var bar = 1; var baz = 2; } };", options: [{ max: 2 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "var bar = 1; var baz = 2; var qux = 3; var waldo = 4;", options: [{ max: 3 }], errors: [{ message: "This line has too many statements. Maximum allowed is 3." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; var qux = 3; }", options: [{ max: 3 }], errors: [{ message: "This line has too many statements. Maximum allowed is 3." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; } else { var bar = 1; var baz = 2; }", options: [{ max: 3 }], errors: [{ message: "This line has too many statements. Maximum allowed is 3." }] },
{ code: "switch (discriminant) { case 'test': var bar = 1; break; default: var bar = 1; break; }", options: [{ max: 3 }], errors: [{ message: "This line has too many statements. Maximum allowed is 3." }] },
{ code: "let bar = bar => { a; }, baz = baz => { b; }, qux = qux => { c; };", options: [{ max: 3 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has too many statements. Maximum allowed is 3." }] },
{ code: "(bar => { a; }) ? (baz => { b; }) : (qux => { c; });", options: [{ max: 3 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has too many statements. Maximum allowed is 3." }] },
{ code: "bar => { a; }, baz => { b; }, qux => { c; }, quux => { d; };", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has too many statements. Maximum allowed is 4." }] },
{ code: "[bar => { a; }, baz => { b; }, qux => { c; }, quux => { d; }];", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has too many statements. Maximum allowed is 4." }] },
{ code: "foo(bar => { a; }, baz => { b; }, qux => { c; }, quux => { d; });", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has too many statements. Maximum allowed is 4." }] },
{ code: "({ bar: bar => { a; }, baz: baz => { b; }, qux: qux => { c; }, quux: quux => { d; }});", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has too many statements. Maximum allowed is 4." }] },
{ code: "a; if (b) { c; d; }\nz;", options: [{ max: 2 }], errors: [{ message: "This line has too many statements. Maximum allowed is 2." }] },
{ code: "export default function foo() { console.log('test') }", options: [{ max: 1 }], parserOptions: {ecmaVersion: 6, sourceType: "module"}, errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] },
{ code: "export function foo() { console.log('test') }", options: [{ max: 1 }], parserOptions: {ecmaVersion: 6, sourceType: "module"}, errors: [{ message: "This line has too many statements. Maximum allowed is 1." }] }
{ code: "{ }", options: [{ max: 0 }], errors: [{ message: "This line has 1 statement(s). Maximum allowed is 0." }] },
{ code: "var bar = 1;", options: [{ max: 0 }], errors: [{ message: "This line has 1 statement(s). Maximum allowed is 0." }] },
{ code: "var bar = 1; var baz = 2;", errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "var bar = 1; var baz = 2;", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) var bar = 1; if (condition) var baz = 2;", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) var bar = 1; else var baz = 1;", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) { } if (condition) { }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) { var bar = 1; } else { }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) { } else { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "if (condition) { var bar = 1; } else { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "for (var i = 0; i < length; ++i) { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "switch (discriminant) { default: break; }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "function foo() { var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "function foo() { if (condition) var bar = 1; }", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "function foo() { if (condition) { var bar = 1; } }", options: [{ max: 1 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 1." }] },
{ code: "(function() { var bar = 1; })();", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "var foo = function foo() { var bar = 1; };", options: [{ max: 1 }], errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "var foo = { prop: () => { var bar = 1; } };", options: [{ max: 1 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "var bar = 1; var baz = 2; var qux = 3;", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "if (condition) { var bar = 1; } else { var bar = 1; }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; } else { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "for (var i = 0; i < length; ++i) { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "switch (discriminant) { case 'test': break; default: break; }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "function foo() { var bar = 1; var baz = 2; }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "function foo() { if (condition) { var bar = 1; } }", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "(function() { var bar = 1; var baz = 2; })();", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "var foo = function foo() { var bar = 1; var baz = 2; };", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "var foo = { prop: () => { var bar = 1; var baz = 2; } };", options: [{ max: 2 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "var bar = 1; var baz = 2; var qux = 3; var waldo = 4;", options: [{ max: 3 }], errors: [{ message: "This line has 4 statement(s). Maximum allowed is 3." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; var qux = 3; }", options: [{ max: 3 }], errors: [{ message: "This line has 4 statement(s). Maximum allowed is 3." }] },
{ code: "if (condition) { var bar = 1; var baz = 2; } else { var bar = 1; var baz = 2; }", options: [{ max: 3 }], errors: [{ message: "This line has 4 statement(s). Maximum allowed is 3." }] },
{ code: "switch (discriminant) { case 'test': var bar = 1; break; default: var bar = 1; break; }", options: [{ max: 3 }], errors: [{ message: "This line has 4 statement(s). Maximum allowed is 3." }] },
{ code: "let bar = bar => { a; }, baz = baz => { b; }, qux = qux => { c; };", options: [{ max: 3 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 4 statement(s). Maximum allowed is 3." }] },
{ code: "(bar => { a; }) ? (baz => { b; }) : (qux => { c; });", options: [{ max: 3 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 4 statement(s). Maximum allowed is 3." }] },
{ code: "bar => { a; }, baz => { b; }, qux => { c; }, quux => { d; };", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 5 statement(s). Maximum allowed is 4." }] },
{ code: "[bar => { a; }, baz => { b; }, qux => { c; }, quux => { d; }];", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 5 statement(s). Maximum allowed is 4." }] },
{ code: "foo(bar => { a; }, baz => { b; }, qux => { c; }, quux => { d; });", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 5 statement(s). Maximum allowed is 4." }] },
{ code: "({ bar: bar => { a; }, baz: baz => { b; }, qux: qux => { c; }, quux: quux => { d; }});", options: [{ max: 4 }], parserOptions: { ecmaVersion: 6 }, errors: [{ message: "This line has 5 statement(s). Maximum allowed is 4." }] },
{ code: "a; if (b) { c; d; }\nz;", options: [{ max: 2 }], errors: [{ message: "This line has 3 statement(s). Maximum allowed is 2." }] },
{ code: "export default function foo() { console.log('test') }", options: [{ max: 1 }], parserOptions: {ecmaVersion: 6, sourceType: "module"}, errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] },
{ code: "export function foo() { console.log('test') }", options: [{ max: 1 }], parserOptions: {ecmaVersion: 6, sourceType: "module"}, errors: [{ message: "This line has 2 statement(s). Maximum allowed is 1." }] }
]
});

0 comments on commit 8f45228

Please sign in to comment.