Skip to content

Commit 742bfd6

Browse files
ota-meshimichalsnik
authored andcommitted
Update: Make vue/max-attributes-per-line fixable (#380)
* [Update] Make `vue/max-attributes-per-line` fixable * [fix] bug and style * [fix] Switch indent calculation method with node and attribute * [fix] don't handle indentation * [add] autofix test max-attributes-per-line.js
1 parent 5eaffdf commit 742bfd6

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

lib/rules/max-attributes-per-line.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = {
1616
category: 'strongly-recommended',
1717
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.2.2/docs/rules/max-attributes-per-line.md'
1818
},
19-
fixable: null,
19+
fixable: 'whitespace', // or "code" or "whitespace"
2020
schema: [
2121
{
2222
type: 'object',
@@ -129,14 +129,15 @@ module.exports = {
129129
}
130130

131131
function showErrors (attributes, node) {
132-
attributes.forEach((prop) => {
132+
attributes.forEach((prop, i) => {
133133
context.report({
134134
node: prop,
135135
loc: prop.loc,
136136
message: 'Attribute "{{propName}}" should be on a new line.',
137137
data: {
138138
propName: prop.key.name
139-
}
139+
},
140+
fix: i === 0 ? (fixer) => fixer.insertTextBefore(prop, '\n') : undefined
140141
})
141142
})
142143
}

tests/lib/rules/max-attributes-per-line.js

+41
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ ruleTester.run('max-attributes-per-line', rule, {
9191
invalid: [
9292
{
9393
code: `<template><component name="John Doe" age="30"></component></template>`,
94+
output: `<template><component name="John Doe"
95+
age="30"></component></template>`,
9496
errors: ['Attribute "age" should be on a new line.']
9597
},
9698
{
@@ -99,6 +101,12 @@ ruleTester.run('max-attributes-per-line', rule, {
99101
age="30">
100102
</component>
101103
</template>`,
104+
output: `<template><component
105+
job="Vet"
106+
name="John Doe"
107+
age="30">
108+
</component>
109+
</template>`,
102110
errors: [{
103111
message: 'Attribute "job" should be on a new line.',
104112
type: 'VAttribute',
@@ -108,6 +116,8 @@ ruleTester.run('max-attributes-per-line', rule, {
108116
{
109117
code: `<template><component name="John Doe" age="30" job="Vet"></component></template>`,
110118
options: [{ singleline: { max: 2 }}],
119+
output: `<template><component name="John Doe" age="30"
120+
job="Vet"></component></template>`,
111121
errors: [{
112122
message: 'Attribute "job" should be on a new line.',
113123
type: 'VAttribute',
@@ -117,6 +127,8 @@ ruleTester.run('max-attributes-per-line', rule, {
117127
{
118128
code: `<template><component name="John Doe" age="30" job="Vet"></component></template>`,
119129
options: [{ singleline: 1, multiline: { max: 1, allowFirstLine: false }}],
130+
output: `<template><component name="John Doe"
131+
age="30" job="Vet"></component></template>`,
120132
errors: [{
121133
message: 'Attribute "age" should be on a new line.',
122134
type: 'VAttribute',
@@ -133,6 +145,11 @@ ruleTester.run('max-attributes-per-line', rule, {
133145
</component>
134146
</template>`,
135147
options: [{ singleline: 3, multiline: { max: 1, allowFirstLine: false }}],
148+
output: `<template><component
149+
name="John Doe"
150+
age="30">
151+
</component>
152+
</template>`,
136153
errors: [{
137154
message: 'Attribute "name" should be on a new line.',
138155
type: 'VAttribute',
@@ -146,6 +163,12 @@ ruleTester.run('max-attributes-per-line', rule, {
146163
</component>
147164
</template>`,
148165
options: [{ singleline: 3, multiline: { max: 1, allowFirstLine: false }}],
166+
output: `<template><component
167+
name="John Doe"
168+
age="30"
169+
job="Vet">
170+
</component>
171+
</template>`,
149172
errors: [{
150173
message: 'Attribute "age" should be on a new line.',
151174
type: 'VAttribute',
@@ -159,6 +182,12 @@ ruleTester.run('max-attributes-per-line', rule, {
159182
</component>
160183
</template>`,
161184
options: [{ singleline: 3, multiline: 1 }],
185+
output: `<template><component
186+
name="John Doe"
187+
age="30"
188+
job="Vet">
189+
</component>
190+
</template>`,
162191
errors: [{
163192
message: 'Attribute "age" should be on a new line.',
164193
type: 'VAttribute',
@@ -172,6 +201,12 @@ ruleTester.run('max-attributes-per-line', rule, {
172201
</component>
173202
</template>`,
174203
options: [{ singleline: 3, multiline: { max: 2, allowFirstLine: false }}],
204+
output: `<template><component
205+
name="John Doe" age="30"
206+
job="Vet" pet="dog"
207+
petname="Snoopy">
208+
</component>
209+
</template>`,
175210
errors: [{
176211
message: 'Attribute "petname" should be on a new line.',
177212
type: 'VAttribute',
@@ -185,6 +220,12 @@ ruleTester.run('max-attributes-per-line', rule, {
185220
</component>
186221
</template>`,
187222
options: [{ singleline: 3, multiline: { max: 2, allowFirstLine: false }}],
223+
output: `<template><component
224+
name="John Doe" age="30"
225+
job="Vet" pet="dog"
226+
petname="Snoopy" extra="foo">
227+
</component>
228+
</template>`,
188229
errors: [{
189230
message: 'Attribute "petname" should be on a new line.',
190231
type: 'VAttribute',

0 commit comments

Comments
 (0)