Skip to content

Commit

Permalink
Rename html-attributes-casing -> attribute-hyphenation
Browse files Browse the repository at this point in the history
Update implementation
  • Loading branch information
armano2 committed Aug 1, 2017
1 parent 2466553 commit 54cd80e
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 290 deletions.
53 changes: 53 additions & 0 deletions docs/rules/attribute-hyphenation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Define if attributes on cusom components can be hyphened. (attribute-hyphenation)

## :wrench: Options

Default casing is set to `allways`

```
'vue/attribute-hyphenation': [2, 'allways'|'never']
```

### `"always"` - Use hyphenated name. (It errors on upper case letters.)

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

```html
<template>
<foo my-prop="prop">
<a onClick="retrun false"></a>
</foo>
</template>
```

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

```html
<template>
<foo myProp="prop">
<a onClick="retrun false"></a>
</foo>
</template>
```

### `"never"` - Don't use hyphenated name. (It errors on hyphens except `data-` and `aria-`.)

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

```html
<template>
<foo myProp="prop">
<a onClick="retrun false"></a>
</foo>
</template>
```

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

```html
<template>
<foo my-prop="prop">
<a onClick="retrun false"></a>
</foo>
</template>
```
35 changes: 0 additions & 35 deletions docs/rules/html-attributes-casing.md

This file was deleted.

80 changes: 80 additions & 0 deletions lib/rules/attribute-hyphenation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @fileoverview Define a style for the props casing in templates.
* @author Armano
*/
'use strict'

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

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

function create (context) {
const sourceCode = context.getSourceCode()
const options = context.options[0]
const useHyphenated = options !== 'never'

const kebabCase = casing.getConverter('kebab-case')
const caseConverter = casing.getConverter(useHyphenated ? 'kebab-case' : 'camelCase')

function reportIssue (node, name) {
const text = sourceCode.getText(node.key)

context.report({
node: node.key,
loc: node.loc,
message: useHyphenated ? "Attribute '{{text}}' must be hyphenated." : "Attribute '{{text}}' cann't be hyphenated.",
data: {
text
},
fix: fixer => fixer.replaceText(node.key, text.replace(name, caseConverter(name)))
})
}

function isIgnoredAttribute (value) {
if (value.indexOf('data-') !== -1 || value.indexOf('aria-') !== -1) {
return true
}
return useHyphenated ? kebabCase(value) === value : kebabCase(value) !== value
}

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

utils.registerTemplateBodyVisitor(context, {
VAttribute (node) {
if (!utils.isCustomComponent(node.parent.parent)) return

const name = !node.directive ? node.key.name : node.key.name === 'bind' ? node.key.argument : false
if (!name) return

if (isIgnoredAttribute(name)) {
return
}
reportIssue(node, name)
}
})

return {}
}

module.exports = {
meta: {
docs: {
description: 'Define a style for the props casing in templates.',
category: 'Stylistic Issues',
recommended: false
},
fixable: 'code',
schema: [
{
enum: ['always', 'never']
}
]
},

create
}
85 changes: 0 additions & 85 deletions lib/rules/html-attributes-casing.js

This file was deleted.

105 changes: 105 additions & 0 deletions tests/lib/rules/attribute-hyphenation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* @fileoverview Define a style for the props casing in templates.
* @author Armano
*/
'use strict'

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

const rule = require('../../../lib/rules/attribute-hyphenation')

const RuleTester = require('eslint').RuleTester

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

const ruleTester = new RuleTester({
parser: 'vue-eslint-parser',
parserOptions: { ecmaVersion: 2015 }
})

ruleTester.run('attribute-hyphenation', rule, {

valid: [
{
filename: 'test.vue',
code: ''
},
{
filename: 'test.vue',
code: '<template><div><custom my-prop="prop"></custom></div></template>',
options: ['always']
},
{
filename: 'test.vue',
code: '<template><div><a onClick="" my-prop="prop"></a></div></template>',
options: ['never']
}
],

invalid: [
{
filename: 'test.vue',
code: '<template><div><custom my-prop="prop"></custom></div></template>',
options: ['never'],
errors: [{
message: "Attribute 'my-prop' cann't be hyphenated.",
type: 'VIdentifier',
line: 1
}]
},
{
filename: 'test.vue',
code: '<template><div><custom MyProp="prop"></custom></div></template>',
options: ['always'],
errors: [{
message: "Attribute 'MyProp' must be hyphenated.",
type: 'VIdentifier',
line: 1
}]
},
{
filename: 'test.vue',
code: '<template><div><custom :my-prop="prop"></custom></div></template>',
options: ['never'],
errors: [{
message: "Attribute ':my-prop' cann't be hyphenated.",
type: 'VDirectiveKey',
line: 1
}]
},
{
filename: 'test.vue',
code: '<template><div><custom :MyProp="prop"></custom></div></template>',
options: ['always'],
errors: [{
message: "Attribute ':MyProp' must be hyphenated.",
type: 'VDirectiveKey',
line: 1
}]
},
{
filename: 'test.vue',
code: '<template><div><custom v-bind:my-prop="prop"></custom></div></template>',
options: ['never'],
errors: [{
message: "Attribute 'v-bind:my-prop' cann't be hyphenated.",
type: 'VDirectiveKey',
line: 1
}]
},
{
filename: 'test.vue',
code: '<template><div><custom v-bind:MyProp="prop"></custom></div></template>',
options: ['always'],
errors: [{
message: "Attribute 'v-bind:MyProp' must be hyphenated.",
type: 'VDirectiveKey',
line: 1
}]
}
]
})
Loading

0 comments on commit 54cd80e

Please sign in to comment.