Skip to content

Commit 609ad90

Browse files
committed
Upgraded vue-eslint-parser. And remove the case of a syntax error from the target to be verified.
1 parent bf99834 commit 609ad90

File tree

4 files changed

+76
-176
lines changed

4 files changed

+76
-176
lines changed

docs/rules/valid-slot-scope.md

-13
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ This rule checks whether every `slot-scope` (or `scope`) attributes is valid.
77
This rule reports `slot-scope` attributes in the following cases:
88

99
- The `slot-scope` attribute does not have that attribute value. E.g. `<div slot-scope></div>`
10-
- The `slot-scope` attribute have the attribute value which is extra access to slot data. E.g. `<div slot-scope="prop, extra"></div>`
11-
- The `slot-scope` attribute have the attribute value which is rest parameter. E.g. `<div slot-scope="...props"></div>`
1210

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

@@ -26,17 +24,6 @@ This rule does not check syntax errors in directives because it's checked by [no
2624
...
2725
</template>
2826
</TheComponent>
29-
<TheComponent>
30-
<template slot-scope="a, b, c">
31-
<!-- `b` and `c` are extra access. -->
32-
...
33-
</template>
34-
</TheComponent>
35-
<TheComponent>
36-
<template slot-scope="...props">
37-
...
38-
</template>
39-
</TheComponent>
4027
</template>
4128
```
4229

lib/rules/valid-slot-scope.js

+24-70
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ const utils = require('../utils')
1414
// Helpers
1515
// ------------------------------------------------------------------------------
1616

17-
/**
18-
* Check whether the given token is a comma.
19-
* @param {Token} token The token to check.
20-
* @returns {boolean} `true` if the token is a comma.
21-
*/
22-
function isComma (token) {
23-
return token != null && token.type === 'Punctuator' && token.value === ','
24-
}
25-
2617
/**
2718
* Check whether the given token is a quote.
2819
* @param {Token} token The token to check.
@@ -33,28 +24,29 @@ function isQuote (token) {
3324
}
3425

3526
/**
36-
* Gets the extra access parameter tokens of the given slot-scope node.
27+
* Check whether the value of the given slot-scope node is a syntax error.
3728
* @param {ASTNode} node The slot-scope node to check.
3829
* @param {TokenStore} tokenStore The TokenStore.
39-
* @returns {Array} the extra tokens.
30+
* @returns {boolean} `true` if the value is a syntax error
4031
*/
41-
function getExtraAccessParameterTokens (node, tokenStore) {
42-
const valueNode = node.value
43-
const result = []
44-
const valueFirstToken = tokenStore.getFirstToken(valueNode)
45-
const valueLastToken = tokenStore.getLastToken(valueNode)
46-
const exprLastToken = isQuote(valueFirstToken) && isQuote(valueLastToken) && valueFirstToken.value === valueLastToken.value
47-
? tokenStore.getTokenBefore(valueLastToken)
48-
: valueLastToken
49-
const idLastToken = tokenStore.getLastToken(valueNode.expression.id)
50-
if (idLastToken !== exprLastToken) {
51-
// e.g. `<div slot-scope="a, b">`
52-
// ^^^ Invalid
53-
result.push(...tokenStore.getTokensBetween(idLastToken, exprLastToken))
54-
result.push(exprLastToken)
32+
function isSyntaxError (node, tokenStore) {
33+
if (node.value == null) {
34+
// does not have value.
35+
return false
5536
}
56-
57-
return result
37+
if (node.value.expression != null) {
38+
return false
39+
}
40+
const tokens = tokenStore.getTokens(node.value)
41+
if (!tokens.length) {
42+
// empty value
43+
return false
44+
}
45+
if (tokens.length === 2 && tokens.every(isQuote)) {
46+
// empty value e.g `""` / `''`
47+
return false
48+
}
49+
return true
5850
}
5951

6052
// ------------------------------------------------------------------------------
@@ -71,10 +63,7 @@ module.exports = {
7163
fixable: null,
7264
schema: [],
7365
messages: {
74-
expectedValue: "'{{attrName}}' attributes require a value.",
75-
unexpectedRestParameter: 'The top level rest parameter is useless.',
76-
unexpectedExtraAccessParams: 'Unexpected extra access parameters `{{value}}`.',
77-
unexpectedTrailingComma: 'Unexpected trailing comma.'
66+
expectedValue: "'{{attrName}}' attributes require a value."
7867
}
7968
},
8069

@@ -85,51 +74,16 @@ module.exports = {
8574
return utils.defineTemplateBodyVisitor(context, {
8675
'VAttribute[directive=true][key.name=/^(slot-)?scope$/]' (node) {
8776
if (!utils.hasAttributeValue(node)) {
77+
if (isSyntaxError(node, tokenStore)) {
78+
// syntax error
79+
return
80+
}
8881
context.report({
8982
node,
9083
loc: node.loc,
9184
messageId: 'expectedValue',
9285
data: { attrName: node.key.name }
9386
})
94-
return
95-
}
96-
97-
const idNode = node.value.expression.id
98-
if (idNode.type === 'RestElement') {
99-
// e.g. `<div slot-scope="...a">`
100-
context.report({
101-
node: idNode,
102-
loc: idNode.loc,
103-
messageId: 'unexpectedRestParameter'
104-
})
105-
}
106-
107-
const extraAccessParameterTokens = getExtraAccessParameterTokens(node, tokenStore)
108-
if (extraAccessParameterTokens.length) {
109-
const startToken = extraAccessParameterTokens[0]
110-
if (extraAccessParameterTokens.length === 1 && isComma(startToken)) {
111-
context.report({
112-
node: startToken,
113-
loc: startToken.loc,
114-
messageId: 'unexpectedTrailingComma'
115-
})
116-
} else {
117-
const endToken = extraAccessParameterTokens[extraAccessParameterTokens.length - 1]
118-
const value = context.getSourceCode().text.slice(
119-
extraAccessParameterTokens.length > 1 && isComma(startToken)
120-
? extraAccessParameterTokens[1].range[0]
121-
: startToken.range[0],
122-
endToken.range[1]
123-
)
124-
context.report({
125-
loc: {
126-
start: startToken.loc.start,
127-
end: endToken.loc.end
128-
},
129-
messageId: 'unexpectedExtraAccessParams',
130-
data: { value }
131-
})
132-
}
13387
}
13488
}
13589
})

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"eslint": "^5.0.0"
4545
},
4646
"dependencies": {
47-
"vue-eslint-parser": "^3.2.1"
47+
"vue-eslint-parser": "^3.3.0"
4848
},
4949
"devDependencies": {
5050
"@types/node": "^4.2.16",

0 commit comments

Comments
 (0)