question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New Rule: continuation-linebreak

See original GitHub issue

Please describe what the rule should do:

This rule sets a strict style guideline for multiline arrow functions and expression statements; it can be customized to require or disallow a line break after => or =.

Ending a line with => or = causes a readability issue by ambiguating what the assignment or function body contains. Moving the function body or second line of assignment inline beside => or = should fix this issue.

This rule can be auto-fixed by bumping the second line to be inline beside => or =. This can also be fixed by wrapping the multiline body in parentheses (see "multilineParens": true option).

Future Rule Extension: Add a guideline for object’s that have keys and values multiple lines i.e. that end a line with a :.

Potential Options:

Array options:

  • ["always", { "multilineParens": false }] - (default) require that a an assignment or arrow function must always have a line break i.e. always break on = or =>. This option takes an additional object parameter with the property multilineParens. Setting "multilineParens": true would require that multiline statements be wrapped in parentheses.

  • ["never", { "multilineParens": false }] - requires that there should never be a linebreak in the middle of a continuation, i.e. never break on a = or =>. This takes an additional object parameter with the property multilineParens. Setting "multilineParens": true would require that multiline statements be wrapped in parentheses.

  • ["conditional", { "multilineParens": false, "maxLength": 80 }] - requires that statements shorter than the maxLength should be inline and statements that are longer than the maxLength need to be split up on multiple lines. This option takes an additional object parameter with the properties multilineParens and maxLength. "multilineParens": true would require wrapping rule violations in parentheses if they exceed the maxLength and inline the continuation when the line length is below the maxLength. The maxLength property can have a default value of 80.

What category of rule is this? (place an “X” next to just one item)

  • Enforces code style
  • Warns about a potential error
  • Suggests an alternate way of doing something
  • Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

Option: "always", { "multilineParens": false }

These options are compatible with Prettier’s auto formatting

// bad
x => "foo";

// bad
(x) => {
  return foo;
}

// bad
const foo = bar
  ? 1
  : 2;

// bad
const foo = "bar";

// good
x =>
  "foo";

// good
(x) =>
  {
    return "foo";
  }

// good
const foo =
  "bar";

// good
const foo =
  bar
  ? 1
  : 2;

Option: "always", { "multilineParens": true }

// bad
x => "foo";

// good
x => (
  "foo"
);

Option: "never", { "multilineParens": false }

// bad
x =>
  "foo";

// bad
(x) =>
  {
    return "foo";
  }

// bad
const foo =
  "bar";

// bad
const foo =
  bar
  ? 1
  : 2;

// good
x => "foo";

// good
(x) => {
  return foo;
}

// good
const foo = bar
  ? 1
  : 2;

// good
const foo = "bar";

Option: "never", { "multilineParens": true }

// bad
x =>
  "foo";

// good
x => (
  "foo"
);

Option: "conditional", { "multilineParens": false, "maxLength": 20 }

default maxLength can be 80, I’m using 20 for demonstrative purposes

// bad
x => "longLongName";

// bad
x =>
  "foo";

// bad
const foo =
  "bar";

// bad
const foo = bar ? 1 : "longLongName";

// good
x =>
  "longLongName";

// good
x => "foo";

// good
const foo = "bar";

// good
const foo = bar
  ? 1
  : "longLongName";

Option: "conditional", { "multilineParens": true, "maxLength": 20 }

default maxLength can be 80, I’m using 20 for demonstrative purposes

// bad
x => "longLongName";

// good
x => (
  "longLongName";
);

Why should this rule be included in ESLint (instead of a plugin)?

This rule is very wide-reaching and can improve overall code style.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
ilyavolodincommented, Oct 25, 2017

In general I’m 👍 for the described scenarios (btw, thanks a lot for providing such clear rule request, it really helps). I’m just not sure about conflating arrows and equal operator into the same rule. I understand the goal, just not sure it makes sense when you are just looking through the list of rules. Would operator-linebreak: ['error', { "overrides": { "=": "none" } }] cover your equal operator requirements? Or do you want the expression to be always wrapped into parentheses? If it covers what you want, then I would suggest creating a separate rule arrow-linebreak without conditional option as described above (conditional can always be added later if needed).

1reaction
ljharbcommented, Oct 25, 2017

@not-an-aardvark @platinumazure it seems like the two paths forward are either:

  1. this rule, without the “conditional” option
  2. add => support to non-block-statement-body-position and = support to operator-linebreak

Are one of those palatable?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Continuation lines - IBM
The line being continued is a continued line ; the succeeding lines are continuation lines . Area A of a continuation line must...
Read more >
How can I do a line break (line continuation) in Python?
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be...
Read more >
Line Break Rules in Go - Go 101
The second rule means the last semicolon in a multi-item declaration before the closing sign ) and the last semicolon within a code...
Read more >
Breaking up long lines of code in Python
Instead, the Python style guide (PEP 8) recommends using implicit line continuation. An implicit line continuation happens whenever Python gets ...
Read more >
Write a long string on multiple lines in Python - nkmk note
In Python, a backslash ( \ ) is a line continuation character. If a backslash is placed at the end of a line,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found