Skip to content

Commit

Permalink
fix: class_declaration in java (#1750)
Browse files Browse the repository at this point in the history
  • Loading branch information
cfabianski authored Jan 29, 2025
1 parent a452532 commit dc8f119
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 32 deletions.
4 changes: 2 additions & 2 deletions pkg/languages/golang/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,6 @@ func (*Pattern) TranslateContent(fromNodeType, toNodeType, content string) strin
return content
}

func (*Pattern) ContainerTypes() []string {
return patternMatchNodeContainerTypes
func (*Pattern) IsContainer(node *tree.Node) bool {
return slices.Contains(patternMatchNodeContainerTypes, node.Type())
}
15 changes: 12 additions & 3 deletions pkg/languages/java/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var (
"formal_parameters",
"modifiers",
"method_declaration",
"class_declaration",
"program",
}

Expand Down Expand Up @@ -147,6 +146,16 @@ func (*Pattern) NodeTypes(node *tree.Node) []string {
return []string{node.Type()}
}

func (*Pattern) ContainerTypes() []string {
return matchNodeContainerTypes
func (*Pattern) IsContainer(node *tree.Node) bool {
if slices.Contains(matchNodeContainerTypes, node.Type()) {
return true
}

if node.Type() == "class_declaration" {
if children := node.NamedChildren(); len(children) != 0 && children[0].Type() == "modifiers" {
return true
}
}

return false
}
4 changes: 2 additions & 2 deletions pkg/languages/javascript/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func (*Pattern) FindUnanchoredPoints(input []byte) [][]int {
return ellipsisRegex.FindAllIndex(input, -1)
}

func (*Pattern) ContainerTypes() []string {
return patternMatchNodeContainerTypes
func (*Pattern) IsContainer(node *tree.Node) bool {
return slices.Contains(patternMatchNodeContainerTypes, node.Type())
}

func (*Pattern) LeafContentTypes() []string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/languages/php/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,6 @@ func (*Pattern) TranslateContent(fromNodeType, toNodeType, content string) strin
return content
}

func (*Pattern) ContainerTypes() []string {
return patternMatchNodeContainerTypes
func (*Pattern) IsContainer(node *tree.Node) bool {
return slices.Contains(patternMatchNodeContainerTypes, node.Type())
}
15 changes: 2 additions & 13 deletions pkg/languages/python/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,6 @@ func (patternLanguage *Pattern) NodeTypes(node *tree.Node) []string {
return []string{node.Type()}
}

// func (*Pattern) TranslateContent(fromNodeType, toNodeType, content string) string {
// if fromNodeType == "string" && toNodeType == "encapsed_string" {
// return fmt.Sprintf(`"%s"`, content[1:len(content)-1])
// }
// if fromNodeType == "encapsed_string" && toNodeType == "string" {
// return fmt.Sprintf("'%s'", content[1:len(content)-1])
// }

// return content
// }

func (*Pattern) ContainerTypes() []string {
return patternMatchNodeContainerTypes
func (*Pattern) IsContainer(node *tree.Node) bool {
return slices.Contains(patternMatchNodeContainerTypes, node.Type())
}
4 changes: 2 additions & 2 deletions pkg/languages/ruby/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func (*Pattern) AnonymousParentTypes() []string {
return anonymousPatternNodeParentTypes
}

func (*Pattern) ContainerTypes() []string {
return patternMatchNodeContainerTypes
func (*Pattern) IsContainer(node *tree.Node) bool {
return slices.Contains(patternMatchNodeContainerTypes, node.Type())
}

func (*Pattern) IsAnchored(node *tree.Node) (bool, bool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ func Build(
builder.setMatchNode(
inputParams.MatchNodeOffset,
focusedVariable,
patternLanguage.ContainerTypes(),
tree.RootNode(),
)
if builder.matchNode == nil {
Expand Down Expand Up @@ -386,7 +385,7 @@ func getVariableFor(
patternLanguage language.Pattern,
variables []language.PatternVariable,
) *language.PatternVariable {
if slices.Contains(patternLanguage.ContainerTypes(), node.Type()) {
if patternLanguage.IsContainer(node) {
return nil
}

Expand All @@ -410,7 +409,6 @@ func (builder *builder) newParam() string {
func (builder *builder) setMatchNode(
offset int,
focusedVariable string,
containerTypes []string,
node *asttree.Node,
) {
err := node.Walk(func(node *asttree.Node, visitChildren func() error) error {
Expand All @@ -420,7 +418,7 @@ func (builder *builder) setMatchNode(
return nil
}
} else {
if node.ContentStart.Byte == offset && !slices.Contains(containerTypes, node.Type()) {
if node.ContentStart.Byte == offset && !builder.patternLanguage.IsContainer(node) {
builder.matchNode = node
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/scanner/language/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ type Pattern interface {
// some_call($<!>$<VAR>)
// we would return `[[10, 14]]`
FindMatchNode(input []byte) [][]int
// ContainerTypes returns a list of node types from which a match node should
// IsContainer returns a bool when node is included in the list of containers
// from which a match node should
// not be able to escape. There can be multiple nodes in the tree at the same
// character position, and we want to allow a match node to be the highest
// position node, terminating at a container node.
Expand All @@ -106,7 +107,7 @@ type Pattern interface {
// given the following Ruby code matching the pattern:
// some_call key: value, other_key: value2
// we want the content of the match to be `key: value` and not `key: value, other_key: value2`
ContainerTypes() []string
IsContainer(node *tree.Node) bool

// Handle cases where the language requires preamble (e.g. PHP requires `<?php`)
AdjustInput(input string) string
Expand Down
4 changes: 2 additions & 2 deletions pkg/scanner/language/patternbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func (*PatternBase) ShouldSkipNode(node *tree.Node) bool {
return false
}

func (*PatternBase) ContainerTypes() []string {
return nil
func (*PatternBase) IsContainer(node *tree.Node) bool {
return false
}

func (*PatternBase) FixupVariableDummyValue(input []byte, node *tree.Node, dummyValue string) string {
Expand Down

0 comments on commit dc8f119

Please sign in to comment.