Skip to content

Commit

Permalink
[FEAT] ignore "Map keys must be unique" in else branches
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Sep 23, 2023
1 parent af3929f commit 0405282
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
17 changes: 15 additions & 2 deletions internal/adapter/yamlls/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,25 @@ func filterDiagnostics(diagnostics []lsp.Diagnostic, ast *sitter.Tree, content s
for _, diagnostic := range diagnostics {
node := lsplocal.NodeAtPosition(ast, diagnostic.Range.Start)
childNode := lsplocal.FindRelevantChildNode(ast.RootNode(), lsplocal.GetSitterPointForLspPos(diagnostic.Range.Start))
diagnostic.Message = "Yamlls: " + diagnostic.Message
if node.Type() == "text" && childNode.Type() == "text" {
logger.Debug("Diagnostic", diagnostic)
logger.Debug("Node", node.Content([]byte(content)))
filtered = append(filtered, diagnostic)
if diagnisticIsRelevant(diagnostic, childNode) {
diagnostic.Message = "Yamlls: " + diagnostic.Message
filtered = append(filtered, diagnostic)
}
}
}
return filtered
}

func diagnisticIsRelevant(diagnostic lsp.Diagnostic, node *sitter.Node) bool {
logger.Println("Diagnostic", diagnostic.Message)
switch diagnostic.Message {
case "Map keys must be unique":
return !lsplocal.IsInElseBranch(node)
default:
return true
}

}
40 changes: 40 additions & 0 deletions internal/lsp/ast_diagnostics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package lsp

import sitter "github.com/smacker/go-tree-sitter"

func IsInElseBranch(node *sitter.Node) bool {

parent := node.Parent()

if parent == nil {
return false
}

if parent.Type() == "if_action" {

childIndex, err := getIndexOfChild(parent, node)
if err != nil {
return IsInElseBranch(parent)
}

logger.Println("ChildIndex is", childIndex)

if childIndex > 4 {
return true
}

}

return IsInElseBranch(parent)
}

func getIndexOfChild(parent *sitter.Node, child *sitter.Node) (int, error) {

count := parent.ChildCount()
for i := 0; i < int(count); i++ {
if parent.Child(i) == child {
return i, nil
}
}
return -1, nil
}
41 changes: 41 additions & 0 deletions internal/lsp/ast_diagnostics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package lsp

import (
"testing"

sitter "github.com/smacker/go-tree-sitter"
)

func TestIsInElseBranch(t *testing.T) {
template := `{{if pipeline}} t1 {{ else if pipeline }} t2 {{ else }} t3 {{ end }}`
var ast = ParseAst(template)
// (template
// (if_action
// (function_call (identifier))
// (text)
// (function_call (identifier))
// (text)
// (text)))

t1_start := sitter.Point{Row: 0, Column: 16}
t1 := ast.RootNode().NamedDescendantForPointRange(t1_start, t1_start)
t2_start := sitter.Point{Row: 0, Column: 42}
t2 := ast.RootNode().NamedDescendantForPointRange(t2_start, t2_start)
t3_start := sitter.Point{Row: 0, Column: 56}
t3 := ast.RootNode().NamedDescendantForPointRange(t3_start, t3_start)

if (t1.Content([]byte(template))) != " t1 " || (t2.Content([]byte(template))) != " t2 " || (t3.Content([]byte(template))) != " t3 " {
t.Errorf("Nodes were not correclty selected")
}

if IsInElseBranch(t1) {
t.Errorf("t1 was incorrectly identified as in else branch")
}
if !IsInElseBranch(t2) {
t.Errorf("t2 was incorrectly identified as not in else branch")
}
if !IsInElseBranch(t3) {
t.Errorf("t3 was incorrectly identified as not in else branch")
}

}

0 comments on commit 0405282

Please sign in to comment.