-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-unused-components.js
146 lines (139 loc) · 4.37 KB
/
no-unused-components.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
/**
* @fileoverview Report used components
* @author Michał Sajnóg
*/
'use strict'
const utils = require('../utils')
const casing = require('../utils/casing')
module.exports = {
meta: {
type: 'suggestion',
docs: {
description:
'disallow registering components that are not used inside templates',
categories: ['vue3-essential', 'vue2-essential'],
url: 'https://eslint.vuejs.org/rules/no-unused-components.html'
},
fixable: null,
schema: [
{
type: 'object',
properties: {
ignoreWhenBindingPresent: {
type: 'boolean'
}
},
additionalProperties: false
}
],
messages: {
unused: 'The "{{name}}" component has been registered but not used.'
}
},
/** @param {RuleContext} context */
create(context) {
const options = context.options[0] || {}
const ignoreWhenBindingPresent =
options.ignoreWhenBindingPresent === undefined
? true
: options.ignoreWhenBindingPresent
const usedComponents = new Set()
/** @type { { node: Property, name: string }[] } */
let registeredComponents = []
let ignoreReporting = false
/** @type {Position} */
let templateLocation
return utils.defineTemplateBodyVisitor(
context,
{
/** @param {VElement} node */
VElement(node) {
if (
(!utils.isHtmlElementNode(node) &&
!utils.isSvgElementNode(node) &&
!utils.isMathElementNode(node)) ||
utils.isHtmlWellKnownElementName(node.rawName) ||
utils.isSvgWellKnownElementName(node.rawName) ||
utils.isMathWellKnownElementName(node.rawName)
) {
return
}
usedComponents.add(node.rawName)
},
/** @param {VDirective} node */
"VAttribute[directive=true][key.name.name='bind'][key.argument.name='is'], VAttribute[directive=true][key.name.name='is']"(
node
) {
if (
!node.value || // `<component :is>`
node.value.type !== 'VExpressionContainer' ||
!node.value.expression // `<component :is="">`
)
return
if (node.value.expression.type === 'Literal') {
usedComponents.add(node.value.expression.value)
} else if (ignoreWhenBindingPresent) {
ignoreReporting = true
}
},
/** @param {VAttribute} node */
"VAttribute[directive=false][key.name='is']"(node) {
if (!node.value) {
return
}
const value = node.value.value.startsWith('vue:') // Usage on native elements 3.1+
? node.value.value.slice(4)
: node.value.value
usedComponents.add(value)
},
/** @param {VElement} node */
"VElement[name='template']"(node) {
templateLocation = templateLocation || node.loc.start
},
/** @param {VElement} node */
"VElement[name='template']:exit"(node) {
if (
node.loc.start !== templateLocation ||
ignoreReporting ||
utils.hasAttribute(node, 'src')
)
return
for (const { node, name } of registeredComponents) {
// If the component name is PascalCase or camelCase
// it can be used in various of ways inside template,
// like "theComponent", "The-component" etc.
// but except snake_case
if (casing.isPascalCase(name) || casing.isCamelCase(name)) {
if (
[...usedComponents].some(
(n) =>
!n.includes('_') &&
(name === casing.pascalCase(n) ||
name === casing.camelCase(n))
)
) {
continue
}
} else {
// In any other case the used component name must exactly match
// the registered name
if (usedComponents.has(name)) {
continue
}
}
context.report({
node,
messageId: 'unused',
data: {
name
}
})
}
}
},
utils.executeOnVue(context, (obj) => {
registeredComponents = utils.getRegisteredComponents(obj)
})
)
}
}