( code: string )
| 304 | * Validate Python code syntax |
| 305 | */ |
| 306 | export async function validatePythonSyntax( |
| 307 | code: string |
| 308 | ): Promise<{ isValid: boolean; error?: string }> { |
| 309 | const pyodide = getPyodide(); |
| 310 | if (!pyodide) { |
| 311 | return { isValid: false, error: 'Pyodide not initialized' }; |
| 312 | } |
| 313 | |
| 314 | try { |
| 315 | await pyodide.runPython(` |
| 316 | import ast |
| 317 | try: |
| 318 | ast.parse('''${code.replace(/'/g, "\\'")}''') |
| 319 | syntax_valid = True |
| 320 | syntax_error = None |
| 321 | except SyntaxError as e: |
| 322 | syntax_valid = False |
| 323 | syntax_error = str(e) |
| 324 | `); |
| 325 | |
| 326 | const isValid = pyodide.globals.get('syntax_valid'); |
| 327 | const error = pyodide.globals.get('syntax_error'); |
| 328 | |
| 329 | return { isValid, error: error || undefined }; |
| 330 | } catch (error) { |
| 331 | return { isValid: false, error: String(error) }; |
| 332 | } |
| 333 | } |
nothing calls this directly
no test coverage detected