@@ -6,14 +6,14 @@ import "github.com/quasilyte/go-ruleguard/dsl"
66// code. Parsing re-validates the string, can panic on user keys with special characters
77// and costs allocations; build the path from parts with structpath.NewPath,
88// NewPathSlice or NewPattern instead. Parsing string literals and parsing in tests is fine.
9+ // Values passed through a variable are not tracked.
910func NoComputedStructpathParse (m dsl.Matcher ) {
10- m .Match (`structpath.MustParsePath($s)` , `structpath.MustParsePattern($s)` ).
11- Where (! m ["s" ].Const && ! m .File ().Name .Matches (`_test\.go$` ) && ! m .File ().PkgPath .Matches (`internal/bundletest` )).
12- Report (`build computed paths with structpath.NewPath / NewPattern instead of parsing them` )
11+ m .Import ("github.com/databricks/cli/libs/structs/structpath" )
12+ m .Import ("github.com/databricks/cli/bundle/config" )
1313
14- m .Match (`structpath.MustParsePaths($*_, $s, $*_ )` ).
14+ m .Match (`structpath.MustParsePath($s)` , `structpath.MustParsePattern($s)` , `structpath.MustParsePaths($*s )` ).
1515 Where (! m ["s" ].Const && ! m .File ().Name .Matches (`_test\.go$` ) && ! m .File ().PkgPath .Matches (`internal/bundletest` )).
16- Report (`build computed paths with structpath.NewPathSlice instead of parsing them` )
16+ Report (`build computed paths with structpath.NewPath / NewPathSlice / NewPattern instead of parsing them` )
1717
1818 m .Match (
1919 `structpath.ParsePath(fmt.Sprintf($*_))` ,
@@ -24,17 +24,30 @@ func NoComputedStructpathParse(m dsl.Matcher) {
2424 Where (! m .File ().Name .Matches (`_test\.go$` )).
2525 Report (`build the path with structpath.NewPath / NewPattern instead of formatting and parsing a string` )
2626
27+ // Re-parsing a node as its own kind is a no-op; parsing a pattern as a path is fine
28+ // (it rejects wildcards).
29+ m .Match (`structpath.ParsePath($p.String())` ).
30+ Where (! m .File ().Name .Matches (`_test\.go$` ) && m ["p" ].Type .Is ("*structpath.PathNode" )).
31+ Report (`use the structpath node instead of rendering and parsing it` )
32+ m .Match (`structpath.ParsePattern($p.String())` ).
33+ Where (! m .File ().Name .Matches (`_test\.go$` ) && m ["p" ].Type .Is ("*structpath.PatternNode" )).
34+ Report (`use the structpath node instead of rendering and parsing it` )
35+
2736 m .Match (
2837 `$c.GetLocations($a + $b)` ,
2938 `$c.GetLocation($a + $b)` ,
3039 `$c.DefinitionLocation($a + $b)` ,
3140 `$c.GetLocations(fmt.Sprintf($*_))` ,
3241 `$c.GetLocation(fmt.Sprintf($*_))` ,
3342 `$c.DefinitionLocation(fmt.Sprintf($*_))` ,
34- `$c.GetLocations($p.String())` ,
35- `$c.GetLocation($p.String())` ,
36- `$c.DefinitionLocation($p.String())` ,
3743 ).
38- Where (! m .File ().Name .Matches (`_test\.go$` )).
44+ Where (! m .File ().Name .Matches (`_test\.go$` ) &&
45+ (m ["c" ].Type .Is ("config.Root" ) || m ["c" ].Type .Is ("*config.Root" ))).
3946 Report (`pass a *structpath.PathNode to GetLocationsOf / GetLocationOf / DefinitionLocationOf instead of a built string` )
47+
48+ m .Match (`$c.GetLocations($p.String())` , `$c.GetLocation($p.String())` , `$c.DefinitionLocation($p.String())` ).
49+ Where (! m .File ().Name .Matches (`_test\.go$` ) &&
50+ (m ["c" ].Type .Is ("config.Root" ) || m ["c" ].Type .Is ("*config.Root" )) &&
51+ m ["p" ].Type .Is ("*structpath.PathNode" )).
52+ Report (`pass the *structpath.PathNode to GetLocationsOf / GetLocationOf / DefinitionLocationOf instead of its string` )
4053}
0 commit comments