Skip to content
This repository has been archived by the owner on Mar 8, 2019. It is now read-only.

fix: allow again use of @event to specify event name #117

Merged
merged 1 commit into from
Feb 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/script-handlers/__tests__/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,24 @@ describe('eventHandler', () => {
}
expect(documentation.getEventDescriptor).toHaveBeenCalledWith('success')
})

it('should find events whose names are only spcified in the JSDoc', () => {
const src = `
export default {
methods: {
testEmit() {
/**
* @event success
*/
this.$emit(A.successEventName, 1, 2)
}
}
}
`
const def = parse(src)
if (def.component) {
eventHandler(documentation, def.component, def.ast)
}
expect(documentation.getEventDescriptor).toHaveBeenCalledWith('success')
})
})
32 changes: 20 additions & 12 deletions src/script-handlers/eventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
EventDescriptor,
ParamTag,
ParamType,
Tag,
} from '../Documentation'
import getDocblock from '../utils/getDocblock'
import getDoclets from '../utils/getDoclets'
Expand All @@ -34,24 +35,31 @@ export default function eventHandler(
if (!args.length) {
return false
}
// fetch the leading comments on the wrapping expression
const docblock = getDocblock(pathExpression.parentPath)
const doclets = getDoclets(docblock || '')
let eventName: string
const eventTags = doclets.tags ? doclets.tags.filter(d => d.title === 'event') : []

// if someone wants to document it with anything else, they can force it
if (eventTags.length) {
eventName = (eventTags[0] as Tag).content as string
} else {
let firstArg = pathExpression.get('arguments', 0)
if (bt.isIdentifier(firstArg.node)) {
firstArg = resolveIdentifier(astPath, firstArg)
}

let firstArg = pathExpression.get('arguments', 0)
if (bt.isIdentifier(firstArg.node)) {
firstArg = resolveIdentifier(astPath, firstArg)
}

if (!bt.isStringLiteral(firstArg.node)) {
return false
if (!bt.isStringLiteral(firstArg.node)) {
return false
}
eventName = firstArg.node.value
}

const eventName = firstArg.node.value

// if this event is documented somewhere else leave it alone
const evtDescriptor = documentation.getEventDescriptor(eventName)

// fetch the leading comments on the wrapping expression
const docblock = getDocblock(pathExpression.parentPath)
setEventDescriptor(evtDescriptor, getDoclets(docblock || ''))
setEventDescriptor(evtDescriptor, doclets)

if (args.length > 1 && !evtDescriptor.type) {
evtDescriptor.type = {
Expand Down