forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-typed-object-prop.js
121 lines (111 loc) · 3.01 KB
/
require-typed-object-prop.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
/**
* @author Przemysław Jan Beigert
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
/**
* @param {RuleContext} context
* @param {Identifier} identifierNode
*/
const checkPropIdentifierType = (context, identifierNode) => {
if (identifierNode.name === 'Object' || identifierNode.name === 'Array') {
context.report({
node: identifierNode,
message: 'Expected type annotation on object prop.'
})
}
}
/**
* @param {RuleContext} context
* @param {ArrayExpression} arrayNode
*/
const checkPropArrayType = (context, arrayNode) => {
for (const elementNode of arrayNode.elements) {
if (elementNode?.type === 'Identifier') {
checkPropIdentifierType(context, elementNode)
}
}
}
/**
* @param {RuleContext} context
* @param {ObjectExpression} objectNode
*/
const checkPropObjectType = (context, objectNode) => {
const typeProperty = objectNode.properties.find(
(prop) =>
prop.type === 'Property' &&
prop.key.type === 'Identifier' &&
prop.key.name === 'type'
)
if (!typeProperty || typeProperty.type !== 'Property') {
return
}
if (typeProperty.value.type === 'Identifier') {
// `foo: { type: String }`
checkPropIdentifierType(context, typeProperty.value)
} else if (typeProperty.value.type === 'ArrayExpression') {
// `foo: { type: [String, Boolean] }`
checkPropArrayType(context, typeProperty.value)
}
}
/**
* @param {import('../utils').ComponentProp} prop
* @param {RuleContext} context
*/
const checkProp = (prop, context) => {
if (prop.type !== 'object') {
return
}
switch (prop.node.value.type) {
case 'Identifier': {
// e.g. `foo: String`
checkPropIdentifierType(context, prop.node.value)
break
}
case 'ArrayExpression': {
// e.g. `foo: [String, Boolean]`
checkPropArrayType(context, prop.node.value)
break
}
case 'ObjectExpression': {
// e.g. `foo: { type: … }`
checkPropObjectType(context, prop.node.value)
return
}
}
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce adding type declarations to object props',
categories: undefined,
recommended: false,
url: 'https://eslint.vuejs.org/rules/require-typed-object-prop.html'
},
fixable: null,
schema: []
},
/** @param {RuleContext} context */
create(context) {
return utils.compositingVisitors(
utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(_node, props) {
for (const prop of props) {
checkProp(prop, context)
}
}
}),
utils.executeOnVue(context, (obj) => {
const props = utils.getComponentPropsFromOptions(obj)
for (const prop of props) {
checkProp(prop, context)
}
})
)
}
}