Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Made not to conflict when html-self-closing and component-name-in-template-casing are autofix #555

Merged
merged 2 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/rules/html-self-closing.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ module.exports = {
if (elementType === ELEMENT_TYPE.VOID) {
return fixer.replaceText(close, '>')
}
return fixer.replaceText(close, `></${node.rawName}>`)
// If only `close` is targeted for replacement, it conflicts with `component-name-in-template-casing`,
// so replace the entire element.
// return fixer.replaceText(close, `></${node.rawName}>`)
const elementPart = sourceCode.text.slice(node.range[0], close.range[0])
return fixer.replaceText(node, elementPart + `></${node.rawName}>`)
}
})
}
Expand Down
98 changes: 98 additions & 0 deletions tests/lib/autofix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* @author Yosuke Ota <https://github.com/ota-meshi>
* See LICENSE file in root directory for full license.
*/
'use strict'

const Linter = require('eslint').Linter
const chai = require('chai')

const rules = require('../..').rules

const assert = chai.assert

const baseConfig = {
parser: 'vue-eslint-parser',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true
}
}
}

describe('Complex autofix test cases', () => {
const linter = new Linter()
for (const key of Object.keys(rules)) {
const ruleId = `vue/${key}`
linter.defineRule(ruleId, rules[key])
}

// 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': {
'vue/html-self-closing': ['error', {
'html': {
'component': 'never'
}
}],
'vue/component-name-in-template-casing': ['error', 'kebab-case']
}})
const pascalConfig = Object.assign({}, baseConfig, { 'rules': {
'vue/html-self-closing': ['error', {
'html': {
'component': 'never'
}
}],
'vue/component-name-in-template-casing': ['error']
}})
it('Even if set kebab-case, the output should be as expected.', () => {
const code = `
<template>
<VueComponent />
</template>`
const output = `
<template>
<vue-component ></vue-component>
</template>`

assert.equal(
linter.verifyAndFix(code, kebabConfig, 'test.vue').output,
output
)
})
it('Even if set PascalCase, the output should be as expected.', () => {
const code = `
<template>
<vue-component />
</template>`
const output = `
<template>
<VueComponent ></VueComponent>
</template>`

assert.equal(
linter.verifyAndFix(code, pascalConfig, 'test.vue').output,
output
)
})
it('Even if element have an attributes, the output should be as expected.', () => {
const code = `
<template>
<vue-component attr
id="item1" />
</template>`
const output = `
<template>
<VueComponent attr
id="item1" ></VueComponent>
</template>`

assert.equal(
linter.verifyAndFix(code, pascalConfig, 'test.vue').output,
output
)
})
})
})