Skip to content

Commit

Permalink
Update sbt-scalafmt to 2.4.6 + formatting fixes (#122)
Browse files Browse the repository at this point in the history
* Update sbt-scalafmt to 2.4.6 + formatting fixes

* Format.
  • Loading branch information
max-leuthaeuser authored Jan 27, 2022
1 parent e9a0c9c commit 2eccd60
Show file tree
Hide file tree
Showing 54 changed files with 1,946 additions and 1,504 deletions.
5 changes: 4 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
version=3.3.0
runner.dialect = scala213
preset = IntelliJ
maxColumn = 100
align = true
align.preset = true
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
addSbtPlugin("com.github.sbt" % "sbt-native-packager" % "1.9.7")
addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "1.5.1")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.6")
addSbtPlugin("io.shiftleft" % "sbt-ci-release-early" % "2.0.18")
addSbtPlugin("com.dwijnand" % "sbt-dynver" % "4.1.1")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.1.0")
Expand Down
52 changes: 27 additions & 25 deletions src/main/scala/io/shiftleft/js2cpg/core/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,28 @@ object Config {

}

case class Config(srcDir: String = "",
tsTranspiling: Boolean = Config.DEFAULT_TS_TRANSPILING,
babelTranspiling: Boolean = Config.DEFAULT_BABEL_TRANSPILING,
vueTranspiling: Boolean = Config.DEFAULT_VUE_TRANSPILING,
nuxtTranspiling: Boolean = Config.DEFAULT_NUXT_TRANSPILING,
templateTranspiling: Boolean = Config.DEFAULT_TEMPLATE_TRANSPILING,
packageJsonLocation: String = PackageJsonParser.PACKAGE_JSON_FILENAME,
outputFile: String = Config.DEFAULT_CPG_OUT_FILE,
withTsTypes: Boolean = Config.DEFAULT_TS_TYPES,
ignoredFilesRegex: Regex = Config.DEFAULT_IGNORED_FILES_REGEX,
ignoredFiles: Seq[Path] = Config.DEFAULT_IGNORED_FILES,
ignoreMinified: Boolean = Config.DEFAULT_IGNORE_MINIFIED,
ignoreTests: Boolean = Config.DEFAULT_IGNORE_TESTS,
ignorePrivateDeps: Boolean = Config.DEFAULT_IGNORE_PRIVATE_DEPS,
privateDeps: Seq[String] = Config.DEFAULT_PRIVATE_DEPS,
includeConfigs: Boolean = Config.DEFAULT_INCLUDE_CONFIGS,
includeHtml: Boolean = Config.DEFAULT_INCLUDE_HTML,
jvmMetrics: Option[Int] = Config.DEFAULT_JVM_METRICS,
moduleMode: Option[String] = Config.DEFAULT_MODULE_MODE,
withNodeModuleFolder: Boolean = Config.DEFAULT_WITH_NODE_MODULES_FOLDER) {
case class Config(
srcDir: String = "",
tsTranspiling: Boolean = Config.DEFAULT_TS_TRANSPILING,
babelTranspiling: Boolean = Config.DEFAULT_BABEL_TRANSPILING,
vueTranspiling: Boolean = Config.DEFAULT_VUE_TRANSPILING,
nuxtTranspiling: Boolean = Config.DEFAULT_NUXT_TRANSPILING,
templateTranspiling: Boolean = Config.DEFAULT_TEMPLATE_TRANSPILING,
packageJsonLocation: String = PackageJsonParser.PACKAGE_JSON_FILENAME,
outputFile: String = Config.DEFAULT_CPG_OUT_FILE,
withTsTypes: Boolean = Config.DEFAULT_TS_TYPES,
ignoredFilesRegex: Regex = Config.DEFAULT_IGNORED_FILES_REGEX,
ignoredFiles: Seq[Path] = Config.DEFAULT_IGNORED_FILES,
ignoreMinified: Boolean = Config.DEFAULT_IGNORE_MINIFIED,
ignoreTests: Boolean = Config.DEFAULT_IGNORE_TESTS,
ignorePrivateDeps: Boolean = Config.DEFAULT_IGNORE_PRIVATE_DEPS,
privateDeps: Seq[String] = Config.DEFAULT_PRIVATE_DEPS,
includeConfigs: Boolean = Config.DEFAULT_INCLUDE_CONFIGS,
includeHtml: Boolean = Config.DEFAULT_INCLUDE_HTML,
jvmMetrics: Option[Int] = Config.DEFAULT_JVM_METRICS,
moduleMode: Option[String] = Config.DEFAULT_MODULE_MODE,
withNodeModuleFolder: Boolean = Config.DEFAULT_WITH_NODE_MODULES_FOLDER
) {

def createPathForPackageJson(): Path = Paths.get(packageJsonLocation) match {
case path if path.isAbsolute => path
Expand Down Expand Up @@ -94,15 +96,15 @@ case class Config(srcDir: String = "",
|\t- Typescript types: $withTsTypes
|\t- Ignored files regex: '$ignoredFilesRegex'
|\t- Ignored folders: ${ignoredFiles
.filter(f => new File(f.toString).isDirectory)
.map(f => s"${System.lineSeparator()}\t\t'${f.toString}'")
.mkString}
.filter(f => new File(f.toString).isDirectory)
.map(f => s"${System.lineSeparator()}\t\t'${f.toString}'")
.mkString}
|\t- Ignore minified files: $ignoreMinified
|\t- Ignore test files: $ignoreTests
|\t- Ignore private dependencies: $ignorePrivateDeps
|\t- Additional private dependencies: ${privateDeps
.map(f => s"${System.lineSeparator()}\t\t'$f'")
.mkString}
.map(f => s"${System.lineSeparator()}\t\t'$f'")
.mkString}
|\t- Include configuration files: $includeConfigs
|\t- Include HTML files: $includeHtml
|\t- Output file: '$outputFile'
Expand Down
46 changes: 26 additions & 20 deletions src/main/scala/io/shiftleft/js2cpg/core/Js2Cpg.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class Js2Cpg {
}
if (config.tsTranspiling) {
logger.warn(
"\t- Typescript compilation did not yield any *.js files. Does a valid 'tsconfig.json' exist in that folder?")
"\t- Typescript compilation did not yield any *.js files. Does a valid 'tsconfig.json' exist in that folder?"
)
}
if (config.vueTranspiling) {
logger.warn("\t- Vue.js transpilation did not yield any *.js files.")
Expand All @@ -45,7 +46,8 @@ class Js2Cpg {

private def handleVsixProject(project: File, tmpProjectDir: File): File = {
logger.debug(
s"Project is a VS code extension file (*$VSIX_SUFFIX). Unpacking it to '$tmpProjectDir'.")
s"Project is a VS code extension file (*$VSIX_SUFFIX). Unpacking it to '$tmpProjectDir'."
)
project.streamedUnzip(tmpProjectDir) / "extension"
}

Expand All @@ -58,11 +60,14 @@ class Js2Cpg {
Try(FileUtils.copyToDirectory(realProjectPath, tmpProjectDir, config)) match {
case Failure(_) =>
logger.debug(
s"Unable to copy project to temporary workspace '$tmpProjectDir'. Does it contain broken symlinks?")
s"Unable to copy project to temporary workspace '$tmpProjectDir'. Does it contain broken symlinks?"
)
logger.debug(
s"Retrying to copy '$realProjectPath' to temporary workspace '$tmpProjectDir' without symlinks.")
FileUtils.copyToDirectory(realProjectPath, tmpProjectDir, config)(
copyOptions = Seq(StandardCopyOption.REPLACE_EXISTING) ++ LinkOptions.noFollow)
s"Retrying to copy '$realProjectPath' to temporary workspace '$tmpProjectDir' without symlinks."
)
FileUtils.copyToDirectory(realProjectPath, tmpProjectDir, config)(copyOptions =
Seq(StandardCopyOption.REPLACE_EXISTING) ++ LinkOptions.noFollow
)
case Success(value) => value
}
}
Expand All @@ -84,26 +89,25 @@ class Js2Cpg {
List(head)
case _ =>
logger.info(s"Found the following sub-projects:${subProjects
.map(
p => projectDir.relativize(p)
)
.map(p => projectDir.relativize(p))
.mkString("\n\t- ", "\n\t- ", "")}")
projectDir.path +: subProjects.toList
}
}

private def isInCi: Boolean = sys.env.get("CI").contains("true")

private def collectJsFiles(jsFiles: List[(Path, Path)],
dir: Path,
config: Config): List[(Path, Path)] = {
private def collectJsFiles(
jsFiles: List[(Path, Path)],
dir: Path,
config: Config
): List[(Path, Path)] = {
val transpiledJsFiles = FileUtils
.getFileTree(dir, config, List(JS_SUFFIX, MJS_SUFFIX))
.map(f => (f, dir))
jsFiles.filterNot {
case (f, rootDir) =>
val filename = f.toString.replace(rootDir.toString, "")
transpiledJsFiles.exists(_._1.toString.endsWith(filename))
jsFiles.filterNot { case (f, rootDir) =>
val filename = f.toString.replace(rootDir.toString, "")
transpiledJsFiles.exists(_._1.toString.endsWith(filename))
} ++ transpiledJsFiles
}

Expand Down Expand Up @@ -210,10 +214,12 @@ class Js2Cpg {
.createAndApply()
new ConfigPass(configFiles(config, List(VUE_SUFFIX)), cpg, vueAsConfigPassPool, report)
.createAndApply()
new PrivateKeyFilePass(configFiles(config, List(KEY_SUFFIX)),
cpg,
privateKeyFilePassPool,
report)
new PrivateKeyFilePass(
configFiles(config, List(KEY_SUFFIX)),
cpg,
privateKeyFilePassPool,
report
)
.createAndApply()

if (config.includeHtml) {
Expand Down
38 changes: 26 additions & 12 deletions src/main/scala/io/shiftleft/js2cpg/core/Js2cpgArgumentsParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class Js2cpgArgumentsParser {
})
opt[String](PACKAGE_JSON)
.text(
s"path to the projects package.json (path relative to $SRCDIR or absolute path; defaults to '${SRCDIR + java.io.File.separator + PackageJsonParser.PACKAGE_JSON_FILENAME}')")
s"path to the projects package.json (path relative to $SRCDIR or absolute path; defaults to '${SRCDIR + java.io.File.separator + PackageJsonParser.PACKAGE_JSON_FILENAME}')"
)
.action((x, c) => c.copy(packageJsonLocation = x))
.validate(path => {
val f = new File(path)
Expand All @@ -88,7 +89,8 @@ class Js2cpgArgumentsParser {
failure("Output file cannot be empty")
} else if (!new File(x).getAbsoluteFile.getParentFile.exists()) {
failure("Directory of the output file does not exist")
} else success)
} else success
)
opt[Unit](NO_TS)
.text("disables transpiling Typescript files to Javascript")
.action((_, c) => c.copy(tsTranspiling = false))
Expand Down Expand Up @@ -118,12 +120,14 @@ class Js2cpgArgumentsParser {
.hidden()
opt[Unit](WITH_NODE_MODULES_FOLDER)
.text(
s"include the node_module folder (defaults to `${Config.DEFAULT_WITH_NODE_MODULES_FOLDER}`)")
s"include the node_module folder (defaults to `${Config.DEFAULT_WITH_NODE_MODULES_FOLDER}`)"
)
.action((_, c) => c.copy(withNodeModuleFolder = true))
.hidden()
opt[Unit](WITH_TS_TYPES)
.text(
s"query types via Typescript; needs a `package.json` (defaults to `${Config.DEFAULT_TS_TYPES}`)")
s"query types via Typescript; needs a `package.json` (defaults to `${Config.DEFAULT_TS_TYPES}`)"
)
.action((_, c) => c.copy(withTsTypes = true))
.hidden() // deprecated
opt[Seq[String]](EXCLUDE)
Expand All @@ -132,20 +136,26 @@ class Js2cpgArgumentsParser {
.text("files to exclude during CPG generation (paths relative to <srcdir> or absolute paths)")
opt[String](EXCLUDE_REGEX)
.action((x, c) => c.copy(ignoredFilesRegex = x.r))
.text("a regex specifying files to exclude during CPG generation (the absolute file path is matched)")
.text(
"a regex specifying files to exclude during CPG generation (the absolute file path is matched)"
)
// for backwards compatibility - has no effect:
opt[Unit](IGNORE_MINIFIED)
.text(
"ignore minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')")
"ignore minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')"
)
.hidden() // deprecated
opt[Unit](WITH_MINIFIED)
.action((_, c) => c.copy(ignoreMinified = false))
.hidden() // deprecated
.text("include minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')")
.text(
"include minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')"
)
opt[Unit](INCLUDE_MINIFIED)
.action((_, c) => c.copy(ignoreMinified = false))
.text(
"include minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')")
"include minified Javascript files (filename ending with '-min.js', '.min.js', or 'bundle.js')"
)
opt[Unit](WITH_TESTS)
.action((_, c) => c.copy(ignoreTests = false))
.hidden() // deprecated
Expand All @@ -155,18 +165,21 @@ class Js2cpgArgumentsParser {
.text("include test files")
opt[Unit](IGNORE_PRIVATE_DEPS)
.text(
s"ignores private modules/dependencies in 'node_modules/' (defaults to `${Config.DEFAULT_IGNORE_PRIVATE_DEPS}`)")
s"ignores private modules/dependencies in 'node_modules/' (defaults to `${Config.DEFAULT_IGNORE_PRIVATE_DEPS}`)"
)
.action((_, c) => c.copy(ignorePrivateDeps = true))
.hidden()
opt[Unit](EXCLUDE_PRIVATE_DEPS)
.text(
s"excludes private modules/dependencies in 'node_modules/' (defaults to `${Config.DEFAULT_IGNORE_PRIVATE_DEPS}`)")
s"excludes private modules/dependencies in 'node_modules/' (defaults to `${Config.DEFAULT_IGNORE_PRIVATE_DEPS}`)"
)
.action((_, c) => c.copy(ignorePrivateDeps = true))
opt[Seq[String]](PRIVATE_DEPS)
.valueName("<dep1>,<dep2>,...")
.action((x, c) => c.copy(privateDeps = c.privateDeps ++ x.flatMap(d => Seq(d, s"@$d"))))
.text(
s"additional private dependencies to be analyzed from '${FileDefaults.NODE_MODULES_DIR_NAME}'")
s"additional private dependencies to be analyzed from '${FileDefaults.NODE_MODULES_DIR_NAME}'"
)
opt[Unit](INCLUDE_CONFIGS)
.text("include configuration files (*.conf.js, *.config.js, *.json)")
.action((_, c) => c.copy(includeConfigs = true))
Expand All @@ -179,7 +192,8 @@ class Js2cpgArgumentsParser {
.hidden()
opt[String](MODULE_MODE)
.text(
s"set the module mode for transpiling (default is ${TypescriptTranspiler.DEFAULT_MODULE}, alternatives are e.g., esnext or es2015)")
s"set the module mode for transpiling (default is ${TypescriptTranspiler.DEFAULT_MODULE}, alternatives are e.g., esnext or es2015)"
)
.action((module, c) => c.copy(moduleMode = Some(module)))
.hidden()
}
Expand Down
32 changes: 19 additions & 13 deletions src/main/scala/io/shiftleft/js2cpg/core/Report.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ object Report {

private type Reports = TrieMap[FileName, ReportEntry]

private case class ReportEntry(loc: Long,
parsed: Boolean,
cpgGen: Boolean,
duration: Long,
isConfig: Boolean = false) {
private case class ReportEntry(
loc: Long,
parsed: Boolean,
cpgGen: Boolean,
duration: Long,
isConfig: Boolean = false
) {
def toSeq: Seq[String] = {
val lines = loc.toString
val dur = if (duration == 0) "-" else TimeUtils.pretty(Duration.fromNanos(duration))
Expand Down Expand Up @@ -48,7 +50,8 @@ class Report {
val rows = table.map(
_.zip(colWidths)
.map { case (item, size) => (" %-" + (size - 1) + "s").format(item) }
.mkString("|", "|", "|"))
.mkString("|", "|", "|")
)
// Formatted separator row, used to separate the header and draw table borders
val separator = colWidths.map("-" * _).mkString("+", "+", "+")
// Put the table together and return
Expand Down Expand Up @@ -80,17 +83,20 @@ class Report {
s"${reports.count(_._2.parsed)}/$numOfReports",
s"${reports.count(_._2.cpgGen)}/$numOfReports",
s"${TimeUtils.pretty(Duration.fromNanos(reports.map(_._2.duration).sum))}"
))
)
)
val table = header ++ rows ++ footer
logger.info(s"Report:${System.lineSeparator()}" + formatTable(table))
}

def addReportInfo(fileName: FileName,
loc: Long,
parsed: Boolean = false,
cpgGen: Boolean = false,
duration: Long = 0,
isConfig: Boolean = false): Unit =
def addReportInfo(
fileName: FileName,
loc: Long,
parsed: Boolean = false,
cpgGen: Boolean = false,
duration: Long = 0,
isConfig: Boolean = false
): Unit =
reports(fileName) = ReportEntry(loc, parsed, cpgGen, duration, isConfig)

def updateReportDuration(fileName: FileName, duration: Long): Unit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package io.shiftleft.js2cpg.cpg.datastructures.scope

import io.shiftleft.codepropertygraph.generated.nodes.NewNode

case class PendingReference(variableName: String,
referenceNodeId: NewNode,
stack: Option[ScopeElement]) {
case class PendingReference(
variableName: String,
referenceNodeId: NewNode,
stack: Option[ScopeElement]
) {

def tryResolve(): Option[ResolvedReference] = {
var foundVariableOption = Option.empty[NewNode]
Expand Down
Loading

0 comments on commit 2eccd60

Please sign in to comment.