Skip to content

Commit 27161c8

Browse files
committed
feat: add tree-sitter support for Java
Signed-off-by: Joseph Kato <joseph@jdkato.io>
1 parent ecf1c23 commit 27161c8

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

internal/core/format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var FormatByExtension = map[string][]string{
6868
`\.(?:go)$`: {".go", "code"},
6969
`\.(?:hs)$`: {".hs", "code"},
7070
`\.(?:html|htm|shtml|xhtml)$`: {".html", "markup"},
71-
`\.(?:java|bsh)$`: {".c", "code"},
71+
`\.(?:java|bsh)$`: {".java", "code"},
7272
`\.(?:jl)$`: {".jl", "code"},
7373
`\.(?:js|jsx)$`: {".js", "code"},
7474
`\.(?:lua)$`: {".lua", "code"},

internal/lint/code/java.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package code
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/errata-ai/vale/v3/internal/core"
7+
"github.com/smacker/go-tree-sitter/java"
8+
)
9+
10+
func Java() *Language {
11+
return &Language{
12+
Delims: regexp.MustCompile(`//|/\*|\*/`),
13+
Parser: java.GetLanguage(),
14+
Queries: []core.Scope{
15+
{Name: "", Expr: "(line_comment)+ @comment", Type: ""},
16+
{Name: "", Expr: "(block_comment)+ @comment", Type: ""},
17+
},
18+
Padding: cStyle,
19+
}
20+
}

internal/lint/code/lang.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func GetLanguageFromExt(ext string) (*Language, error) {
4040
return JavaScript(), nil
4141
case ".jl":
4242
return Julia(), nil
43+
case ".java":
44+
return Java(), nil
4345
case ".ts":
4446
return TypeScript(), nil
4547
case ".tsx":

testdata/features/lint.feature

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ Feature: Lint
3434
test.jl:47:3:vale.Annotations:'XXX' left in text
3535
"""
3636

37+
Scenario: Lint a Java file
38+
When I lint "test.java"
39+
Then the output should contain exactly:
40+
"""
41+
test.java:1:4:vale.Annotations:'XXX' left in text
42+
test.java:9:4:vale.Annotations:'TODO' left in text
43+
"""
44+
3745
Scenario: Lint a proto file
3846
When I lint "test.proto"
3947
Then the output should contain exactly:
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// XXX: This is an annotation
2+
// And this uses too many weasel words like obviously and very.
3+
4+
/*
5+
* This is a multi-line comment.
6+
* It can span across several lines.
7+
* All text within these delimiters is ignored.
8+
*
9+
* TODO: Refactor this code to improve readability.x
10+
*/
11+
public class HelloWorld {
12+
public static void main(String[] args) {
13+
System.out.println("Hello World, XXX!");
14+
}
15+
}

0 commit comments

Comments
 (0)