Skip to content

Commit

Permalink
Fixed source map code re-mapping (#62)
Browse files Browse the repository at this point in the history
We need to generate the original source code content map from the original file the transpiled file comes from.
Previously, we had loaded the source map file itself which is plain wrong.
  • Loading branch information
max-leuthaeuser authored Jan 6, 2022
1 parent a4e79ab commit 01b697b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class JsSource(val srcDir: File, val projectDir: Path, val source: Source) {
s"Could not load source map file for '$originalFilePath'. The source map file refers to '$sourceFilePath' but this does not exist")
None
} else {
val sourceFileMapping = FileUtils.contentMapFromFile(Paths.get(mapFilePath))
val sourceFileMapping = FileUtils.contentMapFromFile(sourceFilePath.path)
logger.debug(
s"Successfully loaded source map file '$mapFilePath':" +
s"\n\t* Transpiled file: '$absoluteFilePath'" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class TranspilationRunnerTest extends AnyWordSpec with Matchers {
result
}

private def codeFields(cpg: Cpg, label: String = NodeTypes.CALL): List[Integer] = {
val result =
TraversalSource(cpg.graph).label(label).property(PropertyNames.CODE).toList
result.size should not be 0
result
}

"TranspilationRunner" should {

"generate js files correctly for a simple Babel project" in {
Expand All @@ -57,6 +64,33 @@ class TranspilationRunnerTest extends AnyWordSpec with Matchers {
}
}

"contain correctly re-mapped code fields in simple Babel project" in {
val projectPath = getClass.getResource("/babel").toURI
File.usingTemporaryDirectory() { tmpDir: File =>
val tmpProjectPath = File(projectPath).copyToDirectory(tmpDir)

val cpgPath = (tmpDir / "cpg.bin.zip").path.toString
Js2CpgMain.main(Array(tmpProjectPath.pathAsString, "--output", cpgPath, "--no-ts"))

val cpg =
CpgLoader
.loadFromOverflowDb(
CpgLoaderConfig.withDefaults.withOverflowConfig(
Config.withDefaults.withStorageLocation(cpgPath)))
fileNames(cpg) should contain theSameElementsAs List("foo.js")
codeFields(cpg) should contain allElementsOf List(
"_tmp_1 = __ecma.Array.factory()",
"_tmp_1.push(1)",
"_tmp_1.push(2)",
"_tmp_1.push(3)",
"(_tmp_0 = [1, 2, 3 [...])",
"(_tmp_0 = [1, 2, 3 [...]).map",
"n + 1",
"[1, 2, 3 [...].map(anonymous)"
)
}
}

"generate and use sourcemap files correctly" in {
val projectPath = getClass.getResource("/typescript").toURI
File.usingTemporaryDirectory() { tmpDir: File =>
Expand Down

0 comments on commit 01b697b

Please sign in to comment.