-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathrequire-typed-object-prop.js
172 lines (160 loc) · 4.56 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
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
/**
* @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') {
const arrayTypeSuggestion = identifierNode.name === 'Array' ? '[]' : ''
context.report({
node: identifierNode,
messageId: 'expectedTypeAnnotation',
suggest: [
{
messageId: 'addTypeAnnotation',
data: { type: `any${arrayTypeSuggestion}` },
fix(fixer) {
return fixer.insertTextAfter(
identifierNode,
` as PropType<any${arrayTypeSuggestion}>`
)
}
},
{
messageId: 'addTypeAnnotation',
data: { type: `unknown${arrayTypeSuggestion}` },
fix(fixer) {
return fixer.insertTextAfter(
identifierNode,
` as PropType<unknown${arrayTypeSuggestion}>`
)
}
}
]
})
}
}
/**
* @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,
url: 'https://eslint.vuejs.org/rules/require-typed-object-prop.html'
},
fixable: null,
hasSuggestions: true,
schema: [],
messages: {
expectedTypeAnnotation: 'Expected type annotation on object prop.',
addTypeAnnotation: 'Add `{{ type }}` type annotation.'
}
},
/** @param {RuleContext} context */
create(context) {
const filename = context.getFilename()
if (!utils.isVueFile(filename) && !utils.isTypeScriptFile(filename)) {
return {}
}
if (utils.isVueFile(filename)) {
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 {}
}
}
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)
}
})
)
}
}