Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions internal/treesitter/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ func TestParse(t *testing.T) {
}
})

t.Run("valid typescript source", func(t *testing.T) {
src := []byte(`interface User { id: number; name: string; } const u: User = { id: 1, name: "Alice" };`)
ast, err := Parse(src, "main.ts")
if err != nil {
t.Fatalf("Parse(): unexpected error: %v", err)
}
if ast == nil {
t.Error("Parse(): expected non-nil AST root")
}
})

t.Run("valid jsx source", func(t *testing.T) {
src := []byte(`const App = () => <div className="app">Hello</div>;`)
ast, err := Parse(src, "App.jsx")
Expand All @@ -101,6 +112,17 @@ func TestParse(t *testing.T) {
}
})

t.Run("valid tsx source", func(t *testing.T) {
src := []byte(`export const App = (): JSX.Element => <div><h1>Hello World</h1></div>;`)
ast, err := Parse(src, "main.tsx")
if err != nil {
t.Fatalf("Parse(): unexpected error: %v", err)
}
if ast == nil {
t.Error("Parse(): expected non-nil AST root")
}
})

t.Run("unknown extension", func(t *testing.T) {
src := []byte(`some text`)
ast, err := Parse(src, "main.xyz")
Expand Down
15 changes: 15 additions & 0 deletions internal/treesitter/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ func TestRulesAliased(t *testing.T) {
"type", "interface", "namespace", "enum", "abstract", "readonly",
},
},
{
lang: "tsx",
operators: []string{
"+", "-", "*", "/", "%", "**",
"==", "!=", "===", "!==", "<", "<=", ">", ">=",
"&&", "||", "??",
"=", "+=", "-=", "*=", "/=", "%=", "**=",
"&&=", "||=", "??=",
"&", "|", "^", "<<", ">>", ">>>",
"!", "~", "++", "--",
"=>",
"type", "interface", "namespace", "enum", "abstract", "readonly",
"/>", "</",
},
},
}

for _, tt := range tests {
Expand Down
151 changes: 151 additions & 0 deletions internal/treesitter/tsx/rules.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
flattened:
- string
- template_string
- comment
aliased:
# Comparison operators
"==": comparison_operator_literal
"!=": comparison_operator_literal
"===": comparison_operator_literal
"!==": comparison_operator_literal
"<": comparison_operator_literal
"<=": comparison_operator_literal
">": comparison_operator_literal
">=": comparison_operator_literal

# Logical operators
"&&": logical_operator_literal
"||": logical_operator_literal
"??": logical_operator_literal

# Assignment operators
"=": assignment_operator_literal
"+=": assignment_operator_literal
"-=": assignment_operator_literal
"*=": assignment_operator_literal
"/=": assignment_operator_literal
"%=": assignment_operator_literal
"**=": assignment_operator_literal
"&&=": assignment_operator_literal
"||=": assignment_operator_literal
"??=": assignment_operator_literal
"&=": assignment_operator_literal
"|=": assignment_operator_literal
"^=": assignment_operator_literal
"<<=": assignment_operator_literal
">>=": assignment_operator_literal
">>>=": assignment_operator_literal

# Arithmetic operators
"+": arithmetic_operator_literal
"-": arithmetic_operator_literal
"*": arithmetic_operator_literal
"/": arithmetic_operator_literal
"%": arithmetic_operator_literal
"**": arithmetic_operator_literal

# Bitwise operators
"&": bitwise_operator_literal
"|": bitwise_operator_literal
"^": bitwise_operator_literal
"<<": bitwise_operator_literal
">>": bitwise_operator_literal
">>>": bitwise_operator_literal

# Unary / Update operators
"!": unary_operator_literal
"~": bitwise_operator_literal
"++": update_operator_literal
"--": update_operator_literal
"=>": arrow

# Keywords
"if": "condition_declaration"
"else": "else"
"for": "for"
"while": "while"
"do": "do"
"switch": "switch"
"case": "case"
"default": "default"
"try": "try"
"catch": "catch"
"finally": "finally"
"throw": "throw"
"return": "return"
"break": "break"
"continue": "continue"
"function": "function"
"class": "class"
"const": "const"
"let": "let"
"var": "var"
"type": "type"
"interface": "interface"
"namespace": "namespace"
"enum": "enum"
"abstract": "abstract"
"readonly": "readonly"
"export": "export"
"import": "import"
"from": "from"
"as": "as"
"async": "async"
"await": "await"

# Punctuation
".": dot
":": colon
",": comma
";": semicolon
"?.": optional_chaining

# JSX element tags
"/>": jsx_self_closing_tag_slash_gt
"</": jsx_closing_tag_lt_slash

ignored:
- "\n"
- "escape_sequence"

scaffolding:
- program
- statement_block
- formal_parameters
- arguments
- parenthesized_expression
- expression_statement
- function_declaration
- method_definition
- arrow_function
- class_declaration
- class_body
- interface_declaration
- interface_body
- enum_declaration
- enum_body
- type_alias_declaration
- if_statement
- for_statement
- for_in_statement
- switch_statement
- switch_body
- switch_case
- try_statement
- catch_clause
- import_statement
- export_statement
- object
- array
- type_arguments
- type_parameters
# JSX Scaffolding
- jsx_element
- jsx_self_closing_element
- jsx_opening_element
- jsx_closing_element
- jsx_fragment
- jsx_opening_fragment
- jsx_closing_fragment
- jsx_attribute
- jsx_expression
137 changes: 137 additions & 0 deletions internal/treesitter/typescript/rules.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
flattened:
- string
- template_string
- comment
aliased:
# Comparison operators
"==": comparison_operator_literal
"!=": comparison_operator_literal
"===": comparison_operator_literal
"!==": comparison_operator_literal
"<": comparison_operator_literal
"<=": comparison_operator_literal
">": comparison_operator_literal
">=": comparison_operator_literal

# Logical operators
"&&": logical_operator_literal
"||": logical_operator_literal
"??": logical_operator_literal

# Assignment operators
"=": assignment_operator_literal
"+=": assignment_operator_literal
"-=": assignment_operator_literal
"*=": assignment_operator_literal
"/=": assignment_operator_literal
"%=": assignment_operator_literal
"**=": assignment_operator_literal
"&&=": assignment_operator_literal
"||=": assignment_operator_literal
"??=": assignment_operator_literal
"&=": assignment_operator_literal
"|=": assignment_operator_literal
"^=": assignment_operator_literal
"<<=": assignment_operator_literal
">>=": assignment_operator_literal
">>>=": assignment_operator_literal

# Arithmetic operators
"+": arithmetic_operator_literal
"-": arithmetic_operator_literal
"*": arithmetic_operator_literal
"/": arithmetic_operator_literal
"%": arithmetic_operator_literal
"**": arithmetic_operator_literal

# Bitwise operators
"&": bitwise_operator_literal
"|": bitwise_operator_literal
"^": bitwise_operator_literal
"<<": bitwise_operator_literal
">>": bitwise_operator_literal
">>>": bitwise_operator_literal

# Unary / Update operators
"!": unary_operator_literal
"~": bitwise_operator_literal
"++": update_operator_literal
"--": update_operator_literal
"=>": arrow

# Keywords
"if": "condition_declaration"
"else": "else"
"for": "for"
"while": "while"
"do": "do"
"switch": "switch"
"case": "case"
"default": "default"
"try": "try"
"catch": "catch"
"finally": "finally"
"throw": "throw"
"return": "return"
"break": "break"
"continue": "continue"
"function": "function"
"class": "class"
"const": "const"
"let": "let"
"var": "var"
"type": "type"
"interface": "interface"
"namespace": "namespace"
"enum": "enum"
"abstract": "abstract"
"readonly": "readonly"
"export": "export"
"import": "import"
"from": "from"
"as": "as"
"async": "async"
"await": "await"

# Punctuation
".": dot
":": colon
",": comma
";": semicolon
"?.": optional_chaining

ignored:
- "\n"
- "escape_sequence"

scaffolding:
- program
- statement_block
- formal_parameters
- arguments
- parenthesized_expression
- expression_statement
- function_declaration
- method_definition
- arrow_function
- class_declaration
- class_body
- interface_declaration
- interface_body
- enum_declaration
- enum_body
- type_alias_declaration
- if_statement
- for_statement
- for_in_statement
- switch_statement
- switch_body
- switch_case
- try_statement
- catch_clause
- import_statement
- export_statement
- object
- array
- type_arguments
- type_parameters
Loading
Loading