mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
feat(ssh_config): Check if IgnoreUnknown contains unused options
This commit is contained in:
parent
cd23ffcb7f
commit
594ceeb5d1
@ -36,6 +36,7 @@ func Analyze(
|
||||
d.Indexes = i
|
||||
|
||||
analyzeValuesAreValid(ctx)
|
||||
analyzeIgnoreUnknownHasNoUnnecessary(ctx)
|
||||
analyzeDependents(ctx)
|
||||
analyzeBlocks(ctx)
|
||||
analyzeMatchBlocks(ctx)
|
||||
|
@ -10,6 +10,10 @@ func analyzeBlocks(
|
||||
ctx *analyzerContext,
|
||||
) {
|
||||
for _, block := range ctx.document.GetAllBlocks() {
|
||||
if block == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if block.GetOptions().Size() == 0 {
|
||||
ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{
|
||||
Range: block.GetEntryOption().LocationRange.ToLSPRange(),
|
||||
|
39
handlers/ssh_config/analyzer/ignore_unknown.go
Normal file
39
handlers/ssh_config/analyzer/ignore_unknown.go
Normal file
@ -0,0 +1,39 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
"config-lsp/common"
|
||||
"config-lsp/handlers/ssh_config/fields"
|
||||
"fmt"
|
||||
|
||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||
)
|
||||
|
||||
var ignoreUnknownOption = fields.CreateNormalizedName("IgnoreUnknown")
|
||||
|
||||
func analyzeIgnoreUnknownHasNoUnnecessary(
|
||||
ctx *analyzerContext,
|
||||
) {
|
||||
for _, block := range ctx.document.GetAllBlocks() {
|
||||
ignoreUnknown, found := ctx.document.Indexes.IgnoredOptions[block]
|
||||
|
||||
if !found {
|
||||
// No `IgnoreUnknown` option specified
|
||||
continue
|
||||
}
|
||||
|
||||
for optionName, ignoreInfo := range ignoreUnknown.IgnoredOptions {
|
||||
info := ctx.document.FindOptionByNameAndBlock(optionName, block)
|
||||
|
||||
if info == nil {
|
||||
ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{
|
||||
Range: ignoreInfo.ToLSPRange(),
|
||||
Message: fmt.Sprintf("Option %s is not present", optionName),
|
||||
Tags: []protocol.DiagnosticTag{
|
||||
protocol.DiagnosticTagUnnecessary,
|
||||
},
|
||||
Severity: &common.SeverityHint,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
26
handlers/ssh_config/analyzer/ignore_unknown_test.go
Normal file
26
handlers/ssh_config/analyzer/ignore_unknown_test.go
Normal file
@ -0,0 +1,26 @@
|
||||
package analyzer
|
||||
|
||||
import (
|
||||
testutils_test "config-lsp/handlers/ssh_config/test_utils"
|
||||
"testing"
|
||||
|
||||
protocol "github.com/tliron/glsp/protocol_3_16"
|
||||
)
|
||||
|
||||
func TestIgnoreUnknownUnnecessary(
|
||||
t *testing.T,
|
||||
) {
|
||||
d := testutils_test.DocumentFromInput(t, `
|
||||
IgnoreUnknown helloWorld
|
||||
PermitRootLogin 'yes'
|
||||
`)
|
||||
ctx := &analyzerContext{
|
||||
document: d,
|
||||
diagnostics: make([]protocol.Diagnostic, 0),
|
||||
}
|
||||
analyzeIgnoreUnknownHasNoUnnecessary(ctx)
|
||||
|
||||
if !(len(ctx.diagnostics) == 1) {
|
||||
t.Errorf("Expected 1 error, got %v", len(ctx.diagnostics))
|
||||
}
|
||||
}
|
@ -72,8 +72,10 @@ func (d SSHDocument) GetAllHostBlocks() []*ast.SSHHostBlock {
|
||||
|
||||
// GetAllBlocks returns all blocks in the document
|
||||
// Note: The blocks are **not** sorted
|
||||
// Note: This also returns `nil` (as the global block)
|
||||
func (d SSHDocument) GetAllBlocks() []ast.SSHBlock {
|
||||
blocks := make([]ast.SSHBlock, 0)
|
||||
blocks = append(blocks, nil)
|
||||
|
||||
for _, block := range d.GetAllHostBlocks() {
|
||||
blocks = append(blocks, block)
|
||||
|
@ -26,9 +26,13 @@ type SSHIndexIncludeLine struct {
|
||||
Block ast.SSHBlock
|
||||
}
|
||||
|
||||
type SSHIndexIgnoredUnknownInfo struct {
|
||||
common.LocationRange
|
||||
}
|
||||
|
||||
type SSHIndexIgnoredUnknowns struct {
|
||||
OptionValue *ast.SSHOption
|
||||
IgnoredOptions map[fields.NormalizedOptionName]struct{}
|
||||
IgnoredOptions map[fields.NormalizedOptionName]SSHIndexIgnoredUnknownInfo
|
||||
}
|
||||
|
||||
type SSHIndexes struct {
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"config-lsp/handlers/ssh_config/fields"
|
||||
)
|
||||
|
||||
func (u SSHIndexIgnoredUnknowns) GetIgnoredForLine(line uint32) map[fields.NormalizedOptionName]struct{} {
|
||||
func (u SSHIndexIgnoredUnknowns) GetIgnoredForLine(line uint32) map[fields.NormalizedOptionName]SSHIndexIgnoredUnknownInfo {
|
||||
if line >= u.OptionValue.Start.Line {
|
||||
return u.IgnoredOptions
|
||||
}
|
||||
|
@ -151,11 +151,26 @@ func addIgnoredOption(
|
||||
block ast.SSHBlock,
|
||||
) {
|
||||
rawIgnored := option.OptionValue.Value.Value
|
||||
ignoredAsSlice := ignoredValuesPattern.FindAllString(rawIgnored, -1)
|
||||
ignored := make(map[fields.NormalizedOptionName]struct{}, 0)
|
||||
ignoredAsSlice := ignoredValuesPattern.FindAllStringIndex(rawIgnored, -1)
|
||||
ignored := make(map[fields.NormalizedOptionName]SSHIndexIgnoredUnknownInfo, 0)
|
||||
|
||||
for _, ig := range ignoredAsSlice {
|
||||
ignored[fields.CreateNormalizedName(ig)] = struct{}{}
|
||||
for _, ignoreInfo := range ignoredAsSlice {
|
||||
start := ignoreInfo[0]
|
||||
end := ignoreInfo[1]
|
||||
name := rawIgnored[start:end]
|
||||
|
||||
ignored[fields.CreateNormalizedName(name)] = SSHIndexIgnoredUnknownInfo{
|
||||
LocationRange: common.LocationRange{
|
||||
Start: common.Location{
|
||||
Line: option.OptionValue.Start.Line,
|
||||
Character: option.OptionValue.Start.Character + uint32(start),
|
||||
},
|
||||
End: common.Location{
|
||||
Line: option.OptionValue.End.Line,
|
||||
Character: option.OptionValue.Start.Character + uint32(end),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
i.IgnoredOptions[block] = SSHIndexIgnoredUnknowns{
|
||||
|
@ -112,4 +112,8 @@ UseKeychain yes
|
||||
if !(len(indexes.IgnoredOptions[nil].IgnoredOptions) == 1 && utils.KeyExists(indexes.IgnoredOptions[nil].IgnoredOptions, "usekeychain")) {
|
||||
t.Errorf("Expected IgnoreOptions to contain 'UseKeychain', but got: %v", indexes.IgnoredOptions[nil].IgnoredOptions)
|
||||
}
|
||||
|
||||
if !(indexes.IgnoredOptions[nil].IgnoredOptions["usekeychain"].Start.Line == 0 && indexes.IgnoredOptions[nil].IgnoredOptions["usekeychain"].Start.Character == 14 && indexes.IgnoredOptions[nil].IgnoredOptions["usekeychain"].End.Character == 25) {
|
||||
t.Errorf("Expected IgnoreOptions to contain 'UseKeychain' on line 0 and from position 14-24, but got: %v", indexes.IgnoredOptions[nil].IgnoredOptions)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user