Skip to content

Commit 22d5483

Browse files
committed
feat(no-deprecated-slot-attribute): regex ignore patterns
1 parent ca97301 commit 22d5483

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

docs/rules/no-deprecated-slot-attribute.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ This rule reports deprecated `slot` attribute in Vue.js v2.6.0+.
4848
}
4949
```
5050

51-
- `"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.
51+
- `"ignore"` (`string[]`) An array of tags or regular expression patterns (e.g. `/^custom-/`) that ignore this rules. This option will check both kebab-case and PascalCase versions of the given tag names. Default is empty.
5252

5353
### `"ignore": ["my-component"]`
5454

lib/rules/syntaxes/slot-attribute.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,29 @@
55
'use strict'
66

77
const canConvertToVSlot = require('./utils/can-convert-to-v-slot')
8+
const regexp = require('../../utils/regexp')
89
const casing = require('../../utils/casing')
910

1011
module.exports = {
1112
deprecated: '2.6.0',
1213
supported: '<3.0.0',
1314
/** @param {RuleContext} context @returns {TemplateListener} */
1415
createTemplateBodyVisitor(context) {
16+
/** @type {{ ignore: string[]}} */
1517
const options = context.options[0] || {}
18+
const { ignore = [] } = options
1619
/** @type {Set<string>} */
17-
const ignore = new Set(options.ignore)
20+
const ignoreStrings = new Set()
21+
/** @type {RegExp[]} */
22+
const ignorePatterns = []
23+
24+
for (const str of ignore) {
25+
if (regexp.isRegExp(str)) {
26+
ignorePatterns.push(regexp.toRegExp(str))
27+
} else {
28+
ignoreStrings.add(str)
29+
}
30+
}
1831

1932
const sourceCode = context.getSourceCode()
2033
const tokenStore =
@@ -123,12 +136,15 @@ module.exports = {
123136
function reportSlot(slotAttr) {
124137
const componentName = slotAttr.parent.parent.rawName
125138
if (
126-
ignore.has(componentName) ||
127-
ignore.has(casing.pascalCase(componentName)) ||
128-
ignore.has(casing.kebabCase(componentName))
139+
ignoreStrings.has(componentName) ||
140+
ignoreStrings.has(casing.pascalCase(componentName)) ||
141+
ignoreStrings.has(casing.kebabCase(componentName))
129142
) {
130143
return
131144
}
145+
if (ignorePatterns.some((pattern) => pattern.test(componentName))) {
146+
return
147+
}
132148

133149
context.report({
134150
node: slotAttr.key,

tests/lib/rules/no-deprecated-slot-attribute.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ tester.run('no-deprecated-slot-attribute', rule, {
5555
</LinkList>
5656
</template>`,
5757
options: [{ ignore: ['one', 'two', 'my-component'] }]
58+
},
59+
{
60+
code: `<template>
61+
<LinkList>
62+
<one slot="one" />
63+
<two slot="two" />
64+
<my-component slot="my-component-slot" />
65+
<myComponent slot="myComponent-slot" />
66+
<MyComponent slot="MyComponent-slot" />
67+
</LinkList>
68+
</template>`,
69+
options: [{ ignore: ['/one/', '/^Two$/i', '/^my-.*/i'] }]
5870
}
5971
],
6072
invalid: [
@@ -644,6 +656,66 @@ tester.run('no-deprecated-slot-attribute', rule, {
644656
],
645657
errors: ['`slot` attributes are deprecated.']
646658
},
659+
{
660+
code: `
661+
<template>
662+
<my-component>
663+
<one slot="one">
664+
A
665+
</one>
666+
<two slot="two">
667+
B
668+
</two>
669+
</my-component>
670+
</template>`,
671+
output: `
672+
<template>
673+
<my-component>
674+
<one slot="one">
675+
A
676+
</one>
677+
<template v-slot:two>\n<two >
678+
B
679+
</two>\n</template>
680+
</my-component>
681+
</template>`,
682+
options: [
683+
{
684+
ignore: ['/one/']
685+
}
686+
],
687+
errors: ['`slot` attributes are deprecated.']
688+
},
689+
{
690+
code: `
691+
<template>
692+
<my-component>
693+
<one slot="one">
694+
A
695+
</one>
696+
<two slot="two">
697+
B
698+
</two>
699+
</my-component>
700+
</template>`,
701+
output: `
702+
<template>
703+
<my-component>
704+
<one slot="one">
705+
A
706+
</one>
707+
<template v-slot:two>\n<two >
708+
B
709+
</two>\n</template>
710+
</my-component>
711+
</template>`,
712+
options: [
713+
{
714+
ignore: ['/^one$/']
715+
}
716+
],
717+
errors: ['`slot` attributes are deprecated.']
718+
},
647719
{
648720
code: `
649721
<template>

0 commit comments

Comments
 (0)