mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
feat(ssh_config): Check for empty blocks
This commit is contained in:
parent
8bb54e62a9
commit
cdb463c4d7
@ -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)
|
||||
|
24
handlers/ssh_config/analyzer/block.go
Normal file
24
handlers/ssh_config/analyzer/block.go
Normal 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
|
||||
}
|
20
handlers/ssh_config/analyzer/block_test.go
Normal file
20
handlers/ssh_config/analyzer/block_test.go
Normal 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))
|
||||
}
|
||||
}
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user