Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rules capturing leading tokens can be suppressed with scalafix:ok #1890

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ object EscapeHatch {
associatedComments: LazyValue[AssociatedComments]
): AnchoredEscapes = {
val enable = TreeMap.newBuilder[EscapeOffset, List[EscapeFilter]]
val disable = TreeMap.newBuilder[EscapeOffset, List[EscapeFilter]]
val disableList =
ListBuffer.newBuilder[(EscapeOffset, List[EscapeFilter])]
val visitedFilterExpression = mutable.Set.empty[Position]
val onOffTracker = new OnOffTracker

Expand Down Expand Up @@ -422,9 +423,11 @@ object EscapeHatch {
case comment @ Token.Comment(ScalafixOkRgx(rules)) =>
if (!visitedFilterExpression.contains(comment.pos)) {
val rulesPos = rulesWithPosition(rules, comment)
val start = EscapeOffset(t.pos.start)
// include line start in case the rule captures leading tokens
val start = EscapeOffset(t.pos.start - t.pos.startColumn)
Comment on lines +426 to +427
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the actual fix - the rest of the changes is to satisfy scalafix-tests/input/src/main/scala-2/test/escapeHatch/Issue694.scala

val end = Some(EscapeOffset(t.pos.end))
disable += (start -> makeFilters(rulesPos, start, end, comment))
disableList +=
(start -> makeFilters(rulesPos, start, end, comment))
visitedFilterExpression += comment.pos
}

Expand All @@ -444,7 +447,8 @@ object EscapeHatch {
case ScalafixOffRgx(rules) =>
val rulesPos = rulesWithPosition(rules, comment)
val start = EscapeOffset(comment.pos.start)
disable += (start -> makeFilters(rulesPos, start, None, comment))
disableList +=
(start -> makeFilters(rulesPos, start, None, comment))
onOffTracker.trackOff(rulesPos, comment)

// matches on anchors
Expand All @@ -470,7 +474,8 @@ object EscapeHatch {
val start =
EscapeOffset(comment.pos.start - comment.pos.startColumn)
val end = Some(EscapeOffset(comment.pos.end))
disable += (start -> makeFilters(rulesPos, start, end, comment))
disableList +=
(start -> makeFilters(rulesPos, start, end, comment))
}

case _ => ()
Expand All @@ -479,10 +484,29 @@ object EscapeHatch {
case _ => ()
}

val disable = TreeMap.newBuilder[EscapeOffset, List[EscapeFilter]]
val unused = ListBuffer.from(onOffTracker.allUnused)

// for filters starting on the same offset, pick the one with the wider range
// and mark the others as unused
disableList.result().groupBy(_._1).valuesIterator.foreach {
filtersForOffset =>
val sorted = filtersForOffset.sortBy { case (_, filters) =>
filters.headOption.flatMap(_.endOffset.map(-_.offset))
}

sorted.result() match {
case head :: tail =>
disable += head
unused ++= tail.flatMap(_._2).map(_.cause)
case _ =>
}
}

new AnchoredEscapes(
enable.result(),
disable.result(),
onOffTracker.allUnused
unused.result()
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ DisableSyntax.regex = [
message = "Numbers ({$1} in this instance) should always have a named parameter attached, or be assigned to a val."
}
"Await\\.result"
".*bar\\s"
]
*/
package test.disableSyntax
Expand Down Expand Up @@ -61,6 +62,8 @@ Numbers (5 in this instance) should always have a named parameter attached, or b
// actually 7.5 million years
Await.result(Future(fortyTwo), someDays) // assert: DisableSyntax.Await\.result

val bar = 1 // scalafix:ok

override def finalize(): Unit = println("exit") // assert: DisableSyntax.noFinalize

val Right(notFound) = 1.asInstanceOf[Either[String, String]]
Expand Down