Skip to content

Commit

Permalink
fix(php): anchoring of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe committed Nov 1, 2023
1 parent f1ce4c2 commit f679cbe
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(*builder.Result)({
Query: (string) (len=100) "([(anonymous_function_creation_expression [ (formal_parameters )] [ (compound_statement )])] @root)",
VariableNames: ([]string) {
},
ParamToVariable: (map[string]string) {
},
EqualParams: ([][]string) <nil>,
ParamToContent: (map[string]map[string]string) {
},
RootVariable: (*language.PatternVariable)(<nil>)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(*builder.Result)({
Query: (string) (len=91) "([(function_definition name: (_) [ (formal_parameters )] [ (compound_statement )])] @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>)
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(*builder.Result)({
Query: (string) (len=234) "([(class_declaration . name: (_) . [(declaration_list [(method_declaration [ (visibility_modifier )] name: (_) . [(formal_parameters . [(simple_parameter . type: (_) name: (_) @match)] . )] [ (compound_statement )])] )] .)] @root)",
Query: (string) (len=232) "([(class_declaration . name: (_) . [(declaration_list [(method_declaration [ (visibility_modifier )] name: (_) [(formal_parameters . [(simple_parameter . type: (_) name: (_) @match)] . )] [ (compound_statement )])] )] .)] @root)",
VariableNames: ([]string) (len=1) {
(string) (len=1) "_"
},
Expand Down
10 changes: 7 additions & 3 deletions internal/languages/php/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,19 @@ func (*Pattern) IsAnchored(node *tree.Node) (bool, bool) {
return false, true
}

if parent.Type() == "method_declaration" {
if slices.Contains([]string{
"method_declaration",
"function_definition",
"anonymous_function_creation_expression",
}, parent.Type()) {
// visibility
if node == parent.ChildByFieldName("name") {
return false, true
}

// type
if node == parent.ChildByFieldName("parameters") {
return true, false
if node == parent.ChildByFieldName("body") {
return false, true
}

return false, false
Expand Down
6 changes: 6 additions & 0 deletions internal/languages/php/php_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ func TestPattern(t *testing.T) {
{"catch clauses and types are unanchored", `
try {} catch ($<_> $<!>$$<_>) {}
`},
{"function names and bodies are unanchored", `
function $<_>() {}
`},
{"anonymous function names and bodies are unanchored", `
function () {};
`},
} {
t.Run(test.name, func(tt *testing.T) {
result, err := patternquerybuilder.Build(php.Get(), test.pattern, "")
Expand Down

0 comments on commit f679cbe

Please sign in to comment.