Skip to content

Commit f0c8b37

Browse files
ota-meshimichalsnik
authored andcommitted
Fix: Made not to conflict when html-self-closing and component-name-in-template-casing are autofix (#555)
* fixed #554 * Deleted useless test case.
1 parent 117ffc6 commit f0c8b37

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

lib/rules/html-self-closing.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,11 @@ module.exports = {
165165
if (elementType === ELEMENT_TYPE.VOID) {
166166
return fixer.replaceText(close, '>')
167167
}
168-
return fixer.replaceText(close, `></${node.rawName}>`)
168+
// If only `close` is targeted for replacement, it conflicts with `component-name-in-template-casing`,
169+
// so replace the entire element.
170+
// return fixer.replaceText(close, `></${node.rawName}>`)
171+
const elementPart = sourceCode.text.slice(node.range[0], close.range[0])
172+
return fixer.replaceText(node, elementPart + `></${node.rawName}>`)
169173
}
170174
})
171175
}

tests/lib/autofix.js

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @author Yosuke Ota <https://github.com/ota-meshi>
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
'use strict'
6+
7+
const Linter = require('eslint').Linter
8+
const chai = require('chai')
9+
10+
const rules = require('../..').rules
11+
12+
const assert = chai.assert
13+
14+
const baseConfig = {
15+
parser: 'vue-eslint-parser',
16+
parserOptions: {
17+
ecmaVersion: 2018,
18+
sourceType: 'module',
19+
ecmaFeatures: {
20+
jsx: true
21+
}
22+
}
23+
}
24+
25+
describe('Complex autofix test cases', () => {
26+
const linter = new Linter()
27+
for (const key of Object.keys(rules)) {
28+
const ruleId = `vue/${key}`
29+
linter.defineRule(ruleId, rules[key])
30+
}
31+
32+
// https://github.com/vuejs/eslint-plugin-vue/issues/554
33+
describe('Autofix of `html-self-closing` and `component-name-in-template-casing` should not conflict.', () => {
34+
const kebabConfig = Object.assign({}, baseConfig, { 'rules': {
35+
'vue/html-self-closing': ['error', {
36+
'html': {
37+
'component': 'never'
38+
}
39+
}],
40+
'vue/component-name-in-template-casing': ['error', 'kebab-case']
41+
}})
42+
const pascalConfig = Object.assign({}, baseConfig, { 'rules': {
43+
'vue/html-self-closing': ['error', {
44+
'html': {
45+
'component': 'never'
46+
}
47+
}],
48+
'vue/component-name-in-template-casing': ['error']
49+
}})
50+
it('Even if set kebab-case, the output should be as expected.', () => {
51+
const code = `
52+
<template>
53+
<VueComponent />
54+
</template>`
55+
const output = `
56+
<template>
57+
<vue-component ></vue-component>
58+
</template>`
59+
60+
assert.equal(
61+
linter.verifyAndFix(code, kebabConfig, 'test.vue').output,
62+
output
63+
)
64+
})
65+
it('Even if set PascalCase, the output should be as expected.', () => {
66+
const code = `
67+
<template>
68+
<vue-component />
69+
</template>`
70+
const output = `
71+
<template>
72+
<VueComponent ></VueComponent>
73+
</template>`
74+
75+
assert.equal(
76+
linter.verifyAndFix(code, pascalConfig, 'test.vue').output,
77+
output
78+
)
79+
})
80+
it('Even if element have an attributes, the output should be as expected.', () => {
81+
const code = `
82+
<template>
83+
<vue-component attr
84+
id="item1" />
85+
</template>`
86+
const output = `
87+
<template>
88+
<VueComponent attr
89+
id="item1" ></VueComponent>
90+
</template>`
91+
92+
assert.equal(
93+
linter.verifyAndFix(code, pascalConfig, 'test.vue').output,
94+
output
95+
)
96+
})
97+
})
98+
})

0 commit comments

Comments
 (0)