-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathts-ast-utils.js
194 lines (182 loc) · 5.76 KB
/
ts-ast-utils.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
191
192
193
194
const { findVariable } = require('eslint-utils')
/**
* @typedef {import('@typescript-eslint/types').TSESTree.TypeNode} TypeNode
* @typedef {import('@typescript-eslint/types').TSESTree.TSInterfaceBody} TSInterfaceBody
* @typedef {import('@typescript-eslint/types').TSESTree.TSTypeLiteral} TSTypeLiteral
*/
/**
* @typedef {import('../../typings/eslint-plugin-vue/util-types/utils').ComponentTypeProp} ComponentTypeProp
*/
module.exports = {
getComponentPropsFromTypeDefine
}
/**
* @param {TypeNode} node
* @returns {node is TSTypeLiteral}
*/
function isTSTypeLiteral(node) {
return node.type === 'TSTypeLiteral'
}
/**
* Get all props by looking at all component's properties
* @param {RuleContext} context The ESLint rule context object.
* @param {TypeNode} propsNode Type with props definition
* @return {ComponentTypeProp[]} Array of component props
*/
function getComponentPropsFromTypeDefine(context, propsNode) {
/** @type {TSInterfaceBody | TSTypeLiteral|null} */
const defNode = resolveQualifiedType(context, propsNode, isTSTypeLiteral)
if (!defNode) {
return []
}
return [...extractRuntimeProps(context, defNode)]
}
/**
* @see https://github.com/vuejs/vue-next/blob/253ca2729d808fc051215876aa4af986e4caa43c/packages/compiler-sfc/src/compileScript.ts#L1512
* @param {RuleContext} context The ESLint rule context object.
* @param {TSTypeLiteral | TSInterfaceBody} node
* @returns {IterableIterator<ComponentTypeProp>}
*/
function* extractRuntimeProps(context, node) {
const members = node.type === 'TSTypeLiteral' ? node.members : node.body
for (const m of members) {
if (
(m.type === 'TSPropertySignature' || m.type === 'TSMethodSignature') &&
m.key.type === 'Identifier'
) {
let type
if (m.type === 'TSMethodSignature') {
type = ['Function']
} else if (m.typeAnnotation) {
type = inferRuntimeType(context, m.typeAnnotation.typeAnnotation)
}
yield {
type: 'type',
key: /** @type {Identifier} */ (m.key),
propName: m.key.name,
value: null,
node: /** @type {TSPropertySignature | TSMethodSignature} */ (m),
required: !m.optional,
types: type || [`null`]
}
}
}
}
/**
* @see https://github.com/vuejs/vue-next/blob/253ca2729d808fc051215876aa4af986e4caa43c/packages/compiler-sfc/src/compileScript.ts#L425
*
* @param {RuleContext} context The ESLint rule context object.
* @param {TypeNode} node
* @param {(n: TypeNode)=> boolean } qualifier
*/
function resolveQualifiedType(context, node, qualifier) {
if (qualifier(node)) {
return node
}
if (node.type === 'TSTypeReference' && node.typeName.type === 'Identifier') {
const refName = node.typeName.name
const variable = findVariable(context.getScope(), refName)
if (variable && variable.defs.length === 1) {
const def = variable.defs[0]
if (def.node.type === 'TSInterfaceDeclaration') {
return /** @type {any} */ (def.node).body
}
if (def.node.type === 'TSTypeAliasDeclaration') {
const typeAnnotation = /** @type {any} */ (def.node).typeAnnotation
return qualifier(typeAnnotation) ? typeAnnotation : null
}
}
}
}
/**
* @param {RuleContext} context The ESLint rule context object.
* @param {TypeNode} node
* @param {Set<TypeNode>} [checked]
* @returns {string[]}
*/
function inferRuntimeType(context, node, checked = new Set()) {
switch (node.type) {
case 'TSStringKeyword':
return ['String']
case 'TSNumberKeyword':
return ['Number']
case 'TSBooleanKeyword':
return ['Boolean']
case 'TSObjectKeyword':
return ['Object']
case 'TSTypeLiteral':
return ['Object']
case 'TSFunctionType':
return ['Function']
case 'TSArrayType':
case 'TSTupleType':
return ['Array']
case 'TSLiteralType':
switch (node.literal.type) {
//@ts-ignore ?
case 'StringLiteral':
return ['String']
//@ts-ignore ?
case 'BooleanLiteral':
return ['Boolean']
//@ts-ignore ?
case 'NumericLiteral':
//@ts-ignore ?
// eslint-disable-next-line no-fallthrough
case 'BigIntLiteral':
return ['Number']
default:
return [`null`]
}
case 'TSTypeReference':
if (node.typeName.type === 'Identifier') {
const variable = findVariable(context.getScope(), node.typeName.name)
if (variable && variable.defs.length === 1) {
const def = variable.defs[0]
if (def.node.type === 'TSInterfaceDeclaration') {
return [`Object`]
}
if (def.node.type === 'TSTypeAliasDeclaration') {
const typeAnnotation = /** @type {any} */ (def.node).typeAnnotation
if (!checked.has(typeAnnotation)) {
checked.add(typeAnnotation)
return inferRuntimeType(context, typeAnnotation, checked)
}
}
}
switch (node.typeName.name) {
case 'Array':
case 'Function':
case 'Object':
case 'Set':
case 'Map':
case 'WeakSet':
case 'WeakMap':
return [node.typeName.name]
case 'Record':
case 'Partial':
case 'Readonly':
case 'Pick':
case 'Omit':
case 'Exclude':
case 'Extract':
case 'Required':
case 'InstanceType':
return ['Object']
}
}
return [`null`]
case 'TSUnionType':
const set = new Set()
for (const t of node.types) {
for (const tt of inferRuntimeType(context, t, checked)) {
set.add(tt)
}
}
return [...set]
case 'TSIntersectionType':
return ['Object']
default:
return [`null`] // no runtime check
}
}