feat(ssh_config): Check for empty blocks

This commit is contained in:
Myzel394 2024-10-01 14:40:28 +02:00
parent 8bb54e62a9
commit cdb463c4d7
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F
4 changed files with 77 additions and 4 deletions

View File

@ -29,6 +29,7 @@ func Analyze(
errors = append(errors, analyzeValuesAreValid(d)...)
errors = append(errors, analyzeDependents(d)...)
errors = append(errors, analyzeBlocks(d)...)
errors = append(errors, analyzeMatchBlocks(d)...)
return common.ErrsToDiagnostics(errors)

View File

@ -0,0 +1,24 @@
package analyzer
import (
"config-lsp/common"
sshconfig "config-lsp/handlers/ssh_config"
"errors"
)
func analyzeBlocks(
d *sshconfig.SSHDocument,
) []common.LSPError {
errs := make([]common.LSPError, 0)
for _, block := range d.GetAllBlocks() {
if block.GetOptions().Size() == 0 {
errs = append(errs, common.LSPError{
Range: block.GetEntryOption().LocationRange,
Err: errors.New("This block is empty"),
})
}
}
return errs
}

View File

@ -0,0 +1,20 @@
package analyzer
import (
testutils_test "config-lsp/handlers/ssh_config/test_utils"
"testing"
)
func TestBlockEmptyBlock(
t *testing.T,
) {
d := testutils_test.DocumentFromInput(t, `
Host *
`)
errors := analyzeBlocks(d)
if !(len(errors) == 1) {
t.Errorf("Expected an error, but got %v", len(errors))
}
}

View File

@ -49,11 +49,39 @@ func (d SSHDocument) GetAllMatchBlocks() []*ast.SSHMatchBlock {
blocks := utils.KeysOfMap(options)
for _, block := range blocks {
switch block.GetBlockType() {
case ast.SSHBlockTypeMatch:
matchBlocks = append(matchBlocks, block.(*ast.SSHMatchBlock))
}
matchBlocks = append(matchBlocks, block.(*ast.SSHMatchBlock))
}
return matchBlocks
}
var hostOption = fields.CreateNormalizedName("Host")
func (d SSHDocument) GetAllHostBlocks() []*ast.SSHHostBlock {
hostBlocks := make([]*ast.SSHHostBlock, 0, 5)
options := d.Indexes.AllOptionsPerName[hostOption]
blocks := utils.KeysOfMap(options)
for _, block := range blocks {
hostBlocks = append(hostBlocks, block.(*ast.SSHHostBlock))
}
return hostBlocks
}
// GetAllBlocks returns all blocks in the document
// Note: The blocks are **not** sorted
func (d SSHDocument) GetAllBlocks() []ast.SSHBlock {
blocks := make([]ast.SSHBlock, 0)
for _, block := range d.GetAllHostBlocks() {
blocks = append(blocks, block)
}
for _, block := range d.GetAllMatchBlocks() {
blocks = append(blocks, block)
}
return blocks
}