-
-
Notifications
You must be signed in to change notification settings - Fork 679
⭐️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
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c51cbe2
⭐️New: Add `vue/valid-slot-scope` rule
ota-meshi f149d81
Add `Related links` to doc
ota-meshi cc1f0fd
Merge branch 'master' into add-rule/valid-slot-scope
ota-meshi bf99834
Fixed lint error
ota-meshi 609ad90
Upgraded `vue-eslint-parser`. And remove the case of a syntax error f…
ota-meshi 656fdb5
Merge branch 'master' into add-rule/valid-slot-scope
ota-meshi 85ee7d9
update document
ota-meshi 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,74 @@ | ||
# enforce valid `slot-scope` attributes (vue/valid-slot-scope) | ||
|
||
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>` | ||
- The `slot-scope` attribute have the attribute value which is extra access to slot data. E.g. `<div slot-scope="prop, extra"></div>` | ||
- The `slot-scope` attribute have the attribute value which is rest parameter. E.g. `<div slot-scope="...props"></div>` | ||
|
||
This rule does not check syntax errors in directives because it's checked by [no-parsing-error] rule. | ||
|
||
:-1: Examples of **incorrect** code for this rule: | ||
|
||
```vue | ||
<template> | ||
<TheComponent> | ||
<template slot-scope> | ||
... | ||
</template> | ||
</TheComponent> | ||
<TheComponent> | ||
<template slot-scope=""> | ||
... | ||
</template> | ||
</TheComponent> | ||
<TheComponent> | ||
<template slot-scope="a, b, c"> | ||
<!-- `b` and `c` are extra access. --> | ||
... | ||
</template> | ||
</TheComponent> | ||
<TheComponent> | ||
<template slot-scope="...props"> | ||
... | ||
</template> | ||
</TheComponent> | ||
</template> | ||
``` | ||
|
||
:+1: Examples of **correct** code for this rule: | ||
|
||
```vue | ||
<template> | ||
<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> | ||
</template> | ||
``` | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :couple: Related rules | ||
|
||
- [no-parsing-error] | ||
|
||
|
||
[no-parsing-error]: no-parsing-error.md |
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,137 @@ | ||
/** | ||
* @fileoverview enforce valid `slot-scope` attributes | ||
* @author Yosuke Ota | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Check whether the given token is a comma. | ||
* @param {Token} token The token to check. | ||
* @returns {boolean} `true` if the token is a comma. | ||
*/ | ||
function isComma (token) { | ||
return token != null && token.type === 'Punctuator' && token.value === ',' | ||
} | ||
|
||
/** | ||
* 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 === "'") | ||
} | ||
|
||
/** | ||
* Gets the extra access parameter tokens of the given slot-scope node. | ||
* @param {ASTNode} node The slot-scope node to check. | ||
* @param {TokenStore} tokenStore The TokenStore. | ||
* @returns {Array} the extra tokens. | ||
*/ | ||
function getExtraAccessParameterTokens (node, tokenStore) { | ||
const valueNode = node.value | ||
const result = [] | ||
const valueFirstToken = tokenStore.getFirstToken(valueNode) | ||
const valueLastToken = tokenStore.getLastToken(valueNode) | ||
const exprLastToken = isQuote(valueFirstToken) && isQuote(valueLastToken) && valueFirstToken.value === valueLastToken.value | ||
? tokenStore.getTokenBefore(valueLastToken) | ||
: valueLastToken | ||
const idLastToken = tokenStore.getLastToken(valueNode.expression.id) | ||
if (idLastToken !== exprLastToken) { | ||
// e.g. `<div slot-scope="a, b">` | ||
// ^^^ Invalid | ||
result.push(...tokenStore.getTokensBetween(idLastToken, exprLastToken)) | ||
result.push(exprLastToken) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// 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-beta.4/docs/rules/valid-slot-scope.md' | ||
}, | ||
fixable: null, | ||
schema: [], | ||
messages: { | ||
expectedValue: "'{{attrName}}' attributes require a value.", | ||
unexpectedRestParameter: 'The top level rest parameter is useless.', | ||
unexpectedExtraAccessParams: 'Unexpected extra access parameters `{{value}}`.', | ||
unexpectedTrailingComma: 'Unexpected trailing comma.' | ||
} | ||
}, | ||
|
||
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)) { | ||
context.report({ | ||
node, | ||
loc: node.loc, | ||
messageId: 'expectedValue', | ||
data: { attrName: node.key.name } | ||
}) | ||
return | ||
} | ||
|
||
const idNode = node.value.expression.id | ||
if (idNode.type === 'RestElement') { | ||
// e.g. `<div slot-scope="...a">` | ||
context.report({ | ||
node: idNode, | ||
loc: idNode.loc, | ||
messageId: 'unexpectedRestParameter' | ||
}) | ||
} | ||
|
||
const extraAccessParameterTokens = getExtraAccessParameterTokens(node, tokenStore) | ||
if (extraAccessParameterTokens.length) { | ||
const startToken = extraAccessParameterTokens[0] | ||
if (extraAccessParameterTokens.length === 1 && isComma(startToken)) { | ||
context.report({ | ||
node: startToken, | ||
loc: startToken.loc, | ||
messageId: 'unexpectedTrailingComma' | ||
}) | ||
} else { | ||
const endToken = extraAccessParameterTokens[extraAccessParameterTokens.length - 1] | ||
const value = context.getSourceCode().text.slice( | ||
extraAccessParameterTokens.length > 1 && isComma(startToken) | ||
? extraAccessParameterTokens[1].range[0] | ||
: startToken.range[0], | ||
endToken.range[1] | ||
) | ||
context.report({ | ||
loc: { | ||
start: startToken.loc.start, | ||
end: endToken.loc.end | ||
}, | ||
messageId: 'unexpectedExtraAccessParams', | ||
data: { value } | ||
}) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
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.