|
| 1 | +/** Compile-time event contracts. Component setup is never executed to read them. */ |
| 2 | +import { macroCall } from './component-contract' |
| 3 | +import { parseStringLiteral } from './directive-arguments' |
| 4 | +import { Tokenizer } from './parser/tokenizer' |
| 5 | +import { localType, skipBalanced, splitTopLevel } from './props-declaration' |
| 6 | +import type { ComponentLibraryEvent } from './types' |
| 7 | + |
| 8 | + |
| 9 | +function eventName(raw: string): string | undefined { |
| 10 | + const name = raw.trim() |
| 11 | + return /^[A-Za-z_$][\w$]*$/.test(name) ? name : parseStringLiteral(name) ?? undefined |
| 12 | +} |
| 13 | + |
| 14 | +/** The native module emitter puts one argument in detail, and multiple in a tuple. */ |
| 15 | +function detailFromTuple(tuple: string): string { |
| 16 | + const members = splitTopLevel(tuple.trim().slice(1, -1), ',', true) |
| 17 | + if (members.length !== 1) return tuple.trim() |
| 18 | + const member = members[0]! |
| 19 | + if (member.startsWith('...')) return tuple.trim() |
| 20 | + const labelled = /^(?:[A-Za-z_$][\w$]*)(\?)?\s*:\s*([\s\S]+)$/.exec(member) |
| 21 | + return labelled ? `${labelled[2]}${labelled[1] ? ' | undefined' : ''}` : member |
| 22 | +} |
| 23 | + |
| 24 | +export interface EmitsDeclaration { |
| 25 | + text: string |
| 26 | + start: number |
| 27 | + end: number |
| 28 | + typeName?: string |
| 29 | + events: Record<string, ComponentLibraryEvent> |
| 30 | +} |
| 31 | + |
| 32 | +export function parseEmitsDeclaration(code: string): EmitsDeclaration | null { |
| 33 | + const call = macroCall(code, ['defineEmits'], true) |
| 34 | + if (!call) return null |
| 35 | + const events: Record<string, ComponentLibraryEvent> = Object.create(null) |
| 36 | + const typeName = /^[A-Za-z_$][\w$]*$/.test(call.type.trim()) ? call.type.trim() : undefined |
| 37 | + const type = typeName ? localType(code, typeName) : call.type.trim() |
| 38 | + if (type?.startsWith('{')) { |
| 39 | + for (const member of splitTopLevel(type.slice(1, -1), ';,\n', true)) { |
| 40 | + // The documented overload form: (event: 'change', value: string): void. |
| 41 | + if (member.startsWith('(')) { |
| 42 | + const end = skipBalanced(member, 0, '(', ')') |
| 43 | + if (end < 0) continue |
| 44 | + const parameters = splitTopLevel(member.slice(1, end - 1), ',', true) |
| 45 | + const first = parameters.shift() || '' |
| 46 | + const colon = first.indexOf(':') |
| 47 | + if (colon < 0) continue |
| 48 | + for (const literal of splitTopLevel(first.slice(colon + 1), '|', true)) { |
| 49 | + const name = eventName(literal) |
| 50 | + if (name !== undefined) events[name] = { detailType: detailFromTuple(`[${parameters.join(', ')}]`) } |
| 51 | + } |
| 52 | + continue |
| 53 | + } |
| 54 | + const tokens = new Tokenizer(member, 'javascript').tokenize().filter(token => !['WHITESPACE', 'COMMENT', 'EOF'].includes(token.type)) |
| 55 | + const key = tokens[0], colon = tokens.findIndex(token => token.value === ':') |
| 56 | + if (!key || colon < 0) continue |
| 57 | + const name = eventName(key.value) |
| 58 | + const value = member.slice(tokens[colon]!.end).trim() |
| 59 | + if (name !== undefined) events[name] = { detailType: value.startsWith('[') ? detailFromTuple(value) : value } |
| 60 | + } |
| 61 | + } |
| 62 | + else { |
| 63 | + const argument = call.args[0]?.trim() || '' |
| 64 | + if (argument.startsWith('[')) { |
| 65 | + for (const item of splitTopLevel(argument.slice(1, -1), ',', false)) { |
| 66 | + const name = eventName(item) |
| 67 | + if (name !== undefined) events[name] = { detailType: 'unknown' } |
| 68 | + } |
| 69 | + } |
| 70 | + else if (argument.startsWith('{')) { |
| 71 | + for (const member of splitTopLevel(argument.slice(1, -1), ',', false)) { |
| 72 | + const key = new Tokenizer(member, 'javascript').tokenize().find(token => !['WHITESPACE', 'COMMENT'].includes(token.type)) |
| 73 | + const name = key && eventName(key.value) |
| 74 | + if (name !== undefined) events[name] = { detailType: 'unknown' } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return { ...call, text: code.slice(call.start, call.end), typeName, events } |
| 79 | +} |
0 commit comments