Skip to content

Commit 7c7b9fa

Browse files
fix schema violations from enum static inits
1 parent 2390cd4 commit 7c7b9fa

File tree

1 file changed

+54
-18
lines changed

1 file changed

+54
-18
lines changed

joern-cli/frontends/jssrc2cpg/src/main/scala/io/joern/jssrc2cpg/astcreation/AstForTypesCreator.scala

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -293,28 +293,30 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
293293
typeRefIdStack.push(typeRefNode_)
294294
scope.pushNewMethodScope(typeFullName, typeName, typeDeclNode_, None)
295295

296-
val memberAsts = tsEnum.json("members").arr.toList.flatMap(m => astsForEnumMember(createBabelNodeInfo(m)))
296+
val enumMembers = tsEnum.json("members").arr.toList
297+
val enumMembersPlain = enumMembers.filter(m => !hasKey(m, "initializer"))
298+
val enumMembersWithInitializer = enumMembers.filter(m => hasKey(m, "initializer") && !m("initializer").isNull)
299+
300+
val memberAsts = enumMembersPlain.flatMap(m => astsForEnumMember(createBabelNodeInfo(m)))
301+
302+
if (enumMembersWithInitializer.isEmpty) {
303+
Ast.storeInDiffGraph(Ast(typeDeclNode_).withChildren(memberAsts), diffGraph)
304+
} else {
305+
val init = staticEnumInitMethodAst(
306+
tsEnum,
307+
typeDeclNode_,
308+
enumMembersWithInitializer,
309+
s"$typeFullName:${io.joern.x2cpg.Defines.StaticInitMethodName}",
310+
Defines.Any
311+
)
312+
Ast.storeInDiffGraph(Ast(typeDeclNode_).withChildren(memberAsts).withChild(init), diffGraph)
313+
}
297314

298315
methodAstParentStack.pop()
299316
dynamicInstanceTypeStack.pop()
300317
typeRefIdStack.pop()
301318
scope.popScope()
302319

303-
val (calls, member) = memberAsts.partition(_.nodes.headOption.exists(_.isInstanceOf[NewCall]))
304-
if (calls.isEmpty) {
305-
Ast.storeInDiffGraph(Ast(typeDeclNode_).withChildren(member), diffGraph)
306-
} else {
307-
val init =
308-
staticInitMethodAst(
309-
tsEnum,
310-
calls,
311-
s"$typeFullName:${io.joern.x2cpg.Defines.StaticInitMethodName}",
312-
None,
313-
Defines.Any
314-
)
315-
Ast.storeInDiffGraph(Ast(typeDeclNode_).withChildren(member).withChild(init), diffGraph)
316-
}
317-
318320
diffGraph.addEdge(methodAstParentStack.head, typeDeclNode_, EdgeTypes.AST)
319321
Ast(typeRefNode_)
320322
}
@@ -358,7 +360,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
358360
))
359361
}
360362

361-
private def staticInitMethodAst(
363+
private def staticClassInitMethodAst(
362364
node: BabelNodeInfo,
363365
typeDeclNode: NewTypeDecl,
364366
staticMemberInits: List[Value],
@@ -391,6 +393,40 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
391393
methodAst(methodNode_, Nil, body, methodReturn, List(staticModifier))
392394
}
393395

396+
private def staticEnumInitMethodAst(
397+
node: BabelNodeInfo,
398+
typeDeclNode: NewTypeDecl,
399+
enumMembersWithInitializer: List[Value],
400+
fullName: String,
401+
returnType: String
402+
): Ast = {
403+
val staticModifier = NewModifier().modifierType(ModifierTypes.STATIC)
404+
val blockNode = NewBlock().typeFullName(Defines.Any)
405+
val methodNode_ = methodNode(node, io.joern.x2cpg.Defines.StaticInitMethodName, fullName, "", parserResult.filename)
406+
407+
methodAstParentStack.push(methodNode_)
408+
scope.pushNewMethodScope(
409+
fullName,
410+
io.joern.x2cpg.Defines.StaticInitMethodName,
411+
blockNode,
412+
typeRefIdStack.headOption
413+
)
414+
localAstParentStack.push(blockNode)
415+
416+
val initAsts = enumMembersWithInitializer.flatMap(m => astsForEnumMember(createBabelNodeInfo(m)))
417+
val (calls, members) = initAsts.partition(_.nodes.headOption.exists(_.isInstanceOf[NewCall]))
418+
419+
members.foreach(m => diffGraph.addEdge(typeDeclNode, m.root.get, EdgeTypes.AST))
420+
val body = blockAst(NewBlock().typeFullName(Defines.Any), calls)
421+
422+
methodAstParentStack.pop()
423+
localAstParentStack.pop()
424+
scope.popScope()
425+
426+
val methodReturn = methodReturnNode(node, returnType)
427+
methodAst(methodNode_, Nil, body, methodReturn, List(staticModifier))
428+
}
429+
394430
protected def astForClass(clazz: BabelNodeInfo, shouldCreateAssignmentCall: Boolean = false): Ast = {
395431
val (typeName, typeFullName) = calcTypeNameAndFullName(clazz)
396432
registerType(typeFullName)
@@ -453,7 +489,7 @@ trait AstForTypesCreator(implicit withSchemaValidation: ValidationMode) { this:
453489
val staticInitBlocks = staticInitBlock.map(block => block("body").arr.toList).getOrElse(List.empty)
454490

455491
if (staticMemberInits.nonEmpty || staticInitBlocks.nonEmpty) {
456-
val init = staticInitMethodAst(
492+
val init = staticClassInitMethodAst(
457493
clazz,
458494
typeDeclNode_,
459495
staticMemberInits,

0 commit comments

Comments
 (0)