diff --git a/lib/rules/max-attributes-per-line.js b/lib/rules/max-attributes-per-line.js index 9a54fd1e0..0d05f0bea 100644 --- a/lib/rules/max-attributes-per-line.js +++ b/lib/rules/max-attributes-per-line.js @@ -16,7 +16,7 @@ module.exports = { category: 'strongly-recommended', url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.2.2/docs/rules/max-attributes-per-line.md' }, - fixable: null, + fixable: 'whitespace', // or "code" or "whitespace" schema: [ { type: 'object', @@ -129,14 +129,15 @@ module.exports = { } function showErrors (attributes, node) { - attributes.forEach((prop) => { + attributes.forEach((prop, i) => { context.report({ node: prop, loc: prop.loc, message: 'Attribute "{{propName}}" should be on a new line.', data: { propName: prop.key.name - } + }, + fix: i === 0 ? (fixer) => fixer.insertTextBefore(prop, '\n') : undefined }) }) } diff --git a/tests/lib/rules/max-attributes-per-line.js b/tests/lib/rules/max-attributes-per-line.js index 773f1b661..0e8ab915f 100644 --- a/tests/lib/rules/max-attributes-per-line.js +++ b/tests/lib/rules/max-attributes-per-line.js @@ -91,6 +91,8 @@ ruleTester.run('max-attributes-per-line', rule, { invalid: [ { code: ``, + output: ``, errors: ['Attribute "age" should be on a new line.'] }, { @@ -99,6 +101,12 @@ ruleTester.run('max-attributes-per-line', rule, { age="30"> `, + output: ``, errors: [{ message: 'Attribute "job" should be on a new line.', type: 'VAttribute', @@ -108,6 +116,8 @@ ruleTester.run('max-attributes-per-line', rule, { { code: ``, options: [{ singleline: { max: 2 }}], + output: ``, errors: [{ message: 'Attribute "job" should be on a new line.', type: 'VAttribute', @@ -117,6 +127,8 @@ ruleTester.run('max-attributes-per-line', rule, { { code: ``, options: [{ singleline: 1, multiline: { max: 1, allowFirstLine: false }}], + output: ``, errors: [{ message: 'Attribute "age" should be on a new line.', type: 'VAttribute', @@ -133,6 +145,11 @@ ruleTester.run('max-attributes-per-line', rule, { `, options: [{ singleline: 3, multiline: { max: 1, allowFirstLine: false }}], + output: ``, errors: [{ message: 'Attribute "name" should be on a new line.', type: 'VAttribute', @@ -146,6 +163,12 @@ ruleTester.run('max-attributes-per-line', rule, { `, options: [{ singleline: 3, multiline: { max: 1, allowFirstLine: false }}], + output: ``, errors: [{ message: 'Attribute "age" should be on a new line.', type: 'VAttribute', @@ -159,6 +182,12 @@ ruleTester.run('max-attributes-per-line', rule, { `, options: [{ singleline: 3, multiline: 1 }], + output: ``, errors: [{ message: 'Attribute "age" should be on a new line.', type: 'VAttribute', @@ -172,6 +201,12 @@ ruleTester.run('max-attributes-per-line', rule, { `, options: [{ singleline: 3, multiline: { max: 2, allowFirstLine: false }}], + output: ``, errors: [{ message: 'Attribute "petname" should be on a new line.', type: 'VAttribute', @@ -185,6 +220,12 @@ ruleTester.run('max-attributes-per-line', rule, { `, options: [{ singleline: 3, multiline: { max: 2, allowFirstLine: false }}], + output: ``, errors: [{ message: 'Attribute "petname" should be on a new line.', type: 'VAttribute',