Skip to content

Commit

Permalink
feat(require-toggle-inside-transition): add additionalDirectives opti…
Browse files Browse the repository at this point in the history
…on (#2535)
  • Loading branch information
cjpearson committed Aug 28, 2024
1 parent 0fbe35e commit c9dea58
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
37 changes: 36 additions & 1 deletion docs/rules/require-toggle-inside-transition.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,42 @@ This rule reports elements inside `<transition>` that do not control the display

## :wrench: Options

Nothing.
```json
{
"vue/require-toggle-inside-transition": ["error", {
"additionalDirectives": []
}]
}
```

- `additionalDirectives` (`string[]`) ... Custom directives which will satisfy this rule in addition to `v-show` and `v-if`. Should be added without the `v-` prefix.

### `additionalDirectives: ["dialog"]`

<eslint-code-block :rules="{'vue/require-toggle-inside-transition': ['error', {additionalDirectives: ['dialog']}]}">

```vue
<template>
<!-- ✓ GOOD -->
<transition><div v-if="show" /></transition>
<transition><div v-show="show" /></transition>
<transition><dialog v-dialog="show" /></transition>
<template>
```

</eslint-code-block>

<eslint-code-block :rules="{'vue/require-toggle-inside-transition': ['error', {additionalDirectives: ['dialog']}]}">

```vue
<template>
<!-- ✗ BAD -->
<transition><div /></transition>
<transition><div v-custom="show" /></transition>
<template>
```

</eslint-code-block>

## :books: Further Reading

Expand Down
24 changes: 22 additions & 2 deletions lib/rules/require-toggle-inside-transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,31 @@ module.exports = {
url: 'https://eslint.vuejs.org/rules/require-toggle-inside-transition.html'
},
fixable: null,
schema: [],
schema: [
{
type: 'object',
properties: {
additionalDirectives: {
type: 'array',
items: {
type: 'string'
}
}
},
additionalProperties: false
}
],
messages: {
expected:
'The element inside `<transition>` is expected to have a `v-if` or `v-show` directive.'
}
},
/** @param {RuleContext} context */
create(context) {
/** @type {Array<string>} */
const additionalDirectives =
(context.options[0] && context.options[0].additionalDirectives) || []

/**
* Check if the given element has display control.
* @param {VElement} element The element node to check.
Expand All @@ -61,7 +78,10 @@ module.exports = {
element.name !== 'slot' &&
!utils.hasDirective(element, 'if') &&
!utils.hasDirective(element, 'show') &&
!utils.hasDirective(element, 'bind', 'key')
!utils.hasDirective(element, 'bind', 'key') &&
!additionalDirectives.some((directive) =>
utils.hasDirective(element, directive)
)
) {
context.report({
node: element.startTag,
Expand Down
29 changes: 29 additions & 0 deletions tests/lib/rules/require-toggle-inside-transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ tester.run('require-toggle-inside-transition', rule, {
{
filename: 'test.vue',
code: '<template><transition :appear="true"><div /></transition></template>'
},
{
filename: 'test.vue',
code: '<template><transition><dialog v-dialog="show" /></transition></template>',
options: [
{
additionalDirectives: ['dialog']
}
]
}
],
invalid: [
Expand Down Expand Up @@ -132,6 +141,26 @@ tester.run('require-toggle-inside-transition', rule, {
filename: 'test.vue',
code: '<template><transition @appear="isLoaded"><div /></transition></template>',
errors: [{ messageId: 'expected' }]
},
{
filename: 'test.vue',
code: '<template><transition><dialog v-dialog="show" /></transition></template>',
options: [
{
additionalDirectives: []
}
],
errors: [{ messageId: 'expected' }]
},
{
filename: 'test.vue',
code: '<template><transition><div v-custom="show" /></transition></template>',
options: [
{
additionalDirectives: ['dialog']
}
],
errors: [{ messageId: 'expected' }]
}
]
})

0 comments on commit c9dea58

Please sign in to comment.