Skip to content

Commit

Permalink
Fixed handling of empty source map entries (#316)
Browse files Browse the repository at this point in the history
Source maps may contain empty map entries / empty file paths.
Fixes: failing gazelle tests (e.g., see and test `npm pack morphi`)
  • Loading branch information
max-leuthaeuser authored Nov 7, 2023
1 parent 0b22b2f commit 30712bb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
1 change: 0 additions & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.16")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.0")
addSbtPlugin("io.shiftleft" % "sbt-ci-release-early" % "2.0.27")
addSbtPlugin("com.dwijnand" % "sbt-dynver" % "4.1.1")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.1.0")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.10.0")
11 changes: 7 additions & 4 deletions src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,22 @@ class JsSource(val srcDir: File, val projectDir: Path, val source: Source) {
}
}

private def constructSourceFilePath(sourceFileName: String): File = sourceFileName match {
case _ if absoluteFilePath.contains(NuxtTranspiler.NUXT_FOLDER) && srcDir.path.compareTo(projectDir) == 0 =>
private def constructSourceFilePath(sourceFileName: String): File = {
if (sourceFileName.isEmpty) {
srcDir / source.getName
} else if (absoluteFilePath.contains(NuxtTranspiler.NUXT_FOLDER) && srcDir.path.compareTo(projectDir) == 0) {
// For nuxt-js transpilation we have the same src and project dir and we need some special handling here
if (sourceFileName.startsWith(WEBPACK_PREFIX)) {
val replacedName = FileUtils.cleanPath(sourceFileName.replace(WEBPACK_PREFIX, ""))
srcDir / replacedName.substring(replacedName.indexOf("/") + 1)
} else {
File(absoluteFilePath).parent / sourceFileName
}
case _ if sourceFileName.startsWith(WEBPACK_PREFIX) =>
} else if (sourceFileName.startsWith(WEBPACK_PREFIX)) {
// Additionally, source map files coming from webpack (e.g., from Vue transpilation) are somewhat hidden
val replacedName = sourceFileName.replace(WEBPACK_PREFIX, "")
srcDir / replacedName.substring(replacedName.indexOf("/") + 1)
case _ =>
} else {
val cleanedPath = FileUtils.cleanPath(sourceFileName)
// having "/" here is fine as JS source maps always have platform independent path separators
val lookupPath = if (cleanedPath.contains("/" + srcDir.name + "/")) {
Expand All @@ -115,6 +117,7 @@ class JsSource(val srcDir: File, val projectDir: Path, val source: Source) {
}
}
srcFilePath
}
}

private def sourceMapOrigin(): Option[SourceMapOrigin] = {
Expand Down

0 comments on commit 30712bb

Please sign in to comment.