-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathvalid-slot-scope.js
91 lines (83 loc) · 2.57 KB
/
valid-slot-scope.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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 }
})
}
}
})
}
}