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.

Rule Proposal: `key-linebreak` enforce linebreaks after object key

See original GitHub issue

key-linebreak

Please describe what the rule should do: This rule enforces consistency in linebreaks after an object key.

For context: we’ve come across places in our code where linebreaks occur after object keys, usually as a result of using an autoformatting tool or to avoid violating max-len.

We would like to make a decision on allowing this code style and it would be great if we could enforce consistent behavior using eslint.

Options: This rule has a string option:

  • "beside": (default) requires that an object key has a continuation of the value on the same line
  • "below": requires that an object key is followed by a linebreak before the value

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:

"beside" option (default):

// 👍  good
this.state = {
  scanMethod: flowContextLongLongName === FLOW_CONTEXTS.SUSPENSION_APPEAL
    ? DEVICE_OPTIONS.CURRENT_DEVICE
    : DEVICE_OPTIONS.MOBILE_APP,
  scanMethod1: 'string ',
  [dynamic]: 'foo',
  parens: (
    something === somethingElse
  ),
}

// 👎  bad
this.state = {
  scanMethod:
    flowContextLongLongName === FLOW_CONTEXTS.SUSPENSION_APPEAL
      ? DEVICE_OPTIONS.CURRENT_DEVICE
      : DEVICE_OPTIONS.MOBILE_APP,
  scanMethod1:
    'string ',
  [dynamic]:
    'foo',
  parens:
    (
      something === somethingElse
    ),
}

"below" option:

// 👍  good
this.state = {
  scanMethod:
    flowContextLongLongName === FLOW_CONTEXTS.SUSPENSION_APPEAL
      ? DEVICE_OPTIONS.CURRENT_DEVICE
      : DEVICE_OPTIONS.MOBILE_APP,
  scanMethod1:
    'string ',
  [dynamic]:
    'foo',
  parens:
    (
      something === somethingElse
    ),
}

// 👎  bad
this.state = {
  scanMethod: flowContextLongLongName === FLOW_CONTEXTS.SUSPENSION_APPEAL
    ? DEVICE_OPTIONS.CURRENT_DEVICE
    : DEVICE_OPTIONS.MOBILE_APP,
  scanMethod1: 'string ',
  [dynamic]: 'foo',
  parens: (
    something === somethingElse
  ),
}

Why should this rule be included in ESLint (instead of a plugin)? This rule will affect code style in a very broad way and it doesn’t seem like there are any plugins that will be suitable for this case.

cc. @ljharb

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
sharmilajesupaulcommented, Jan 26, 2018

@not-an-aardvark thanks for suggesting this, that should work well for this use case.

We could add configuration to key-spacing’s multiline option called linebreak that takes “below”, “beside”, and “consistent” as values?

2reactions
ljharbcommented, Jan 5, 2018

If so, then an option name like multiLineObjectValuePlacement or something might work, with possible values of “below”, “beside”, “consistent”, and “ignore” (and “ignore” would be the default, for back compat)?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rule Enhancement Proposal: object-property-newline with ...
An object - requires line breaks if any of the following conditions are satisfied. Otherwise, disallows line breaks. multiline (default is false ) ......
Read more >
object-curly-newline - ESLint - Pluggable JavaScript Linter
Enforce consistent line breaks after opening and before closing braces ... This rule requires or disallows a line break between { and its...
Read more >
Where Lines Break is Complicated. Here's all the ... - CSS-Tricks
The text hanging out of the box is a visual problem. One possibility is overflow: hidden; which is a bit of a blunt...
Read more >
How do I enter a "soft return" ("line break" or "newline ...
I need to be able to enter a "soft return" (aka "line break" or "newline character") to force text to a new line...
Read more >
line-break - CSS: Cascading Style Sheets - MDN Web Docs
The line-break CSS property sets how to break lines of Chinese, Japanese, or Korean (CJK) text when working with punctuation and symbols.
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