Skip to content
Closed
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 @@ -38,6 +38,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
case InterpolatedStringExpression => astForInterpolatedStringExpression(expr)
case ConditionalAccessExpression => astForConditionalAccessExpression(expr)
case SuppressNullableWarningExpression => astForSuppressNullableWarningExpression(expr)
case CoalesceExpression => astForCoalesceExpression(expr)
case _: BaseLambdaExpression => astForSimpleLambdaExpression(expr)
case ParenthesizedExpression => astForParenthesizedExpression(expr)
case _ => notHandledYet(expr)
Expand All @@ -48,6 +49,13 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
astForNode(parenExpr.json(ParserKeys.Expression))
}

private def astForCoalesceExpression(coalesceExpression: DotNetNodeInfo): Seq[Ast] = {
val leftAst = astForExpression(createDotNetNodeInfo(coalesceExpression.json(ParserKeys.Left)))
val rightAst = astForExpression(createDotNetNodeInfo(coalesceExpression.json(ParserKeys.Right)))

leftAst ++ rightAst
}
Copy link
Contributor

@xavierpinho xavierpinho Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest placing its arguments inside an <operator> call of some sorts or, alternatively, the ternary leftAst != null ? leftAst : rightAst.

Otherwise we won't have their context.

EDIT: This is duplicate of #4788


private def astForAwaitExpression(awaitExpr: DotNetNodeInfo): Seq[Ast] = {
/* fullName is the name in case of STATIC_DISPATCH */
val node =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ object DotNetJsonAst {

object ParenthesizedExpression extends BaseExpr

object CoalesceExpression extends BaseExpr

object Unknown extends DotNetParserNode

object AccessorList extends DotNetParserNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,34 @@ class CallTests extends CSharpCode2CpgFixture {
}
}

"null-coalescing operator" should {
val cpg = code("""
|namespace Baz
|{
| public class Foo
| {
| private readonly IDatabase db;
|
| public async AnyValue GetValue(string x)
| {
| var value = await db.get(x) ?? new AnyValue();
| return value;
| }
| }
|}
|""".stripMargin).moreCode("""
|namespace Baz;
|
|public interface IDatabase {
| public AnyValue get(string x) {}
|}
|""".stripMargin)

"resolve methodFullName" in {
inside(cpg.call.name("get").methodFullName.l) {
case x :: Nil => x shouldBe "Baz.IDatabase.get:AnyValue(System.String)"
case _ => fail("Unexpected call node structure")
}
}
}
}
Loading