Skip to content

feat(no-deprecated-slot-attribute): regex ignore patterns #2773

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tangy-eagles-start.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion docs/rules/no-deprecated-slot-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]`

Expand Down
19 changes: 14 additions & 5 deletions lib/rules/syntaxes/slot-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
'use strict'

const canConvertToVSlot = require('./utils/can-convert-to-v-slot')
const regexp = require('../../utils/regexp')
const casing = require('../../utils/casing')

module.exports = {
deprecated: '2.6.0',
supported: '<3.0.0',
/** @param {RuleContext} context @returns {TemplateListener} */
createTemplateBodyVisitor(context) {
/** @type {{ ignore: string[] }} */
const options = context.options[0] || {}
/** @type {Set<string>} */
const ignore = new Set(options.ignore)
const { ignore = [] } = options
/** @type {RegExp[]} */
const ignorePatterns = ignore.map(regexp.toRegExp)

const sourceCode = context.getSourceCode()
const tokenStore =
Expand Down Expand Up @@ -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
}
Expand Down
88 changes: 88 additions & 0 deletions tests/lib/rules/no-deprecated-slot-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ tester.run('no-deprecated-slot-attribute', rule, {
</LinkList>
</template>`,
options: [{ ignore: ['one', 'two', 'my-component'] }]
},
{
code: `<template>
<LinkList>
<one slot="one" />
<two slot="two" />
<my-component slot="my-component-slot" />
<myComponent slot="myComponent-slot" />
<MyComponent slot="MyComponent-slot" />
</LinkList>
</template>`,
options: [{ ignore: ['/one/', '/^Two$/i', '/^my-.*/i'] }]
}
],
invalid: [
Expand Down Expand Up @@ -644,6 +656,82 @@ tester.run('no-deprecated-slot-attribute', rule, {
],
errors: ['`slot` attributes are deprecated.']
},
{
code: `
<template>
<my-component>
<one slot="one">
A
</one>
<two slot="two">
B
</two>
</my-component>
</template>`,
output: `
<template>
<my-component>
<one slot="one">
A
</one>
<template v-slot:two>\n<two >
B
</two>\n</template>
</my-component>
</template>`,
options: [
{
ignore: ['/one/']
}
],
errors: [
{
message: '`slot` attributes are deprecated.',
line: 7,
endLine: 7,
column: 16,
endColumn: 20
}
]
},
{
code: `
<template>
<my-component>
<one slot="one">
A
</one>
<two slot="two">
B
</two>
</my-component>
</template>`,
output: `
<template>
<my-component>
<one slot="one">
A
</one>
<template v-slot:two>\n<two >
B
</two>\n</template>
</my-component>
</template>`,
options: [
{
ignore: ['/^one$/']
}
],
errors: [
{
message: '`slot` attributes are deprecated.',
line: 7,
endLine: 7,
column: 16,
endColumn: 20
}
]
},
{
code: `
<template>
Expand Down
Loading