forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-explicit-slots.js
186 lines (168 loc) · 5.01 KB
/
require-explicit-slots.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* @author Mussin Benarbia
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
/**
* @typedef {import('@typescript-eslint/types').TSESTree.TypeNode} TypeNode
* @typedef {import('@typescript-eslint/types').TSESTree.TypeElement} TypeElement
*/
/**
* @param {TypeElement} node
* @return {string | null}
*/
function getSlotsName(node) {
if (
node.type !== 'TSMethodSignature' &&
node.type !== 'TSPropertySignature'
) {
return null
}
const key = node.key
if (key.type === 'Literal') {
return typeof key.value === 'string' ? key.value : null
}
if (key.type === 'Identifier') {
return key.name
}
return null
}
/**
* @param {VElement} node
* @return {VAttribute | VDirective | undefined}
*/
function getSlotNameNode(node) {
return node.startTag.attributes.find(
(node) =>
(!node.directive && node.key.name === 'name') ||
(node.directive &&
node.key.name.name === 'bind' &&
node.key.argument?.type === 'VIdentifier' &&
node.key.argument?.name === 'name')
)
}
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'require slots to be explicitly defined',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/require-explicit-slots.html'
},
fixable: null,
schema: [],
messages: {
requireExplicitSlots: 'Slots must be explicitly defined.',
alreadyDefinedSlot: 'Slot {{slotName}} is already defined.'
}
},
/** @param {RuleContext} context */
create(context) {
const sourceCode = context.getSourceCode()
const documentFragment =
sourceCode.parserServices.getDocumentFragment &&
sourceCode.parserServices.getDocumentFragment()
if (!documentFragment) {
return {}
}
const scripts = documentFragment.children.filter(
/** @returns {element is VElement} */
(element) => utils.isVElement(element) && element.name === 'script'
)
if (scripts.every((script) => !utils.hasAttribute(script, 'lang', 'ts'))) {
return {}
}
const slotsDefined = new Set()
/**
* @param {VElement} node
* @param {string | undefined} slotName
*/
function reportMissingSlot(node, slotName) {
if (!slotsDefined.has(slotName)) {
context.report({
node,
messageId: 'requireExplicitSlots'
})
}
}
return utils.compositingVisitors(
utils.defineScriptSetupVisitor(context, {
onDefineSlotsEnter(_node, slots) {
for (const slot of slots) {
if (!slot.slotName) {
continue
}
if (slotsDefined.has(slot.slotName)) {
context.report({
node: slot.node,
messageId: 'alreadyDefinedSlot',
data: {
slotName: slot.slotName
}
})
} else {
slotsDefined.add(slot.slotName)
}
}
}
}),
utils.executeOnVue(context, (obj) => {
const slotsProperty = utils.findProperty(obj, 'slots')
if (!slotsProperty) return
const slotsTypeHelper =
slotsProperty.value.type === 'TSAsExpression' &&
slotsProperty.value.typeAnnotation?.typeName.name === 'SlotsType' &&
slotsProperty.value.typeAnnotation
if (!slotsTypeHelper) return
const typeArguments =
'typeArguments' in slotsTypeHelper
? slotsTypeHelper.typeArguments
: slotsTypeHelper.typeParameters
const param = /** @type {TypeNode|undefined} */ (
typeArguments?.params[0]
)
if (!param) return
if (param.type === 'TSTypeLiteral') {
for (const memberNode of param.members) {
const slotName = getSlotsName(memberNode)
if (!slotName) continue
if (slotsDefined.has(slotName)) {
context.report({
node: memberNode,
messageId: 'alreadyDefinedSlot',
data: {
slotName
}
})
} else {
slotsDefined.add(slotName)
}
}
}
}),
utils.defineTemplateBodyVisitor(context, {
/** @param {VElement} node */
"VElement[name='slot']"(node) {
const nameNode = getSlotNameNode(node)
// if no slot name is declared, default to 'default'
if (!nameNode) {
reportMissingSlot(node, 'default')
return
}
if (nameNode.directive) {
const expression = nameNode.value?.expression
// ignore attribute binding except string literal
if (!expression || !utils.isStringLiteral(expression)) {
return
}
const name = utils.getStringLiteralValue(expression) || undefined
reportMissingSlot(node, name)
} else {
reportMissingSlot(node, nameNode.value?.value)
}
}
})
)
}
}