feat(aliases): Add required keys analyzer

This commit is contained in:
Myzel394 2024-08-31 21:47:24 +02:00
parent 4c2422c3da
commit 742dc48656
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
5 changed files with 51 additions and 2 deletions

View File

@ -15,6 +15,17 @@ type LocationRange struct {
End Location End Location
} }
var GlobalLocationRange = LocationRange{
Start: Location{
Line: 0,
Character: 0,
},
End: Location{
Line: 0,
Character: 0,
},
}
func (l LocationRange) ToLSPRange() protocol.Range { func (l LocationRange) ToLSPRange() protocol.Range {
return protocol.Range{ return protocol.Range{
Start: protocol.Position{ Start: protocol.Position{

View File

@ -23,6 +23,7 @@ func Analyze(
} }
errors = append(errors, analyzeDoubleKeys(d)...) errors = append(errors, analyzeDoubleKeys(d)...)
errors = append(errors, analyzeContainsRequiredKeys(*d)...)
return utils.Map( return utils.Map(
errors, errors,

View File

@ -20,7 +20,7 @@ func analyzeValuesAreValid(
if entry.Key == nil { if entry.Key == nil {
errors = append(errors, common.LSPError{ errors = append(errors, common.LSPError{
Range: entry.Location, Range: entry.Location,
Err: ers.New("A name is required"), Err: ers.New("An alias is required"),
}) })
continue continue

View File

@ -0,0 +1,33 @@
package analyzer
import (
"config-lsp/common"
"config-lsp/handlers/aliases"
"config-lsp/handlers/aliases/indexes"
"fmt"
ers "errors"
)
var requiredFields = []string{
indexes.NormalizeKey("mailer-daemon"),
indexes.NormalizeKey("hostmaster"),
indexes.NormalizeKey("postmaster"),
}
func analyzeContainsRequiredKeys(
d aliases.AliasesDocument,
) []common.LSPError {
errors := make([]common.LSPError, 0)
for _, requiredField := range requiredFields {
if _, found := d.Indexes.Keys[requiredField]; !found {
errors = append(errors, common.LSPError{
Range: common.GlobalLocationRange,
Err: ers.New(fmt.Sprintf("Please add the alias '%s'. It is required by the aliases file.", requiredField)),
})
}
}
return errors
}

View File

@ -11,6 +11,10 @@ type AliasesIndexes struct {
Keys map[string]*ast.AliasKey Keys map[string]*ast.AliasKey
} }
func NormalizeKey(key string) string {
return strings.ToLower(key)
}
func CreateIndexes(parser ast.AliasesParser) (AliasesIndexes, []common.LSPError) { func CreateIndexes(parser ast.AliasesParser) (AliasesIndexes, []common.LSPError) {
errors := make([]common.LSPError, 0) errors := make([]common.LSPError, 0)
indexes := &AliasesIndexes{ indexes := &AliasesIndexes{
@ -22,7 +26,7 @@ func CreateIndexes(parser ast.AliasesParser) (AliasesIndexes, []common.LSPError)
for it.Next() { for it.Next() {
entry := it.Value().(*ast.AliasEntry) entry := it.Value().(*ast.AliasEntry)
normalizedAlias := strings.ToLower(entry.Key.Value) normalizedAlias := NormalizeKey(entry.Key.Value)
if existingEntry, found := indexes.Keys[normalizedAlias]; found { if existingEntry, found := indexes.Keys[normalizedAlias]; found {
errors = append(errors, common.LSPError{ errors = append(errors, common.LSPError{