Skip to content

Commit

Permalink
feat(vue3): inspect event listeners on components
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Jan 20, 2023
1 parent 3fa04ed commit 01e7c21
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 9 deletions.
26 changes: 26 additions & 0 deletions packages/app-backend-vue3/src/components/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function getInstanceState (instance) {
processProvide(instance),
processInject(instance, mergedType),
processRefs(instance),
processEventListeners(instance),
)
}

Expand Down Expand Up @@ -367,6 +368,31 @@ function processRefs (instance) {
}))
}

function processEventListeners (instance) {
const emitsDefinition = instance.type.emits
const declaredEmits = Array.isArray(emitsDefinition) ? emitsDefinition : Object.keys(emitsDefinition ?? {})
const keys = Object.keys(instance.vnode.props ?? {})
const result = []
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: {
display: isDeclared ? '✅ Declared' : '⚠️ Not declared',
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,
},
},
})
}
}
return result
}

export function editState ({ componentInstance, path, state, type }: HookPayloads[Hooks.EDIT_COMPONENT_STATE], stateEditor: StateEditor, ctx: BackendContext) {
if (!['data', 'props', 'computed', 'setup'].includes(type)) return
let target: any
Expand Down
10 changes: 10 additions & 0 deletions packages/app-frontend/src/assets/style/index.postcss
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.v-popper__popper.v-popper--theme-tooltip code {
@apply bg-gray-500/50 rounded px-1 text-[11px] font-mono;
}

// @TODO remove when vue-ui style is updated

.vue-ui-group:not(.vertical) > .indicator >.content {
margin: 0 12px;
width: calc(100% - 24px);
}
8 changes: 0 additions & 8 deletions packages/app-frontend/src/assets/style/index.styl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ $arrow-color = rgba(100, 100, 100, 0.5)
.vue-ui-dark-mode .v-popper__popper.v-popper--theme-tooltip .vue-ui-icon svg
fill #666


.v-popper__popper.v-popper--theme-dropdown .v-popper__inner
max-height calc(100vh - 32px - 8px - 4px)
overflow-y auto
Expand All @@ -153,10 +152,3 @@ $arrow-color = rgba(100, 100, 100, 0.5)
.right-icon-reveal:not(:hover)
.vue-ui-icon.right
opacity 0

// @TODO remove when vue-ui style is updated

.vue-ui-group:not(.vertical) > .indicator >.content {
margin: 0 12px;
width: calc(100% - 24px);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const keyOrder = {
refs: 6,
$attrs: 7,
attrs: 7,
'event listeners': 7,
'setup (other)': 8,
}

Expand Down
1 change: 1 addition & 0 deletions packages/app-frontend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './assets/style/index.styl'
import './assets/style/index.postcss'

import { initStorage, Shell } from '@vue-devtools/shared-utils'
import { createApp, connectApp } from './app'
Expand Down
13 changes: 12 additions & 1 deletion packages/shell-dev-vue3/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export default {
stopTimer () {
clearInterval(this.timer)
},

onFoo (...args) {
console.log('on foo', ...args)
},

onBar (...args) {
console.log('on bar', ...args)
},
},
}
</script>
Expand Down Expand Up @@ -116,7 +124,10 @@ export default {
<Child question="Life" />
<NestedMore />
<NativeTypes ref="nativeTypes" />
<EventEmit />
<EventEmit
@foo="onFoo"
@bar="onBar"
/>
<EventNesting />
<AsyncComponent />
<SuspenseExample />
Expand Down
7 changes: 7 additions & 0 deletions packages/shell-dev-vue3/src/EventEmit.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
<script setup>
// eslint-disable-next-line no-undef
defineEmits([
'foo',
])
</script>

<template>
<div style="display: inline-block;">
<button @click="$emit('foo', 42, 'a')">
Expand Down
4 changes: 4 additions & 0 deletions packages/shell-dev-vue3/src/EventNesting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export default {
default: 0,
},
},

emits: [
'notify',
],
}
</script>

Expand Down

0 comments on commit 01e7c21

Please sign in to comment.