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.

`object-curly-newline`: new option request

See original GitHub issue

I’m on eslint v2.13.1.

The multiline: true option is excellent: it lets me require that when newlines exist, every property must be on its own line.

Combining it with minProperties is half useful, however: when it’s set to, say, 3, that means that any objects with 3 or more properties must be split onto multiple lines, which is great! Unfortunately, this also means that objects with less than 3 properties must not be split onto multiple lines.

What I’d like instead is something like { multiline: true, maxPropertiesBeforeForcedNewline: 2 } which says that if there are > 2 properties, the object must be multiline, but if there are <= 2, the object may be single or multi, provided that it’s consistent with the multiline: true option.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:12
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

12reactions
mysticateacommented, Jun 21, 2016

Thank you for this issue.

Maybe similar to #6369. I think this is reasonable.

What do you think either option?

/*eslint object-curly-newline: [error, {either: true}]*/

// ✘ BAD
var obj = {a: 1
}
var obj = {
    a: 1}

// ✔ GOOD
var obj = {a: 1}
var obj = {
    a: 1
}
  • "always" - requires line breaks always.
  • "never" - disallows line breaks.
  • {multiline: true} (default) - requires line breaks if there are line breaks inside properties or between properties. Otherwise, disallows line breaks.
  • {minProperties: <integer>} - requires line breaks if the number of properties is more than the given integer. Otherwise, disallows line breaks.
  • {either: true} - requires line breaks (of both braces) if either brace has a line break.

multiline and minProperties and either can be combined.

  • {multiline: true, minProperties: <integer>, either: true} - requires line breaks if there are line breaks inside properties or between properties, or if the number of properties is more than the given integer, or if either brace has a line break. Otherwise, disallows line breaks.

If either option is enabled, this rule never disallows line breaks.

8reactions
ljharbcommented, Oct 25, 2016

What rule do you want to change? object-curly-newline

Does this change cause the rule to produce more or fewer warnings? Fewer.

How will the change be implemented? (New option, new default behavior, etc.)? New option: consistent. When true, it requires that either both curly braces, or neither, directly enclose newlines.

Please provide some example code that this change will affect:

cat temp
var one = { a: 1 };

var two = { a: 1, b: 2 };

var three = { a: 1, b: 2, c: 3 };

var oneM = {
  a: 1
};

var twoM = {
  a: 1,
  b: 2
};

var threeM = {
  a: 1,
  b: 2,
  c: 3
};

var wat = { a: 1
};

var wat2 = {
a: 1 };

var wat3 = { a: 1,
  b: 2,
  c: 3 };

What does the rule currently do for this code?

cat temp | ./node_modules/.bin/eslint --stdin --no-eslintrc --rule='object-curly-newline:[2, {"minProperties": 3, "multiline": true}]'

<text>
   5:13  error  Expected a line break after this opening brace   object-curly-newline
   5:32  error  Expected a line break before this closing brace  object-curly-newline
   7:12  error  Unexpected line break after this opening brace   object-curly-newline
   9:1   error  Unexpected line break before this closing brace  object-curly-newline
  23:1   error  Unexpected line break before this closing brace  object-curly-newline
  25:12  error  Unexpected line break after this opening brace   object-curly-newline
  28:12  error  Expected a line break after this opening brace   object-curly-newline
  30:8   error  Expected a line break before this closing brace  object-curly-newline

✖ 8 problems (8 errors, 0 warnings)

What will the rule do after it’s changed?

cat temp | ./node_modules/.bin/eslint --stdin --no-eslintrc --rule='object-curly-newline:[2, {"minProperties": 3, "multiline": true, "consistent": true}]'

<text>
   5:13  error  Expected a line break after this opening brace   object-curly-newline
   5:32  error  Expected a line break before this closing brace  object-curly-newline
  23:1   error  Unexpected line break before this closing brace  object-curly-newline
  25:12  error  Unexpected line break after this opening brace   object-curly-newline
  28:12  error  Expected a line break after this opening brace   object-curly-newline
  30:8   error  Expected a line break before this closing brace  object-curly-newline

✖ 6 problems (6 errors, 0 warnings)

Perhaps a better alternative is actually fixing minProperties to mean “min properties to force multiline”, instead of meaning “number of properties that determines whether it must be multiline or single-line”, but that gets into the require/disallow complexity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

object-curly-newline - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
javascript - ESLint - object-curly-newline - Stack Overflow
Is there a way to allow for {} (an empty object) while keeping the rest as is?
Read more >
vue/object-curly-newline - eslint-plugin-vue
vue/object-curly-newline #. Enforce consistent line breaks after opening and before closing braces in <template>. The --fix option on ...
Read more >
Code style for JS6 destructured object lacks settings in ...
WEB-32601 The auto-formatter doesn't follow ESLint rules when applying the `object-curly-newline` rule. 2. I want to have something like that:.
Read more >
A brand new website interface for an even better experience!
Proposal: "object-curly-newline" add configuration for named imports and exports (or create new rule for import/export specifiers) · Javascript Required. Kindly ...
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