Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ruby] Implement hashCode for RubyExpression #4986

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
}

protected def astForDoBlock(block: Block & RubyExpression): Seq[Ast] = {
// Create closure structures: [MethodDecl, TypeRef, MethodRef]
if (closureToRefs.contains(block)) {
closureToRefs(block).map(x => Ast(x.copy))
} else {
val methodName = nextClosureName()

// Create closure structures: [TypeRef, MethodRef]
val methodRefAsts = block.body match {
case x: Block =>
astForMethodDeclaration(x.toMethodDeclaration(methodName, Option(block.parameters)), isClosure = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package io.joern.rubysrc2cpg.astcreation

import io.joern.rubysrc2cpg.astcreation.RubyIntermediateAst.{AllowedTypeDeclarationChild, RubyStatement}
import io.joern.rubysrc2cpg.passes.{Defines, GlobalTypes}
import io.shiftleft.codepropertygraph.generated.nodes.NewNode

import scala.annotation.tailrec
import java.util.Objects

object RubyIntermediateAst {

Expand Down Expand Up @@ -33,6 +32,8 @@ object RubyIntermediateAst {
def offset: Option[(Int, Int)] = span.offset

def text: String = span.text

override def hashCode(): Int = Objects.hash(span)
maltek marked this conversation as resolved.
Show resolved Hide resolved
}

/** Ruby statements evaluate to some value (and thus are expressions), but also perform some operation, e.g.,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,5 +1000,15 @@ class MethodTests extends RubyCode2CpgFixture {
cpg.typeRef.typeFullName(".*Proc").size shouldBe 5
cpg.typeRef.whereNot(_.astParent).size shouldBe 0
}

"resolve cached lambdas correctly" in {
def getLineNumberOfLambdaForCall(callName: String) =
cpg.call.nameExact(callName).argument.isTypeRef.typ.referencedTypeDecl.lineNumber.head

getLineNumberOfLambdaForCall("with_index") shouldBe 3
getLineNumberOfLambdaForCall("sort_by") shouldBe 4
getLineNumberOfLambdaForCall("reject") shouldBe 5
getLineNumberOfLambdaForCall("map") shouldBe 6
}
}
}
Loading