Skip to content

Commit b03344c

Browse files
committed
docs(website): fix event type generation for unquoted and nested events
Rewrite eventDetails parsing with brace-counting to correctly handle unquoted event names (e.g. click in ToolbarItem) and skip false positives from nested detail type fields (e.g. item inside detail-click). Adds onClick to ToolbarButton, removes false onItem/onSelected events.
1 parent 12272c0 commit b03344c

2 files changed

Lines changed: 345 additions & 54 deletions

File tree

packages/website/scripts/generate-monaco-types.mjs

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -370,28 +370,57 @@ function parseDeclarationFile(dtsContent, componentName, packagesDir = null, inc
370370

371371
// Extract eventDetails to get event names and their detail types
372372
// Match eventDetails: { ... } or eventDetails: ParentType["eventDetails"] & { ... }
373-
const eventDetailsMatch = classBody.match(/eventDetails\s*:[^{]*\{([^}]+(?:\{[^}]*\}[^}]*)*)\}/);
374-
if (eventDetailsMatch) {
375-
const eventBody = eventDetailsMatch[1];
376-
// Match event names and their detail type references
377-
const eventMatches = eventBody.matchAll(/["']([a-z][a-z0-9-]*)["']\s*:\s*([^;\n,]+)/gi);
378-
for (const match of eventMatches) {
379-
const eventName = match[1];
380-
const detailTypeName = match[2].trim();
381-
// Skip internal events starting with _
382-
if (!eventName.startsWith('_')) {
383-
// Resolve the detail type fields
384-
let detailFields = null;
385-
if (detailTypeName !== "void" && detailTypes.has(detailTypeName)) {
386-
detailFields = detailTypes.get(detailTypeName);
387-
}
388-
// Override inherited event with same name if present
389-
const existingIndex = events.findIndex(e => e.name === eventName);
390-
if (existingIndex >= 0) {
391-
events[existingIndex] = { name: eventName, detailTypeName: detailTypeName, detailFields };
392-
} else {
393-
events.push({ name: eventName, detailTypeName: detailTypeName, detailFields });
373+
const eventDetailsIdx = classBody.indexOf("eventDetails");
374+
if (eventDetailsIdx >= 0) {
375+
// Find the opening brace of eventDetails
376+
const openBrace = classBody.indexOf("{", eventDetailsIdx);
377+
if (openBrace >= 0) {
378+
// Use brace counting to find the matching close brace
379+
let depth = 1;
380+
let i = openBrace + 1;
381+
while (i < classBody.length && depth > 0) {
382+
if (classBody[i] === "{") depth++;
383+
if (classBody[i] === "}") depth--;
384+
i++;
385+
}
386+
const eventDetailsBody = classBody.substring(openBrace + 1, i - 1);
387+
388+
// Parse top-level event entries only (depth 0 relative to eventDetails body)
389+
// Skip entries nested inside inline detail type objects like { item: ListItem; }
390+
let entryDepth = 0;
391+
let pos = 0;
392+
while (pos < eventDetailsBody.length) {
393+
if (eventDetailsBody[pos] === "{") { entryDepth++; pos++; continue; }
394+
if (eventDetailsBody[pos] === "}") { entryDepth--; pos++; continue; }
395+
396+
// Only match at top level (entryDepth === 0)
397+
if (entryDepth === 0) {
398+
// Try to match: "event-name": Type or eventName: Type
399+
const entryMatch = eventDetailsBody.substring(pos).match(/^["']?([a-z][a-z0-9-]*)["']?\s*:\s*([^;{\n,]+)/i);
400+
if (entryMatch) {
401+
const eventName = entryMatch[1];
402+
const detailTypeName = entryMatch[2].trim();
403+
pos += entryMatch[0].length;
404+
405+
// Skip internal events starting with _
406+
if (!eventName.startsWith('_')) {
407+
// Resolve the detail type fields
408+
let detailFields = null;
409+
if (detailTypeName !== "void" && detailTypes.has(detailTypeName)) {
410+
detailFields = detailTypes.get(detailTypeName);
411+
}
412+
// Override inherited event with same name if present
413+
const existingIndex = events.findIndex(e => e.name === eventName);
414+
if (existingIndex >= 0) {
415+
events[existingIndex] = { name: eventName, detailTypeName, detailFields };
416+
} else {
417+
events.push({ name: eventName, detailTypeName, detailFields });
418+
}
419+
}
420+
continue;
421+
}
394422
}
423+
pos++;
395424
}
396425
}
397426
}

0 commit comments

Comments
 (0)