Skip to content

Commit

Permalink
Error: add a wrapper Exception WithCode
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 19, 2024
1 parent d86150e commit 26f9ccf
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
2 changes: 2 additions & 0 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/Error.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,6 @@ object Error {

case object MegaTestFailed extends Error("Mega test failed.")

case class WithCode(error: Throwable, code: String) extends Exception(error)

}
18 changes: 10 additions & 8 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/Scalafmt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,16 @@ object Scalafmt {
): Try[String] = {
val runner = style.runner
val codeToInput: String => Input = toInput(_, file)
val parsed = runner.parse(Rewrite(codeToInput(code), style, codeToInput))
parsed.fold(
_.details match {
case ed: ParseException =>
val dialect = runner.dialectName
val msg = s"[dialect $dialect] ${ed.shortMessage}"
Failure(new ParseException(ed.pos, msg))
case ed => Failure(ed)
val original = codeToInput(code)
val rewritten = Rewrite(original, style)
runner.parse(rewritten.fold(original)(codeToInput)).fold(
x => {
val err = x.details match {
case ParseException(pos, msg) =>
ParseException(pos, s"[dialect ${runner.dialectName}] $msg")
case ed => ed
}
Failure(Error.WithCode(err, rewritten.getOrElse(code)))
},
tree => {
implicit val formatOps = new FormatOps(tree, style, file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,9 @@ object Rewrite {

val default: Seq[Rewrite] = name2rewrite.values.toSeq

def apply(
input: Input,
style: ScalafmtConfig,
toInput: String => Input,
): Input = {
def apply(input: Input, style: ScalafmtConfig): Option[String] = {
val rewrites = style.rewrite.rewriteFactoryRules
if (rewrites.isEmpty) input
if (rewrites.isEmpty) None
else style.runner.parse(input) match {
case Parsed.Success(ast) =>
val ctx = RewriteCtx(style, input, ast)
Expand All @@ -155,8 +151,8 @@ object Rewrite {
}
}
traverser(ast)
toInput(ctx.applyPatches)
case _ => input
Some(ctx.applyPatches)
case _ => None
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
case Left(e) if err.nonEmpty && e.getMessage.contains(err) => t.expected
case Left(e: Incomplete) => e.formattedCode
case Left(e: SearchStateExploded) => logger.elem(e); e.partialOutput
case Left(e) => throw FormatException(e, t.original)
case Left(e: Error.WithCode) => throw e
case Left(e) => throw Error.WithCode(e, t.original)
case Right(code) => code
}
def assertVisits(
Expand Down Expand Up @@ -101,7 +102,13 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
"test does not parse: " + parseException2Message(e, obtained),
t.expected,
)
case Left(e) => throw FormatException(e, obtained)
case Left(Error.WithCode(e: ParseException, code)) if !onlyManual =>
assertEquals(
"test does not parse: " + parseException2Message(e, code),
t.expected,
)
case Left(e: Error.WithCode) => throw e
case Left(e) => throw Error.WithCode(e, obtained)
case Right(code) =>
if (onlyManual) {
assertEquals(code, obtained, "Idempotency violated")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.scalafmt.util

import org.scalafmt.Error

import scala.meta.parsers.ParseException

import munit.FunSuite
Expand All @@ -16,7 +18,7 @@ trait CanRunTests extends FunSuite with HasTests {
else test(paddedName) {
try run(t)
catch {
case FormatException(e: ParseException, code) =>
case Error.WithCode(e: ParseException, code) =>
fail("test does not parse\n" + parseException2Message(e, code), e)
}
}
Expand Down

This file was deleted.

0 comments on commit 26f9ccf

Please sign in to comment.