diff --git a/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala b/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala index 66d21bce2..55b67e355 100644 --- a/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala +++ b/src/main/scala/io/shiftleft/js2cpg/parser/JsSource.scala @@ -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'" + diff --git a/src/test/scala/io/shiftleft/js2cpg/preprocessing/TranspilationRunnerTest.scala b/src/test/scala/io/shiftleft/js2cpg/preprocessing/TranspilationRunnerTest.scala index 97c2586cf..32074953a 100644 --- a/src/test/scala/io/shiftleft/js2cpg/preprocessing/TranspilationRunnerTest.scala +++ b/src/test/scala/io/shiftleft/js2cpg/preprocessing/TranspilationRunnerTest.scala @@ -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 { @@ -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 =>