-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathwarn_malformed_references.go
More file actions
72 lines (61 loc) · 1.71 KB
/
warn_malformed_references.go
File metadata and controls
72 lines (61 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package mutator
import (
"context"
"errors"
"slices"
"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/dyn"
"github.com/databricks/cli/libs/interpolation"
)
type warnMalformedReferences struct{}
// WarnMalformedReferences returns a mutator that emits warnings for strings
// containing malformed variable references (e.g. "${foo.bar-}").
func WarnMalformedReferences() bundle.Mutator {
return &warnMalformedReferences{}
}
func (*warnMalformedReferences) Name() string {
return "WarnMalformedReferences"
}
func (*warnMalformedReferences) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
var diags diag.Diagnostics
err := b.Config.Mutate(func(root dyn.Value) (dyn.Value, error) {
_, err := dyn.Walk(root, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
// Only check values with source locations to avoid false positives
// from synthesized/computed values.
if len(v.Locations()) == 0 {
return v, nil
}
s, ok := v.AsString()
if !ok {
return v, nil
}
_, parseErr := interpolation.Parse(s)
if parseErr == nil {
return v, nil
}
var pe *interpolation.ParseError
if !errors.As(parseErr, &pe) {
return v, nil
}
// Clone locations and adjust column with the position offset
// so the diagnostic points to the problematic reference.
locs := slices.Clone(v.Locations())
if len(locs) > 0 {
locs[0].Column += pe.Pos
}
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: pe.Msg,
Locations: locs,
Paths: []dyn.Path{p},
})
return v, nil
})
return root, err
})
if err != nil {
diags = diags.Extend(diag.FromErr(err))
}
return diags
}