Skip to content

Commit

Permalink
[ruby] Fixed Paramater without method issue (#5159)
Browse files Browse the repository at this point in the history
* Fixed issue where InstanceFieldIdentifier was wrongly mapped to SimpleIdentifier

* Instance Field Assignment made more type safe
  • Loading branch information
AndreiDreyer authored Dec 4, 2024
1 parent eca8117 commit e6b37c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1045,8 +1045,12 @@ class RubyJsonToNodeCreator(
case Some(rhs) =>
SingleAssignment(lhs, "=", rhs)(obj.toTextSpan)
case None =>
// `lvasgn` is used in exec_var for rescueExpr, which only has LHS
MandatoryParameter(lhs.span.text)(lhs.span)
if (AstType.fromString(obj(ParserKeys.Type).str) == AstType.LocalVariableAssign) {
// `lvasgn` is used in exec_var for rescueExpr, which only has LHS
MandatoryParameter(lhs.span.text)(lhs.span)
} else {
lhs
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1311,4 +1311,27 @@ class ClassTests extends RubyCode2CpgFixture {

cpg.typeDecl("Foo").astChildren.whereNot(_.or(_.isMethod, _.isModifier, _.isTypeDecl, _.isMember)).size shouldBe 0
}

"Proc-param in method with instance field assignment and instance field argument" in {
val cpg = code("""
|class Batches
| def as_batches(query, &)
| records.each(&)
| @limit -= 100
| return if @limit.zero?
| end
|end
|""".stripMargin)

inside(cpg.method.name("as_batches").l) {
case batchesMethod :: Nil =>
inside(batchesMethod.parameter.l) {
case _ :: _ :: procParam :: Nil =>
procParam.code shouldBe "&"
procParam.name shouldBe "<proc-param-0>"
case xs => fail(s"Expected three parameters, got (${xs.size}) [${xs.code.mkString(",")}]")
}
case xs =>
}
}
}

0 comments on commit e6b37c5

Please sign in to comment.