-
-
Notifications
You must be signed in to change notification settings - Fork 681
feat: implement require-explicit-slots #2325
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
ota-meshi
merged 7 commits into
vuejs:master
from
mussinbenarbia:require-explicit-slots
Jan 16, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c6b1e1
feat: implement require-explicit-slots
8c0cdba
Merge branch 'master' into require-explicit-slots
63c62ab
feat: make rule compatible with options api
40b48af
Merge branch 'master' into require-explicit-slots
ota-meshi e1da252
refactor: apply suggestions and improvements
73046fd
docs: wrap each case in eslint-code-block
880b9db
docs: reduce number of examples and link related rule
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--- | ||
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 | ||
|
||
- :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 either in the `script setup` block with the [`defineSlots`](https://vuejs.org/api/sfc-script-setup.html) macro, or with the [`slots property`](https://vuejs.org/api/options-rendering.html#slots) in the Options API. | ||
|
||
<eslint-code-block :rules="{'vue/require-explicit-slots': ['error']}"> | ||
|
||
```vue | ||
<template> | ||
<div> | ||
<!-- ✓ GOOD --> | ||
<slot /> | ||
<slot name="foo" /> | ||
<!-- ✗ BAD --> | ||
<slot name="bar" /> | ||
</div> | ||
</template> | ||
<script setup lang="ts"> | ||
defineSlots<{ | ||
default(props: { msg: string }): any | ||
foo(props: { msg: string }): any | ||
}>() | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/require-explicit-slots': ['error']}"> | ||
|
||
```vue | ||
<template> | ||
<div> | ||
<!-- ✓ GOOD --> | ||
<slot /> | ||
<slot name="foo" /> | ||
<!-- ✗ BAD --> | ||
<slot name="bar" /> | ||
</div> | ||
</template> | ||
<script lang="ts"> | ||
import { SlotsType } from 'vue' | ||
|
||
defineComponent({ | ||
slots: Object as SlotsType<{ | ||
default: { msg: string } | ||
foo: { msg: string } | ||
}> | ||
}) | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* @author Mussin Benarbia | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
/** | ||
* @typedef {import('@typescript-eslint/types').TSESTree.TypeNode} TypeNode | ||
*/ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'require slots to be explicitly defined', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/require-explicit-slots.html' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
requireExplicitSlots: 'Slots must be explicitly defined.', | ||
alreadyDefinedSlot: 'Slot {{slotName}} is already defined.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const sourceCode = context.getSourceCode() | ||
const documentFragment = | ||
sourceCode.parserServices.getDocumentFragment && | ||
sourceCode.parserServices.getDocumentFragment() | ||
if (!documentFragment) { | ||
return {} | ||
} | ||
const scripts = documentFragment.children.filter( | ||
(element) => utils.isVElement(element) && element.name === 'script' | ||
) | ||
if (scripts.every((script) => !utils.hasAttribute(script, 'lang', 'ts'))) { | ||
return {} | ||
} | ||
const slotsDefined = new Set() | ||
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return utils.compositingVisitors( | ||
utils.defineScriptSetupVisitor(context, { | ||
onDefineSlotsEnter(node) { | ||
const typeArguments = | ||
'typeArguments' in node ? node.typeArguments : node.typeParameters | ||
const param = /** @type {TypeNode|undefined} */ ( | ||
typeArguments?.params[0] | ||
) | ||
if (!param) return | ||
|
||
if (param.type === 'TSTypeLiteral') { | ||
for (const memberNode of param.members) { | ||
const slotName = memberNode.key.name | ||
if (slotsDefined.has(slotName)) { | ||
context.report({ | ||
node: memberNode, | ||
messageId: 'alreadyDefinedSlot', | ||
data: { | ||
slotName | ||
} | ||
}) | ||
} else { | ||
slotsDefined.add(slotName) | ||
} | ||
} | ||
} | ||
} | ||
}), | ||
utils.executeOnVue(context, (obj) => { | ||
const slotsProperty = utils.findProperty(obj, 'slots') | ||
if (!slotsProperty) return | ||
|
||
const slotsTypeHelper = | ||
slotsProperty.value.typeAnnotation?.typeName.name === 'SlotsType' && | ||
slotsProperty.value.typeAnnotation | ||
if (!slotsTypeHelper) return | ||
|
||
const typeArguments = | ||
'typeArguments' in slotsTypeHelper | ||
? slotsTypeHelper.typeArguments | ||
: slotsTypeHelper.typeParameters | ||
const param = /** @type {TypeNode|undefined} */ ( | ||
typeArguments?.params[0] | ||
) | ||
if (!param) return | ||
|
||
if (param.type === 'TSTypeLiteral') { | ||
for (const memberNode of param.members) { | ||
const slotName = memberNode.key.name | ||
if (slotsDefined.has(slotName)) { | ||
context.report({ | ||
node: memberNode, | ||
messageId: 'alreadyDefinedSlot', | ||
data: { | ||
slotName | ||
} | ||
}) | ||
} else { | ||
slotsDefined.add(slotName) | ||
} | ||
} | ||
} | ||
}), | ||
utils.defineTemplateBodyVisitor(context, { | ||
"VElement[name='slot']"(node) { | ||
let slotName = 'default' | ||
|
||
const slotNameAttr = utils.getAttribute(node, 'name') | ||
|
||
if (slotNameAttr) { | ||
slotName = slotNameAttr.value.value | ||
} | ||
|
||
if (!slotsDefined.has(slotName)) { | ||
context.report({ | ||
node, | ||
messageId: 'requireExplicitSlots' | ||
}) | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.