Skip to content

Commit

Permalink
fix(php): try catch anchoring (#1338)
Browse files Browse the repository at this point in the history
* refactor: use correct name for IsAnchored result

* fix: unanchor catch blocks and exception types
  • Loading branch information
didroe authored Oct 18, 2023
1 parent 1f4b733 commit b0cd69a
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions internal/languages/golang/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func (*Pattern) IsAnchored(node *tree.Node) (bool, bool) {
"var_declaration",
}

isUnanchored := !slices.Contains(unAnchored, parent.Type())
return isUnanchored, isUnanchored
isAnchored := !slices.Contains(unAnchored, parent.Type())
return isAnchored, isAnchored
}

func (*Pattern) IsRoot(node *tree.Node) bool {
Expand Down
4 changes: 2 additions & 2 deletions internal/languages/java/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func (*Pattern) IsAnchored(node *tree.Node) (bool, bool) {
// try {} catch () {}
unAnchored := []string{"class_body", "block", "try_statement", "catch_type", "resource_specification"}

isUnanchored := !slices.Contains(unAnchored, parent.Type())
return isUnanchored, isUnanchored
isAnchored := !slices.Contains(unAnchored, parent.Type())
return isAnchored, isAnchored
}

func (*Pattern) IsRoot(node *tree.Node) bool {
Expand Down
4 changes: 2 additions & 2 deletions internal/languages/javascript/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (*Pattern) IsAnchored(node *tree.Node) (bool, bool) {
// method statement_block
unAnchored := []string{"statement_block", "class_body", "object_pattern", "named_imports"}

isUnanchored := !slices.Contains(unAnchored, parent.Type())
return isUnanchored, isUnanchored
isAnchored := !slices.Contains(unAnchored, parent.Type())
return isAnchored, isAnchored
}

func (*Pattern) IsRoot(node *tree.Node) bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(*builder.Result)({
Query: (string) (len=158) "([(try_statement . [ (compound_statement )] [(catch_clause . [(type_list (_))] . [(variable_name . (_) .)] @match . [ (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=210) "([(class_declaration . (_) . [(declaration_list [(method_declaration [ (visibility_modifier )] (_) . [(formal_parameters . [(simple_parameter . (_) (_) @match)] . )] [ (compound_statement )])] )] .)] @root)",
Query: (string) (len=224) "([(class_declaration . (_) . [(declaration_list [(method_declaration [ (visibility_modifier )] (_) . [(formal_parameters . [(simple_parameter . [(type_list (_))] (_) @match)] . )] [ (compound_statement )])] )] .)] @root)",
VariableNames: ([]string) (len=1) {
(string) (len=1) "_"
},
Expand Down
12 changes: 12 additions & 0 deletions internal/languages/php/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func (analyzer *analyzer) Analyze(node *sitter.Node, visitChildren func() error)
return analyzer.analyzeDynamicVariableName(node, visitChildren)
case "subscript_expression":
return analyzer.analyzeSubscript(node, visitChildren)
case "catch_clause":
return analyzer.analyzeCatchClause(node, visitChildren)
case "binary_expression",
"unary_op_expression",
"argument",
Expand Down Expand Up @@ -193,6 +195,16 @@ func (analyzer *analyzer) analyzeSubscript(node *sitter.Node, visitChildren func
return visitChildren()
}

// catch(FooException | BarException $e) {}
func (analyzer *analyzer) analyzeCatchClause(node *sitter.Node, visitChildren func() error) error {
return analyzer.withScope(language.NewScope(analyzer.scope), func() error {
name := node.ChildByFieldName("name")
analyzer.scope.Declare(analyzer.builder.ContentFor(name), name)

return visitChildren()
})
}

// default analysis, where the children are assumed to be aliases
func (analyzer *analyzer) analyzeGenericConstruct(node *sitter.Node, visitChildren func() error) error {
analyzer.builder.Alias(node, analyzer.builder.ChildrenFor(node)...)
Expand Down
10 changes: 6 additions & 4 deletions internal/languages/php/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var (
patternQueryVariableRegex = regexp.MustCompile(`\$<(?P<name>[^>:!\.]+)(?::(?P<types>[^>]+))?>`)
matchNodeRegex = regexp.MustCompile(`\$<!>`)
ellipsisRegex = regexp.MustCompile(`\$<\.\.\.>`)
unanchoredPatternNodeTypes = []string{}
patternMatchNodeContainerTypes = []string{"formal_parameters", "simple_parameter", "argument"}
unanchoredPatternNodeTypes = []string{"catch_clause"}
patternMatchNodeContainerTypes = []string{"formal_parameters", "simple_parameter", "argument", "type_list"}

allowedPatternQueryTypes = []string{"_"}

Expand Down Expand Up @@ -203,13 +203,15 @@ func (*Pattern) IsAnchored(node *tree.Node) (bool, bool) {

// Class body declaration_list
// function/block compound_statement
// Type1 | Type2
unAnchored := []string{
"declaration_list",
"compound_statement",
"type_list",
}

isUnanchored := !slices.Contains(unAnchored, parent.Type())
return isUnanchored, isUnanchored
isAnchored := !slices.Contains(unAnchored, parent.Type())
return isAnchored, isAnchored
}

func (*Pattern) IsRoot(node *tree.Node) bool {
Expand Down
3 changes: 3 additions & 0 deletions internal/languages/php/php_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func TestPattern(t *testing.T) {
public function $<_>($<_> $<!>$<_>) {}
}
`},
{"catch clauses and types are unanchored", `
try {} catch ($<_> $<!>$$<_>) {}
`},
} {
t.Run(test.name, func(tt *testing.T) {
result, err := patternquerybuilder.Build(php.Get(), test.pattern, "")
Expand Down
4 changes: 2 additions & 2 deletions internal/languages/python/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ func (*Pattern) IsAnchored(node *tree.Node) (bool, bool) {
// function/block compound_statement
unAnchored := []string{}

isUnanchored := !slices.Contains(unAnchored, parent.Type())
return isUnanchored, isUnanchored
isAnchored := !slices.Contains(unAnchored, parent.Type())
return isAnchored, isAnchored
}

func (*Pattern) IsRoot(node *tree.Node) bool {
Expand Down

0 comments on commit b0cd69a

Please sign in to comment.