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

jssrc2cpg: added support for with statement #3255

Merged
merged 1 commit into from
Jul 26, 2023
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 @@ -218,6 +218,7 @@ class AstCreator(
case JSXSpreadAttribute => astForJsxSpreadAttribute(nodeInfo)
case JSXFragment => astForJsxFragment(nodeInfo)
case JSXAttribute => astForJsxAttribute(nodeInfo)
case WithStatement => astForWithStatement(nodeInfo)
case EmptyStatement => Ast()
case DebuggerStatement => Ast()
case _ => notHandledYet(nodeInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ trait AstForStatementsCreator { this: AstCreator =>
blockAsts
}

protected def astForWithStatement(withStatement: BabelNodeInfo): Ast = {
val blockNode = createBlockNode(withStatement)
scope.pushNewBlockScope(blockNode)
localAstParentStack.push(blockNode)
val objectAst = astForNodeWithFunctionReferenceAndCall(withStatement.json("object"))
val bodyNodeInfo = createBabelNodeInfo(withStatement.json("body"))
val bodyAsts = bodyNodeInfo.node match {
case BlockStatement => createBlockStatementAsts(bodyNodeInfo.json("body"))
case _ => List(astForNodeWithFunctionReferenceAndCall(bodyNodeInfo.json))
}
val blockStatementAsts = objectAst +: bodyAsts
setArgumentIndices(blockStatementAsts)
localAstParentStack.pop()
scope.popScope()
blockAst(blockNode, blockStatementAsts)
}

protected def astForBlockStatement(block: BabelNodeInfo): Ast = {
val blockNode = createBlockNode(block)
scope.pushNewBlockScope(blockNode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ class SimpleAstCreationPassTest extends AbstractPassTest {

"AST generation for simple fragments" should {

"have correct structure for with statement with block" in AstFixture("""
|with(foo()) {
| bar();
|}
|""".stripMargin) { cpg =>
val List(method) = cpg.method.nameExact(":program").l
val List(methodBlock) = method.astChildren.isBlock.l
val List(withBlock) = methodBlock.astChildren.isBlock.l
withBlock.astChildren.isCall.code.l shouldBe List("foo()", "bar()")
}

"have correct structure for with statement without block" in AstFixture("""
|with(foo())
| bar();
|baz();
|""".stripMargin) { cpg =>
val List(method) = cpg.method.nameExact(":program").l
val List(methodBlock) = method.astChildren.isBlock.l
methodBlock.astChildren.isCall.code.l shouldBe List("baz()")
val List(withBlock) = methodBlock.astChildren.isBlock.l
withBlock.astChildren.isCall.code.l shouldBe List("foo()", "bar()")
}

"have correct structure for long numeric literal" in AstFixture("console.log(1e20)") { cpg =>
val List(lit) = cpg.literal.l
lit.code shouldBe "1e20"
Expand Down