Skip to content

Commit c3ec794

Browse files
authored
Add vue/object-property-newline rule (#1193)
1 parent 898740e commit c3ec794

File tree

6 files changed

+130
-0
lines changed

6 files changed

+130
-0
lines changed

docs/rules/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ The following rules extend the rules provided by ESLint itself and apply them to
331331
| [vue/no-restricted-syntax](./no-restricted-syntax.md) | disallow specified syntax | |
332332
| [vue/no-useless-concat](./no-useless-concat.md) | disallow unnecessary concatenation of literals or template literals | |
333333
| [vue/object-curly-spacing](./object-curly-spacing.md) | enforce consistent spacing inside braces | :wrench: |
334+
| [vue/object-property-newline](./object-property-newline.md) | enforce placing object properties on separate lines | :wrench: |
334335
| [vue/prefer-template](./prefer-template.md) | require template literals instead of string concatenation | :wrench: |
335336
| [vue/space-in-parens](./space-in-parens.md) | enforce consistent spacing inside parentheses | :wrench: |
336337
| [vue/space-infix-ops](./space-infix-ops.md) | require spacing around infix operators | :wrench: |

docs/rules/object-property-newline.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/object-property-newline
5+
description: enforce placing object properties on separate lines
6+
---
7+
# vue/object-property-newline
8+
> enforce placing object properties on separate lines
9+
10+
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.
11+
12+
This rule is the same rule as core [object-property-newline] rule but it applies to the expressions in `<template>`.
13+
14+
## :books: Further reading
15+
16+
- [object-property-newline]
17+
18+
[object-property-newline]: https://eslint.org/docs/rules/object-property-newline
19+
20+
## :mag: Implementation
21+
22+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/object-property-newline.js)
23+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/object-property-newline.js)
24+
25+
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/object-property-newline)</sup>

lib/configs/no-layout-rules.js

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module.exports = {
3131
'vue/no-multi-spaces': 'off',
3232
'vue/no-spaces-around-equal-signs-in-attribute': 'off',
3333
'vue/object-curly-spacing': 'off',
34+
'vue/object-property-newline': 'off',
3435
'vue/padding-line-between-blocks': 'off',
3536
'vue/script-indent': 'off',
3637
'vue/singleline-html-element-content-newline': 'off',

lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ module.exports = {
105105
'no-v-model-argument': require('./rules/no-v-model-argument'),
106106
'no-watch-after-await': require('./rules/no-watch-after-await'),
107107
'object-curly-spacing': require('./rules/object-curly-spacing'),
108+
'object-property-newline': require('./rules/object-property-newline'),
108109
'one-component-per-file': require('./rules/one-component-per-file'),
109110
'order-in-components': require('./rules/order-in-components'),
110111
'padding-line-between-blocks': require('./rules/padding-line-between-blocks'),

lib/rules/object-property-newline.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const { wrapCoreRule } = require('../utils')
7+
8+
// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
9+
module.exports = wrapCoreRule(
10+
require('eslint/lib/rules/object-property-newline'),
11+
{ skipDynamicArguments: true }
12+
)
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @author Yosuke Ota
3+
*/
4+
'use strict'
5+
6+
const RuleTester = require('eslint').RuleTester
7+
const rule = require('../../../lib/rules/object-property-newline')
8+
9+
const tester = new RuleTester({
10+
parser: require.resolve('vue-eslint-parser'),
11+
parserOptions: { ecmaVersion: 2020 }
12+
})
13+
14+
tester.run('object-property-newline', rule, {
15+
valid: [
16+
`
17+
<template>
18+
<div :foo="{a: 1,
19+
b: [2, {a: 3,
20+
b: 4}]}" />
21+
</template>
22+
`,
23+
`
24+
<template>
25+
<div :foo="{a: 1,
26+
b: 2}" />
27+
</template>
28+
`,
29+
`
30+
<template>
31+
<div :[{a:1,b:2}]="value" />
32+
</template>
33+
`,
34+
{
35+
code: `
36+
<template>
37+
<div :foo="{a: 1, b: [2, {a: 3, b: 4}]}" />
38+
</template>
39+
`,
40+
options: [{ allowAllPropertiesOnSameLine: true }]
41+
}
42+
],
43+
invalid: [
44+
{
45+
code: `
46+
<template>
47+
<div :foo="{a: 1, b: [2, {a: 3, b: 4}]}" />
48+
</template>
49+
`,
50+
output: `
51+
<template>
52+
<div :foo="{a: 1,
53+
b: [2, {a: 3,
54+
b: 4}]}" />
55+
</template>
56+
`,
57+
errors: [
58+
{
59+
message: 'Object properties must go on a new line.',
60+
line: 3,
61+
column: 27
62+
},
63+
{
64+
message: 'Object properties must go on a new line.',
65+
line: 3,
66+
column: 41
67+
}
68+
]
69+
},
70+
{
71+
code: `
72+
<template>
73+
<div :foo="{a: 1, b: 2,
74+
c: 3}" />
75+
</template>
76+
`,
77+
output: `
78+
<template>
79+
<div :foo="{a: 1,
80+
b: 2,
81+
c: 3}" />
82+
</template>
83+
`,
84+
options: [{ allowAllPropertiesOnSameLine: true }],
85+
errors: [
86+
"Object properties must go on a new line if they aren't all on the same line."
87+
]
88+
}
89+
]
90+
})

0 commit comments

Comments
 (0)