Skip to content

Commit 6ab25ce

Browse files
authored
Use correct start indices in ModulePositionScanner (#3207)
* Test the root cause of #3206 * Reproduce #3206 as rewrite test * Use correct start indices in ModulePositionScanner `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`. * Reformat test
1 parent 7f380e0 commit 6ab25ce

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

modules/core/src/main/scala/org/scalasteward/core/edit/update/ModulePositionScanner.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,49 +36,49 @@ object ModulePositionScanner {
3636
fileData: FileData
3737
): Iterator[ModulePosition] =
3838
sbtModuleIdRegex(dependency).findAllIn(fileData.content).matchData.map { m =>
39-
val groupId = Substring.Position.fromMatch(fileData.path, m, dependency.groupId.value)
40-
val artifactId = Substring.Position.fromMatch(fileData.path, m, dependency.artifactId.name)
41-
val version = Substring.Position.fromMatch(fileData.path, m, m.group(1))
39+
val groupId = Substring.Position(fileData.path, m.start(1), dependency.groupId.value)
40+
val artifactId = Substring.Position(fileData.path, m.start(2), dependency.artifactId.name)
41+
val version = Substring.Position(fileData.path, m.start(3), m.group(3))
4242
ModulePosition(groupId, artifactId, version)
4343
}
4444

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

5151
private def findMillDependency(
5252
dependency: Dependency,
5353
fileData: FileData
5454
): Iterator[ModulePosition] =
5555
millDependencyRegex(dependency).findAllIn(fileData.content).matchData.map { m =>
56-
val groupId = Substring.Position.fromMatch(fileData.path, m, dependency.groupId.value)
57-
val artifactId = Substring.Position.fromMatch(fileData.path, m, dependency.artifactId.name)
58-
val version = Substring.Position.fromMatch(fileData.path, m, m.group(1))
56+
val groupId = Substring.Position(fileData.path, m.start(1), dependency.groupId.value)
57+
val artifactId = Substring.Position(fileData.path, m.start(2), dependency.artifactId.name)
58+
val version = Substring.Position(fileData.path, m.start(3), m.group(3))
5959
ModulePosition(groupId, artifactId, version)
6060
}
6161

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

6868
private def findMavenDependency(
6969
dependency: Dependency,
7070
fileData: FileData
7171
): Iterator[ModulePosition] =
7272
mavenDependencyRegex(dependency).findAllIn(fileData.content).matchData.map { m =>
73-
val groupId = Substring.Position.fromMatch(fileData.path, m, dependency.groupId.value)
74-
val artifactId = Substring.Position.fromMatch(fileData.path, m, dependency.artifactId.name)
75-
val version = Substring.Position.fromMatch(fileData.path, m, m.group(2))
73+
val groupId = Substring.Position(fileData.path, m.start(1), dependency.groupId.value)
74+
val artifactId = Substring.Position(fileData.path, m.start(2), dependency.artifactId.name)
75+
val version = Substring.Position(fileData.path, m.start(4), m.group(4))
7676
ModulePosition(groupId, artifactId, version)
7777
}
7878

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

modules/core/src/test/scala/org/scalasteward/core/edit/RewriteTest.scala

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -890,58 +890,63 @@ class RewriteTest extends FunSuite {
890890
runApplyUpdate(update, original, expected)
891891
}
892892

893-
test("issue-2877: sbt using same version in a val and a literal using a Seq addition") {
893+
// https://github.com/scala-steward-org/scala-steward/issues/2877
894+
test("sbt using same version in a val and a literal using a Seq addition") {
894895
val update = ("org.scalatest".g % Nel.of(
895896
"scalatest".a,
896897
"scalactic".a
897898
) % "3.2.13" %> "3.2.14").group
898899
val original = Map(
899900
"build.sbt" ->
900-
"""
901-
|val ScalaTestVersion = "3.2.13"
901+
"""val ScalaTestVersion = "3.2.13"
902902
|libraryDependencies ++= Seq(
903903
| "org.scalatest" %% "scalatest" % ScalaTestVersion,
904904
| "org.scalatest" %% "scalactic" % "3.2.13"
905-
|)
906-
|""".stripMargin
905+
|)""".stripMargin
907906
)
908907
val expected = Map(
909908
"build.sbt" ->
910-
"""
911-
|val ScalaTestVersion = "3.2.14"
909+
"""val ScalaTestVersion = "3.2.14"
912910
|libraryDependencies ++= Seq(
913911
| "org.scalatest" %% "scalatest" % ScalaTestVersion,
914912
| "org.scalatest" %% "scalactic" % "3.2.14"
915-
|)
916-
|""".stripMargin
913+
|)""".stripMargin
917914
)
918915
runApplyUpdate(update, original, expected)
919916
}
920917

921-
test("issue-2877: sbt using same version in a val and a literal using individual additions") {
918+
// https://github.com/scala-steward-org/scala-steward/issues/2877
919+
test("sbt using same version in a val and a literal using individual additions") {
922920
val update = ("org.scalatest".g % Nel.of(
923921
"scalatest".a,
924922
"scalactic".a
925923
) % "3.2.13" %> "3.2.14").group
926924
val original = Map(
927925
"build.sbt" ->
928-
"""
929-
|val ScalaTestVersion = "3.2.13"
926+
"""val ScalaTestVersion = "3.2.13"
930927
|libraryDependencies += "org.scalatest" %% "scalatest" % ScalaTestVersion
931928
|libraryDependencies += "org.scalatest" %% "scalactic" % "3.2.13"
932929
|""".stripMargin
933930
)
934931
val expected = Map(
935932
"build.sbt" ->
936-
"""
937-
|val ScalaTestVersion = "3.2.14"
933+
"""val ScalaTestVersion = "3.2.14"
938934
|libraryDependencies += "org.scalatest" %% "scalatest" % ScalaTestVersion
939935
|libraryDependencies += "org.scalatest" %% "scalactic" % "3.2.14"
940936
|""".stripMargin
941937
)
942938
runApplyUpdate(update, original, expected)
943939
}
944940

941+
// https://github.com/scala-steward-org/scala-steward/issues/3206
942+
test("sbt module where the artifactId is also part of the groupId") {
943+
val update = ("com.typesafe.play".g % "play".a % "2.9.0" %> "3.0.0").single
944+
.copy(newerGroupId = Some("org.playframework".g), newerArtifactId = Some("play"))
945+
val original = Map("build.sbt" -> """ "com.typesafe.play" %% "play" % "2.9.0" """)
946+
val expected = Map("build.sbt" -> """ "org.playframework" %% "play" % "3.0.0" """)
947+
runApplyUpdate(update, original, expected)
948+
}
949+
945950
private def runApplyUpdate(
946951
update: Update.Single,
947952
files: Map[String, String],

modules/core/src/test/scala/org/scalasteward/core/edit/update/ModulePositionScannerTest.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,18 @@ class ModulePositionScannerTest extends FunSuite {
3333
)
3434
assertEquals(obtained, expected)
3535
}
36+
37+
test("sbt module where the artifactId is also part of the groupId") {
38+
val d = "com.typesafe.play".g % "play".a % "2.9.0"
39+
val fd = FileData("build.sbt", s""""com.typesafe.play" %% "play" % "2.9.0"""")
40+
val obtained = ModulePositionScanner.findPositions(d, fd)
41+
val expected = List(
42+
ModulePosition(
43+
Substring.Position(fd.path, 1, d.groupId.value),
44+
Substring.Position(fd.path, 24, d.artifactId.name),
45+
Substring.Position(fd.path, 32, s"\"${d.version}\"")
46+
)
47+
)
48+
assertEquals(obtained, expected)
49+
}
3650
}

0 commit comments

Comments
 (0)