Skip to content

Commit

Permalink
Fixed ref edges for destructed params (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-leuthaeuser authored Feb 3, 2023
1 parent 303239b commit 4e83eb8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/main/scala/io/shiftleft/js2cpg/astcreation/AstCreator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,11 @@ class AstCreator(diffGraph: DiffGraphBuilder, source: JsSource, usedIdentNodes:
astEdgeBuilder.addAstEdge(destructingAssignmentId, blockId, blockOrder)
case None =>
paramComponents.foreach { param =>
val paramName = param.getName
val localParamId = createIdentifierNode(paramName, param)
val paramId = createIdentifierNode(name, param)
val paramName = param.getName
val localParamId = createIdentifierNode(paramName, param)
val paramId = createIdentifierNode(name, param)
scope.addVariableReference(name, paramId)

val localParamLocal = astNodeBuilder.createLocalNode(paramName, Defines.ANY.label)
addLocalToAst(localParamLocal)
scope.addVariable(paramName, localParamLocal, MethodScope)
Expand Down Expand Up @@ -257,13 +259,9 @@ class AstCreator(diffGraph: DiffGraphBuilder, source: JsSource, usedIdentNodes:
astNodeBuilder.createParameterInNode(name, code, methodId, parameter, new OrderTracker(index))
initStatement match {
case Some(initExpr: VarNode) =>
val paramName = initExpr.getName.getName

val paramName = initExpr.getName.getName
val localParamId = createIdentifierNode(paramName, initExpr)

val rhs =
createRhsForConditionalParameterInit(initExpr.getAssignmentSource, name, initExpr)

val rhs = createRhsForConditionalParameterInit(initExpr.getAssignmentSource, name, initExpr)
val assignmentCallId =
astNodeBuilder.createAssignmentNode(localParamId, rhs, astNodeBuilder.lineAndColumn(initExpr))
astEdgeBuilder.addAstEdge(assignmentCallId, blockId, blockOrder)
Expand All @@ -272,18 +270,14 @@ class AstCreator(diffGraph: DiffGraphBuilder, source: JsSource, usedIdentNodes:
convertDestructingAssignment(initExpr.getExpression.asInstanceOf[BinaryNode], Some(name))
astEdgeBuilder.addAstEdge(destructingAssignmentId, blockId, blockOrder)
case None =>
val paramName = name

val localParam = astNodeBuilder.createLocalNode(paramName, Defines.ANY.label)
addLocalToAst(localParam)

val localParamId = createIdentifierNode(paramName, parameter)

val paramName = name
val localParamId = createIdentifierNode(paramName, parameter)
val localParamLocal = astNodeBuilder.createLocalNode(paramName, Defines.ANY.label)
addLocalToAst(localParamLocal)
scope.addVariable(paramName, localParamLocal, MethodScope)

val paramId = createIdentifierNode(name, parameter)
scope.addVariableReference(name, paramId)

val keyId = astNodeBuilder.createFieldIdentifierNode(paramName, parameter)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.shiftleft.codepropertygraph.generated.{
Operators,
PropertyNames
}
import io.shiftleft.codepropertygraph.generated.nodes.MethodParameterIn
import io.shiftleft.js2cpg.core.{Config, Report}
import io.shiftleft.js2cpg.io.FileDefaults.JS_SUFFIX
import io.shiftleft.js2cpg.io.FileUtils
Expand Down Expand Up @@ -3131,6 +3132,16 @@ class AstCreationPassTest extends AbstractPassTest {
tmpReturnIdentifier.checkProperty(PropertyNames.NAME, "_tmp_0")
}

"have correct ref edge (destructing parameter)" in AstFixture("""
|const WindowOpen = ({ value }) => {
| return () => windowOpenButton(value);
|};""".stripMargin) { cpg =>
val List(param) = cpg.identifier.name("param1_0").refsTo.collectAll[MethodParameterIn].l
param.name shouldBe "param1_0"
param.code shouldBe "{value}"
param.method.fullName shouldBe "test.js::program:anonymous"
}

"have correct structure for object deconstruction in function parameter" in AstFixture(
"function foo({ a }, b) {}"
) { cpg =>
Expand Down

0 comments on commit 4e83eb8

Please sign in to comment.