Skip to content

feat(no-deprecated-slot-attribute): add ignoreParents option #2784

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/beige-teams-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-vue': minor
---

Added `ignoreParents` option to [`vue/no-deprecated-slot-attribute`](https://eslint.vuejs.org/rules/no-deprecated-slot-attribute.html)
38 changes: 36 additions & 2 deletions docs/rules/no-deprecated-slot-attribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ This rule reports deprecated `slot` attribute in Vue.js v2.6.0+.
```json
{
"vue/no-deprecated-slot-attribute": ["error", {
"ignore": ["my-component"]
"ignore": ["my-component"],
"ignoreParents": ["my-web-component"],
}]
}
```

- `"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.
- `"ignoreParents"` (`string[]`) An array of tags or regular expression patterns (e.g. `/^custom-/`) for parents that ignore these rules. This option is especially useful for [Web-Components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components). Default is empty.

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

<eslint-code-block fix :rules="{'vue/no-dupe-keys': ['error', {ignore: ['my-component']}]}">
<eslint-code-block fix :rules="{'vue/no-deprecated-slot-attribute': ['error', {ignore: ['my-component']}]}">

```vue
<template>
Expand Down Expand Up @@ -81,9 +83,41 @@ This rule reports deprecated `slot` attribute in Vue.js v2.6.0+.

</eslint-code-block>

### `"ignoreParents": ["my-web-component"]`

<eslint-code-block fix :rules="{'vue/no-deprecated-slot-attribute': ['error', {ignoreParents: ['my-web-component']}]}">

```vue
<template>
<my-web-component>
<!-- ✓ GOOD -->
<template v-slot:name>
{{ props.title }}
</template>
</my-web-component>

<my-web-component>
<!-- ✓ GOOD -->
<my-component slot="name">
{{ props.title }}
</my-component>
</my-web-component>

<other-component>
<!-- ✗ BAD -->
<my-component slot="name">
{{ props.title }}
</my-component>
</other-component>
</template>
```

</eslint-code-block>

## :books: Further Reading

- [API - slot](https://v2.vuejs.org/v2/api/#slot-deprecated)
- [Web - slot](https://developer.mozilla.org/en-US/docs/Web/API/Element/slot)

## :rocket: Version

Expand Down
5 changes: 5 additions & 0 deletions lib/rules/no-deprecated-slot-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ module.exports = {
type: 'array',
items: { type: 'string' },
uniqueItems: true
},
ignoreParents: {
type: 'array',
items: { type: 'string' },
uniqueItems: true
}
},
additionalProperties: false
Expand Down
15 changes: 12 additions & 3 deletions lib/rules/syntaxes/slot-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
const canConvertToVSlot = require('./utils/can-convert-to-v-slot')
const regexp = require('../../utils/regexp')
const casing = require('../../utils/casing')
const { isVElement } = require('../../utils')

module.exports = {
deprecated: '2.6.0',
supported: '<3.0.0',
/** @param {RuleContext} context @returns {TemplateListener} */
createTemplateBodyVisitor(context) {
/** @type {{ ignore: string[] }} */
/** @type {{ ignore: string[], ignoreParents: string[] }} */
const options = context.options[0] || {}
const { ignore = [] } = options
const { ignore = [], ignoreParents = [] } = options
const isAnyIgnored = regexp.toRegExpGroupMatcher(ignore)
const isParentIgnored = regexp.toRegExpGroupMatcher(ignoreParents)

const sourceCode = context.getSourceCode()
const tokenStore =
Expand Down Expand Up @@ -123,7 +125,8 @@ module.exports = {
* @returns {void}
*/
function reportSlot(slotAttr) {
const componentName = slotAttr.parent.parent.rawName
const component = slotAttr.parent.parent
const componentName = component.rawName

if (
isAnyIgnored(
Expand All @@ -135,6 +138,12 @@ module.exports = {
return
}

const parent = component.parent
const parentName = isVElement(parent) ? parent.rawName : null
if (parentName && isParentIgnored(parentName)) {
return
}

context.report({
node: slotAttr.key,
messageId: 'forbiddenSlotAttribute',
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2462,7 +2462,7 @@ function isAssignmentProperty(node) {
}
/**
* Checks whether the given node is VElement.
* @param {VElement | VExpressionContainer | VText} node
* @param {VElement | VExpressionContainer | VText | VDocumentFragment} node
* @returns {node is VElement}
*/
function isVElement(node) {
Expand Down
108 changes: 108 additions & 0 deletions tests/lib/rules/no-deprecated-slot-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ tester.run('no-deprecated-slot-attribute', rule, {
</LinkList>
</template>`,
options: [{ ignore: ['/one/', '/^Two$/i', '/^my-.*/i'] }]
},
{
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: [{ ignoreParents: ['LinkList'] }]
},
{
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: [{ ignoreParents: ['/^Link/'] }]
}
],
invalid: [
Expand Down Expand Up @@ -732,6 +756,90 @@ tester.run('no-deprecated-slot-attribute', rule, {
}
]
},
{
code: `
<template>
<my-component>
<one slot="one">
A
</one>
</my-component>
<my-component2>
<two slot="two">
B
</two>
</my-component2>
</template>`,
output: `
<template>
<my-component>
<one slot="one">
A
</one>
</my-component>
<my-component2>
<template v-slot:two>\n<two >
B
</two>\n</template>
</my-component2>
</template>`,
options: [
{
ignoreParents: ['my-component']
}
],
errors: [
{
message: '`slot` attributes are deprecated.',
line: 9,
column: 16,
endLine: 9,
endColumn: 20
}
]
},
{
code: `
<template>
<my-component>
<one slot="one">
A
</one>
</my-component>
<my-component2>
<two slot="two">
B
</two>
</my-component2>
</template>`,
output: `
<template>
<my-component>
<one slot="one">
A
</one>
</my-component>
<my-component2>
<template v-slot:two>\n<two >
B
</two>\n</template>
</my-component2>
</template>`,
options: [
{
ignoreParents: ['/component$/']
}
],
errors: [
{
message: '`slot` attributes are deprecated.',
line: 9,
column: 16,
endLine: 9,
endColumn: 20
}
]
},
{
code: `
<template>
Expand Down