Rule Enhancement Proposal: object-property-newline with never and some options
See original GitHub issueFrom 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 isfalse
) - requires line breaks if there are line breaks inside of properties. If this isfalse
, this condition is disabled.minProperties
(default is0
) - requires line breaks if the number of properties is more than the given integer. If this is0
, this condition is disabled.minLongformProperties
(default is0
) - requires line breaks if the number of non-shorthand properties is more than the given integer. If this is0
, this condition is disabled. (#6211)
2nd option:
allowMultiplePropertiesPerLine
(default isfalse
) - 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:
- Created 7 years ago
- Reactions:19
- Comments:24 (19 by maintainers)
Top GitHub Comments
@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.)
Rip