Skip to content

Commit

Permalink
Merge pull request #130 from xuwei-k/scalafmt-config-err
Browse files Browse the repository at this point in the history
fix scalafmt config error handling
  • Loading branch information
xuwei-k committed Mar 20, 2024
2 parents dcc2f3f + 0f41f26 commit f94a1f2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 19 deletions.
31 changes: 19 additions & 12 deletions core/src/main/scala-latest-js/scalameta_ast/MainCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,32 @@ trait MainCompat {
)

private def runFormat(source: String, conf: ScalafmtConfig): String = {
try {
org.scalafmt.Scalafmt.format(source, conf).get
} catch {
case NonFatal(e) =>
e.printStackTrace()
source
}
org.scalafmt.Scalafmt.format(source, conf).get
}

private def metaConfigToScalafmtConfig(conf: Conf): ScalafmtConfig = {
ScalafmtConfig.decoder.read(None, conf).get
}

@JSExport
def format(source: String, scalafmtConfJsonStr: String): String =
runFormat(
source = source,
scalafmtConfig = hoconToMetaConfig(scalafmtConfJsonStr)
)
def format(source: String, scalafmtConfJsonStr: String): js.Object =
try {
val res = runFormat(
source = source,
scalafmtConfig = hoconToMetaConfig(scalafmtConfJsonStr)
)
new js.Object {
val result = res
val error = null
}
} catch {
case NonFatal(e) =>
e.printStackTrace()
new js.Object {
val result = source
val error = e.toString()
}
}

private[this] def hoconToMetaConfig(config: String): Conf =
convertSConfigToMetaConfig(ConfigFactory.parseString(config))
Expand Down
29 changes: 29 additions & 0 deletions localServer/src/test/scala/scalameta_ast/IntegrationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,35 @@ abstract class IntegrationTest(
)
)
}

"invalid config" in withBrowser { page =>
def render(): Unit = inputElem(page).press("\n")
setInput(page, "null")
setScalafmtConfig(
page,
Seq(
"""invalid = """,
)
)
render()
assert(output(page).textContent() == "")
assert(infoElem(page).getAttribute("class") == "alert alert-danger")
assert(
infoElem(page)
.textContent() == "org.ekrich.config.ConfigException$Parse: String: 1: Expecting a value but got wrong token: end of file"
)
setScalafmtConfig(
page,
Nil
)
render()
assert(output(page).textContent() == "Lit.Null()\n")
assert(infoElem(page).getAttribute("class") == "alert alert-success")
val x = infoElem(page).textContent()
assert(x.contains("ast:"))
assert(x.contains(" ms"))
assert(!x.contains("Exception"))
}
}

"Initial extractor" in withBrowser { page =>
Expand Down
23 changes: 16 additions & 7 deletions sources/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ const App = () => {
: ScalametaAstMainScalafixCompat;

const formatInput = () => {
const result = ScalametaAstMainLatest.format(inputScala, scalafmtConfig);
setInputScala(result);
const res = ScalametaAstMainLatest.format(inputScala, scalafmtConfig);
if (res.error === null) {
setInputScala(res.result);
}
};

let r = main.convert(
Expand All @@ -147,11 +149,18 @@ const App = () => {

if (r.ast == null || format === false) {
} else {
const formatted = ScalametaAstMainLatest.format(r.ast, scalafmtConfig);
r = {
ast: formatted,
astBuildMs: r.astBuildMs,
};
const res = ScalametaAstMainLatest.format(r.ast, scalafmtConfig);
if (res.error === null) {
r = {
ast: res.result,
astBuildMs: r.astBuildMs,
};
} else {
r = {
ast: null,
errorString: res.error,
};
}
}

let result = "";
Expand Down

0 comments on commit f94a1f2

Please sign in to comment.