Skip to content
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

feat: implement require-explicit-slots #2325

Merged
merged 7 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
71 changes: 71 additions & 0 deletions docs/rules/require-explicit-slots.md
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/require-explicit-slots
description: require slots to be explicitly defined with defineSlots
---

# vue/require-explicit-slots

> require slots to be explicitly defined with defineSlots

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

## :book: Rule Details

This rule enforces all slots used in the template to be defined once
in the `script setup` block with the [`defineSlots`](https://vuejs.org/api/sfc-script-setup.html) macro.
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved

<eslint-code-block :rules="{'vue/require-explicit-slots': ['error']}">

```vue
<!-- ✓ GOOD -->
<template>
<div>
<slot />
</div>
</template>
<script setup lang="ts">
defineSlots<{
default(props: { msg: string }): any
}>()
</script>

<!-- ✓ GOOD -->
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
<template>
<div>
<slot name="foo" />
</div>
</template>
<script setup lang="ts">
defineSlots<{
foo(props: { msg: string }): any
}>()
</script>

<!-- ✗ BAD -->
<template>
<div>
<slot />
</div>
</template>
<script setup lang="ts"></script>

<!-- ✗ BAD -->
<template>
<div>
<slot name="foo" />
</div>
</template>
<script setup lang="ts">
defineSlots<{
bar(props: { msg: string }): any
}>()
</script>
```

</eslint-code-block>

## :wrench: Options

Nothing.
73 changes: 73 additions & 0 deletions lib/rules/require-explicit-slots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @author Mussin Benarbia
* See LICENSE file in root directory for full license.
*/
'use strict'

const utils = require('../utils')

module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require slots to be explicitly defined with defineSlots',
categories: ['vue3-strongly-recommended'],
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
url: 'https://eslint.vuejs.org/rules/require-explicit-slots.html'
},
fixable: null,
schema: [],
messages: {
requireExplicitSlots:
'Slots must be explicitly defined with the defineSlots macro.',
alreadyDefinedSlot: 'Slot {{slotName}} is already defined.'
}
},
/** @param {RuleContext} context */
create(context) {
const slotsDefined = new Set()
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved

return utils.compositingVisitors(
utils.defineScriptSetupVisitor(context, {
onDefineSlotsEnter(node) {
const typeArgument = node.typeArguments.params[0]
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved
const slotsDefinitions = typeArgument.members

for (const slotDefinition of slotsDefinitions) {
const slotName = slotDefinition.key.name
if (slotsDefined.has(slotName)) {
context.report({
node: slotDefinition,
messageId: 'alreadyDefinedSlot',
data: {
slotName
}
})
} else {
slotsDefined.add(slotName)
}
}
}
}),
utils.defineTemplateBodyVisitor(context, {
"VElement[name='slot']"(node) {
let slotName = 'default'

const slotNameAttr = node.startTag.attributes.find(
(attribute) => attribute.key.name === 'name'
)
ota-meshi marked this conversation as resolved.
Show resolved Hide resolved

if (slotNameAttr) {
slotName = slotNameAttr.value.value
}

if (!slotsDefined.has(slotName)) {
context.report({
node,
messageId: 'requireExplicitSlots'
})
}
}
})
)
}
}
180 changes: 180 additions & 0 deletions tests/lib/rules/require-explicit-slots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/**
* @author Mussin Benarbia
* See LICENSE file in root directory for full license.
*/
'use strict'

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/require-explicit-slots')

const tester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
parser: require.resolve('@typescript-eslint/parser'),
ecmaVersion: 2020,
sourceType: 'module'
}
})

tester.run('require-explicit-slots', rule, {
valid: [
{
filename: 'test.vue',
code: `
<template>
<div>
<slot />
</div>
</template>
<script setup lang="ts">
defineSlots<{
default(props: { msg: string }): any
}>()
</script>`
},
{
filename: 'test.vue',
code: `
<template>
<div>
<slot></slot>
</div>
</template>
<script setup lang="ts">
defineSlots<{
default(props: { msg: string }): any
}>()
</script>`
},
{
filename: 'test.vue',
code: `
<template>
<div>
<slot name="foo"></slot>
</div>
</template>
<script setup lang="ts">
defineSlots<{
foo(props: { msg: string }): any
}>()
</script>`
}
],
invalid: [
{
filename: 'test.vue',
code: `
<template>
<div>
<slot />
</div>
</template>
<script setup lang="ts">
</script>`,
errors: [
{
message:
'Slots must be explicitly defined with the defineSlots macro.'
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div>
<slot></slot>
</div>
</template>
<script setup lang="ts">
</script>`,
errors: [
{
message:
'Slots must be explicitly defined with the defineSlots macro.'
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div>
<slot name="foo" />
</div>
</template>
<script setup lang="ts">
</script>`,
errors: [
{
message:
'Slots must be explicitly defined with the defineSlots macro.'
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div>
<slot name="foo" />
</div>
</template>
<script setup lang="ts">
defineSlots<{
default(props: { msg: string }): any
}>()
</script>`,
errors: [
{
message:
'Slots must be explicitly defined with the defineSlots macro.'
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div>
<slot name="foo" />
</div>
</template>
<script setup lang="ts">
defineSlots<{
foo(props: { msg: string }): any
foo(props: { msg: string }): any
}>()
</script>`,
errors: [
{
message: 'Slot foo is already defined.'
}
]
},
{
filename: 'test.vue',
code: `
<template>
<div>
<slot name="foo" />
</div>
</template>
<script setup lang="ts">
defineSlots<{
foo(props: { msg: string }): any
}>()
defineSlots<{
default(props: { msg: string }): any,
foo(props: { msg: string }): any
}>()
</script>`,
errors: [
{
message: 'Slot foo is already defined.'
}
]
}
]
})