@@ -14,15 +14,6 @@ const utils = require('../utils')
14
14
// Helpers
15
15
// ------------------------------------------------------------------------------
16
16
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
-
26
17
/**
27
18
* Check whether the given token is a quote.
28
19
* @param {Token } token The token to check.
@@ -33,28 +24,29 @@ function isQuote (token) {
33
24
}
34
25
35
26
/**
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 .
37
28
* @param {ASTNode } node The slot-scope node to check.
38
29
* @param {TokenStore } tokenStore The TokenStore.
39
- * @returns {Array } the extra tokens.
30
+ * @returns {boolean } `true` if the value is a syntax error
40
31
*/
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
55
36
}
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
58
50
}
59
51
60
52
// ------------------------------------------------------------------------------
@@ -71,10 +63,7 @@ module.exports = {
71
63
fixable : null ,
72
64
schema : [ ] ,
73
65
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."
78
67
}
79
68
} ,
80
69
@@ -85,51 +74,16 @@ module.exports = {
85
74
return utils . defineTemplateBodyVisitor ( context , {
86
75
'VAttribute[directive=true][key.name=/^(slot-)?scope$/]' ( node ) {
87
76
if ( ! utils . hasAttributeValue ( node ) ) {
77
+ if ( isSyntaxError ( node , tokenStore ) ) {
78
+ // syntax error
79
+ return
80
+ }
88
81
context . report ( {
89
82
node,
90
83
loc : node . loc ,
91
84
messageId : 'expectedValue' ,
92
85
data : { attrName : node . key . name }
93
86
} )
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
- }
133
87
}
134
88
}
135
89
} )
0 commit comments