-
-
Notifications
You must be signed in to change notification settings - Fork 651
/
no-unused-properties.js
167 lines (142 loc) · 4.58 KB
/
no-unused-properties.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
/**
* @fileoverview Disallow unused properties, data and computed properties.
* @author Learning Equality
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const remove = require('lodash/remove')
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------
const GROUP_PROPERTY = 'props'
const GROUP_DATA = 'data'
const GROUP_COMPUTED_PROPERTY = 'computed'
const GROUP_WATCHER = 'watch'
const PROPERTY_LABEL = {
[GROUP_PROPERTY]: 'property',
[GROUP_DATA]: 'data',
[GROUP_COMPUTED_PROPERTY]: 'computed property'
}
// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
/**
* Extract names from references objects.
*/
const getReferencesNames = references => {
if (!references || !references.length) {
return []
}
return references.map(reference => {
if (!reference.id || !reference.id.name) {
return
}
return reference.id.name
})
}
/**
* Report all unused properties.
*/
const reportUnusedProperties = (context, properties) => {
if (!properties || !properties.length) {
return
}
properties.forEach(property => {
context.report({
node: property.node,
message: `Unused ${PROPERTY_LABEL[property.groupName]} found: "${property.name}"`
})
})
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow unused properties, data and computed properties',
category: undefined,
url: 'https://eslint.vuejs.org/rules/no-unused-properties.html'
},
fixable: null,
schema: []
},
create (context) {
let hasTemplate
let rootTemplateEnd
let unusedProperties = []
const thisExpressionsVariablesNames = []
const initialize = {
Program (node) {
if (context.parserServices.getTemplateBodyTokenStore == null) {
context.report({
loc: { line: 1, column: 0 },
message:
'Use the latest vue-eslint-parser. See also https://vuejs.github.io/eslint-plugin-vue/user-guide/#what-is-the-use-the-latest-vue-eslint-parser-error.'
})
return
}
hasTemplate = Boolean(node.templateBody)
}
}
const scriptVisitor = Object.assign(
{},
{
'MemberExpression[object.type="ThisExpression"][property.type="Identifier"][property.name]' (
node
) {
thisExpressionsVariablesNames.push(node.property.name)
}
},
utils.executeOnVue(context, obj => {
unusedProperties = Array.from(
utils.iterateProperties(obj, new Set([GROUP_PROPERTY, GROUP_DATA, GROUP_COMPUTED_PROPERTY]))
)
const watchers = Array.from(utils.iterateProperties(obj, new Set([GROUP_WATCHER])))
const watchersNames = watchers.map(watcher => watcher.name)
remove(unusedProperties, property => {
return (
thisExpressionsVariablesNames.includes(property.name) ||
watchersNames.includes(property.name)
)
})
if (!hasTemplate && unusedProperties.length) {
reportUnusedProperties(context, unusedProperties)
}
})
)
const templateVisitor = {
'VExpressionContainer[expression!=null][references]' (node) {
const referencesNames = getReferencesNames(node.references)
remove(unusedProperties, property => {
return referencesNames.includes(property.name)
})
},
// save root template end location - just a helper to be used
// for a decision if a parser reached the end of the root template
"VElement[name='template']" (node) {
if (rootTemplateEnd) {
return
}
rootTemplateEnd = node.loc.end
},
"VElement[name='template']:exit" (node) {
if (node.loc.end !== rootTemplateEnd) {
return
}
if (unusedProperties.length) {
reportUnusedProperties(context, unusedProperties)
}
}
}
return Object.assign(
{},
initialize,
utils.defineTemplateBodyVisitor(context, templateVisitor, scriptVisitor)
)
}
}