import ast def validate_tsx_syntax(ts_code: str) -> bool: """ Very crude validation of React/TSX output by parsing it as Python. While TSX is not Python, basic syntax structures like balanced brackets often crash the AST if they are heavily truncated. A more robust validation would require wrapping `esbuild` or `tsc` in a subprocess. For now, we just ensure it's not completely empty or malformed text. """ if not ts_code or len(ts_code) < 50: return False # Check for basic required React component signatures if "export default function" not in ts_code and "export const" not in ts_code and "const" not in ts_code and "function" not in ts_code: return False # Check for balanced brackets brackets = {"{": "}", "[": "]", "(": ")", "<": ">"} stack = [] # We ignore angle brackets in TSX as they can be JSX tags or type params # and are notoriously hard to perfectly match without a real parser for char in ts_code: if char in "{\[(": stack.append(char) elif char in "}\])": if not stack: return False # Unmatched closing bracket last = stack.pop() if brackets[last] != char: return False # Mismatched brackets # If there are unclosed brackets (ignoring angle brackets), it might be truncated if stack: return False return True