fix(aliases): Add double keys analyzer

This commit is contained in:
Myzel394 2024-08-31 20:52:46 +02:00
parent 31faa9a96d
commit 4ea6f9f160
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
4 changed files with 119 additions and 5 deletions

View File

@ -1,13 +1,33 @@
package analyzer
import (
"config-lsp/handlers/hosts"
"config-lsp/common"
"config-lsp/handlers/aliases"
"config-lsp/utils"
protocol "github.com/tliron/glsp/protocol_3_16"
)
func Analyze(
d *hosts.HostsDocument,
d *aliases.AliasesDocument,
) []protocol.Diagnostic {
return nil
errors := analyzeValuesAreValid(*d.Parser)
if len(errors) > 0 {
return utils.Map(
errors,
func(err common.LSPError) protocol.Diagnostic {
return err.ToDiagnostic()
},
)
}
errors = append(errors, analyzeDoubleKeys(d)...)
return utils.Map(
errors,
func(err common.LSPError) protocol.Diagnostic {
return err.ToDiagnostic()
},
)
}

View File

@ -0,0 +1,21 @@
package analyzer
import (
"config-lsp/common"
"config-lsp/handlers/aliases"
"config-lsp/handlers/aliases/indexes"
)
func analyzeDoubleKeys(
d *aliases.AliasesDocument,
) []common.LSPError {
indexes, errors := indexes.CreateIndexes(*d.Parser)
d.Indexes = &indexes
if len(errors) > 0 {
return errors
}
return nil
}

View File

@ -0,0 +1,73 @@
package analyzer
import (
"config-lsp/handlers/aliases"
"config-lsp/handlers/aliases/ast"
"config-lsp/utils"
"testing"
)
func TestWorks(
t *testing.T,
) {
input := utils.Dedent(`
support: michael
marketing: john
support: jane
`)
p := ast.NewAliasesParser()
errors := p.Parse(input)
d := aliases.AliasesDocument{
Parser: &p,
}
if len(errors) != 0 {
t.Errorf("Expected no errors, got %v", errors)
}
errors = analyzeDoubleKeys(&d)
if !(len(errors) == 1) {
t.Errorf("Expected 1 error, got %v", errors)
}
if d.Indexes == nil {
t.Errorf("Expected indexes to be set")
}
}
func TestValidWorks(
t *testing.T,
) {
input := utils.Dedent(`
support: michael
marketing: john
supportgroup: jane
suppor: jane
`)
p := ast.NewAliasesParser()
errors := p.Parse(input)
d := aliases.AliasesDocument{
Parser: &p,
}
if len(errors) != 0 {
t.Errorf("Expected no errors, got %v", errors)
}
errors = analyzeDoubleKeys(&d)
if !(len(errors) == 0) {
t.Errorf("Expected 0 errors, got %v", errors)
}
if d.Indexes == nil {
t.Errorf("Expected indexes to be set")
}
if d.Indexes.Keys["support"] == nil {
t.Errorf("Expected support to be in indexes")
}
}

View File

@ -7,9 +7,9 @@ import (
protocol "github.com/tliron/glsp/protocol_3_16"
)
type HostsDocument struct {
type AliasesDocument struct {
Parser *ast.AliasesParser
Indexes *indexes.AliasesIndexes
}
var DocumentParserMap = map[protocol.DocumentUri]*HostsDocument{}
var DocumentParserMap = map[protocol.DocumentUri]*AliasesDocument{}