Skip to content
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
29 changes: 28 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,34 @@ 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>

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

</eslint-code-block>

## :books: Further Reading

Expand Down
53 changes: 48 additions & 5 deletions lib/rules/require-toggle-inside-transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ function isValidBindAppear(vBindAppear) {
return true
}

/**
* @param {string[]} directives
*/
function createDirectiveList(directives) {
let str = ''

for (const [index, directive] of directives.entries()) {
if (index === 0) {
str += `\`v-${directive}\``
} else if (index < directives.length - 1) {
str += `, \`v-${directive}\``
} else {
str += ` or \`v-${directive}\``
}
}

return str
}

module.exports = {
meta: {
type: 'problem',
Expand All @@ -30,14 +49,34 @@ 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'
},
uniqueItems: true
}
},
additionalProperties: false
}
],
messages: {
expected:
'The element inside `<transition>` is expected to have a `v-if` or `v-show` directive.'
'The element inside `<transition>` is expected to have a {{allowedDirectives}} directive.'
}
},
/** @param {RuleContext} context */
create(context) {
/** @type {Array<string>} */
const additionalDirectives =
(context.options[0] && context.options[0].additionalDirectives) || []
const allowedDirectives = ['if', 'show', ...additionalDirectives]
const allowedDirectivesString = createDirectiveList(allowedDirectives)

/**
* Check if the given element has display control.
* @param {VElement} element The element node to check.
Expand All @@ -59,14 +98,18 @@ module.exports = {

if (
element.name !== 'slot' &&
!utils.hasDirective(element, 'if') &&
!utils.hasDirective(element, 'show') &&
!allowedDirectives.some((directive) =>
utils.hasDirective(element, directive)
) &&
!utils.hasDirective(element, 'bind', 'key')
) {
context.report({
node: element.startTag,
loc: element.startTag.loc,
messageId: 'expected'
messageId: 'expected',
data: {
allowedDirectives: allowedDirectivesString
}
})
}
}
Expand Down
39 changes: 39 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,36 @@ 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',
data: { allowedDirectives: '`v-if` or `v-show`' }
}
]
},
{
filename: 'test.vue',
code: '<template><transition><div v-custom="show" /></transition></template>',
options: [
{
additionalDirectives: ['dialog']
}
],
errors: [
{
messageId: 'expected',
data: { allowedDirectives: '`v-if`, `v-show` or `v-dialog`' }
}
]
}
]
})
Loading