fix(server): Improve spec field analyzer

This commit is contained in:
Myzel394 2025-02-15 01:00:15 +01:00
parent 68be6e5904
commit 7a92594135
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F
4 changed files with 51 additions and 6 deletions

View File

@ -48,11 +48,15 @@ func (v PathValue) GetTypeDescription() []string {
func (v PathValue) DeprecatedCheckIsValid(value string) []*InvalidValue {
if !utils.DoesPathExist(value) {
return []*InvalidValue{{
Err: PathDoesNotExistError{},
Start: 0,
End: uint32(len(value)),
}}
if v.RequiredType == PathTypeExistenceOptional {
return nil
} else {
return []*InvalidValue{{
Err: PathDoesNotExistError{},
Start: 0,
End: uint32(len(value)),
}}
}
}
isValid := false

View File

@ -0,0 +1,38 @@
package analyzer
import (
"config-lsp/common"
"config-lsp/handlers/fstab/ast"
"regexp"
protocol "github.com/tliron/glsp/protocol_3_16"
)
var volatileBlockFields = regexp.MustCompile(`^/dev/(sd|nvme|mmcblk|sr|vd|loop|cdrom)[a-zA-Z0-9]*$`)
func analyzeSpecField(
ctx *analyzerContext,
field *ast.FstabField,
) {
if field == nil {
return
}
if field.Value.Value == "" {
return
}
if !volatileBlockFields.MatchString(field.Value.Value) {
return
}
codeDescription := protocol.CodeDescription{
HRef: protocol.URI("https://wiki.archlinux.org/title/Persistent_block_device_naming"),
}
ctx.diagnostics = append(ctx.diagnostics, protocol.Diagnostic{
Range: field.ToLSPRange(),
Message: "Kernel name descriptors for block devices are not persistent and can change each boot, they should not be used in configuration files. Prefer device UUIDs or LABELs instead.",
CodeDescription: &codeDescription,
Severity: &common.SeverityWarning,
})
}

View File

@ -21,6 +21,8 @@ func analyzeValuesAreValid(
checkField(ctx, entry.Fields.MountPoint, fields.MountPointField)
checkField(ctx, entry.Fields.FilesystemType, fields.FileSystemTypeField)
analyzeSpecField(ctx, entry.Fields.Spec)
if entry.Fields.Options != nil {
mountOptions := entry.FetchMountOptionsField(true)

View File

@ -6,7 +6,8 @@ import (
)
var UuidField = docvalues.RegexValue{
Regex: *regexp.MustCompile(`[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}`),
// Can either be a UUID or UID
Regex: *regexp.MustCompile(`(?i)([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}|[a-f0-9]{4}-[a-f0-9]{4})`),
}
var LabelField = docvalues.RegexValue{
Regex: *regexp.MustCompile(`\S+`),