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
1 change: 1 addition & 0 deletions internal/core/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ var FormatByExtension = map[string][]string{
`\.(?:ipynb)$`: {".ipynb", "markup"},
`\.(?:java|bsh)$`: {".java", "code"},
`\.(?:jl)$`: {".jl", "code"},
`\.(?:kt|kts)$`: {".kt", "code"},
`\.(?:js|jsx)$`: {".js", "code"},
`\.(?:lua)$`: {".lua", "code"},
`\.(?:md|mdown|markdown|markdn|[Rr]md)$`: {".md", "markup"},
Expand Down
10 changes: 7 additions & 3 deletions internal/lint/code/comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func TestComments(t *testing.T) {
cleaned = append(cleaned, f)
}

for i, f := range cleaned {
for _, f := range cleaned {
// Match each input to its output by name: the directory is read in
// lexical order, so `10.kt` would otherwise pair with `2.json`.
name := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))

b, err1 := os.ReadFile(fmt.Sprintf("%s/in/%s", testDir, f.Name()))
if err1 != nil {
t.Error(err1)
Expand All @@ -59,14 +63,14 @@ func TestComments(t *testing.T) {
t.Error(err3)
}

b2, err4 := os.ReadFile(fmt.Sprintf("%s/out/%d.json", testDir, i))
b2, err4 := os.ReadFile(fmt.Sprintf("%s/out/%s.json", testDir, name))
if err4 != nil {
t.Error(err4)
}

markup := toJSON(comments)
if markup != string(b2) {
bin := filepath.Join(binDir, fmt.Sprintf("%d.json", i))
bin := filepath.Join(binDir, name+".json")
_ = os.WriteFile(bin, []byte(markup), 0600)
t.Errorf("%s", markup)
}
Expand Down
27 changes: 27 additions & 0 deletions internal/lint/code/kt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package code

import (
"regexp"

"github.com/smacker/go-tree-sitter/kotlin"
"github.com/vale-cli/vale/v3/internal/core"
)

// Kotlin extracts `//` line comments and `/* */` block comments, including
// KDoc (`/** */`), which the grammar parses as an ordinary block comment.
func Kotlin() *Language {
return &Language{
Delims: regexp.MustCompile(`//|/\*\*?|\*/`),
Prefix: cStylePrefix,
Parser: kotlin.GetLanguage(),
Queries: []core.Scope{
{Name: "", Expr: "(line_comment)+ @comment", Type: ""},
{Name: "", Expr: "(multiline_comment) @comment", Type: ""},
},
// `/**` is listed so that a one-line KDoc comment's padding matches
// what Delims takes off; cStyle alone stops at `/*`.
Padding: func(s string) int {
return computePadding(s, []string{"//", "/*", "/**"})
},
}
}
2 changes: 2 additions & 0 deletions internal/lint/code/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func GetLanguageFromExt(ext string) (*Language, error) {
return Julia(), nil
case ".java":
return Java(), nil
case ".kt":
return Kotlin(), nil
case ".lua":
return Lua(), nil
case ".php":
Expand Down
22 changes: 22 additions & 0 deletions testdata/comments/in/10.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// NOTE: a line comment
// XXX: continued on the next line
package test

/**
* TODO: a KDoc block.
*
* @param name FIXME: a tag description.
*/
fun greet(name: String): String {
/* NOTE: a block comment */
val s = "TODO: a string, not a comment"
return s // FIXME: a trailing comment
}

/*
* XXX: a multi-line block comment.
*/
class Test

/** NOTE: a one-line KDoc. */
fun one() = 1
7 changes: 7 additions & 0 deletions testdata/comments/in/11.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// A script's leading comment.
plugins {
kotlin("jvm") version "2.0.0" /* inline block */
}

/** One line of KDoc. */
val url = "https://example.com // not a comment"
44 changes: 44 additions & 0 deletions testdata/comments/out/10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"Text": "NOTE: a line comment\nXXX: continued on the next line\n",
"Source": "// NOTE: a line comment\n// XXX: continued on the next line\n",
"Line": 1,
"Offset": 0,
"Scope": "text.comment.line"
},
{
"Text": "\nTODO: a KDoc block.\n\n@param name FIXME: a tag description.\n\n",
"Source": "/**\n * TODO: a KDoc block.\n *\n * @param name FIXME: a tag description.\n */",
"Line": 5,
"Offset": 0,
"Scope": "text.comment.block"
},
{
"Text": "NOTE: a block comment ",
"Source": "/* NOTE: a block comment */",
"Line": 11,
"Offset": 4,
"Scope": "text.comment.line"
},
{
"Text": "FIXME: a trailing comment",
"Source": "// FIXME: a trailing comment",
"Line": 13,
"Offset": 13,
"Scope": "text.comment.line"
},
{
"Text": "\nXXX: a multi-line block comment.\n\n",
"Source": "/*\n * XXX: a multi-line block comment.\n */",
"Line": 16,
"Offset": 0,
"Scope": "text.comment.block"
},
{
"Text": "NOTE: a one-line KDoc. ",
"Source": "/** NOTE: a one-line KDoc. */",
"Line": 21,
"Offset": 0,
"Scope": "text.comment.line"
}
]
23 changes: 23 additions & 0 deletions testdata/comments/out/11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"Text": "A script's leading comment.",
"Source": "// A script's leading comment.",
"Line": 1,
"Offset": 0,
"Scope": "text.comment.line"
},
{
"Text": "inline block ",
"Source": "/* inline block */",
"Line": 3,
"Offset": 34,
"Scope": "text.comment.line"
},
{
"Text": "One line of KDoc. ",
"Source": "/** One line of KDoc. */",
"Line": 6,
"Offset": 0,
"Scope": "text.comment.line"
}
]
13 changes: 13 additions & 0 deletions testdata/e2e/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ cases:
test.java:1:4:vale.Annotations:'XXX' left in text
test.java:9:4:vale.Annotations:'TODO' left in text

- name: kotlin
args: test.kt
exit: 0
want: |
test.kt:1:4:vale.Annotations:'NOTE' left in text
test.kt:2:4:vale.Annotations:'XXX' left in text
test.kt:6:4:vale.Annotations:'TODO' left in text
test.kt:8:16:vale.Annotations:'FIXME' left in text
test.kt:11:8:vale.Annotations:'NOTE' left in text
test.kt:13:17:vale.Annotations:'FIXME' left in text
test.kt:17:4:vale.Annotations:'XXX' left in text
test.kt:21:5:vale.Annotations:'NOTE' left in text

- name: proto
args: test.proto
exit: 0
Expand Down
22 changes: 22 additions & 0 deletions testdata/fixtures/formats/test.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// NOTE: a line comment
// XXX: continued on the next line
package test

/**
* TODO: a KDoc block.
*
* @param name FIXME: a tag description.
*/
fun greet(name: String): String {
/* NOTE: a block comment */
val s = "TODO: a string, not a comment"
return s // FIXME: a trailing comment
}

/*
* XXX: a multi-line block comment.
*/
class Test

/** NOTE: a one-line KDoc. */
fun one() = 1
Loading