From f679cbe4eea5b12915ba948d994687a1c8dc149d Mon Sep 17 00:00:00 2001 From: David Roe Date: Wed, 1 Nov 2023 16:29:22 +0000 Subject: [PATCH] fix(php): anchoring of functions --- ...nonymous_function_names_and_bodies_are_unanchored | 11 +++++++++++ ...tPattern-function_names_and_bodies_are_unanchored | 12 ++++++++++++ .../TestPattern-parameter_names_are_unanchored | 2 +- internal/languages/php/pattern/pattern.go | 10 +++++++--- internal/languages/php/php_test.go | 6 ++++++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 internal/languages/php/.snapshots/TestPattern-anonymous_function_names_and_bodies_are_unanchored create mode 100644 internal/languages/php/.snapshots/TestPattern-function_names_and_bodies_are_unanchored diff --git a/internal/languages/php/.snapshots/TestPattern-anonymous_function_names_and_bodies_are_unanchored b/internal/languages/php/.snapshots/TestPattern-anonymous_function_names_and_bodies_are_unanchored new file mode 100644 index 000000000..edc2b1927 --- /dev/null +++ b/internal/languages/php/.snapshots/TestPattern-anonymous_function_names_and_bodies_are_unanchored @@ -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) , + ParamToContent: (map[string]map[string]string) { + }, + RootVariable: (*language.PatternVariable)() +}) diff --git a/internal/languages/php/.snapshots/TestPattern-function_names_and_bodies_are_unanchored b/internal/languages/php/.snapshots/TestPattern-function_names_and_bodies_are_unanchored new file mode 100644 index 000000000..2994ac64a --- /dev/null +++ b/internal/languages/php/.snapshots/TestPattern-function_names_and_bodies_are_unanchored @@ -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) , + ParamToContent: (map[string]map[string]string) { + }, + RootVariable: (*language.PatternVariable)() +}) diff --git a/internal/languages/php/.snapshots/TestPattern-parameter_names_are_unanchored b/internal/languages/php/.snapshots/TestPattern-parameter_names_are_unanchored index b0636a062..e22739caf 100644 --- a/internal/languages/php/.snapshots/TestPattern-parameter_names_are_unanchored +++ b/internal/languages/php/.snapshots/TestPattern-parameter_names_are_unanchored @@ -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) "_" }, diff --git a/internal/languages/php/pattern/pattern.go b/internal/languages/php/pattern/pattern.go index 4e10d9aa6..6363f8cfd 100644 --- a/internal/languages/php/pattern/pattern.go +++ b/internal/languages/php/pattern/pattern.go @@ -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 diff --git a/internal/languages/php/php_test.go b/internal/languages/php/php_test.go index f5f2efb9d..d8ac79b6d 100644 --- a/internal/languages/php/php_test.go +++ b/internal/languages/php/php_test.go @@ -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, "")