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

⭐️New: Add vue/html-element-name-kebab-casing #501

Closed
Closed
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
34 changes: 34 additions & 0 deletions docs/rules/html-element-name-kebab-casing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# enforce the tag name of the Vue component and HTML element to be `kebab-case` (vue/html-element-name-kebab-casing)

- :wrench: The `--fix` option on the [command line](http://eslint.org/docs/user-guide/command-line-interface#fix) can automatically fix some of the problems reported by this rule.

This rule enforce the tag name of the Vue component, HTML element, and custom element to be `kebab-case`.
It does not target elements of MathML or SVG.


Do not use this rule if you prefer `PascalCase` to the Vue component name.

## :book: Rule Details

:+1: Examples of **correct** code:

```html
<template>
<div />
<vue-component />
<custome-element />
</template>
```

:-1: Examples of **incorrect** code:

```html
<template>
<Div />
<VueComponent />
<vueComponent />
<Vuecomponent />
<Vue-component />
<CustomeElement />
</template>
```
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
'comment-directive': require('./rules/comment-directive'),
'html-closing-bracket-newline': require('./rules/html-closing-bracket-newline'),
'html-closing-bracket-spacing': require('./rules/html-closing-bracket-spacing'),
'html-element-name-kebab-casing': require('./rules/html-element-name-kebab-casing'),
'html-end-tags': require('./rules/html-end-tags'),
'html-indent': require('./rules/html-indent'),
'html-quotes': require('./rules/html-quotes'),
Expand Down
83 changes: 83 additions & 0 deletions lib/rules/html-element-name-kebab-casing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @author Yosuke Ota
* issue https://github.com/vuejs/eslint-plugin-vue/issues/499
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const utils = require('../utils')
const casing = require('../utils/casing')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

const kebabConverter = casing.getConverter('kebab-case')

module.exports = {
meta: {
docs: {
description: 'enforce the tag name of the Vue component and HTML element to be `kebab-case`',
category: undefined,
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.7.0/docs/rules/html-element-name-kebab-casing.md'
},
fixable: 'code',
schema: []
},

create (context) {
const tokens = context.parserServices.getTemplateBodyTokenStore && context.parserServices.getTemplateBodyTokenStore()
const sourceCode = context.getSourceCode()

let hasInvalidEOF = false

return utils.defineTemplateBodyVisitor(context, {
'VElement' (node) {
if (hasInvalidEOF) {
return
}

if (utils.isSvgElementNode(node) || utils.isMathMLElementNode(node)) {
return
}

const name = node.rawName
const casingName = kebabConverter(name)
if (casingName !== name) {
const startTag = node.startTag
const open = tokens.getFirstToken(startTag)

context.report({
node: open,
loc: open.loc,
message: 'Element name "{{name}}" is not kebab-case.',
data: {
name
},
fix: fixer => {
const endTag = node.endTag
if (!endTag) {
return fixer.replaceText(open, `<${casingName}`)
}
const endTagOpen = tokens.getFirstToken(endTag)
// If we can upgrade requirements to `eslint@>4.1.0`, this code can be replaced by:
// return [
// fixer.replaceText(open, `<${casingName}`),
// fixer.replaceText(endTagOpen, `</${casingName}`)
// ]
const code = `<${casingName}${sourceCode.text.slice(open.range[1], endTagOpen.range[0])}</${casingName}`
return fixer.replaceTextRange([open.range[0], endTagOpen.range[1]], code)
}
})
}
}
}, {
Program (node) {
hasInvalidEOF = utils.hasInvalidEOF(node)
}
})
}
}
204 changes: 204 additions & 0 deletions tests/lib/rules/html-element-name-kebab-casing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/**
* @author Yosuke Ota
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const rule = require('../../../lib/rules/html-element-name-kebab-casing')
const RuleTester = require('eslint').RuleTester

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const tester = new RuleTester({
parser: 'vue-eslint-parser'
})

tester.run('html-element-name-kebab-casing', rule, {
valid: [
'<template><div/></template>',
'<template><img></template>',
'<template><the-component/></template>',
'<template><svg><path/></svg></template>',
'<template><svg><clipPath></clipPath></svg></template>',
'<template><math><mspace/></math></template>',
'<template><math><MSPACE/></math></template>',
'<template><div><slot></slot></div></template>',
// Invalid EOF
'<template><TheComponent a=">test</TheComponent></template>',
'<template><TheComponent><!--test</TheComponent></template>'
],
invalid: [
{
code: `
<template>
<TheComponent id="id">
<!-- comment -->
</TheComponent>
</template>
`,
output: `
<template>
<the-component id="id">
<!-- comment -->
</the-component>
</template>
`,
errors: ['Element name "TheComponent" is not kebab-case.']
},
{
code: `
<template>
<TheComponent id="id"/>
</template>
`,
output: `
<template>
<the-component id="id"/>
</template>
`,
errors: ['Element name "TheComponent" is not kebab-case.']
},
{
code: `
<template>
<TheComponent
id="id"/>
</template>
`,
output: `
<template>
<the-component
id="id"/>
</template>
`,
errors: ['Element name "TheComponent" is not kebab-case.']
},
{
code: `
<template>
<TheComponent/>
</template>
`,
output: `
<template>
<the-component/>
</template>
`,
errors: ['Element name "TheComponent" is not kebab-case.']
},
{
code: `
<template>
<TheComponent></TheComponent>
</template>
`,
output: `
<template>
<the-component></the-component>
</template>
`,
errors: ['Element name "TheComponent" is not kebab-case.']
},
{
code: `
<template>
<theComponent/>
</template>
`,
output: `
<template>
<the-component/>
</template>
`,
errors: ['Element name "theComponent" is not kebab-case.']
},
{
code: `
<template>
<The-component/>
</template>
`,
output: `
<template>
<the-component/>
</template>
`,
errors: ['Element name "The-component" is not kebab-case.']
},
{
code: `
<template>
<Thecomponent/>
</template>
`,
output: `
<template>
<thecomponent/>
</template>
`,
errors: ['Element name "Thecomponent" is not kebab-case.']
},
{
code: `
<template>
<TheComponent></TheComponent >
</template>
`,
output: `
<template>
<the-component></the-component >
</template>
`,
errors: ['Element name "TheComponent" is not kebab-case.']
},
{
code: `
<template>
<TheComponent></TheComponent
>
</template>
`,
output: `
<template>
<the-component></the-component
>
</template>
`,
errors: ['Element name "TheComponent" is not kebab-case.']
},
{
code: `
<template>
<TheComponent></TheComponent end-tag-attr="attr" >
</template>
`,
output: `
<template>
<the-component></the-component end-tag-attr="attr" >
</template>
`,
errors: ['Element name "TheComponent" is not kebab-case.']
},
{
code: `
<template>
<Div/><INPUT>
</template>
`,
output: `
<template>
<div/><input>
</template>
`,
errors: [
'Element name "Div" is not kebab-case.',
'Element name "INPUT" is not kebab-case.'
]
}
]
})