diff --git a/.changeset/tangy-eagles-start.md b/.changeset/tangy-eagles-start.md new file mode 100644 index 000000000..1a8deb530 --- /dev/null +++ b/.changeset/tangy-eagles-start.md @@ -0,0 +1,5 @@ +--- +'eslint-plugin-vue': minor +--- + +[`vue/no-deprecated-slot-attribute`](https://eslint.vuejs.org/rules/no-deprecated-slot-attribute.html) `ignore` option now supports regex patterns. diff --git a/docs/rules/no-deprecated-slot-attribute.md b/docs/rules/no-deprecated-slot-attribute.md index 64f2c5e00..df4575cc4 100644 --- a/docs/rules/no-deprecated-slot-attribute.md +++ b/docs/rules/no-deprecated-slot-attribute.md @@ -48,7 +48,7 @@ This rule reports deprecated `slot` attribute in Vue.js v2.6.0+. } ``` -- `"ignore"` (`string[]`) An array of tags that ignore this rules. This option will check both kebab-case and PascalCase versions of the given tag names. Default is empty. +- `"ignore"` (`string[]`) An array of tags or regular expression patterns (e.g. `/^custom-/`) that ignore these rules. This option will check both kebab-case and PascalCase versions of the given tag names. Default is empty. ### `"ignore": ["my-component"]` diff --git a/lib/rules/syntaxes/slot-attribute.js b/lib/rules/syntaxes/slot-attribute.js index 27087cb37..b77fc6c20 100644 --- a/lib/rules/syntaxes/slot-attribute.js +++ b/lib/rules/syntaxes/slot-attribute.js @@ -5,6 +5,7 @@ 'use strict' const canConvertToVSlot = require('./utils/can-convert-to-v-slot') +const regexp = require('../../utils/regexp') const casing = require('../../utils/casing') module.exports = { @@ -12,9 +13,11 @@ module.exports = { supported: '<3.0.0', /** @param {RuleContext} context @returns {TemplateListener} */ createTemplateBodyVisitor(context) { + /** @type {{ ignore: string[] }} */ const options = context.options[0] || {} - /** @type {Set} */ - const ignore = new Set(options.ignore) + const { ignore = [] } = options + /** @type {RegExp[]} */ + const ignorePatterns = ignore.map(regexp.toRegExp) const sourceCode = context.getSourceCode() const tokenStore = @@ -122,10 +125,16 @@ module.exports = { */ function reportSlot(slotAttr) { const componentName = slotAttr.parent.parent.rawName + const componentNamePascalCase = casing.pascalCase(componentName) + const componentNameKebabCase = casing.kebabCase(componentName) + if ( - ignore.has(componentName) || - ignore.has(casing.pascalCase(componentName)) || - ignore.has(casing.kebabCase(componentName)) + ignorePatterns.some( + (pattern) => + pattern.test(componentName) || + pattern.test(componentNamePascalCase) || + pattern.test(componentNameKebabCase) + ) ) { return } diff --git a/tests/lib/rules/no-deprecated-slot-attribute.js b/tests/lib/rules/no-deprecated-slot-attribute.js index 2fd5fa401..e420788ce 100644 --- a/tests/lib/rules/no-deprecated-slot-attribute.js +++ b/tests/lib/rules/no-deprecated-slot-attribute.js @@ -55,6 +55,18 @@ tester.run('no-deprecated-slot-attribute', rule, { `, options: [{ ignore: ['one', 'two', 'my-component'] }] + }, + { + code: ``, + options: [{ ignore: ['/one/', '/^Two$/i', '/^my-.*/i'] }] } ], invalid: [ @@ -644,6 +656,82 @@ tester.run('no-deprecated-slot-attribute', rule, { ], errors: ['`slot` attributes are deprecated.'] }, + { + code: ` + `, + output: ` + `, + options: [ + { + ignore: ['/one/'] + } + ], + errors: [ + { + message: '`slot` attributes are deprecated.', + line: 7, + endLine: 7, + column: 16, + endColumn: 20 + } + ] + }, + { + code: ` + `, + output: ` + `, + options: [ + { + ignore: ['/^one$/'] + } + ], + errors: [ + { + message: '`slot` attributes are deprecated.', + line: 7, + endLine: 7, + column: 16, + endColumn: 20 + } + ] + }, { code: `