diff --git a/lib/rules/order-in-components.js b/lib/rules/order-in-components.js index ffd103ebb..c95294958 100644 --- a/lib/rules/order-in-components.js +++ b/lib/rules/order-in-components.js @@ -193,22 +193,22 @@ module.exports = { if (hasSideEffectsPossibility) { return undefined } - const comma = sourceCode.getTokenAfter(propertyNode) - const hasAfterComma = isComma(comma) + const afterComma = sourceCode.getTokenAfter(propertyNode) + const hasAfterComma = isComma(afterComma) - const codeStart = sourceCode.getTokenBefore(propertyNode).range[1] // to include comments - const codeEnd = hasAfterComma ? comma.range[1] : propertyNode.range[1] + const beforeComma = sourceCode.getTokenBefore(propertyNode) + const codeStart = beforeComma.range[1] // to include comments + const codeEnd = hasAfterComma ? afterComma.range[1] : propertyNode.range[1] const propertyCode = sourceCode.text.slice(codeStart, codeEnd) + (hasAfterComma ? '' : ',') const insertTarget = sourceCode.getTokenBefore(firstUnorderedPropertyNode) - // If we can upgrade requirements to `eslint@>4.1.0`, this code can be replaced by: - // return [ - // fixer.removeRange([codeStart, codeEnd]), - // fixer.insertTextAfter(insertTarget, propertyCode) - // ] - const insertStart = insertTarget.range[1] - const newCode = propertyCode + sourceCode.text.slice(insertStart, codeStart) - return fixer.replaceTextRange([insertStart, codeEnd], newCode) + + const removeStart = hasAfterComma ? codeStart : beforeComma.range[0] + + return [ + fixer.removeRange([removeStart, codeEnd]), + fixer.insertTextAfter(insertTarget, propertyCode) + ] } }) } diff --git a/tests/lib/autofix.js b/tests/lib/autofix.js index 7d1b9604d..5a38dd0e4 100644 --- a/tests/lib/autofix.js +++ b/tests/lib/autofix.js @@ -29,6 +29,64 @@ describe('Complex autofix test cases', () => { linter.defineRule(ruleId, rules[key]) } + // https://github.com/vuejs/eslint-plugin-vue/issues/566 + describe('Autofix of `vue/order-in-components` and `comma-dangle` should not conflict.', () => { + const config = Object.assign({}, baseConfig, { 'rules': { + 'vue/order-in-components': ['error'], + 'comma-dangle': ['error', 'always'] + }}) + + it('Even if set comma-dangle:always, the output should be as expected.', () => { + const code = ` + ` + const output = ` + ` + assert.equal( + linter.verifyAndFix(code, config, 'test.vue').output, + output + ) + }) + + it('Even if include comments, the output should be as expected.', () => { + const code = ` + ` + const output = ` + ` + assert.equal( + linter.verifyAndFix(code, config, 'test.vue').output, + output + ) + }) + }) + // https://github.com/vuejs/eslint-plugin-vue/issues/554 describe('Autofix of `html-self-closing` and `component-name-in-template-casing` should not conflict.', () => { const kebabConfig = Object.assign({}, baseConfig, { 'rules': { @@ -39,6 +97,7 @@ describe('Complex autofix test cases', () => { }], 'vue/component-name-in-template-casing': ['error', 'kebab-case'] }}) + const pascalConfig = Object.assign({}, baseConfig, { 'rules': { 'vue/html-self-closing': ['error', { 'html': { @@ -47,6 +106,7 @@ describe('Complex autofix test cases', () => { }], 'vue/component-name-in-template-casing': ['error'] }}) + it('Even if set kebab-case, the output should be as expected.', () => { const code = `