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

Commit c0cb2db

Browse files
authored
fix: allow again use of @event to specify event name (#117)
fixes #115
1 parent a54be11 commit c0cb2db

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/script-handlers/__tests__/eventHandler.ts

+20
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,24 @@ describe('eventHandler', () => {
112112
}
113113
expect(documentation.getEventDescriptor).toHaveBeenCalledWith('success')
114114
})
115+
116+
it('should find events whose names are only spcified in the JSDoc', () => {
117+
const src = `
118+
export default {
119+
methods: {
120+
testEmit() {
121+
/**
122+
* @event success
123+
*/
124+
this.$emit(A.successEventName, 1, 2)
125+
}
126+
}
127+
}
128+
`
129+
const def = parse(src)
130+
if (def.component) {
131+
eventHandler(documentation, def.component, def.ast)
132+
}
133+
expect(documentation.getEventDescriptor).toHaveBeenCalledWith('success')
134+
})
115135
})

src/script-handlers/eventHandler.ts

+20-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
EventDescriptor,
99
ParamTag,
1010
ParamType,
11+
Tag,
1112
} from '../Documentation'
1213
import getDocblock from '../utils/getDocblock'
1314
import getDoclets from '../utils/getDoclets'
@@ -34,24 +35,31 @@ export default function eventHandler(
3435
if (!args.length) {
3536
return false
3637
}
38+
// fetch the leading comments on the wrapping expression
39+
const docblock = getDocblock(pathExpression.parentPath)
40+
const doclets = getDoclets(docblock || '')
41+
let eventName: string
42+
const eventTags = doclets.tags ? doclets.tags.filter(d => d.title === 'event') : []
43+
44+
// if someone wants to document it with anything else, they can force it
45+
if (eventTags.length) {
46+
eventName = (eventTags[0] as Tag).content as string
47+
} else {
48+
let firstArg = pathExpression.get('arguments', 0)
49+
if (bt.isIdentifier(firstArg.node)) {
50+
firstArg = resolveIdentifier(astPath, firstArg)
51+
}
3752

38-
let firstArg = pathExpression.get('arguments', 0)
39-
if (bt.isIdentifier(firstArg.node)) {
40-
firstArg = resolveIdentifier(astPath, firstArg)
41-
}
42-
43-
if (!bt.isStringLiteral(firstArg.node)) {
44-
return false
53+
if (!bt.isStringLiteral(firstArg.node)) {
54+
return false
55+
}
56+
eventName = firstArg.node.value
4557
}
4658

47-
const eventName = firstArg.node.value
48-
4959
// if this event is documented somewhere else leave it alone
5060
const evtDescriptor = documentation.getEventDescriptor(eventName)
5161

52-
// fetch the leading comments on the wrapping expression
53-
const docblock = getDocblock(pathExpression.parentPath)
54-
setEventDescriptor(evtDescriptor, getDoclets(docblock || ''))
62+
setEventDescriptor(evtDescriptor, doclets)
5563

5664
if (args.length > 1 && !evtDescriptor.type) {
5765
evtDescriptor.type = {

0 commit comments

Comments
 (0)