-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] ignore "Map keys must be unique" in else branches
- Loading branch information
Showing
3 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
|
||
} |