/
no-use-computed-property-like-method.js
190 lines (165 loc) · 5.41 KB
/
no-use-computed-property-like-method.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
187
188
189
190
/**
* @author tyankatsu <https://github.com/tyankatsu0105>
* See LICENSE file in root directory for full license.
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const {
defineVueVisitor,
getComputedProperties,
getComponentProps,
isProperty,
getStaticPropertyName,
unwrapTypes
} = require('../utils')
/**
* @typedef {import('../utils').ComponentComputedProperty} ComponentComputedProperty
* @typedef {import('../utils').ComponentObjectProp} ComponentObjectProp
*/
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/**
* @typedef { {key: string | null, value: BlockStatement | null} } ComponentMethodProperty
*/
/**
* Get all method by looking at all component's properties
* @param {ObjectExpression} componentObject Object with component definition
* @return {ComponentMethodProperty[]} Array of methods in format: [{key: String, value: ASTNode}]
*/
const getMethodProperties = (componentObject) => {
const methodsNode = componentObject.properties.find(
/**
* @param {ESNode} property
* @returns {property is (Property & { key: Identifier & {name: 'method'}, value: ObjectExpression })}
*/
(property) => {
return (
property.type === 'Property' &&
property.key.type === 'Identifier' &&
property.key.name === 'methods' &&
property.value.type === 'ObjectExpression'
)
}
)
if (!methodsNode) {
return []
}
return methodsNode.value.properties.filter(isProperty).map((method) => {
const key = getStaticPropertyName(method)
/** @type {Expression} */
const propValue = unwrapTypes(method.value)
/** @type {BlockStatement | null} */
let value = null
if (propValue.type === 'FunctionExpression') {
value = propValue.body
} else if (propValue.type === 'ObjectExpression') {
const get = propValue.properties.find(
/**
* @param {ESNode} p
* @returns { p is (Property & { value: FunctionExpression }) }
*/
(p) =>
p.type === 'Property' &&
p.key.type === 'Identifier' &&
p.key.name === 'get' &&
p.value.type === 'FunctionExpression'
)
value = get ? get.value.body : null
}
return { key, value }
})
}
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'enforce',
categories: undefined,
url:
'https://eslint.vuejs.org/rules/no-use-computed-property-like-method.html'
},
fixable: null,
schema: [],
messages: {
unexpected: 'Unexpected multiple objects. Merge objects.'
}
},
/** @param {RuleContext} context */
create(context) {
/**
* @typedef {object} ScopeStack
* @property {ScopeStack | null} upper
* @property {BlockStatement | Expression} body
*/
/** @type {Map<ObjectExpression, ComponentComputedProperty[]>} */
const computedPropertiesMap = new Map()
/**
* @typedef {object} ScopeStack
* @property {ScopeStack | null} upper
* @property {BlockStatement | Expression} body
*/
/** @type {Map<ObjectExpression, ComponentObjectProp[]>} */
const propsMap = new Map()
/**
* @typedef {object} ScopeStack
* @property {ScopeStack | null} upper
* @property {BlockStatement | Expression} body
*/
/** @type {Map<ObjectExpression, ComponentMthodProperty[]>} */
const methodPropertiesMap = new Map()
return defineVueVisitor(context, {
onVueObjectEnter(node) {
computedPropertiesMap.set(node, getComputedProperties(node))
propsMap.set(node, getComponentProps(node))
methodPropertiesMap.set(node, getMethodProperties(node))
},
/** @param {MemberExpression} node */
'MemberExpression[object.type="ThisExpression"]'(
node,
{ node: vueNode }
) {
if (node.property.type !== 'Identifier') return
const computedProperties = computedPropertiesMap
.get(vueNode)
.map((item) => item.key)
const methodProperties = methodPropertiesMap
.get(vueNode)
.map((item) => item.key)
/**
* propsProperties that excluded when type is array, and props property type is `Function`
*/
const propsProperties = propsMap.get(vueNode).reduce((acc, current) => {
// ignore `props: ['props1', 'props2']`
if (current.type === 'array') return acc
current.value.properties.reduce((accProperties, property) => {
// ignore `type: Function`
if (
property.key.name === 'type' &&
property.value.name === 'Function'
)
return accProperties
accProperties.push(property)
return accProperties
}, [])
acc.push(current.propName)
return acc
}, [])
const properties = [
...computedProperties,
...methodProperties,
...propsProperties
]
console.log(properties)
// if (!computedProperties.includes(node.property.name)) return
// context.report({
// node: node.property,
// loc: node.property.loc,
// messageId:
// })
}
})
}
}