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 Enhancement Proposal: object-property-newline with never and some options

See original GitHub issue

From disallowObjectKeysOnNewLine.

I had forgotten to make this proposal in my previous vacation.

JSCS has disallowObjectKeysOnNewLine rule, but object-property-newline rule doesn’t support it, so I think we need to add "always"/"never" and some other options into object-property-newline rule. It would be similar options to #6072, #6073, and #6075. These options are “conditions to need line breaks”. I like consistency, so I hope this proposal’s option will be so also.

There are too many options in this proposal. In my head, those are for autofix.


(fixable) The --fix option on the command line automatically fixes problems reported by this rule.

Options

{
    "object-property-newline": [
        "error",
        {"allowMultiplePropertiesPerLine": false}
    ],
    // OR
    "object-property-newline": [
        "error",
        "always" or "never" or {"multiline": false, "minProperties": 0, "minNonShorthandProperties": 0},
        {"allowMultiplePropertiesPerLine": false}
    ]
}

1st option: when does it require line breaks between properties?

  • "always" (default) - requires line breaks always.
  • "never" - disallows line breaks always.
  • An object - requires line breaks if any of the following conditions are satisfied. Otherwise, disallows line breaks.
    • multiline (default is false) - requires line breaks if there are line breaks inside of properties. If this is false, this condition is disabled.
    • minProperties (default is 0) - requires line breaks if the number of properties is more than the given integer. If this is 0, this condition is disabled.
    • minLongformProperties (default is 0) - requires line breaks if the number of non-shorthand properties is more than the given integer. If this is 0, this condition is disabled. (#6211)

2nd option:

  • allowMultiplePropertiesPerLine (default is false) - allows all keys and values to be on the same line.

always

Examples of incorrect code for this rule with the "always" option:

/*eslint object-property-newline: ["error", "always"]*/
/*eslint-env es6*/

let c = {foo: 1, bar: 2};
let d = {foo: 1, bar: 2, baz: 3};
let e = {foo, bar, baz};
let f = {
    foo, bar() {
        dosomething();
    }
};

Examples of correct code for this rule with the "always" option:

/*eslint object-property-newline: ["error", "always"]*/
/*eslint-env es6*/

let a = {};
let b = {foo: 1};
let c = {foo: 1,
    bar: 2};
let d = {foo: 1,
    bar: 2,
    baz: 3};
let e = {foo,
    bar,
    baz};
let f = {
    foo,
    bar() {
        dosomething();
    }
};

never

Examples of incorrect code for this rule with the "never" option:

/*eslint object-property-newline: ["error", "never"]*/
/*eslint-env es6*/

let c = {foo: 1,
    bar: 2};
let d = {foo: 1,
    bar: 2,
    baz: 3};
let e = {foo,
    bar,
    baz};
let f = {
    foo,
    bar() {
        dosomething();
    }
};

Examples of correct code for this rule with the "never" option:

/*eslint object-property-newline: ["error", "never"]*/
/*eslint-env es6*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1, bar: 2, baz: 3};
let e = {foo, bar, baz};
let f = {
    foo, bar() {
        dosomething();
    }
};

multiline

Examples of incorrect code for this rule with the {"multiline": true} option:

/*eslint object-property-newline: ["error", {"multiline": true}]*/
/*eslint-env es6*/

let c = {foo: 1,
    bar: 2};
let d = {foo: 1,
    bar: 2,
    baz: 3};
let e = {foo,
    bar,
    baz};
let f = {
    foo, bar() {
        dosomething();
    }
};

Examples of correct code for this rule with the {"multiline": true} option:

/*eslint object-property-newline: ["error", {"multiline": true}]*/
/*eslint-env es6*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1, bar: 2, baz: 3};
let e = {foo, bar, baz};
let f = {
    foo,
    bar() {
        dosomething();
    }
};

minProperties

Examples of incorrect code for this rule with the {"minProperties": 3} option:

/*eslint object-property-newline: ["error", {"minProperties": 3}]*/
/*eslint-env es6*/

let c = {foo: 1,
    bar: 2};
let d = {foo: 1, bar: 2, baz: 3};
let e = {foo, bar, baz};
let f = {
    foo,
    bar() {
        dosomething();
    }
};

Examples of correct code for this rule with the {"minProperties": 3} option:

/*eslint object-property-newline: ["error", {"minProperties": 3}]*/
/*eslint-env es6*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
    bar: 2,
    baz: 3};
let e = {foo,
    bar,
    baz};
let f = {
    foo, bar() {
        dosomething();
    }
};

minLongformProperties

Examples of incorrect code for this rule with the {"minLongformProperties": 3} option:

/*eslint object-property-newline: ["error", {"minLongformProperties": 3}]*/
/*eslint-env es6*/

let c = {foo: 1,
    bar: 2};
let d = {foo: 1, bar: 2, baz: 3};
let e = {foo,
    bar,
    baz};
let f = {
    foo,
    bar() {
        dosomething();
    }
};

Examples of correct code for this rule with the {"minLongformProperties": 3} option:

/*eslint object-property-newline: ["error", {"minLongformProperties": 3}]*/
/*eslint-env es6*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
    bar: 2,
    baz: 3};
let e = {foo, bar, baz};
let f = {
    foo, bar() {
        dosomething();
    }
};

a combination of multiline and minLongformProperties

Examples of incorrect code for this rule with the {"multiline": true, "minLongformProperties": 3} option:

/*eslint object-property-newline: ["error", {"multiline": true, "minLongformProperties": 3}]*/
/*eslint-env es6*/

let c = {foo: 1,
    bar: 2};
let d = {foo: 1, bar: 2, baz: 3};
let e = {foo,
    bar,
    baz};
let f = {
    foo, bar() {
        dosomething();
    }
};

Examples of correct code for this rule with the {"multiline": true, "minLongformProperties": 3} option:

/*eslint object-property-newline: ["error", {"multiline": true, "minLongformProperties": 3}]*/
/*eslint-env es6*/

let a = {};
let b = {foo: 1};
let c = {foo: 1, bar: 2};
let d = {foo: 1,
    bar: 2,
    baz: 3};
let e = {foo, bar, baz};
let f = {
    foo,
    bar() {
        dosomething();
    }
};

Related Rules

brace style space style line break style
block brace-style block-spacing max-statements-per-line
object #6072 object-curly-newline object-curly-spacing object-property-newline
array #6073 array-bracket-newline array-bracket-spacing #6075 array-element-newline

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:19
  • Comments:24 (19 by maintainers)

github_iconTop GitHub Comments

2reactions
platinumazurecommented, Aug 16, 2016

@eslint/eslint-team Can we get more eyes on this proposal?

I’m a little leery of the number of options (only in terms of, is this worth doing as multiple issues/PRs?), but that’s not a huge issue. But we should really make sure we get the option names right.

I’m going to offer one slightly different schema: In order to emphasize that most of the options are conditions for requiring the property newline, maybe they could be grouped in an object with a key indicating that? (I’ve also changed some of the other keys, but I’m not strongly attached to these names.)

{
    "object-property-newline": ["error", {
        "requiredWhen": {
            "propertyCount": 3,           // Equivalent to minProperties
            "longformPropertyCount": 3,   // Equivalent to minLongformProperties
            "hasMultilineProperty": true  // Equivalent to multiline
        }
    }
}
1reaction
nijikokuncommented, Sep 21, 2018

Rip

Read more comments on GitHub >

github_iconTop Results From Across the Web

object-property-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 >
How to Write Beautiful Python Code With PEP 8 - Real Python
Learn how to write high-quality, readable code by using the Python style guidelines laid out in PEP 8. Following these guidelines helps you...
Read more >
eslint/eslint - Gitter
Here it is: Error: rule-tester: Configuration for rule "object-property-newline" is invalid: Value "[object Object]" should NOT have additional properties.
Read more >
JavaScript Tutorial: The Basics
ECMAScript 2021 (ES12): Added enhancement to strings ( replaceAll ) ... However, in the early days, some efforts were made to adopt Java ......
Read more >
Best practices for writing code comments - Stack Overflow Blog
While all of these points are true, it would be a mistake to go to the other extreme and never write comments. Here...
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