From eda37413ecb95b12f59726b758aee5cba12ee986 Mon Sep 17 00:00:00 2001 From: ohta2346 Date: Tue, 6 Feb 2018 14:08:21 +0900 Subject: [PATCH 1/5] [Update] Make `vue/max-attributes-per-line` fixable --- lib/rules/max-attributes-per-line.js | 31 +++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/rules/max-attributes-per-line.js b/lib/rules/max-attributes-per-line.js index 9a54fd1e0..f91cf48c2 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', @@ -70,6 +70,7 @@ module.exports = { const multilineMaximum = configuration.multiline const singlelinemMaximum = configuration.singleline const canHaveFirstLine = configuration.allowFirstLine + const sourceCode = context.getSourceCode() return utils.defineTemplateBodyVisitor(context, { 'VStartTag' (node) { @@ -129,14 +130,24 @@ 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) => { + const indent = getIndentText(prop) + const last = indent[indent.length - 1]; + if (indent[indent.length - 1] === '\t') { + indent += '\t'; + } else { + indent += last + last; + } + return fixer.insertTextBefore(prop, `\n${indent}`) + } : undefined }) }) } @@ -155,5 +166,19 @@ module.exports = { return propsPerLine } + function getIndentText (node) { + const text = sourceCode.text + let indentStart = node.range[0] - 1 + while (indentStart >= 0 && !LT_CHAR.test(text[indentStart])) { + indentStart -= 1 + } + let indentEnd = indentStart + 1 + + while (!text[indentEnd].trim()) { + indentEnd += 1 + } + + return text.slice(indentStart + 1, indentEnd) + } } } From a80ce333779783a3ca087ec6302e691075ba85f3 Mon Sep 17 00:00:00 2001 From: ohta2346 Date: Tue, 6 Feb 2018 14:11:59 +0900 Subject: [PATCH 2/5] [fix] bug and style --- lib/rules/max-attributes-per-line.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/rules/max-attributes-per-line.js b/lib/rules/max-attributes-per-line.js index f91cf48c2..84e3298f1 100644 --- a/lib/rules/max-attributes-per-line.js +++ b/lib/rules/max-attributes-per-line.js @@ -9,6 +9,8 @@ // ------------------------------------------------------------------------------ const utils = require('../utils') +const LT_CHAR = /[\r\n\u2028\u2029]/ + module.exports = { meta: { docs: { @@ -139,12 +141,12 @@ module.exports = { propName: prop.key.name }, fix: i === 0 ? (fixer) => { - const indent = getIndentText(prop) - const last = indent[indent.length - 1]; + let indent = getIndentText(prop) + const last = indent[indent.length - 1] if (indent[indent.length - 1] === '\t') { - indent += '\t'; + indent += '\t' } else { - indent += last + last; + indent += last + last } return fixer.insertTextBefore(prop, `\n${indent}`) } : undefined @@ -166,6 +168,7 @@ module.exports = { return propsPerLine } + function getIndentText (node) { const text = sourceCode.text let indentStart = node.range[0] - 1 From e324f32724111ad80db6f8d5d903792282d6c374 Mon Sep 17 00:00:00 2001 From: ohta2346 Date: Tue, 6 Feb 2018 14:59:04 +0900 Subject: [PATCH 3/5] [fix] Switch indent calculation method with node and attribute --- lib/rules/max-attributes-per-line.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/rules/max-attributes-per-line.js b/lib/rules/max-attributes-per-line.js index 84e3298f1..706a7a463 100644 --- a/lib/rules/max-attributes-per-line.js +++ b/lib/rules/max-attributes-per-line.js @@ -142,11 +142,13 @@ module.exports = { }, fix: i === 0 ? (fixer) => { let indent = getIndentText(prop) - const last = indent[indent.length - 1] - if (indent[indent.length - 1] === '\t') { - indent += '\t' - } else { - indent += last + last + if (prop.loc.start.line === node.loc.start.line) { + const last = indent[indent.length - 1] + if (indent[indent.length - 1] === '\t') { + indent += '\t' + } else { + indent += last + last + } } return fixer.insertTextBefore(prop, `\n${indent}`) } : undefined From 50b34eccf72045ffe507fdd2332c820b23038775 Mon Sep 17 00:00:00 2001 From: ohta2346 Date: Tue, 6 Feb 2018 15:05:18 +0900 Subject: [PATCH 4/5] [fix] don't handle indentation --- lib/rules/max-attributes-per-line.js | 31 +--------------------------- 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/lib/rules/max-attributes-per-line.js b/lib/rules/max-attributes-per-line.js index 706a7a463..0d05f0bea 100644 --- a/lib/rules/max-attributes-per-line.js +++ b/lib/rules/max-attributes-per-line.js @@ -9,8 +9,6 @@ // ------------------------------------------------------------------------------ const utils = require('../utils') -const LT_CHAR = /[\r\n\u2028\u2029]/ - module.exports = { meta: { docs: { @@ -72,7 +70,6 @@ module.exports = { const multilineMaximum = configuration.multiline const singlelinemMaximum = configuration.singleline const canHaveFirstLine = configuration.allowFirstLine - const sourceCode = context.getSourceCode() return utils.defineTemplateBodyVisitor(context, { 'VStartTag' (node) { @@ -140,18 +137,7 @@ module.exports = { data: { propName: prop.key.name }, - fix: i === 0 ? (fixer) => { - let indent = getIndentText(prop) - if (prop.loc.start.line === node.loc.start.line) { - const last = indent[indent.length - 1] - if (indent[indent.length - 1] === '\t') { - indent += '\t' - } else { - indent += last + last - } - } - return fixer.insertTextBefore(prop, `\n${indent}`) - } : undefined + fix: i === 0 ? (fixer) => fixer.insertTextBefore(prop, '\n') : undefined }) }) } @@ -170,20 +156,5 @@ module.exports = { return propsPerLine } - - function getIndentText (node) { - const text = sourceCode.text - let indentStart = node.range[0] - 1 - while (indentStart >= 0 && !LT_CHAR.test(text[indentStart])) { - indentStart -= 1 - } - let indentEnd = indentStart + 1 - - while (!text[indentEnd].trim()) { - indentEnd += 1 - } - - return text.slice(indentStart + 1, indentEnd) - } } } From 3f7b1c5c590c8e6d9c70e8f6131fb201821412ca Mon Sep 17 00:00:00 2001 From: ohta2346 Date: Tue, 6 Feb 2018 15:18:18 +0900 Subject: [PATCH 5/5] [add] autofix test max-attributes-per-line.js --- tests/lib/rules/max-attributes-per-line.js | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) 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',