Skip to content

Commit 01e7c21

Browse files
committed
feat(vue3): inspect event listeners on components
1 parent 3fa04ed commit 01e7c21

File tree

8 files changed

+61
-9
lines changed

8 files changed

+61
-9
lines changed

packages/app-backend-vue3/src/components/data.ts

+26
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ function getInstanceState (instance) {
8080
processProvide(instance),
8181
processInject(instance, mergedType),
8282
processRefs(instance),
83+
processEventListeners(instance),
8384
)
8485
}
8586

@@ -367,6 +368,31 @@ function processRefs (instance) {
367368
}))
368369
}
369370

371+
function processEventListeners (instance) {
372+
const emitsDefinition = instance.type.emits
373+
const declaredEmits = Array.isArray(emitsDefinition) ? emitsDefinition : Object.keys(emitsDefinition ?? {})
374+
const keys = Object.keys(instance.vnode.props ?? {})
375+
const result = []
376+
for (const key of keys) {
377+
const [prefix, ...eventNameParts] = key.split(/(?=[A-Z])/)
378+
if (prefix === 'on') {
379+
const eventName = eventNameParts.join('-').toLowerCase()
380+
const isDeclared = declaredEmits.includes(eventName)
381+
result.push({
382+
type: 'event listeners',
383+
key: eventName,
384+
value: {
385+
_custom: {
386+
display: isDeclared ? '✅ Declared' : '⚠️ Not declared',
387+
tooltip: !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,
388+
},
389+
},
390+
})
391+
}
392+
}
393+
return result
394+
}
395+
370396
export function editState ({ componentInstance, path, state, type }: HookPayloads[Hooks.EDIT_COMPONENT_STATE], stateEditor: StateEditor, ctx: BackendContext) {
371397
if (!['data', 'props', 'computed', 'setup'].includes(type)) return
372398
let target: any
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.v-popper__popper.v-popper--theme-tooltip code {
2+
@apply bg-gray-500/50 rounded px-1 text-[11px] font-mono;
3+
}
4+
5+
// @TODO remove when vue-ui style is updated
6+
7+
.vue-ui-group:not(.vertical) > .indicator >.content {
8+
margin: 0 12px;
9+
width: calc(100% - 24px);
10+
}

packages/app-frontend/src/assets/style/index.styl

-8
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ $arrow-color = rgba(100, 100, 100, 0.5)
139139
.vue-ui-dark-mode .v-popper__popper.v-popper--theme-tooltip .vue-ui-icon svg
140140
fill #666
141141

142-
143142
.v-popper__popper.v-popper--theme-dropdown .v-popper__inner
144143
max-height calc(100vh - 32px - 8px - 4px)
145144
overflow-y auto
@@ -153,10 +152,3 @@ $arrow-color = rgba(100, 100, 100, 0.5)
153152
.right-icon-reveal:not(:hover)
154153
.vue-ui-icon.right
155154
opacity 0
156-
157-
// @TODO remove when vue-ui style is updated
158-
159-
.vue-ui-group:not(.vertical) > .indicator >.content {
160-
margin: 0 12px;
161-
width: calc(100% - 24px);
162-
}

packages/app-frontend/src/features/inspector/StateInspector.vue

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const keyOrder = {
2323
refs: 6,
2424
$attrs: 7,
2525
attrs: 7,
26+
'event listeners': 7,
2627
'setup (other)': 8,
2728
}
2829

packages/app-frontend/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import './assets/style/index.styl'
2+
import './assets/style/index.postcss'
23

34
import { initStorage, Shell } from '@vue-devtools/shared-utils'
45
import { createApp, connectApp } from './app'

packages/shell-dev-vue3/src/App.vue

+12-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ export default {
8181
stopTimer () {
8282
clearInterval(this.timer)
8383
},
84+
85+
onFoo (...args) {
86+
console.log('on foo', ...args)
87+
},
88+
89+
onBar (...args) {
90+
console.log('on bar', ...args)
91+
},
8492
},
8593
}
8694
</script>
@@ -116,7 +124,10 @@ export default {
116124
<Child question="Life" />
117125
<NestedMore />
118126
<NativeTypes ref="nativeTypes" />
119-
<EventEmit />
127+
<EventEmit
128+
@foo="onFoo"
129+
@bar="onBar"
130+
/>
120131
<EventNesting />
121132
<AsyncComponent />
122133
<SuspenseExample />

packages/shell-dev-vue3/src/EventEmit.vue

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
<script setup>
2+
// eslint-disable-next-line no-undef
3+
defineEmits([
4+
'foo',
5+
])
6+
</script>
7+
18
<template>
29
<div style="display: inline-block;">
310
<button @click="$emit('foo', 42, 'a')">

packages/shell-dev-vue3/src/EventNesting.vue

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export default {
1010
default: 0,
1111
},
1212
},
13+
14+
emits: [
15+
'notify',
16+
],
1317
}
1418
</script>
1519

0 commit comments

Comments
 (0)