From cdb463c4d775cea523c79b6592dd0fa17593de95 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Tue, 1 Oct 2024 14:40:28 +0200 Subject: [PATCH] feat(ssh_config): Check for empty blocks --- handlers/ssh_config/analyzer/analyzer.go | 1 + handlers/ssh_config/analyzer/block.go | 24 +++++++++++++++ handlers/ssh_config/analyzer/block_test.go | 20 ++++++++++++ handlers/ssh_config/document_fields.go | 36 +++++++++++++++++++--- 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 handlers/ssh_config/analyzer/block.go create mode 100644 handlers/ssh_config/analyzer/block_test.go diff --git a/handlers/ssh_config/analyzer/analyzer.go b/handlers/ssh_config/analyzer/analyzer.go index 3c11a37..b06312e 100644 --- a/handlers/ssh_config/analyzer/analyzer.go +++ b/handlers/ssh_config/analyzer/analyzer.go @@ -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) diff --git a/handlers/ssh_config/analyzer/block.go b/handlers/ssh_config/analyzer/block.go new file mode 100644 index 0000000..704137e --- /dev/null +++ b/handlers/ssh_config/analyzer/block.go @@ -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 +} diff --git a/handlers/ssh_config/analyzer/block_test.go b/handlers/ssh_config/analyzer/block_test.go new file mode 100644 index 0000000..caddd53 --- /dev/null +++ b/handlers/ssh_config/analyzer/block_test.go @@ -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)) + } +} diff --git a/handlers/ssh_config/document_fields.go b/handlers/ssh_config/document_fields.go index e93c799..e1d10d7 100644 --- a/handlers/ssh_config/document_fields.go +++ b/handlers/ssh_config/document_fields.go @@ -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 +}