|
| 1 | +/** |
| 2 | + * Extract prop names from a Vue SFC's `defineProps` declaration. |
| 3 | + * |
| 4 | + * Supports all three Vue syntaxes: |
| 5 | + * 1. TypeScript generic: `defineProps<{ title: string, desc?: string }>()` |
| 6 | + * 2. Runtime object: `defineProps({ title: String, desc: { type: String } })` |
| 7 | + * 3. Array shorthand: `defineProps(['title', 'desc'])` |
| 8 | + * |
| 9 | + * Operates on raw source text (no AST parser needed). |
| 10 | + */ |
| 11 | + |
| 12 | +const RE_SCRIPT_SETUP = /<script\s[^>]*setup[^>]*>([\s\S]*?)<\/script>/ |
| 13 | + |
| 14 | +// Match top-level keys in a TS interface/object type literal: `{ title: string, desc?: number }` |
| 15 | +const RE_TS_PROP_KEY = /(?:^|[;,\n}])\s*(\w+)\s*(?:\?\s*)?:/g |
| 16 | + |
| 17 | +// Match top-level keys in a runtime object: `{ title: String, desc: { type: Number } }` |
| 18 | +const RE_RUNTIME_PROP_KEY = /(?:^|[,\n}])\s*(\w+)\s*:/g |
| 19 | + |
| 20 | +// Match array items: `['title', 'desc']` or `["title", "desc"]` |
| 21 | +const RE_ARRAY_ITEM = /['"](\w+)['"]/g |
| 22 | + |
| 23 | +function findBalanced(source: string, openChar: string, closeChar: string, startIndex: number): string | null { |
| 24 | + let depth = 0 |
| 25 | + let start = -1 |
| 26 | + for (let i = startIndex; i < source.length; i++) { |
| 27 | + if (source[i] === openChar) { |
| 28 | + if (depth === 0) |
| 29 | + start = i + 1 |
| 30 | + depth++ |
| 31 | + } |
| 32 | + else if (source[i] === closeChar) { |
| 33 | + depth-- |
| 34 | + if (depth === 0 && start !== -1) |
| 35 | + return source.slice(start, i) |
| 36 | + } |
| 37 | + } |
| 38 | + return null |
| 39 | +} |
| 40 | + |
| 41 | +export function extractPropNamesFromVue(code: string): string[] { |
| 42 | + const match = RE_SCRIPT_SETUP.exec(code) |
| 43 | + if (!match) |
| 44 | + return [] |
| 45 | + |
| 46 | + const src = match[1] |
| 47 | + |
| 48 | + const dpIndex = src.indexOf('defineProps') |
| 49 | + if (dpIndex === -1) |
| 50 | + return [] |
| 51 | + |
| 52 | + // Check for TypeScript generic syntax: defineProps<...>() |
| 53 | + const afterDp = src.slice(dpIndex + 'defineProps'.length).trimStart() |
| 54 | + if (afterDp.startsWith('<')) { |
| 55 | + const typeBody = findBalanced(src, '<', '>', dpIndex + 'defineProps'.length) |
| 56 | + if (typeBody) |
| 57 | + return extractTopLevelKeys(typeBody, RE_TS_PROP_KEY) |
| 58 | + } |
| 59 | + |
| 60 | + // Check for runtime object or array syntax: defineProps({...}) or defineProps([...]) |
| 61 | + const parenContent = findBalanced(src, '(', ')', dpIndex + 'defineProps'.length) |
| 62 | + if (!parenContent) |
| 63 | + return [] |
| 64 | + |
| 65 | + const trimmed = parenContent.trim() |
| 66 | + |
| 67 | + // Array syntax: defineProps(['title', 'desc']) |
| 68 | + if (trimmed.startsWith('[')) { |
| 69 | + const names: string[] = [] |
| 70 | + let m: RegExpExecArray | null |
| 71 | + // eslint-disable-next-line no-cond-assign |
| 72 | + while (m = RE_ARRAY_ITEM.exec(trimmed)) |
| 73 | + names.push(m[1]) |
| 74 | + RE_ARRAY_ITEM.lastIndex = 0 |
| 75 | + return names |
| 76 | + } |
| 77 | + |
| 78 | + // Runtime object syntax: defineProps({ title: String }) |
| 79 | + if (trimmed.startsWith('{')) |
| 80 | + return extractTopLevelKeys(trimmed, RE_RUNTIME_PROP_KEY) |
| 81 | + |
| 82 | + return [] |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Extract top-level property keys from a braced block, skipping nested braces. |
| 87 | + */ |
| 88 | +function extractTopLevelKeys(body: string, re: RegExp): string[] { |
| 89 | + const keys: string[] = [] |
| 90 | + let depth = 0 |
| 91 | + let flat = '' |
| 92 | + |
| 93 | + for (const ch of body) { |
| 94 | + if (ch === '{' || ch === '<' || ch === '(') { |
| 95 | + depth++ |
| 96 | + if (depth > 1) { flat += ' '; continue } |
| 97 | + } |
| 98 | + else if (ch === '}' || ch === '>' || ch === ')') { |
| 99 | + if (depth > 1) { flat += ' '; depth--; continue } |
| 100 | + depth-- |
| 101 | + } |
| 102 | + flat += depth <= 1 ? ch : ' ' |
| 103 | + } |
| 104 | + |
| 105 | + let m: RegExpExecArray | null |
| 106 | + // eslint-disable-next-line no-cond-assign |
| 107 | + while (m = re.exec(flat)) |
| 108 | + keys.push(m[1]) |
| 109 | + re.lastIndex = 0 |
| 110 | + return keys |
| 111 | +} |
0 commit comments