Skip to content

Commit

Permalink
[jssrc2cpg] Fixed handling of astgen parse failures (#3925)
Browse files Browse the repository at this point in the history
Fixes: #3923
max-leuthaeuser authored Dec 11, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent d0f8d4d commit 216c88a
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
jssrc2cpg {
astgen_version: "3.11.0"
astgen_version: "3.12.0"
}
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ class AstCreationPass(cpg: Cpg, astGenRunnerResult: AstGenRunnerResult, config:
val fileLOC = Try(IOUtils.readLinesInFile(filePath)) match {
case Success(filecontent) => filecontent.size
case Failure(exception) =>
logger.warn(s"Failed to read file: '${filePath}'", exception)
logger.warn(s"Failed to read file: '$filePath'", exception)
0
}
report.addReportInfo(fileName, fileLOC)
Original file line number Diff line number Diff line change
@@ -154,6 +154,11 @@ class AstGenRunner(config: Config) {

private def skippedFiles(in: File, astGenOut: List[String]): List[String] = {
val skipped = astGenOut.collect {
case out if out.startsWith("Parsing") =>
val filename = out.substring(out.indexOf(" ") + 1, out.indexOf(":") - 1)
val reason = out.substring(out.indexOf(":") + 2)
logger.warn(s"\t- failed to parse '${in / filename}': '$reason'")
Option(filename)
case out if !out.startsWith("Converted") && !out.startsWith("Retrieving") =>
val filename = out.substring(0, out.indexOf(" "))
val reason = out.substring(out.indexOf(" ") + 1)
Original file line number Diff line number Diff line change
@@ -47,9 +47,26 @@ class ProjectParseTest extends JsSrc2CpgSuite with BeforeAndAfterAll {
dir
}

private val projectWithStrangeFilenames: File = {
val dir = File.newTemporaryDirectory("jssrc2cpgTestsFilenames")
List("good_%component-name%_.js", "good_%component-name%_Foo.js").foreach { testFile =>
val file = dir / testFile
file.createIfNotExists(createParents = true)
file.write(s"""console.log("${file.canonicalPath}");""")
}
List("broken_%component-name%_.js", "broken_%component-name%_Foo.js").foreach { testFile =>
val file = dir / testFile
file.createIfNotExists(createParents = true)
file.write(s"""const x = new <%ComponentName%>Foo();""")
}
dir
}

override def afterAll(): Unit = {
projectWithSubfolders.delete(swallowIOExceptions = true)
projectWithBrokenFile.delete(swallowIOExceptions = true)
projectWithUtf8.delete(swallowIOExceptions = true)
projectWithStrangeFilenames.delete(swallowIOExceptions = true)
}

private object ProjectParseTestsFixture {
@@ -83,6 +100,10 @@ class ProjectParseTest extends JsSrc2CpgSuite with BeforeAndAfterAll {
cpg.fieldAccess.argument(2).code.l shouldBe List("error")
}

"handle strange filenames correctly" in ProjectParseTestsFixture(projectWithStrangeFilenames) { cpg =>
cpg.file.name.l shouldBe List("good_%component-name%_.js", "good_%component-name%_Foo.js")
}

}

}

0 comments on commit 216c88a

Please sign in to comment.