-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[jssrc2cpg] Enable file content and content for method and type decl (#…
- Loading branch information
1 parent
03e0ba6
commit 339a0c7
Showing
5 changed files
with
112 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
.../frontends/jssrc2cpg/src/test/scala/io/joern/jssrc2cpg/io/CodeDumperFromContentTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.joern.jssrc2cpg.io | ||
|
||
import io.joern.jssrc2cpg.testfixtures.JsSrc2CpgSuite | ||
import io.joern.jssrc2cpg.Config | ||
import io.shiftleft.semanticcpg.language.* | ||
|
||
class CodeDumperFromContentTest extends JsSrc2CpgSuite { | ||
|
||
private implicit val finder: NodeExtensionFinder = DefaultNodeExtensionFinder | ||
|
||
"dumping code from content" should { | ||
val cpg = code( | ||
""" | ||
|// A comment | ||
|function my_func(param1) | ||
|{ | ||
| var x = foo(param1); | ||
|}""".stripMargin, | ||
"index.js" | ||
).withConfig(Config().withDisableFileContent(false)) | ||
|
||
"allow one to dump a method node's source code from `File.contents`" in { | ||
inside(cpg.method.nameExact("my_func").dumpRaw.l) { | ||
case content :: Nil => | ||
content.linesIterator.map(_.strip).l shouldBe List( | ||
"function my_func(param1) /* <=== index.js::program:my_func */", | ||
"{", | ||
"var x = foo(param1);", | ||
"}" | ||
) | ||
case content => fail(s"Expected exactly 1 content dump, but got: $content") | ||
} | ||
} | ||
} | ||
|
||
"code from method content" should { | ||
val myFuncContent = | ||
"""function my_func(param1) | ||
|{ | ||
| var x = foo(param1); | ||
|}""".stripMargin | ||
|
||
val cpg = code( | ||
s""" | ||
|// A comment | ||
|$myFuncContent | ||
|""".stripMargin, | ||
"index.js" | ||
).withConfig(Config().withDisableFileContent(false)) | ||
|
||
"allow one to dump a method node's source code from `Method.content`" in { | ||
val List(content) = cpg.method.nameExact("my_func").content.l | ||
content shouldBe myFuncContent | ||
} | ||
} | ||
|
||
"code from typedecl content" should { | ||
val myClassContent = | ||
"""class Foo | ||
|{ | ||
| x = 'foo'; | ||
|}""".stripMargin | ||
|
||
val cpg = code( | ||
s""" | ||
|// A comment | ||
|$myClassContent | ||
|""".stripMargin, | ||
"index.js" | ||
).withConfig(Config().withDisableFileContent(false)) | ||
|
||
"allow one to dump a method node's source code from `TypeDecl.content`" in { | ||
val List(content) = cpg.typeDecl.nameExact("Foo").content.l | ||
content shouldBe myClassContent | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters