Skip to content

Commit

Permalink
Update: Rename and deprecate object-property-newline option (#9570)
Browse files Browse the repository at this point in the history
Object option allowMultiplePropertiesPerLine is stricter than the name implies.
It creates an exception to object-property-newline only if ALL properties are
on a SINGLE line. Renamed accordingly. Old name left deprecated, and 2 tests with
deprecated name (1 valid and 1 invalid) left in force.
  • Loading branch information
jrpool authored and kaicataldo committed Jan 8, 2018
1 parent acde640 commit 2cf4522
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 39 deletions.
10 changes: 5 additions & 5 deletions docs/rules/object-property-newline.md
Expand Up @@ -74,7 +74,7 @@ Another benefit of this rule is specificity of diffs when a property is changed:

### Optional Exception

The rule offers one object option, `allowMultiplePropertiesPerLine`. If you set it to `true`, object literals such as the first two above, with all property specifications on the same line, will be permitted, but one like
The rule offers one object option, `allowAllPropertiesOnSameLine` (a deprecated synonym is `allowMultiplePropertiesPerLine`). If you set it to `true`, object literals such as the first two above, with all property specifications on the same line, will be permitted, but one like

```js
const newObject = {
Expand Down Expand Up @@ -172,7 +172,7 @@ As illustrated above, the `--fix` option, applied to this rule, does not comply

## Examples

Examples of **incorrect** code for this rule, with no object option or with `allowMultiplePropertiesPerLine` set to `false`:
Examples of **incorrect** code for this rule, with no object option or with `allowAllPropertiesOnSameLine` set to `false`:

```js
/*eslint object-property-newline: "error"*/
Expand Down Expand Up @@ -208,7 +208,7 @@ const obj5 = {
]: true};
```

Examples of **correct** code for this rule, with no object option or with `allowMultiplePropertiesPerLine` set to `false`:
Examples of **correct** code for this rule, with no object option or with `allowAllPropertiesOnSameLine` set to `false`:

```js
/*eslint object-property-newline: "error"*/
Expand Down Expand Up @@ -238,10 +238,10 @@ const obj3 = {
};
```

Examples of additional **correct** code for this rule with the `{ "allowMultiplePropertiesPerLine": true }` option:
Examples of additional **correct** code for this rule with the `{ "allowAllPropertiesOnSameLine": true }` option:

```js
/*eslint object-property-newline: ["error", { "allowMultiplePropertiesPerLine": true }]*/
/*eslint object-property-newline: ["error", { "allowAllPropertiesOnSameLine": true }]*/

const obj = { foo: "foo", bar: "bar", baz: "baz" };

Expand Down
10 changes: 8 additions & 2 deletions lib/rules/object-property-newline.js
Expand Up @@ -22,7 +22,10 @@ module.exports = {
{
type: "object",
properties: {
allowMultiplePropertiesPerLine: {
allowAllPropertiesOnSameLine: {
type: "boolean"
},
allowMultiplePropertiesPerLine: { // Deprecated
type: "boolean"
}
},
Expand All @@ -34,7 +37,10 @@ module.exports = {
},

create(context) {
const allowSameLine = context.options[0] && Boolean(context.options[0].allowMultiplePropertiesPerLine);
const allowSameLine = context.options[0] && (
Boolean(context.options[0].allowAllPropertiesOnSameLine) ||
Boolean(context.options[0].allowMultiplePropertiesPerLine) // Deprecated
);
const errorMessage = allowSameLine
? "Object properties must go on a new line if they aren't all on the same line."
: "Object properties must go on a new line.";
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config-eslint/default.yml
Expand Up @@ -129,7 +129,7 @@ rules:
no-var: "error"
object-curly-newline: ["error", { "consistent": true, "multiline": true }]
object-curly-spacing: ["error", "always"]
object-property-newline: ["error", { "allowMultiplePropertiesPerLine": true }]
object-property-newline: ["error", { "allowAllPropertiesOnSameLine": true }]
object-shorthand: "error"
one-var-declaration-per-line: "error"
operator-assignment: "error"
Expand Down
80 changes: 49 additions & 31 deletions tests/lib/rules/object-property-newline.js
Expand Up @@ -44,24 +44,27 @@ ruleTester.run("object-property-newline", rule, {
{ code: "foo({ k1: 'val1',\nk2: 'val2',\n...{} });", parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } },
{ code: "foo({ ...{} });", parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } },

// allowMultiplePropertiesPerLine: true
{ code: "var obj = { k1: 'val1', k2: 'val2', k3: 'val3' };", options: [{ allowMultiplePropertiesPerLine: true }] },
{ code: "var obj = {\nk1: 'val1', k2: 'val2', k3: 'val3'\n};", options: [{ allowMultiplePropertiesPerLine: true }] },
{ code: "var obj = { k1: 'val1' };", options: [{ allowMultiplePropertiesPerLine: true }] },
{ code: "var obj = {\nk1: 'val1'\n};", options: [{ allowMultiplePropertiesPerLine: true }] },
{ code: "var obj = {};", options: [{ allowMultiplePropertiesPerLine: true }] },
{ code: "var obj = { 'k1': 'val1', k2: 'val2', ...{} };", options: [{ allowMultiplePropertiesPerLine: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } },
{ code: "var obj = {\n'k1': 'val1', k2: 'val2', ...{}\n};", options: [{ allowMultiplePropertiesPerLine: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } },
{ code: "foo({ k1: 'val1', k2: 'val2' });", options: [{ allowMultiplePropertiesPerLine: true }] },
{ code: "foo({\nk1: 'val1', k2: 'val2'\n});", options: [{ allowMultiplePropertiesPerLine: true }] },
{ code: "foo({ a, b });", options: [{ allowMultiplePropertiesPerLine: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "foo({ bar() {}, baz });", options: [{ allowMultiplePropertiesPerLine: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "foo({ [bar]: 'baz', baz })", options: [{ allowMultiplePropertiesPerLine: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "foo({ 'k1': 'val1', k2: 'val2', ...{} });", options: [{ allowMultiplePropertiesPerLine: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } },
{ code: "foo({\n'k1': 'val1', k2: 'val2', ...{}\n});", options: [{ allowMultiplePropertiesPerLine: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } },
{ code: "var obj = {k1: ['foo', 'bar'], k2: 'val1', k3: 'val2'};", options: [{ allowMultiplePropertiesPerLine: true }] },
{ code: "var obj = {\nk1: ['foo', 'bar'], k2: 'val1', k3: 'val2'\n};", options: [{ allowMultiplePropertiesPerLine: true }] },
{ code: "var obj = {\nk1: 'val1', k2: {e1: 'foo', e2: 'bar'}, k3: 'val2'\n};", options: [{ allowMultiplePropertiesPerLine: true }] }
// allowAllPropertiesOnSameLine: true
{ code: "var obj = { k1: 'val1', k2: 'val2', k3: 'val3' };", options: [{ allowAllPropertiesOnSameLine: true }] },
{ code: "var obj = {\nk1: 'val1', k2: 'val2', k3: 'val3'\n};", options: [{ allowAllPropertiesOnSameLine: true }] },
{ code: "var obj = { k1: 'val1' };", options: [{ allowAllPropertiesOnSameLine: true }] },
{ code: "var obj = {\nk1: 'val1'\n};", options: [{ allowAllPropertiesOnSameLine: true }] },
{ code: "var obj = {};", options: [{ allowAllPropertiesOnSameLine: true }] },
{ code: "var obj = { 'k1': 'val1', k2: 'val2', ...{} };", options: [{ allowAllPropertiesOnSameLine: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } },
{ code: "var obj = {\n'k1': 'val1', k2: 'val2', ...{}\n};", options: [{ allowAllPropertiesOnSameLine: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } },
{ code: "foo({ k1: 'val1', k2: 'val2' });", options: [{ allowAllPropertiesOnSameLine: true }] },
{ code: "foo({\nk1: 'val1', k2: 'val2'\n});", options: [{ allowAllPropertiesOnSameLine: true }] },
{ code: "foo({ a, b });", options: [{ allowAllPropertiesOnSameLine: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "foo({ bar() {}, baz });", options: [{ allowAllPropertiesOnSameLine: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "foo({ [bar]: 'baz', baz })", options: [{ allowAllPropertiesOnSameLine: true }], parserOptions: { ecmaVersion: 6 } },
{ code: "foo({ 'k1': 'val1', k2: 'val2', ...{} });", options: [{ allowAllPropertiesOnSameLine: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } },
{ code: "foo({\n'k1': 'val1', k2: 'val2', ...{}\n});", options: [{ allowAllPropertiesOnSameLine: true }], parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } } },
{ code: "var obj = {k1: ['foo', 'bar'], k2: 'val1', k3: 'val2'};", options: [{ allowAllPropertiesOnSameLine: true }] },
{ code: "var obj = {\nk1: ['foo', 'bar'], k2: 'val1', k3: 'val2'\n};", options: [{ allowAllPropertiesOnSameLine: true }] },
{ code: "var obj = {\nk1: 'val1', k2: {e1: 'foo', e2: 'bar'}, k3: 'val2'\n};", options: [{ allowAllPropertiesOnSameLine: true }] },

// allowMultiplePropertiesPerLine: true (deprecated)
{ code: "var obj = { k1: 'val1', k2: 'val2', k3: 'val3' };", options: [{ allowMultiplePropertiesPerLine: true }] }
],

invalid: [
Expand Down Expand Up @@ -378,11 +381,11 @@ ruleTester.run("object-property-newline", rule, {
]
},

// allowMultiplePropertiesPerLine: true
// allowAllPropertiesOnSameLine: true
{
code: "var obj = {\nk1: 'val1',\nk2: 'val2', k3: 'val3'\n};",
output: "var obj = {\nk1: 'val1',\nk2: 'val2',\nk3: 'val3'\n};",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
errors: [
{
message: "Object properties must go on a new line if they aren't all on the same line.",
Expand All @@ -395,7 +398,7 @@ ruleTester.run("object-property-newline", rule, {
{
code: "var obj = {\nk1:\n'val1', k2: 'val2', k3:\n'val3'\n};",
output: "var obj = {\nk1:\n'val1',\nk2: 'val2',\nk3:\n'val3'\n};",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
errors: [
{
message: "Object properties must go on a new line if they aren't all on the same line.",
Expand All @@ -414,7 +417,7 @@ ruleTester.run("object-property-newline", rule, {
{
code: "var obj = {k1: [\n'foo',\n'bar'\n], k2: 'val1'};",
output: "var obj = {k1: [\n'foo',\n'bar'\n],\nk2: 'val1'};",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
errors: [
{
message: "Object properties must go on a new line if they aren't all on the same line.",
Expand All @@ -427,7 +430,7 @@ ruleTester.run("object-property-newline", rule, {
{
code: "var obj = {k1: [\n'foo', 'bar'\n], k2: 'val1'};",
output: "var obj = {k1: [\n'foo', 'bar'\n],\nk2: 'val1'};",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
errors: [
{
message: "Object properties must go on a new line if they aren't all on the same line.",
Expand All @@ -440,7 +443,7 @@ ruleTester.run("object-property-newline", rule, {
{
code: "var obj = {\nk1: 'val1', k2: {\ne1: 'foo', e2: 'bar'\n}, k3: 'val2'\n};",
output: "var obj = {\nk1: 'val1',\nk2: {\ne1: 'foo', e2: 'bar'\n},\nk3: 'val2'\n};",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
errors: [
{
message: "Object properties must go on a new line if they aren't all on the same line.",
Expand All @@ -459,7 +462,7 @@ ruleTester.run("object-property-newline", rule, {
{
code: "var obj = { k1: 'val1',\nk2: [\n'val2a', 'val2b', 'val2c'\n], k3: 'val3' };",
output: "var obj = { k1: 'val1',\nk2: [\n'val2a', 'val2b', 'val2c'\n],\nk3: 'val3' };",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
errors: [
{
message: "Object properties must go on a new line if they aren't all on the same line.",
Expand All @@ -472,7 +475,7 @@ ruleTester.run("object-property-newline", rule, {
{
code: "var obj = { [\nk1]: 'val1', k2: 'val2' };",
output: "var obj = { [\nk1]: 'val1',\nk2: 'val2' };",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
parserOptions: { ecmaVersion: 6 },
errors: [
{
Expand All @@ -486,7 +489,7 @@ ruleTester.run("object-property-newline", rule, {
{
code: "var obj = {\nk1: 'val1',\nk2: 'val2', ...{}\n};",
output: "var obj = {\nk1: 'val1',\nk2: 'val2',\n...{}\n};",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } },
errors: [
{
Expand All @@ -500,7 +503,7 @@ ruleTester.run("object-property-newline", rule, {
{
code: "var obj = {\n...{},\nk1: 'val1', k2: 'val2'\n};",
output: "var obj = {\n...{},\nk1: 'val1',\nk2: 'val2'\n};",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } },
errors: [
{
Expand All @@ -514,7 +517,7 @@ ruleTester.run("object-property-newline", rule, {
{
code: "foo({ [\nk1]: 'val1', k2: 'val2' })",
output: "foo({ [\nk1]: 'val1',\nk2: 'val2' })",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
parserOptions: { ecmaVersion: 6 },
errors: [
{
Expand All @@ -528,7 +531,7 @@ ruleTester.run("object-property-newline", rule, {
{
code: "foo({\nk1: 'val1',\nk2: 'val2', ...{}\n})",
output: "foo({\nk1: 'val1',\nk2: 'val2',\n...{}\n})",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } },
errors: [
{
Expand All @@ -542,7 +545,7 @@ ruleTester.run("object-property-newline", rule, {
{
code: "foo({\n...{},\nk1: 'val1', k2: 'val2'\n})",
output: "foo({\n...{},\nk1: 'val1',\nk2: 'val2'\n})",
options: [{ allowMultiplePropertiesPerLine: true }],
options: [{ allowAllPropertiesOnSameLine: true }],
parserOptions: { ecmaVersion: 6, ecmaFeatures: { experimentalObjectRestSpread: true } },
errors: [
{
Expand All @@ -552,6 +555,21 @@ ruleTester.run("object-property-newline", rule, {
column: 13
}
]
},

// allowMultiplePropertiesPerLine: true (deprecated)
{
code: "var obj = {\nk1: 'val1',\nk2: 'val2', k3: 'val3'\n};",
output: "var obj = {\nk1: 'val1',\nk2: 'val2',\nk3: 'val3'\n};",
options: [{ allowMultiplePropertiesPerLine: true }],
errors: [
{
message: "Object properties must go on a new line if they aren't all on the same line.",
type: "ObjectExpression",
line: 3,
column: 13
}
]
}
]
});

0 comments on commit 2cf4522

Please sign in to comment.