Skip to content

Commit

Permalink
Merge branch 'master' into c2cpgTypeRecovery
Browse files Browse the repository at this point in the history
  • Loading branch information
khemrajrathore authored Jul 24, 2024
2 parents 8a2d53f + 29715a5 commit 26e3c7e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ 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 _ => notHandledYet(expr)
}
}

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
}

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 @@ -270,6 +270,8 @@ object DotNetJsonAst {

object Attribute extends BaseExpr

object CoalesceExpression extends BaseExpr

object Unknown extends DotNetParserNode

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,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")
}
}
}
}

0 comments on commit 26e3c7e

Please sign in to comment.