eslint: Rule Enhancement Proposal: object-property-newline with never and some options
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 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 |
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 19
- Comments: 24 (19 by maintainers)
Commits related to this issue
- Docs: Amend rule document to correct and complete it (refs #6251). (#9498) — committed to eslint/eslint by jrpool 7 years ago
@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
What about spaces between properties as well? Will the padding features be affected by this or has that not been taken into consideration?
For example, rules to handle such a case:
VS:
After an initial review, I propose to liquidate this issue with the following steps:
Revise the user documentation on the existing rule object-property-newline to cover cases not documented and more fully describe the behavior of the
--fix
option.Revise the tests of the existing rule object-property-newline to cover cases handled but not tested.
Evaluate whether the proposed rule should be a modification of the existing rule object-property-newline or a distinct new rule.
Implement the proposal insofar as necessary to produce equivalent functionality to JSCS rule disallowObjectKeysOnNewLine.
Implement the remainder of the proposal.
If there is any disagreement with this, please let me know. Thanks!