Skip to content

Commit b1e1bb9

Browse files
ota-meshimichalsnik
authored andcommitted
Fix: if no trailing comma, not to leave trailing comma after fixed of order-in-components (#569)
* Fix: if no trailing comma, not to leave trailing comma after fixed of `order-in-components` * use array fixer
1 parent 562fde1 commit b1e1bb9

File tree

3 files changed

+103
-15
lines changed

3 files changed

+103
-15
lines changed

Diff for: lib/rules/order-in-components.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,22 @@ module.exports = {
193193
if (hasSideEffectsPossibility) {
194194
return undefined
195195
}
196-
const comma = sourceCode.getTokenAfter(propertyNode)
197-
const hasAfterComma = isComma(comma)
196+
const afterComma = sourceCode.getTokenAfter(propertyNode)
197+
const hasAfterComma = isComma(afterComma)
198198

199-
const codeStart = sourceCode.getTokenBefore(propertyNode).range[1] // to include comments
200-
const codeEnd = hasAfterComma ? comma.range[1] : propertyNode.range[1]
199+
const beforeComma = sourceCode.getTokenBefore(propertyNode)
200+
const codeStart = beforeComma.range[1] // to include comments
201+
const codeEnd = hasAfterComma ? afterComma.range[1] : propertyNode.range[1]
201202

202203
const propertyCode = sourceCode.text.slice(codeStart, codeEnd) + (hasAfterComma ? '' : ',')
203204
const insertTarget = sourceCode.getTokenBefore(firstUnorderedPropertyNode)
204-
// If we can upgrade requirements to `eslint@>4.1.0`, this code can be replaced by:
205-
// return [
206-
// fixer.removeRange([codeStart, codeEnd]),
207-
// fixer.insertTextAfter(insertTarget, propertyCode)
208-
// ]
209-
const insertStart = insertTarget.range[1]
210-
const newCode = propertyCode + sourceCode.text.slice(insertStart, codeStart)
211-
return fixer.replaceTextRange([insertStart, codeEnd], newCode)
205+
206+
const removeStart = hasAfterComma ? codeStart : beforeComma.range[0]
207+
208+
return [
209+
fixer.removeRange([removeStart, codeEnd]),
210+
fixer.insertTextAfter(insertTarget, propertyCode)
211+
]
212212
}
213213
})
214214
}

Diff for: tests/lib/autofix.js

+62
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,64 @@ describe('Complex autofix test cases', () => {
2929
linter.defineRule(ruleId, rules[key])
3030
}
3131

32+
// https://github.com/vuejs/eslint-plugin-vue/issues/566
33+
describe('Autofix of `vue/order-in-components` and `comma-dangle` should not conflict.', () => {
34+
const config = Object.assign({}, baseConfig, { 'rules': {
35+
'vue/order-in-components': ['error'],
36+
'comma-dangle': ['error', 'always']
37+
}})
38+
39+
it('Even if set comma-dangle:always, the output should be as expected.', () => {
40+
const code = `
41+
<script>
42+
export default {
43+
data() {
44+
},
45+
name: 'burger'
46+
};
47+
</script>`
48+
const output = `
49+
<script>
50+
export default {
51+
name: 'burger',
52+
data() {
53+
},
54+
};
55+
</script>`
56+
assert.equal(
57+
linter.verifyAndFix(code, config, 'test.vue').output,
58+
output
59+
)
60+
})
61+
62+
it('Even if include comments, the output should be as expected.', () => {
63+
const code = `
64+
<script>
65+
export default {
66+
/**data*/
67+
data() {
68+
}/*after data*/,
69+
/**name*/
70+
name: 'burger'/*after name*/
71+
};
72+
</script>`
73+
const output = `
74+
<script>
75+
export default {
76+
/**name*/
77+
name: 'burger',
78+
/**data*/
79+
data() {
80+
},/*after data*//*after name*/
81+
};
82+
</script>`
83+
assert.equal(
84+
linter.verifyAndFix(code, config, 'test.vue').output,
85+
output
86+
)
87+
})
88+
})
89+
3290
// https://github.com/vuejs/eslint-plugin-vue/issues/554
3391
describe('Autofix of `html-self-closing` and `component-name-in-template-casing` should not conflict.', () => {
3492
const kebabConfig = Object.assign({}, baseConfig, { 'rules': {
@@ -39,6 +97,7 @@ describe('Complex autofix test cases', () => {
3997
}],
4098
'vue/component-name-in-template-casing': ['error', 'kebab-case']
4199
}})
100+
42101
const pascalConfig = Object.assign({}, baseConfig, { 'rules': {
43102
'vue/html-self-closing': ['error', {
44103
'html': {
@@ -47,6 +106,7 @@ describe('Complex autofix test cases', () => {
47106
}],
48107
'vue/component-name-in-template-casing': ['error']
49108
}})
109+
50110
it('Even if set kebab-case, the output should be as expected.', () => {
51111
const code = `
52112
<template>
@@ -62,6 +122,7 @@ describe('Complex autofix test cases', () => {
62122
output
63123
)
64124
})
125+
65126
it('Even if set PascalCase, the output should be as expected.', () => {
66127
const code = `
67128
<template>
@@ -77,6 +138,7 @@ describe('Complex autofix test cases', () => {
77138
output
78139
)
79140
})
141+
80142
it('Even if element have an attributes, the output should be as expected.', () => {
81143
const code = `
82144
<template>

Diff for: tests/lib/rules/order-in-components.js

+29-3
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ ruleTester.run('order-in-components', rule, {
374374
data() {
375375
},
376376
test: 'ok',
377-
name: 'burger',
377+
name: 'burger'
378378
};
379379
`,
380380
options: [{ order: ['data', 'test', 'name'] }],
@@ -401,7 +401,33 @@ ruleTester.run('order-in-components', rule, {
401401
name: 'burger',
402402
/** data provider */
403403
data() {
404-
},
404+
}
405+
};
406+
`,
407+
errors: [{
408+
message: 'The "name" property should be above the "data" property on line 4.',
409+
line: 7
410+
}]
411+
},
412+
{
413+
filename: 'example.vue',
414+
code: `
415+
export default {
416+
/** data provider */
417+
data() {
418+
}/*test*/,
419+
/** name of vue component */
420+
name: 'burger'
421+
};
422+
`,
423+
parserOptions,
424+
output: `
425+
export default {
426+
/** name of vue component */
427+
name: 'burger',
428+
/** data provider */
429+
data() {
430+
}/*test*/
405431
};
406432
`,
407433
errors: [{
@@ -413,7 +439,7 @@ ruleTester.run('order-in-components', rule, {
413439
filename: 'example.vue',
414440
code: `export default {data(){},name:'burger'};`,
415441
parserOptions,
416-
output: `export default {name:'burger',data(){},};`,
442+
output: `export default {name:'burger',data(){}};`,
417443
errors: [{
418444
message: 'The "name" property should be above the "data" property on line 1.',
419445
line: 1

0 commit comments

Comments
 (0)