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

[C#] Ast structure for CoalesceExpression #4788

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably add a <operator>.coalesce so that we can embed the correct semantics

Copy link
Contributor

Choose a reason for hiding this comment

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

Should this not also be a callNode or something similar with the operator you suggested above?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@AndreiDreyer Yes, something like callAst(callNode, 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 @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would like AST tests here too, please. Specifically to verify the coalescing operator.

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