From a9ee8437e7d344027eb002d980e9b5f765cc95f0 Mon Sep 17 00:00:00 2001 From: Josh Welham Date: Wed, 18 Apr 2018 16:56:42 +0100 Subject: [PATCH 1/5] ignore phpstorm editor config --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a56cde83a..165f76571 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea /.nyc_output /coverage /tests/integrations/*/node_modules From b247a1996ce53d9609633eb039b4b168662b6cbd Mon Sep 17 00:00:00 2001 From: Josh Welham Date: Wed, 18 Apr 2018 16:57:20 +0100 Subject: [PATCH 2/5] updated rule, tests, and markdown to include a custom list for ignoring attributes --- docs/rules/attribute-hyphenation.md | 24 ++++++++++++---- lib/rules/attribute-hyphenation.js | 36 ++++++++++++++++++++++-- tests/lib/rules/attribute-hyphenation.js | 11 ++++++-- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/docs/rules/attribute-hyphenation.md b/docs/rules/attribute-hyphenation.md index e161172fb..0a39e0880 100644 --- a/docs/rules/attribute-hyphenation.md +++ b/docs/rules/attribute-hyphenation.md @@ -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` ``` -### `"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 ``` + +### `[2, "never", { 'ignore': ['data-', 'aria-', 'slot-scope', 'custom-prop'] }]` - Don't use hyphenated name but allow custom attributes + +:+1: Examples of **correct** code`: + +```html + +``` + +:-1: Examples of **incorrect** code`: + +```html + +``` diff --git a/lib/rules/attribute-hyphenation.js b/lib/rules/attribute-hyphenation.js index 8a394d42a..361799848 100644 --- a/lib/rules/attribute-hyphenation.js +++ b/lib/rules/attribute-hyphenation.js @@ -22,14 +22,39 @@ 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, optionsPayload] = context.options + const useHyphenated = option !== 'never' + let ignoredAttributes = [] + + if (optionsPayload && optionsPayload.ignore) { + ignoredAttributes = optionsPayload.ignore + } else { + ignoredAttributes = ['data-', 'aria-', 'slot-scope'] + } const caseConverter = casing.getConverter(useHyphenated ? 'kebab-case' : 'camelCase') @@ -48,9 +73,14 @@ module.exports = { } function isIgnoredAttribute (value) { - if (value.indexOf('data-') !== -1 || value.indexOf('aria-') !== -1) { + const isIgnored = !ignoredAttributes.every(function (attr) { + return value.indexOf(attr) === -1 + }) + + if (isIgnored) { return true } + return useHyphenated ? value.toLowerCase() === value : !/-/.test(value) } diff --git a/tests/lib/rules/attribute-hyphenation.js b/tests/lib/rules/attribute-hyphenation.js index b5dabe4a0..4d3e5e77b 100644 --- a/tests/lib/rules/attribute-hyphenation.js +++ b/tests/lib/rules/attribute-hyphenation.js @@ -30,18 +30,23 @@ ruleTester.run('attribute-hyphenation', rule, { }, { filename: 'test.vue', - code: '', + code: '', options: ['always'] }, { filename: 'test.vue', - code: '', + code: '', options: ['never'] }, { filename: 'test.vue', - code: '', + code: '', options: ['never'] + }, + { + filename: 'test.vue', + code: '', + options: ['never', { 'ignore': ['data-', 'aria-', 'slot-scope', 'custom-hypen'] }] } ], From 639f503d6a360bd426c32fe1a8306657b9b907e6 Mon Sep 17 00:00:00 2001 From: Josh Welham Date: Wed, 18 Apr 2018 17:28:33 +0100 Subject: [PATCH 3/5] dont do es6 destructuring assignment --- lib/rules/attribute-hyphenation.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/rules/attribute-hyphenation.js b/lib/rules/attribute-hyphenation.js index 361799848..6137ee5fe 100644 --- a/lib/rules/attribute-hyphenation.js +++ b/lib/rules/attribute-hyphenation.js @@ -46,7 +46,8 @@ module.exports = { create (context) { const sourceCode = context.getSourceCode() - const [option, optionsPayload] = context.options + const option = context.options[0] + const optionsPayload = context.options[1] const useHyphenated = option !== 'never' let ignoredAttributes = [] From 3505bbf94d8fbd2de26657695da556443d0d6612 Mon Sep 17 00:00:00 2001 From: Josh Welham Date: Thu, 19 Apr 2018 11:04:27 +0100 Subject: [PATCH 4/5] cleaner code and do not overwrite default ignore list --- docs/rules/attribute-hyphenation.md | 8 ++++---- lib/rules/attribute-hyphenation.js | 12 +++++------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/docs/rules/attribute-hyphenation.md b/docs/rules/attribute-hyphenation.md index 0a39e0880..3dfd9f152 100644 --- a/docs/rules/attribute-hyphenation.md +++ b/docs/rules/attribute-hyphenation.md @@ -8,7 +8,7 @@ Default casing is set to `always` with `['data-', 'aria-', 'slot-scope']` set to be ignored ``` -'vue/attribute-hyphenation': [2, 'always'|'never', { 'ignore': ['data-', 'aria-', 'slot-scope'] }] +'vue/attribute-hyphenation': [2, 'always'|'never', { 'ignore': ['custom-prop'] }] ``` ### `[2, "always"]` - Use hyphenated name. (It errors on upper case letters.) @@ -39,16 +39,16 @@ Default casing is set to `always` with `['data-', 'aria-', 'slot-scope']` set to ``` -### `[2, "never", { 'ignore': ['data-', 'aria-', 'slot-scope', 'custom-prop'] }]` - Don't use hyphenated name but allow custom attributes +### `[2, "never", { 'ignore': ['custom-prop'] }]` - Don't use hyphenated name but allow custom attributes :+1: Examples of **correct** code`: ```html - + ``` :-1: Examples of **incorrect** code`: ```html - + ``` diff --git a/lib/rules/attribute-hyphenation.js b/lib/rules/attribute-hyphenation.js index 6137ee5fe..dc577256c 100644 --- a/lib/rules/attribute-hyphenation.js +++ b/lib/rules/attribute-hyphenation.js @@ -14,7 +14,7 @@ const casing = require('../utils/casing') module.exports = { meta: { docs: { - description: 'enforce attribute naming style in template', + description: 'enforce attribute naming style on custom components in template', category: 'strongly-recommended', url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v4.4.0/docs/rules/attribute-hyphenation.md' }, @@ -49,12 +49,10 @@ module.exports = { const option = context.options[0] const optionsPayload = context.options[1] const useHyphenated = option !== 'never' - let ignoredAttributes = [] + const ignoredAttributes = ['data-', 'aria-', 'slot-scope'] if (optionsPayload && optionsPayload.ignore) { - ignoredAttributes = optionsPayload.ignore - } else { - ignoredAttributes = ['data-', 'aria-', 'slot-scope'] + ignoredAttributes.push(optionsPayload.ignore) } const caseConverter = casing.getConverter(useHyphenated ? 'kebab-case' : 'camelCase') @@ -74,8 +72,8 @@ module.exports = { } function isIgnoredAttribute (value) { - const isIgnored = !ignoredAttributes.every(function (attr) { - return value.indexOf(attr) === -1 + const isIgnored = ignoredAttributes.some(function (attr) { + return value.indexOf(attr) !== -1 }) if (isIgnored) { From 6d18b755d11aacf64fc7f079ea847a48b74119f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sajn=C3=B3g?= Date: Sun, 22 Apr 2018 00:14:41 +0800 Subject: [PATCH 5/5] Update attribute-hyphenation.js --- tests/lib/rules/attribute-hyphenation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/rules/attribute-hyphenation.js b/tests/lib/rules/attribute-hyphenation.js index 4d3e5e77b..340bd32d6 100644 --- a/tests/lib/rules/attribute-hyphenation.js +++ b/tests/lib/rules/attribute-hyphenation.js @@ -46,7 +46,7 @@ ruleTester.run('attribute-hyphenation', rule, { { filename: 'test.vue', code: '', - options: ['never', { 'ignore': ['data-', 'aria-', 'slot-scope', 'custom-hypen'] }] + options: ['never', { 'ignore': ['custom-hypen'] }] } ],