Skip to content

⭐️New: Add vue/valid-slot-scope rule #670

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

Closed
wants to merge 7 commits into from
Closed
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
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ For example:
| Rule ID | Description | |
|:--------|:------------|:---|
| [vue/script-indent](./script-indent.md) | enforce consistent indentation in `<script>` | :wrench: |
| [vue/valid-slot-scope](./valid-slot-scope.md) | enforce valid `slot-scope` attributes | |

## Deprecated

Expand Down
74 changes: 74 additions & 0 deletions docs/rules/valid-slot-scope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/valid-slot-scope
description: enforce valid `slot-scope` attributes
---
# vue/valid-slot-scope
> enforce valid `slot-scope` attributes

This rule checks whether every `slot-scope` (or `scope`) attributes is valid.

## :book: Rule Details

This rule reports `slot-scope` attributes in the following cases:

- The `slot-scope` attribute does not have that attribute value. E.g. `<div slot-scope></div>`

<eslint-code-block :rules="{'vue/valid-slot-scope': ['error']}">
```vue
<template>
<!-- ✓ GOOD -->
<TheComponent>
<template slot-scope="prop">
...
</template>
</TheComponent>
<TheComponent>
<template slot-scope="{ a, b, c }">
...
</template>
</TheComponent>
<TheComponent>
<template slot-scope="[ a, b, c ]">
...
</template>
</TheComponent>

<!-- ✗ BAD -->
<TheComponent>
<template slot-scope>
...
</template>
</TheComponent>
<TheComponent>
<template slot-scope="">
...
</template>
</TheComponent>
</template>
```
</eslint-code-block>

::: warning Note
This rule does not check syntax errors in directives because it's checked by [no-parsing-error] rule.
:::

## :wrench: Options

Nothing.

## :couple: Related rules

- [no-parsing-error]

## :books: Further reading

- [Guide - Scoped Slots](https://vuejs.org/v2/guide/components-slots.html#Scoped-Slots)

[no-parsing-error]: no-parsing-error.md

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/valid-slot-scope.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/valid-slot-scope.js)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
'use-v-on-exact': require('./rules/use-v-on-exact'),
'v-bind-style': require('./rules/v-bind-style'),
'v-on-style': require('./rules/v-on-style'),
'valid-slot-scope': require('./rules/valid-slot-scope'),
'valid-template-root': require('./rules/valid-template-root'),
'valid-v-bind': require('./rules/valid-v-bind'),
'valid-v-cloak': require('./rules/valid-v-cloak'),
Expand Down
91 changes: 91 additions & 0 deletions lib/rules/valid-slot-scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* @fileoverview enforce valid `slot-scope` attributes
* @author Yosuke Ota
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

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

// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------

/**
* Check whether the given token is a quote.
* @param {Token} token The token to check.
* @returns {boolean} `true` if the token is a quote.
*/
function isQuote (token) {
return token != null && token.type === 'Punctuator' && (token.value === '"' || token.value === "'")
}

/**
* Check whether the value of the given slot-scope node is a syntax error.
* @param {ASTNode} node The slot-scope node to check.
* @param {TokenStore} tokenStore The TokenStore.
* @returns {boolean} `true` if the value is a syntax error
*/
function isSyntaxError (node, tokenStore) {
if (node.value == null) {
// does not have value.
return false
}
if (node.value.expression != null) {
return false
}
const tokens = tokenStore.getTokens(node.value)
if (!tokens.length) {
// empty value
return false
}
if (tokens.length === 2 && tokens.every(isQuote)) {
// empty value e.g `""` / `''`
return false
}
return true
}

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description: 'enforce valid `slot-scope` attributes',
category: undefined,
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0/docs/rules/valid-slot-scope.md'
},
fixable: null,
schema: [],
messages: {
expectedValue: "'{{attrName}}' attributes require a value."
}
},

create (context) {
const tokenStore =
context.parserServices.getTemplateBodyTokenStore &&
context.parserServices.getTemplateBodyTokenStore()
return utils.defineTemplateBodyVisitor(context, {
'VAttribute[directive=true][key.name=/^(slot-)?scope$/]' (node) {
if (!utils.hasAttributeValue(node)) {
if (isSyntaxError(node, tokenStore)) {
// syntax error
return
}
context.report({
node,
loc: node.loc,
messageId: 'expectedValue',
data: { attrName: node.key.name }
})
}
}
})
}
}
Loading