forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforce-types-on-object-props.js
119 lines (113 loc) · 3.42 KB
/
force-types-on-object-props.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
/**
* @author Przemysław Jan Beigert
* See LICENSE file in root directory for full license.
*/
'use strict'
// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------
/**
* Check if all keys and values from second object are resent in first object
*
* @param {{ [key: string]: any }} a object to
* @param {{ [key: string]: any }} b The string to escape.
* @returns {boolean} Returns the escaped string.
*/
const isLooksLike = (a, b) =>
a &&
b &&
Object.keys(b).every((bKey) => {
const bVal = b[bKey]
const aVal = a[bKey]
if (typeof bVal === 'function') {
return bVal(aVal)
}
return bVal == null || /^[bns]/.test(typeof bVal)
? bVal === aVal
: isLooksLike(aVal, bVal)
})
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce user to add type declaration to object props',
categories: ['base'],
recommended: false,
url: 'https://eslint.vuejs.org/rules/force-types-on-object-props.html'
},
fixable: null,
schema: []
},
/** @param {RuleContext} context */
create(context) {
return {
/** @param {ExportDefaultDeclaration} node */
ExportDefaultDeclaration(node) {
if (node.declaration.type !== 'ObjectExpression') {
return
}
if (!Array.isArray(node.declaration.properties)) {
return
}
const property = node.declaration.properties.find(
(property) =>
property.type === 'Property' &&
isLooksLike(property.key, { type: 'Identifier', name: 'props' }) &&
property.value.type === 'ObjectExpression'
)
if (
!property ||
property.type === 'SpreadElement' ||
!('properties' in property.value)
) {
return
}
const properties = property.value.properties
.filter(
(prop) =>
prop.type === 'Property' && prop.value.type === 'ObjectExpression'
)
.map((prop) =>
prop.value.properties.find((propValueProperty) =>
isLooksLike(propValueProperty.key, {
type: 'Identifier',
name: 'type'
})
)
)
for (const prop of properties) {
if (!prop) {
continue
}
if (isLooksLike(prop.value, { type: 'Identifier', name: 'Object' })) {
context.report({
node: prop,
message: 'Object props has to contains type.'
})
}
if (prop.value.type === 'TSAsExpression') {
const { typeAnnotation } = prop.value
if (
[
'TSAnyKeyword',
'TSTypeLiteral',
'TSUnknownKeyword',
'TSObjectKeyword'
].includes(typeAnnotation.type) ||
!typeAnnotation.typeName ||
typeAnnotation.typeName.name !== 'Prop'
) {
context.report({
node: prop,
message: 'Object props has to contains type.'
})
}
}
}
}
}
}
}