codeflash-internal/django/aiservice/aiservice/validators/javascript_validator.py
Saurabh Misra 198c0c1a4e
codeflash-omni-java (#2335)
# Pull Request Checklist

## Description
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Co-authored-by: HeshamHM28 <HeshamMohamedFathy@outlook.com>
Co-authored-by: Ubuntu <ubuntu@ip-172-31-39-200.ec2.internal>
Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Kevin Turcios <turcioskevinr@gmail.com>
Co-authored-by: Kevin Turcios <106575910+KRRT7@users.noreply.github.com>
2026-02-13 23:26:55 +05:30

34 lines
962 B
Python

"""JavaScript/TypeScript syntax validation.
Uses Node.js for validation when available, with a basic regex fallback.
"""
from __future__ import annotations
from functools import lru_cache
import tree_sitter_javascript
import tree_sitter_typescript
from tree_sitter import Language, Parser
js_parser = Parser(Language(tree_sitter_javascript.language()))
ts_parser = Parser(Language(tree_sitter_typescript.language_typescript()))
@lru_cache(maxsize=100)
def validate_javascript_syntax(code: str) -> tuple[bool, str | None]:
tree = js_parser.parse(bytes(code, "utf8"))
has_error = tree.root_node.has_error
if has_error:
return False, "Invalid syntax"
return True, None
@lru_cache(maxsize=100)
def validate_typescript_syntax(code: str) -> tuple[bool, str | None]:
tree = ts_parser.parse(bytes(code, "utf8"))
has_error = tree.root_node.has_error
if has_error:
return False, "Invalid syntax"
return True, None