Language Support
Diff Guardian uses WASM-compiled Tree-Sitter grammars to parse source code. Each language has a dedicated translator module that extracts structured signatures from the syntax tree.
Supported languages
| Language | Extensions | Grammar | Translator |
|---|---|---|---|
| TypeScript | .ts, .tsx | tree-sitter-typescript | translators/typescript.ts |
| JavaScript | .js, .jsx | tree-sitter-javascript | translators/typescript.ts |
| Python | .py | tree-sitter-python | translators/python.ts |
| Go | .go | tree-sitter-go | translators/go.ts |
| Java | .java | tree-sitter-java | translators/java.ts |
| Rust | .rs | tree-sitter-rust | translators/rust.ts |
What gets extracted
Every translator extracts the same four signature types from the syntax tree, regardless of language:
FunctionSignature
- Name, parameters (name, type, optional flag, default value, rest flag)
- Return type, async modifier, visibility (public/protected/private)
- Generic type parameters with constraints
- Constructor detection, static detection, export status
InterfaceSignature
- Name, properties (name, type, optional flag)
- Generic type parameters
EnumSignature
- Name, members (name, optional value)
TypeAliasSignature
- Name, the full type expression, generics
Language-specific notes
TypeScript and JavaScript
TypeScript and JavaScript share the same translator module. The TypeScript grammar handles JSX/TSX syntax natively. JavaScript files are parsed with the TypeScript grammar since it is a superset.
Extracted constructs: function declarations, class methods, arrow functions assigned to const/let,interface, type aliases, and enum.
Python
Python functions are identified by def statements. Type annotations (PEP 484+) are extracted when present. Since Python does not have native interfaces or enums at the language level, only function signatures and class methods are analyzed. Dataclasses and TypedDict are treated as interfaces when decorated.
Go
Go functions and methods are extracted from func andfunc (receiver) name syntax. Interfaces are extracted fromtype Name interface blocks. Structs with exported fields are treated as interface-like contracts. Enums are inferred fromconst blocks with iota.
Java
Java methods, constructors, and interfaces are fully supported. The translator extracts visibility modifiers, generics, and static flags. Enums are extracted from enum declarations with their constant values.
Rust
Rust functions (fn), trait definitions (trait), and enums (enum) are supported. Visibility is determined by the pub modifier. Generic lifetime and type parameters are extracted. The translator handles impl blocks for method extraction.
Adding a new language
To add support for a new language, you need:
- Install the Tree-Sitter grammar:
npm install tree-sitter-langname - Copy the WASM file to the
grammars/directory - Add a translator module in
src/parsers/translators/ - Register the extension mapping in
src/core/constants.ts - Add a case to the
ASTMapper.dispatch()switch
Related
- How It Works — AST mapper phase
- Architecture — source tree and data contracts