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] Handle NEXT control structure #4758

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -57,6 +57,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
case node: BreakStatement => astForBreakStatement(node)
case node: StatementList => astForStatementList(node)
case node: ReturnExpression => astForReturnStatement(node)
case node: NextExpression => astForNextExpression(node)
case node: DummyNode => Ast(node.node)
case node: Unknown => astForUnknown(node)
case x =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ trait AstForStatementsCreator(implicit withSchemaValidation: ValidationMode) { t
returnAst(returnNode_, argumentAsts)
}

protected def astForNextExpression(node: NextExpression): Ast = {
val nextNode = NewControlStructure()
.controlStructureType(ControlStructureTypes.CONTINUE)
.lineNumber(line(node))
.columnNumber(column(node))
.code(code(node))
Ast(nextNode)
}

protected def astForStatementListReturningLastExpression(node: StatementList): Ast = {
val block = blockNode(node)
scope.pushNewScope(BlockScope(block))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ object RubyIntermediateAst {
extends RubyNode(span)
with ControlFlowClause

final case class NextExpression()(span: TextSpan) extends RubyNode(span) with ControlFlowExpression

final case class ReturnExpression(expressions: List[RubyNode])(span: TextSpan) extends RubyNode(span)

/** Represents an unqualified identifier e.g. `X`, `x`, `@@x`, `$x`, `$<`, etc. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class RubyNodeCreator extends RubyParserBaseVisitor[RubyNode] {
StatementList(ctx.getStatements.map(visit))(ctx.toTextSpan)
}

override def visitNextWithoutArguments(ctx: RubyParser.NextWithoutArgumentsContext): RubyNode = {
NextExpression()(ctx.toTextSpan)
}

override def visitGroupingStatement(ctx: RubyParser.GroupingStatementContext): RubyNode = {
// When there's only 1 statement, we can use it directly, instead of wrapping it in a StatementList.
val statements = ctx.compoundStatement().getStatements.map(visit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,18 @@ class ControlStructureTests extends RubyCode2CpgFixture {
}
}
}

"Generate continue node for next" in {
val cpg = code("""
|for i in arr do
| next if i % 2 == 0
|end
|""".stripMargin)

inside(cpg.controlStructure.controlStructureType(ControlStructureTypes.CONTINUE).l) {
case nextControl :: Nil =>
nextControl.code shouldBe "next"
case xs => fail(s"Expected next to be continue, got [${xs.code.mkString(",")}]")
}
}
}
Loading