Skip to content

Commit

Permalink
feat: support custom scopes with engine: tree-stitter
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Jan 10, 2025
1 parent f65c0bf commit eeaa7e3
Show file tree
Hide file tree
Showing 25 changed files with 82 additions and 46 deletions.
7 changes: 5 additions & 2 deletions internal/check/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,12 @@ func checkScopes(scopes []string, path string) error {
fmt.Sprintf("scope '%v' is no longer supported; use 'raw' instead.", scope),
"scope",
path)
} else if !core.StringInSlice(scope, allowedScopes) {
}

// No spaces
if strings.Contains(scope, " ") {
return core.NewE201FromTarget(
fmt.Sprintf("'%v' is not a valid scope; must be one of %v", scope, allowedScopes),
fmt.Sprintf("scope '%v' contains spaces.", scope),
"scope",
path)
}
Expand Down
14 changes: 7 additions & 7 deletions internal/lint/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ import (
"github.com/errata-ai/vale/v3/internal/nlp"
)

func updateQueries(f *core.File, blueprints map[string]*core.Blueprint) ([]string, error) {
func updateQueries(f *core.File, blueprints map[string]*core.Blueprint) ([]core.Scope, error) {
var found []core.Scope

for syntax, blueprint := range blueprints {
sec, err := glob.Compile(syntax)
if err != nil {
return nil, err
} else if sec.Match(f.Path) {
found := []string{}
for _, query := range blueprint.Scopes {
found = append(found, query.Expr)
}
return found, nil
found = blueprint.Scopes
}
}
return []string{}, nil

return found, nil
}

func (l *Linter) lintCode(f *core.File) error {
Expand All @@ -52,6 +51,7 @@ func (l *Linter) lintCode(f *core.File) error {

last := 0
for _, comment := range comments {
l.SetMetaScope(comment.Scope)
if core.StringInSlice("comment", ignored) {
continue
} else if core.StringInSlice(comment.Scope, ignored) {
Expand Down
9 changes: 6 additions & 3 deletions internal/lint/code/c.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/c"
)

func C() *Language {
return &Language{
Delims: regexp.MustCompile(`//|/\*|\*/`),
Parser: c.GetLanguage(),
Queries: []string{`(comment)+ @comment`},
Delims: regexp.MustCompile(`//|/\*|\*/`),
Parser: c.GetLanguage(),
Queries: []core.Scope{
{Name: "", Expr: "(comment) @comment", Type: ""},
},
Padding: cStyle,
}
}
4 changes: 2 additions & 2 deletions internal/lint/code/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ func GetComments(source []byte, lang *Language) ([]Comment, error) {
engine := NewQueryEngine(tree, lang)

for _, query := range lang.Queries {
q, qErr := sitter.NewQuery([]byte(query), lang.Parser)
q, qErr := sitter.NewQuery([]byte(query.Expr), lang.Parser)
if qErr != nil {
return comments, qErr
}
comments = append(comments, engine.run(q, source)...)
comments = append(comments, engine.run(query.Name, q, source)...)
}

if len(lang.Queries) > 1 {
Expand Down
3 changes: 2 additions & 1 deletion internal/lint/code/cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/cpp"
)

func Cpp() *Language {
return &Language{
Delims: regexp.MustCompile(`//|/\*!?|\*/`),
Parser: cpp.GetLanguage(),
Queries: []string{`(comment)+ @comment`},
Queries: []core.Scope{{Name: "", Expr: "(comment) @comment", Type: ""}},
Padding: cStyle,
}
}
3 changes: 2 additions & 1 deletion internal/lint/code/css.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/css"
)

func CSS() *Language {
return &Language{
Delims: regexp.MustCompile(`/\*!?|\*/`),
Parser: css.GetLanguage(),
Queries: []string{`(comment)+ @comment`},
Queries: []core.Scope{{Name: "", Expr: "(comment) @comment", Type: ""}},
Padding: func(s string) int {
return computePadding(s, []string{"/*"})
},
Expand Down
3 changes: 2 additions & 1 deletion internal/lint/code/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/golang"
)

func Go() *Language {
return &Language{
Delims: regexp.MustCompile(`//|/\*|\*/`),
Parser: golang.GetLanguage(),
Queries: []string{`(comment)+ @comment`},
Queries: []core.Scope{{Name: "", Expr: "(comment) @comment", Type: ""}},
Padding: cStyle,
}
}
7 changes: 4 additions & 3 deletions internal/lint/code/jl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/jdkato/go-tree-sitter-julia/julia"
)

func Julia() *Language {
return &Language{
Delims: regexp.MustCompile(`#|#=|=#`),
Parser: julia.GetLanguage(),
Queries: []string{
`(line_comment)+ @comment`,
`(block_comment)+ @comment`,
Queries: []core.Scope{
{Name: "", Expr: "(line_comment)+ @comment", Type: ""},
{Name: "", Expr: "(block_comment)+ @comment", Type: ""},
},
Padding: func(s string) int {
return computePadding(s, []string{"#", `#=`, `=#`})
Expand Down
3 changes: 2 additions & 1 deletion internal/lint/code/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/javascript"
)

Expand All @@ -11,7 +12,7 @@ func JavaScript() *Language {
Delims: regexp.MustCompile(`//|/\*\*?|\*/`),
Parser: javascript.GetLanguage(),
//Cutset: " *",
Queries: []string{`(comment)+ @comment`},
Queries: []core.Scope{{Name: "", Expr: "(comment) @comment", Type: ""}},
Padding: cStyle,
}
}
2 changes: 1 addition & 1 deletion internal/lint/code/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type padding func(string) int
type Language struct {
Delims *regexp.Regexp
Parser *sitter.Language
Queries []string
Queries []core.Scope
Cutset string
Padding padding
}
Expand Down
3 changes: 2 additions & 1 deletion internal/lint/code/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/protobuf"
)

func Protobuf() *Language {
return &Language{
Delims: regexp.MustCompile(`//|/\*|\*/`),
Parser: protobuf.GetLanguage(),
Queries: []string{`(comment)+ @comment`},
Queries: []core.Scope{{Name: "", Expr: "(comment) @comment", Type: ""}},
Padding: cStyle,
}
}
23 changes: 12 additions & 11 deletions internal/lint/code/py.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,27 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/python"
)

func Python() *Language {
return &Language{
Delims: regexp.MustCompile(`#|"""|'''`),
Parser: python.GetLanguage(),
Queries: []string{
`(comment)+ @comment`,
// Function docstring
`((function_definition
Queries: []core.Scope{
{Name: "", Expr: `(comment)+ @comment`, Type: ""},
// Function docstrings
{Name: "", Expr: `((function_definition
body: (block . (expression_statement (string) @docstring)))
(#offset! @docstring 0 3 0 -3))`,
// Class docstring
`((class_definition
(#offset! @docstring 0 3 0 -3))`, Type: ""},
// Class docstrings
{Name: "", Expr: `((class_definition
body: (block . (expression_statement (string) @docstring)))
(#offset! @docstring 0 3 0 -3))`,
// Module docstring
`((module . (expression_statement (string) @docstring))
(#offset! @docstring 0 3 0 -3))`,
(#offset! @docstring 0 3 0 -3))`, Type: ""},
// Module docstrings
{Name: "", Expr: `((module . (expression_statement (string) @docstring))
(#offset! @docstring 0 3 0 -3))`, Type: ""},
},
Padding: func(s string) int {
return computePadding(s, []string{"#", `"""`, "'''"})
Expand Down
10 changes: 7 additions & 3 deletions internal/lint/code/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ func NewQueryEngine(tree *sitter.Tree, lang *Language) *QueryEngine {
}
}

func (qe *QueryEngine) run(q *sitter.Query, source []byte) []Comment {
func (qe *QueryEngine) run(meta string, q *sitter.Query, source []byte) []Comment {
var comments []Comment

if meta != "" {
meta = "." + meta
}

qc := sitter.NewQueryCursor()
qc.Exec(q, qe.tree.RootNode())

Expand All @@ -43,9 +47,9 @@ func (qe *QueryEngine) run(q *sitter.Query, source []byte) []Comment {
rText := c.Node.Content(source)
cText := qe.lang.Delims.ReplaceAllString(rText, "")

scope := "text.comment.line"
scope := "text.comment" + meta + ".line"
if strings.Count(cText, "\n") > 1 {
scope = "text.comment.block"
scope = "text.comment" + meta + ".block"

buf := bytes.Buffer{}
for _, line := range strings.Split(cText, "\n") {
Expand Down
3 changes: 2 additions & 1 deletion internal/lint/code/rb.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/ruby"
)

func Ruby() *Language {
return &Language{
Delims: regexp.MustCompile(`#|=begin|=end`),
Parser: ruby.GetLanguage(),
Queries: []string{`(comment)+ @comment`},
Queries: []core.Scope{{Name: "", Expr: "(comment) @comment", Type: ""}},
Padding: func(s string) int {
return computePadding(s, []string{"#", `=begin`, `=end`})
},
Expand Down
3 changes: 2 additions & 1 deletion internal/lint/code/rs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/rust"
)

func Rust() *Language {
return &Language{
Delims: regexp.MustCompile(`/{2,3}!?`),
Parser: rust.GetLanguage(),
Queries: []string{`(line_comment)+ @comment`},
Queries: []core.Scope{{Name: "", Expr: `(line_comment)+ @comment`, Type: ""}},
Padding: func(s string) int {
return computePadding(s, []string{"//", "//!", "///"})
},
Expand Down
3 changes: 2 additions & 1 deletion internal/lint/code/ts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/typescript/typescript"
)

func TypeScript() *Language {
return &Language{
Delims: regexp.MustCompile(`//|/\*|\*/`),
Parser: typescript.GetLanguage(),
Queries: []string{`(comment)+ @comment`},
Queries: []core.Scope{{Name: "", Expr: "(comment) @comment", Type: ""}},
Padding: cStyle,
}
}
3 changes: 2 additions & 1 deletion internal/lint/code/tsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/typescript/tsx"
)

func Tsx() *Language {
return &Language{
Delims: regexp.MustCompile(`//|/\*|\*/`),
Parser: tsx.GetLanguage(),
Queries: []string{`(comment)+ @comment`},
Queries: []core.Scope{{Name: "", Expr: "(comment) @comment", Type: ""}},
Padding: cStyle,
}
}
3 changes: 2 additions & 1 deletion internal/lint/code/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package code
import (
"regexp"

"github.com/errata-ai/vale/v3/internal/core"
"github.com/smacker/go-tree-sitter/yaml"
)

func YAML() *Language {
return &Language{
Delims: regexp.MustCompile(`#`),
Parser: yaml.GetLanguage(),
Queries: []string{`(comment)+ @comment`},
Queries: []core.Scope{{Name: "", Expr: "(comment) @comment", Type: ""}},
Padding: func(s string) int {
return computePadding(s, []string{"#"})
},
Expand Down
1 change: 1 addition & 0 deletions internal/lint/fragment.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (l *Linter) lintFragments(f *core.File) error {

last := 0
for _, comment := range comments {
l.SetMetaScope(comment.Scope)
f.SetText(comment.Text)

switch f.NormedExt {
Expand Down
3 changes: 2 additions & 1 deletion testdata/features/blueprints.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ Feature: Blueprints
When I test "blueprints"
Then the output should contain exactly:
"""
API.yml:3:10:OpenAPI.Titles:'sample API' should be capitalized
API.yml:3:10:Scopes.Titles:'sample API' should be capitalized
API.yml:4:25:Vale.Spelling:Did you really mean 'multiline'?
API.yml:9:70:Vale.Spelling:Did you really mean 'serrver'?
API.yml:13:17:Vale.Spelling:Did you really mean 'serrver'?
API.yml:15:70:Vale.Spelling:Did you really mean 'serrver'?
Rule.yml:3:39:Vale.Repetition:'can' is repeated!
test.py:1:3:Scopes.Code:'FIXME' should not be capitalized
test.py:1:3:vale.Annotations:'FIXME' left in text
test.py:11:3:vale.Annotations:'XXX' left in text
test.py:13:16:vale.Annotations:'XXX' left in text
Expand Down
6 changes: 5 additions & 1 deletion testdata/fixtures/blueprints/.vale.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
StylesPath = ../../styles
MinAlertLevel = suggestion

[formats]
py = md

[*.py]
vale.Annotations = YES
Scopes.Code = YES

Blueprint = Python

[*.yml]
BasedOnStyles = Vale, OpenAPI
BasedOnStyles = Vale, Scopes

[API.yml]
Blueprint = OpenAPI
Expand Down
Loading

0 comments on commit eeaa7e3

Please sign in to comment.