Skip to content

Commit

Permalink
fix(php): make property names unanchored (#1316)
Browse files Browse the repository at this point in the history
fix: make php property names unanchored
  • Loading branch information
didroe authored Oct 10, 2023
1 parent 6689428 commit 6a53fa5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(*builder.Result)({
Query: (string) (len=128) "([(class_declaration . (_) . [(declaration_list [(property_declaration . [ (visibility_modifier )] (_) @match )] )] .)] @root)",
VariableNames: ([]string) (len=1) {
(string) (len=1) "_"
},
ParamToVariable: (map[string]string) {
},
EqualParams: ([][]string) <nil>,
ParamToContent: (map[string]map[string]string) {
},
RootVariable: (*language.PatternVariable)(<nil>)
})
11 changes: 9 additions & 2 deletions internal/languages/php/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ func (*Pattern) FixupMissing(node *tree.Node) string {
}

func (*Pattern) FixupVariableDummyValue(input []byte, node *tree.Node, dummyValue string) string {
if slices.Contains([]string{"named_type"}, node.Parent().Type()) {
return "$" + dummyValue
if parent := node.Parent(); parent != nil {
if parent.Type() == "named_type" ||
(parent.Type() == "ERROR" && parent.Parent() != nil && parent.Parent().Type() == "declaration_list") {
return "$" + dummyValue
}
}

return dummyValue
Expand Down Expand Up @@ -131,6 +134,10 @@ func (*Pattern) IsAnchored(node *tree.Node) (bool, bool) {
return false, false
}

if node.Type() == "property_element" {
return false, true
}

parent := node.Parent()
if parent == nil {
return true, true
Expand Down
5 changes: 5 additions & 0 deletions internal/languages/php/php_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func TestPattern(t *testing.T) {
{"named arguments are unanchored", `
foo(x: $<!>$<_>)
`},
{"property names are unanchored", `
class $<_> {
public $<!>$<_>;
}
`},
} {
t.Run(test.name, func(tt *testing.T) {
result, err := patternquerybuilder.Build(php.Get(), test.pattern, "")
Expand Down

0 comments on commit 6a53fa5

Please sign in to comment.