-
-
Notifications
You must be signed in to change notification settings - Fork 679
Update attribute-hyphenation to allow a custom ignore list #461
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
Changes from 3 commits
a9ee843
b247a19
639f503
3505bbf
6d18b75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
/.nyc_output | ||
/coverage | ||
/tests/integrations/*/node_modules | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
# enforce attribute naming style in template (vue/attribute-hyphenation) | ||
# enforce attribute naming style on custom components in template (vue/attribute-hyphenation) | ||
|
||
- :gear: This rule is included in `"plugin:vue/strongly-recommended"` and `"plugin:vue/recommended"`. | ||
- :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. | ||
|
||
## :wrench: Options | ||
|
||
Default casing is set to `always` | ||
Default casing is set to `always` with `['data-', 'aria-', 'slot-scope']` set to be ignored | ||
|
||
``` | ||
'vue/attribute-hyphenation': [2, 'always'|'never'] | ||
'vue/attribute-hyphenation': [2, 'always'|'never', { 'ignore': ['data-', 'aria-', 'slot-scope'] }] | ||
``` | ||
|
||
### `"always"` - Use hyphenated name. (It errors on upper case letters.) | ||
### `[2, "always"]` - Use hyphenated name. (It errors on upper case letters.) | ||
|
||
:+1: Examples of **correct** code`: | ||
|
||
|
@@ -25,7 +25,7 @@ Default casing is set to `always` | |
<MyComponent myProp="prop"/> | ||
``` | ||
|
||
### `"never"` - Don't use hyphenated name. (It errors on hyphens except `data-` and `aria-`.) | ||
### `[2, "never"]` - Don't use hyphenated name. (It errors on hyphens except `data-`, `aria-` and `slot-scope-`.) | ||
|
||
:+1: Examples of **correct** code`: | ||
|
||
|
@@ -38,3 +38,17 @@ Default casing is set to `always` | |
```html | ||
<MyComponent my-prop="prop"/> | ||
``` | ||
|
||
### `[2, "never", { 'ignore': ['data-', 'aria-', 'slot-scope', 'custom-prop'] }]` - Don't use hyphenated name but allow custom attributes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether it wouldn't be easier for users to just merge their settings with default ones. I can't imagine a case where you'd want to remote any of the default settings - they are sort of required for this rule to work properly. So I'd just assume these setting are always there and whatever user adds here - will be merged to the default array. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was providing more flexibility for long term use if things change, but I do agree that it's unlikely people will want to overwrite these defaults. I have updated it so it will not overwrite the default array but instead |
||
|
||
:+1: Examples of **correct** code`: | ||
|
||
```html | ||
<MyComponent myProp="prop" custom-prop="foo"/> | ||
``` | ||
|
||
:-1: Examples of **incorrect** code`: | ||
|
||
```html | ||
<MyComponent my-prop="prop" custom-prop="foo"/> | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,14 +22,40 @@ module.exports = { | |
schema: [ | ||
{ | ||
enum: ['always', 'never'] | ||
}, | ||
{ | ||
type: 'object', | ||
properties: { | ||
'ignore': { | ||
type: 'array', | ||
items: { | ||
allOf: [ | ||
{ type: 'string' }, | ||
{ not: { type: 'string', pattern: ':exit$' }}, | ||
{ not: { type: 'string', pattern: '^\\s*$' }} | ||
] | ||
}, | ||
uniqueItems: true, | ||
additionalItems: false | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
|
||
create (context) { | ||
const sourceCode = context.getSourceCode() | ||
const options = context.options[0] | ||
const useHyphenated = options !== 'never' | ||
const option = context.options[0] | ||
const optionsPayload = context.options[1] | ||
const useHyphenated = option !== 'never' | ||
let ignoredAttributes = [] | ||
|
||
if (optionsPayload && optionsPayload.ignore) { | ||
ignoredAttributes = optionsPayload.ignore | ||
} else { | ||
ignoredAttributes = ['data-', 'aria-', 'slot-scope'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can set ignoredAttributes above and remove the whole There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense, I have done this change. |
||
} | ||
|
||
const caseConverter = casing.getConverter(useHyphenated ? 'kebab-case' : 'camelCase') | ||
|
||
|
@@ -48,9 +74,14 @@ module.exports = { | |
} | ||
|
||
function isIgnoredAttribute (value) { | ||
if (value.indexOf('data-') !== -1 || value.indexOf('aria-') !== -1) { | ||
const isIgnored = !ignoredAttributes.every(function (attr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps you can use
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That does make more sense, thanks. |
||
return value.indexOf(attr) === -1 | ||
}) | ||
|
||
if (isIgnored) { | ||
return true | ||
} | ||
|
||
return useHyphenated ? value.toLowerCase() === value : !/-/.test(value) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,18 +30,23 @@ ruleTester.run('attribute-hyphenation', rule, { | |
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div><custom data-id="foo" aria-test="bar" my-prop="prop"></custom></div></template>', | ||
code: '<template><div><custom data-id="foo" aria-test="bar" slot-scope="{ data }" my-prop="prop"></custom></div></template>', | ||
options: ['always'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div><custom data-id="foo" aria-test="bar" myProp="prop"></custom></div></template>', | ||
code: '<template><div><custom data-id="foo" aria-test="bar" slot-scope="{ data }" myProp="prop"></custom></div></template>', | ||
options: ['never'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div data-id="foo" aria-test="bar"><a onClick="" my-prop="prop"></a></div></template>', | ||
code: '<template><div data-id="foo" aria-test="bar" slot-scope="{ data }"><a onClick="" my-prop="prop"></a></div></template>', | ||
options: ['never'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div data-id="foo" aria-test="bar" slot-scope="{ data }" custom-hypen="foo"><a onClick="" my-prop="prop"></a></div></template>', | ||
options: ['never', { 'ignore': ['data-', 'aria-', 'slot-scope', 'custom-hypen'] }] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, one thing you missed. We don't need to pass |
||
} | ||
], | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part is automatically generated based on the rule's metadata informations. If you want to update it please - do so in rule file instead. Otherwise it'll be overwritten during next release.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it just this one line coming from
meta.docs.description
and needs updating? If so I've done it, let me know if there is anything else.