Skip to content

Commit

Permalink
Fixed dependency removal (#240)
Browse files Browse the repository at this point in the history
Anything other than object and array node is ignored now to handle unknown package.json configurations.
Fixes: https://shiftleftinc.atlassian.net/browse/SEN-420
Updated deps.
  • Loading branch information
max-leuthaeuser authored Jan 25, 2023
1 parent 12a64f2 commit e02471c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val cpgVersion = "1.3.585"
val joernVersion = "1.1.1378"
val cpgVersion = "1.3.587"
val joernVersion = "1.1.1403"

val gitCommitString = SettingKey[String]("gitSha")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ object PackageJsonParser {
"peerDependenciesMeta",
"optionalDependencies",
"resolutions",
"bundleDependencies",
"bundledDependencies"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.shiftleft.js2cpg.preprocessing
import better.files.File
import better.files.File.LinkOptions
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ArrayNode
import com.fasterxml.jackson.databind.node.ObjectNode
import io.shiftleft.js2cpg.core.Config
import io.shiftleft.js2cpg.io.FileDefaults
Expand Down Expand Up @@ -130,14 +131,21 @@ class TranspilationRunner(projectPath: Path, tmpTranspileDir: Path, config: Conf

// remove all project specific dependencies (only keep the ones required for transpiling)
PackageJsonParser.PROJECT_DEPENDENCIES.foreach { dep =>
Option(jsonObject.get(dep).asInstanceOf[ObjectNode]).foreach { depNode =>
val fieldsToRemove =
depNode
Option(jsonObject.get(dep)) match {
case Some(depNode: ObjectNode) =>
val fieldsToRemove = depNode
.fieldNames()
.asScala
.toList
.toSet
.filterNot(f => DEPS_TO_KEEP.exists(f.startsWith))
fieldsToRemove.foreach(depNode.remove)
fieldsToRemove.foreach(depNode.remove)
case Some(depNode: ArrayNode) =>
val allFields = depNode.elements().asScala.toSet
val fieldsToRemove = allFields.filterNot(f => DEPS_TO_KEEP.exists(f.asText().startsWith))
val remainingElements = allFields -- fieldsToRemove
depNode.removeAll()
remainingElements.foreach(depNode.add)
case _ => // this is fine; we ignore all other nodes intentionally
}
}
// remove project specific engine restrictions and script hooks
Expand Down

0 comments on commit e02471c

Please sign in to comment.