From 98f76fd839b6418ff73ab8952deaf18859abc049 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Mon, 17 Feb 2025 22:40:47 +0100 Subject: [PATCH] fix(server): Improve structure analyzer for ssh_config Signed-off-by: Myzel394 --- .../handlers/ssh_config/analyzer/options.go | 50 +++++++++---------- server/handlers/ssh_config/analyzer/quotes.go | 12 ----- .../ssh_config/analyzer/quotes_test.go | 20 ++++++-- 3 files changed, 40 insertions(+), 42 deletions(-) diff --git a/server/handlers/ssh_config/analyzer/options.go b/server/handlers/ssh_config/analyzer/options.go index 0356cf5..90dee52 100644 --- a/server/handlers/ssh_config/analyzer/options.go +++ b/server/handlers/ssh_config/analyzer/options.go @@ -39,9 +39,26 @@ func checkOption( option *ast.SSHOption, block ast.SSHBlock, ) { + if option.Key == nil { + return + } + + ///// General checks checkIsUsingDoubleQuotes(ctx, option.Key.Value, option.Key.LocationRange) checkQuotesAreClosed(ctx, option.Key.Value, option.Key.LocationRange) + if option.Separator == nil || option.Separator.Value.Value == "" { + ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{ + Range: option.Key.LocationRange.ToLSPRange(), + Message: fmt.Sprintf("There should be a separator between an option and its value"), + Severity: &common.SeverityError, + }) + } else { + checkIsUsingDoubleQuotes(ctx, option.Separator.Value, option.Separator.LocationRange) + checkQuotesAreClosed(ctx, option.Separator.Value, option.Separator.LocationRange) + } + + ///// Check if the key is valid docOption, optionFound := fields.Options[option.Key.Key] if !optionFound { @@ -58,22 +75,13 @@ func checkOption( Option: option, Block: block, } - - return } - } - if block != nil && block.GetBlockType() == ast.SSHBlockTypeHost && utils.KeyExists(fields.HostDisallowedOptions, option.Key.Key) { - ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{ - Range: option.Key.LocationRange.ToLSPRange(), - Message: fmt.Sprintf("Option '%s' is not allowed in Host blocks", option.Key.Key), - Severity: &common.SeverityError, - }) - } - - // Check for values that are not allowed in Host blocks - if block != nil && block.GetBlockType() == ast.SSHBlockTypeHost { - if utils.KeyExists(fields.HostDisallowedOptions, option.Key.Key) { + // Since we don't know the option, we can't verify the value + return + } else { + // Check for values that are not allowed in Host blocks + if block != nil && block.GetBlockType() == ast.SSHBlockTypeHost && utils.KeyExists(fields.HostDisallowedOptions, option.Key.Key) { ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{ Range: option.Key.LocationRange.ToLSPRange(), Message: fmt.Sprintf("Option '%s' is not allowed in Host blocks", option.Key.Key), @@ -82,7 +90,8 @@ func checkOption( } } - if option.OptionValue != nil && optionFound { + ///// Check if the value is valid + if option.OptionValue != nil { checkIsUsingDoubleQuotes(ctx, option.OptionValue.Value, option.OptionValue.LocationRange) checkQuotesAreClosed(ctx, option.OptionValue.Value, option.OptionValue.LocationRange) @@ -98,17 +107,6 @@ func checkOption( }) } } - - if option.Separator == nil || option.Separator.Value.Value == "" { - ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{ - Range: option.Key.LocationRange.ToLSPRange(), - Message: fmt.Sprintf("There should be a separator between an option and its value"), - Severity: &common.SeverityError, - }) - } else { - checkIsUsingDoubleQuotes(ctx, option.Separator.Value, option.Separator.LocationRange) - checkQuotesAreClosed(ctx, option.Separator.Value, option.Separator.LocationRange) - } } func checkBlock( diff --git a/server/handlers/ssh_config/analyzer/quotes.go b/server/handlers/ssh_config/analyzer/quotes.go index 50a3399..3835519 100644 --- a/server/handlers/ssh_config/analyzer/quotes.go +++ b/server/handlers/ssh_config/analyzer/quotes.go @@ -9,18 +9,6 @@ import ( protocol "github.com/tliron/glsp/protocol_3_16" ) -func analyzeQuotesAreValid( - ctx *analyzerContext, -) { - for _, info := range ctx.document.Config.GetAllOptions() { - checkIsUsingDoubleQuotes(ctx, info.Option.Key.Value, info.Option.Key.LocationRange) - checkIsUsingDoubleQuotes(ctx, info.Option.OptionValue.Value, info.Option.OptionValue.LocationRange) - - checkQuotesAreClosed(ctx, info.Option.Key.Value, info.Option.Key.LocationRange) - checkQuotesAreClosed(ctx, info.Option.OptionValue.Value, info.Option.OptionValue.LocationRange) - } -} - func checkIsUsingDoubleQuotes( ctx *analyzerContext, value commonparser.ParsedString, diff --git a/server/handlers/ssh_config/analyzer/quotes_test.go b/server/handlers/ssh_config/analyzer/quotes_test.go index c52a218..e5e0041 100644 --- a/server/handlers/ssh_config/analyzer/quotes_test.go +++ b/server/handlers/ssh_config/analyzer/quotes_test.go @@ -7,6 +7,18 @@ import ( protocol "github.com/tliron/glsp/protocol_3_16" ) +func testQuotes( + ctx *analyzerContext, +) { + for _, info := range ctx.document.Config.GetAllOptions() { + checkIsUsingDoubleQuotes(ctx, info.Option.Key.Value, info.Option.Key.LocationRange) + checkIsUsingDoubleQuotes(ctx, info.Option.OptionValue.Value, info.Option.OptionValue.LocationRange) + + checkQuotesAreClosed(ctx, info.Option.Key.Value, info.Option.Key.LocationRange) + checkQuotesAreClosed(ctx, info.Option.OptionValue.Value, info.Option.OptionValue.LocationRange) + } +} + func TestSimpleInvalidQuotesExample( t *testing.T, ) { @@ -17,7 +29,7 @@ PermitRootLogin 'yes' document: d, diagnostics: make([]protocol.Diagnostic, 0), } - analyzeQuotesAreValid(ctx) + testQuotes(ctx) if !(len(ctx.diagnostics) == 1) { t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics)) @@ -34,7 +46,7 @@ func TestSingleQuotesKeyAndOptionExample( document: d, diagnostics: make([]protocol.Diagnostic, 0), } - analyzeQuotesAreValid(ctx) + testQuotes(ctx) if !(len(ctx.diagnostics) == 2) { t.Errorf("Expected 2 ctx.diagnostics, got %v", len(ctx.diagnostics)) @@ -51,7 +63,7 @@ PermitRootLogin "yes document: d, diagnostics: make([]protocol.Diagnostic, 0), } - analyzeQuotesAreValid(ctx) + testQuotes(ctx) if !(len(ctx.diagnostics) == 1) { t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics)) @@ -68,7 +80,7 @@ func TestIncompleteQuotesExample( document: d, diagnostics: make([]protocol.Diagnostic, 0), } - analyzeQuotesAreValid(ctx) + testQuotes(ctx) if !(len(ctx.diagnostics) == 1) { t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics))