Skip to content

Commit

Permalink
Use correct start indices in ModulePositionScanner
Browse files Browse the repository at this point in the history
`ModulePositionScanner` currently reports wrong substring positions if
an an artifactId is a proper substring of a groupId. With this change,
regex capturing groups are used to get the correct start indices of
substring positions of `ModulePositions`.
  • Loading branch information
fthomas committed Nov 11, 2023
1 parent 153df5d commit b848984
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,49 @@ object ModulePositionScanner {
fileData: FileData
): Iterator[ModulePosition] =
sbtModuleIdRegex(dependency).findAllIn(fileData.content).matchData.map { m =>
val groupId = Substring.Position.fromMatch(fileData.path, m, dependency.groupId.value)
val artifactId = Substring.Position.fromMatch(fileData.path, m, dependency.artifactId.name)
val version = Substring.Position.fromMatch(fileData.path, m, m.group(1))
val groupId = Substring.Position(fileData.path, m.start(1), dependency.groupId.value)
val artifactId = Substring.Position(fileData.path, m.start(2), dependency.artifactId.name)
val version = Substring.Position(fileData.path, m.start(3), m.group(3))
ModulePosition(groupId, artifactId, version)
}

private def sbtModuleIdRegex(dependency: Dependency): Regex = {
val g = Regex.quote(dependency.groupId.value)
val a = Regex.quote(dependency.artifactId.name)
raw""""$g"\s*%+\s*"$a"\s*%+\s*(.*)""".r
raw""""($g)"\s*%+\s*"($a)"\s*%+\s*(.*)""".r
}

private def findMillDependency(
dependency: Dependency,
fileData: FileData
): Iterator[ModulePosition] =
millDependencyRegex(dependency).findAllIn(fileData.content).matchData.map { m =>
val groupId = Substring.Position.fromMatch(fileData.path, m, dependency.groupId.value)
val artifactId = Substring.Position.fromMatch(fileData.path, m, dependency.artifactId.name)
val version = Substring.Position.fromMatch(fileData.path, m, m.group(1))
val groupId = Substring.Position(fileData.path, m.start(1), dependency.groupId.value)
val artifactId = Substring.Position(fileData.path, m.start(2), dependency.artifactId.name)
val version = Substring.Position(fileData.path, m.start(3), m.group(3))
ModulePosition(groupId, artifactId, version)
}

private def millDependencyRegex(dependency: Dependency): Regex = {
val g = Regex.quote(dependency.groupId.value)
val a = Regex.quote(dependency.artifactId.name)
raw"""["`]$g:+$a:+(.*)["`;]""".r
raw"""["`]($g):+($a):+(.*)["`;]""".r
}

private def findMavenDependency(
dependency: Dependency,
fileData: FileData
): Iterator[ModulePosition] =
mavenDependencyRegex(dependency).findAllIn(fileData.content).matchData.map { m =>
val groupId = Substring.Position.fromMatch(fileData.path, m, dependency.groupId.value)
val artifactId = Substring.Position.fromMatch(fileData.path, m, dependency.artifactId.name)
val version = Substring.Position.fromMatch(fileData.path, m, m.group(2))
val groupId = Substring.Position(fileData.path, m.start(1), dependency.groupId.value)
val artifactId = Substring.Position(fileData.path, m.start(2), dependency.artifactId.name)
val version = Substring.Position(fileData.path, m.start(4), m.group(4))
ModulePosition(groupId, artifactId, version)
}

private def mavenDependencyRegex(dependency: Dependency): Regex = {
val g = Regex.quote(dependency.groupId.value)
val a = Regex.quote(dependency.artifactId.name)
raw"""<groupId>$g</groupId>\s*<artifactId>$a(|_[^<]+)</artifactId>\s*<version>(.*)</version>""".r
raw"""<groupId>($g)</groupId>\s*<artifactId>($a)(|_[^<]+)</artifactId>\s*<version>(.*)</version>""".r
}
}

0 comments on commit b848984

Please sign in to comment.