/** * v4 wrapped a condition on one line (`var show = true; ; return show;`), so a * condition made only of comments and ending in `//` commented out the return and hid the field. */ export function isCommentOnlyConditional(customConditional: unknown): boolean { if (typeof customConditional !== 'string') return false; const text = customConditional; let index = 0; while (index < text.length) { if (/[\s;]/.test(text[index])) { index += 1; } else if (text.startsWith('//', index)) { const newline = text.slice(index).search(/[\r\n]/); if (newline === -1) return true; index += newline; } else if (text.startsWith('/*', index)) { const end = text.indexOf('*/', index + 2); if (end === -1) return false; index = end + 2; } else { return false; } } return false; } export interface CommentOnlyConditionalFinding { path: string; key: string; customConditional: string; } export function findCommentOnlyConditionals(schema: unknown): CommentOnlyConditionalFinding[] { const findings: CommentOnlyConditionalFinding[] = []; const visit = (node: unknown, parentPath: string): void => { if (Array.isArray(node)) { node.forEach((child) => visit(child, parentPath)); return; } if (!node || typeof node !== 'object') return; const component = node as Record; const key = typeof component.key === 'string' ? component.key : ''; const path = key ? (parentPath ? `${parentPath}.${key}` : key) : parentPath; if (isCommentOnlyConditional(component.customConditional)) { findings.push({ path, key, customConditional: component.customConditional as string }); } for (const field of ['components', 'columns', 'rows']) { if (field in component) visit(component[field], path); } }; visit(schema, ''); return findings; }