-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathprocess.ts
330 lines (295 loc) · 9.55 KB
/
process.ts
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { DEVTOOLS_API_INSPECT_STATE_KEY, camelize } from '@vue/devtools-shared'
import type { VueAppInstance } from '../../../types'
import type { InspectorState } from '../types'
import { ensurePropertyExists, returnError } from '../utils'
import { vueBuiltins } from './constants'
import { getPropType, getSetupStateType, toRaw } from './util'
function mergeOptions(
to: any,
from: any,
instance: VueAppInstance,
) {
if (typeof from === 'function')
from = from.options
if (!from)
return to
const { mixins, extends: extendsOptions } = from
extendsOptions && mergeOptions(to, extendsOptions, instance)
mixins
&& mixins.forEach(m =>
mergeOptions(to, m, instance),
)
for (const key of ['computed', 'inject']) {
if (Object.prototype.hasOwnProperty.call(from, key)) {
if (!to[key])
to[key] = from[key]
else
Object.assign(to[key], from[key])
}
}
return to
}
// @TODO: maybe should be exported from vue core?
function resolveMergedOptions(
instance: VueAppInstance,
) {
const raw = instance?.type
if (!raw)
return {}
const { mixins, extends: extendsOptions } = raw
const globalMixins = instance.appContext.mixins
if (!globalMixins.length && !mixins && !extendsOptions)
return raw
const options = {}
globalMixins.forEach(m => mergeOptions(options, m, instance))
mergeOptions(options, raw, instance)
return options
}
/**
* Process the props of an instance.
* Make sure return a plain object because window.postMessage()
* will throw an Error if the passed object contains Functions.
*
*/
function processProps(instance: VueAppInstance) {
const props: InspectorState[] = []
const propDefinitions = instance.type.props
for (const key in instance.props) {
const propDefinition = propDefinitions ? propDefinitions[key] : null
const camelizeKey = camelize(key)
props.push({
type: 'props',
key: camelizeKey,
value: returnError(() => instance.props[key]),
meta: propDefinition
? {
type: propDefinition.type ? getPropType(propDefinition.type) : 'any',
required: !!propDefinition.required,
...propDefinition.default
? {
default: propDefinition.default.toString(),
}
: {},
}
: { type: 'invalid' },
})
}
return props
}
/**
* Process state, filtering out props and "clean" the result
* with a JSON dance. This removes functions which can cause
* errors during structured clone used by window.postMessage.
*
*/
function processState(instance: VueAppInstance) {
const type = instance.type
const props = type.props
const getters
= type.vuex
&& type.vuex.getters
const computedDefs = type.computed
const data = {
...instance.data,
...instance.renderContext,
}
return Object.keys(data)
.filter(key => (
!(props && key in props)
&& !(getters && key in getters)
&& !(computedDefs && key in computedDefs)
))
.map(key => ({
key,
type: 'data',
value: returnError(() => data[key]),
editable: true,
}))
}
function getStateTypeAndName(info: ReturnType<typeof getSetupStateType>) {
// the order is important !!!
const stateType = info.computed ? 'computed' : info.ref ? 'ref' : info.reactive ? 'reactive' : null
const stateTypeName = stateType ? `${stateType.charAt(0).toUpperCase()}${stateType.slice(1)}` : null
return {
stateType,
stateTypeName,
}
}
function processSetupState(instance: VueAppInstance) {
const raw = instance.devtoolsRawSetupState || {}
const customInspectStateKeys = raw[DEVTOOLS_API_INSPECT_STATE_KEY] || []
// Shallow clone to prevent mutating the original
const setupState = {
...instance.setupState,
...customInspectStateKeys.reduce((map, key) => {
map[key] = raw[key]
return map
}, {}),
}
return Object.keys(setupState)
.filter(key => !vueBuiltins.has(key) && key.split(/(?=[A-Z])/)[0] !== 'use')
.map((key) => {
const value = returnError(() => toRaw(setupState[key])) as unknown as {
render: Function
__asyncLoader: Function
}
const rawData = raw[key] as {
effect: {
raw: Function
fn: Function
}
}
let result: Partial<InspectorState>
let isOtherType = typeof value === 'function'
|| (ensurePropertyExists(value, 'render') && typeof value.render === 'function') // Components
|| (ensurePropertyExists(value, '__asyncLoader') && typeof value.__asyncLoader === 'function') // Components
|| (typeof value === 'object' && value && ('setup' in value || 'props' in value)) // Components
|| /^v[A-Z]/.test(key) // Directives
if (rawData) {
const info = getSetupStateType(rawData)
const { stateType, stateTypeName } = getStateTypeAndName(info)
const isState = info.ref || info.computed || info.reactive
const raw = ensurePropertyExists(rawData, 'effect')
? rawData.effect?.raw?.toString() || rawData.effect?.fn?.toString()
: null
if (stateType)
isOtherType = false
result = {
...stateType ? { stateType, stateTypeName: stateTypeName! } : {},
...raw ? { raw } : {},
editable: isState && !info.readonly,
}
}
const type = isOtherType ? 'setup (other)' : 'setup'
return {
key,
value,
type,
// @ts-expect-error ignore
...result,
} satisfies InspectorState
})
}
/**
* Process the computed properties of an instance.
*/
function processComputed(instance: VueAppInstance, mergedType: Record<string, unknown>) {
const type = mergedType
const computed: InspectorState[] = []
const defs = type.computed || {}
// use for...in here because if 'computed' is not defined
// on component, computed properties will be placed in prototype
// and Object.keys does not include
// properties from object's prototype
for (const key in defs) {
const def = defs[key]
const type = typeof def === 'function' && def.vuex
? 'vuex bindings'
: 'computed'
computed.push({
type,
key,
value: returnError(() => instance?.proxy?.[key]),
editable: typeof def.set === 'function',
})
}
return computed
}
function processAttrs(instance: VueAppInstance) {
return Object.keys(instance.attrs)
.map(key => ({
type: 'attrs',
key,
value: returnError(() => instance.attrs[key]),
}))
}
function processProvide(instance: VueAppInstance) {
return Reflect.ownKeys(instance.provides)
.map(key => ({
type: 'provided',
key: key.toString(),
value: returnError(() => instance.provides[key]),
}))
}
function processInject(instance: VueAppInstance, mergedType: Record<string, unknown>) {
if (!mergedType?.inject)
return []
let keys: any[] = []
let defaultValue
if (Array.isArray(mergedType.inject)) {
keys = mergedType.inject.map(key => ({
key,
originalKey: key,
}))
}
else {
keys = Reflect.ownKeys(mergedType.inject).map((key) => {
const value = (mergedType.inject as Record<symbol, unknown>)[key]
let originalKey
if (typeof value === 'string' || typeof value === 'symbol') {
originalKey = value
}
else {
originalKey = value.from
defaultValue = value.default
}
return {
key,
originalKey,
}
})
}
return keys.map(({ key, originalKey }) => ({
type: 'injected',
key: originalKey && key !== originalKey ? `${originalKey.toString()} ➞ ${key.toString()}` : key.toString(),
// eslint-disable-next-line no-prototype-builtins
value: returnError(() => instance.ctx.hasOwnProperty(key) ? instance.ctx[key] : instance.provides.hasOwnProperty(originalKey) ? instance.provides[originalKey] : defaultValue),
}))
}
function processRefs(instance: VueAppInstance) {
return Object.keys(instance.refs)
.map(key => ({
type: 'template refs',
key,
value: returnError(() => instance.refs[key]),
}))
}
function processEventListeners(instance: VueAppInstance) {
const emitsDefinition = instance.type.emits
const declaredEmits = Array.isArray(emitsDefinition) ? emitsDefinition : Object.keys(emitsDefinition ?? {})
const keys = Object.keys(instance?.vnode?.props ?? {})
const result: InspectorState[] = []
for (const key of keys) {
const [prefix, ...eventNameParts] = key.split(/(?=[A-Z])/)
if (prefix === 'on') {
const eventName = eventNameParts.join('-').toLowerCase()
const isDeclared = declaredEmits.includes(eventName)
result.push({
type: 'event listeners',
key: eventName,
value: {
_custom: {
displayText: isDeclared ? '✅ Declared' : '⚠️ Not declared',
key: isDeclared ? '✅ Declared' : '⚠️ Not declared',
value: isDeclared ? '✅ Declared' : '⚠️ Not declared',
tooltipText: !isDeclared ? `The event <code>${eventName}</code> is not declared in the <code>emits</code> option. It will leak into the component's attributes (<code>$attrs</code>).` : null,
},
},
})
}
}
return result
}
export function processInstanceState(instance: VueAppInstance) {
const mergedType = resolveMergedOptions(instance)
return processProps(instance).concat(
processState(instance),
processSetupState(instance),
processComputed(instance, mergedType),
processAttrs(instance),
processProvide(instance),
processInject(instance, mergedType),
processRefs(instance),
processEventListeners(instance),
)
}