From a65570db9ac8c59d59b79a3f8a07134a90a3c6f2 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Sun, 10 Mar 2024 20:43:15 +0900 Subject: [PATCH] delete jquery. use preact --- build.sbt | 2 +- core/src/main/js/scalameta_ast/Main.scala | 44 +- .../scala/scalameta_ast/IntegrationTest.scala | 5 +- sources/index.html | 194 +---- sources/main.js | 705 +++++++++++------- 5 files changed, 466 insertions(+), 484 deletions(-) diff --git a/build.sbt b/build.sbt index ab3f732..e53c253 100644 --- a/build.sbt +++ b/build.sbt @@ -150,7 +150,7 @@ lazy val localServer = project.settings( commonSettings, run / fork := true, run / baseDirectory := (LocalRootProject / baseDirectory).value, - Test / testOptions += Tests.Argument("-oD"), + Test / testOptions += Tests.Argument("-oDF"), Test / test := (Test / test).dependsOn(LocalRootProject / copyFilesFull).value, Test / testOptions ++= { if (scala.util.Properties.isMac) { diff --git a/core/src/main/js/scalameta_ast/Main.scala b/core/src/main/js/scalameta_ast/Main.scala index 1c060c0..c71d395 100644 --- a/core/src/main/js/scalameta_ast/Main.scala +++ b/core/src/main/js/scalameta_ast/Main.scala @@ -26,24 +26,32 @@ object Main { removeNewFields: Boolean, initialExtractor: Boolean, ): js.Object = { - val output = - new ScalametaAST().convert( - src = source, - format = format, - scalafmtConfig = hoconToMetaConfig(scalafmtConfJsonStr), - outputType = outputType, - packageName = Option(packageName).filter(_.trim.nonEmpty), - wildcardImport = wildcardImport, - ruleNameOption = Option(ruleName).filter(_.trim.nonEmpty), - dialect = Option(dialect).filter(_.trim.nonEmpty), - patch = Option(patch).filter(_.trim.nonEmpty), - removeNewFields = removeNewFields, - initialExtractor = initialExtractor, - ) - new js.Object { - var ast = output.ast - var astBuildMs = output.astBuildMs.toDouble - var formatMs = output.formatMs.toDouble + try { + val output = + new ScalametaAST().convert( + src = source, + format = format, + scalafmtConfig = hoconToMetaConfig(scalafmtConfJsonStr), + outputType = outputType, + packageName = Option(packageName).filter(_.trim.nonEmpty), + wildcardImport = wildcardImport, + ruleNameOption = Option(ruleName).filter(_.trim.nonEmpty), + dialect = Option(dialect).filter(_.trim.nonEmpty), + patch = Option(patch).filter(_.trim.nonEmpty), + removeNewFields = removeNewFields, + initialExtractor = initialExtractor, + ) + new js.Object { + var ast = output.ast + var astBuildMs = output.astBuildMs.toDouble + var formatMs = output.formatMs.toDouble + } + } catch { + case e: Throwable => + new js.Object { + var error = e + var errorString: String = e.toString + } } } diff --git a/localServer/src/test/scala/scalameta_ast/IntegrationTest.scala b/localServer/src/test/scala/scalameta_ast/IntegrationTest.scala index 0e3c663..13e9e45 100644 --- a/localServer/src/test/scala/scalameta_ast/IntegrationTest.scala +++ b/localServer/src/test/scala/scalameta_ast/IntegrationTest.scala @@ -116,7 +116,7 @@ abstract class IntegrationTest(browserType: Playwright => BrowserType) extends A .getByRole(AriaRole.RADIO) .all() .asScala - .find(_.getAttribute("id") == outputType) + .find(_.getAttribute("value") == outputType) .getOrElse(sys.error(s"not found ${outputType}")) .check() } @@ -145,6 +145,7 @@ abstract class IntegrationTest(browserType: Playwright => BrowserType) extends A } "change input" in withBrowser { page => + changeOutputType(page, "raw") setInput(page, "class A") val expect = Seq( """Defn.Class.After_4_6_0(""", @@ -350,7 +351,7 @@ abstract class IntegrationTest(browserType: Playwright => BrowserType) extends A assert(wildcardImport(page).isChecked == wildcard) assert(initialExtractor(page).isChecked == _initialExtractor) assert( - page.getByRole(AriaRole.RADIO).all().asScala.filter(_.isChecked).map(_.getAttribute("id")) == Seq(outputType) + page.getByRole(AriaRole.RADIO).all().asScala.filter(_.isChecked).map(_.getAttribute("value")) == Seq(outputType) ) assert(selectedDialect(page) == dialect) assert(packageName(page).inputValue() == pkg) diff --git a/sources/index.html b/sources/index.html index 3a673cc..96a05d5 100644 --- a/sources/index.html +++ b/sources/index.html @@ -3,17 +3,10 @@ scalameta-ast - - - - -
-
- close header -
-
-

-          
-
-
-
- - -
-
- - -
-
- - -
-
- - -
-
-
-

-
-
-
- -
-
- -
-
-
-
-
-
- output type -
- - -
-
- - -
-
- - -
-
- - -
-
-
-
-
- - -
-
-
-
-
-
- - -
-
-
-
- - -
-
-
-
- - -
-
-
-
- - -
-
-
-
-
-
-
- -
-
-
-
-
-
-
-

- scalafmt config -

- -
-
- -
+
diff --git a/sources/main.js b/sources/main.js index 0b604c3..2b765f6 100644 --- a/sources/main.js +++ b/sources/main.js @@ -4,291 +4,456 @@ import { ScalametaAstMainScalafixCompat } from "./scalafix-compat/main.js"; import { ScalametaAstMainLatest } from "./latest/main.js"; -$(() => { - [ScalametaAstMainLatest, ScalametaAstMainScalafixCompat].forEach((main) => { - try { - // force initialize for avoid error - main.convert("", true, "", "", "", false, "", "", "", false, false); - } catch (e) { - console.log(e); - } - }); - - const headerAllConfig = document.getElementById("header_all_scalafix_config"); - headerAllConfig.addEventListener("toggle", (event) => { - const summary = headerAllConfig.getElementsByTagName("summary")[0]; - if (headerAllConfig.open) { - summary.innerHTML = "close header"; - } else { - summary.innerHTML = "open header"; - } - }); - - $("#format_input").click(() => { - const input = $("#input_scala").val(); - const scalafmt = $("#scalafmt").val(); - const main = - $("#scalameta").val() == "latest" - ? ScalametaAstMainLatest - : ScalametaAstMainScalafixCompat; - const result = main.format(input, scalafmt); - if (input != result) { - $("#input_scala").val(result); - } - }); - - $("#clear_local_storage").click(() => localStorage.clear()); - - const run = () => { - try { - const scalafmt = $("#scalafmt").val(); - const input = $("#input_scala").val(); - const outputType = $("input[name=output_type]:checked").val(); - const packageName = $("#package").val(); - const ruleName = $("#rule_name").val(); - const dialect = $("#dialect").val(); - const scalameta = $("#scalameta").val(); - const main = - scalameta == "latest" - ? ScalametaAstMainLatest - : ScalametaAstMainScalafixCompat; - const patch = $("#patch").val(); - - ["package", "rule_name", "wildcard_import", "patch"].forEach((i) => - $(`#${i}`).prop( - "disabled", - outputType === "raw" || outputType === "tokens", - ), - ); - - const r = main.convert( - input, - $("#format").prop("checked") === true, - scalafmt, - outputType === undefined ? "" : outputType, - packageName === undefined ? "" : packageName, - $("#wildcard_import").prop("checked") === true, - ruleName === undefined ? "" : ruleName, - dialect === undefined ? "" : dialect, - patch === undefined ? "" : patch, - $("#remove_new_fields").prop("checked") === true, - $("#initial_extractor").prop("checked") === true, - ); - $("#output_scala").text(r.ast); - $("#info") - .text(`ast: ${r.astBuildMs} ms\nfmt: ${r.formatMs} ms`) - .addClass("alert-success") - .removeClass("alert-danger"); - - const saveLimit = 1024; - - [ - ["patch", patch], - ["scalameta", scalameta], - ["dialect", dialect], - ["rule_name", ruleName], - ["package", packageName], - ["output_type", outputType], - ].forEach((xs) => { - try { - localStorage.setItem(xs[0], xs[1]); - } catch (e) { - console.trace(e); - } - }); - - if (input.length < saveLimit) { - try { - localStorage.setItem("source", input); - } catch (e) { - console.trace(e); - } - } - if (scalafmt.length < saveLimit) { - try { - localStorage.setItem("scalafmt", scalafmt); - } catch (e) { - console.trace(e); - } - } - - document - .getElementById("output_scala") - .removeAttribute("data-highlighted"); - - hljs.highlightAll(); - } catch (e) { - console.trace(e); - $("#output_scala").text(""); - $("#info").text(e).addClass("alert-danger").removeClass("alert-success"); - } - }; - - document.getElementById("output_scala").addEventListener("dblclick", (e) => { - const s = window.getSelection(); - const r = document.createRange(); - r.selectNodeContents(e.target); - s.removeAllRanges(); - s.addRange(r); - }); - - $("#input_scala").keyup((event) => run()); - - $("#package").keyup((event) => run()); - - $("#rule_name").keyup((event) => run()); - - $("input[name=output_type]").on("change", () => run()); - - $("#dialect").change(() => run()); - - $("#patch").change(() => run()); - - $("#scalameta").change(() => run()); - - $("#format").change(() => { - run(); - localStorage.setItem( - "format", - ($("#format").prop("checked") === true).toString(), - ); - }); - - $("#wildcard_import").change(() => { - run(); - localStorage.setItem( - "wildcard_import", - ($("#wildcard_import").prop("checked") === true).toString(), - ); - }); - - $("#remove_new_fields").change(() => { - run(); - localStorage.setItem( - "remove_new_fields", - ($("#remove_new_fields").prop("checked") === true).toString(), - ); - }); - - $("#initial_extractor").change(() => { - run(); - localStorage.setItem( - "initial_extractor", - ($("#initial_extractor").prop("checked") === true).toString(), - ); - }); - - $(document).ready(() => { - hljs.addPlugin(new CopyButtonPlugin()); - - const savedSource = localStorage.getItem("source"); - const savedScalafmt = localStorage.getItem("scalafmt"); - const savedPackage = localStorage.getItem("package"); - const savedRuleName = localStorage.getItem("rule_name"); - const savedDialect = localStorage.getItem("dialect"); - const savedScalameta = localStorage.getItem("scalameta"); - const savedPatch = localStorage.getItem("patch"); +[ScalametaAstMainLatest, ScalametaAstMainScalafixCompat].forEach((main) => { + try { + // force initialize for avoid error + main.convert("", true, "", "", "", false, "", "", "", false, false); + } catch (e) { + console.log(e); + } +}); - if (savedScalameta != null) { - $(`[name="scalameta"] option[value="${savedScalameta}"]`).prop( - "selected", - true, - ); +import { + html, + render, + useState, +} from "https://unpkg.com/htm@3.1.1/preact/standalone.module.js"; + +import hljs from "https://unpkg.com/@highlightjs/cdn-assets@11.9.0/es/highlight.min.js"; +import scala from "https://unpkg.com/@highlightjs/cdn-assets@11.9.0/es/languages/scala.min.js"; +hljs.registerLanguage("scala", scala); + +const getFromStorageOr = (key, defaultValue, fun) => { + const saved = localStorage.getItem(key); + if (saved === null) { + console.log("not found saved value. use default " + defaultValue); + return defaultValue; + } else { + console.log(`found saved value "${key}" = "${saved}"`); + if (fun == null) { + return saved; } else { - $(`[name="scalameta"] option[value="latest"]`).prop("selected", true); - } - - if (savedPatch != null) { - $(`[name="patch"] option[value="${savedPatch}"]`).prop("selected", true); + console.log("convert " + key + " " + fun(saved)); + return fun(saved); } + } +}; - if (savedDialect != null) { - $(`[name="dialect"] option[value="${savedDialect}"]`).prop( - "selected", - true, - ); - } - - if (savedPackage != null) { - $("#rule_name").val(savedRuleName); - } +const getBoolFromStorageOr = (key, defaultValue) => { + return getFromStorageOr(key, defaultValue, (a) => a === "true"); +}; - if (savedPackage != null) { - $("#package").val(savedPackage); - } else { - $("#package").val("fix"); - } - - if (savedScalafmt != null) { - $("#scalafmt").val(savedScalafmt); - } else { - const defaultConfig = ` +const defaultScalafmtConfig = ` maxColumn = 50 runner.dialect = "Scala3" align.preset = "none" continuationIndent.defnSite = 2 continuationIndent.extendSite = 2 ` - .split("\n") - .map((c) => c.trim()) - .filter((c) => c.length > 0) - .join("\n"); - $("#scalafmt").val(defaultConfig); - } - - if (savedSource != null) { - $("#input_scala").val(savedSource); - } else { - $("#input_scala").val("def a = b"); - } - - if (localStorage.getItem("format") === "false") { - $("#format").prop("checked", false); - } - - if (localStorage.getItem("wildcard_import") === "true") { - $("#wildcard_import").prop("checked", true); - } - - if (localStorage.getItem("remove_new_fields") === "false") { - $("#remove_new_fields").prop("checked", false); - } - - if (localStorage.getItem("initial_extractor") === "true") { - $("#initial_extractor").prop("checked", true); - } - - switch (localStorage.getItem("output_type")) { - case "semantic": - $("input[name=output_type][value='semantic']").prop("checked", true); - break; - case "syntactic": - $("input[name=output_type][value='syntactic']").prop("checked", true); + .split("\n") + .map((c) => c.trim()) + .filter((c) => c.length > 0) + .join("\n"); + +const initialSource = getFromStorageOr("source", "def a = b"); +const initialScalafmt = getFromStorageOr("scalafmt", defaultScalafmtConfig); +const initialPackage = getFromStorageOr("package", "fix"); +const initialRuleName = getFromStorageOr("rule_name", ""); +const initialDialect = getFromStorageOr("dialect", "Auto"); +const initialScalameta = getFromStorageOr("scalameta", "latest"); +const initialPatch = getFromStorageOr("patch", "warn"); +const initialOutputType = getFromStorageOr("output_type", "syntactic"); + +const initialFormat = getBoolFromStorageOr("format", true); +const initialWildcardImport = getBoolFromStorageOr("wildcard_import", false); +const initialRemoveNewFields = getBoolFromStorageOr("remove_new_fields", false); +const initialInitialExtractor = getBoolFromStorageOr( + "initial_extractor", + false, +); + +const App = () => { + const [summary, setSummary] = useState("close header"); + const [inputScala, setInputScala] = useState(initialSource); + const [scalafmtConfig, setScalafmtConfig] = useState(initialScalafmt); + const [packageName, setPackageName] = useState(initialPackage); + const [ruleName, setRuleName] = useState(initialRuleName); + const [dialect, setDialect] = useState(initialDialect); + const [scalameta, setScalameta] = useState(initialScalameta); + const [patch, setPatch] = useState(initialPatch); + const [outputType, setOutputType] = useState(initialOutputType); + + const [format, setFormat] = useState(initialFormat); + const [wildcardImport, setWildcardImport] = useState(initialWildcardImport); + const [removeNewFields, setRemoveNewFields] = useState( + initialRemoveNewFields, + ); + const [initialExtractor, setInitialExtractor] = useState( + initialInitialExtractor, + ); + + const changeDetails = (e) => { + switch (e.newState) { + case "open": + setSummary("close header"); break; - case "tokens": - $("input[name=output_type][value='tokens']").prop("checked", true); + case "closed": + setSummary("open header"); break; - default: - $("input[name=output_type][value='raw']").prop("checked", true); } + }; - $.getJSON("./scalafix-compat/build_info.json", (data) => { - document.getElementById("scalameta_scalafix_compat").innerHTML += - ` ${data.scalametaVersion}`; - }); + const main = + scalameta == "latest" + ? ScalametaAstMainLatest + : ScalametaAstMainScalafixCompat; - $.getJSON("./latest/build_info.json", (data) => { - document.getElementById("scalameta_latest").innerHTML += - ` ${data.scalametaVersion}`; + const formatInput = () => { + const result = main.format(inputScala, scalafmtConfig); + setInputScala(result); + }; - const githubUrl = `https://github.com/xuwei-k/scalameta-ast/tree/${data.gitHash}`; - const link = document.createElement("a"); - link.append(githubUrl); - link.href = githubUrl; - link.target = "_blank"; - document.getElementById("footer").appendChild(link); + console.log([ + inputScala, + format, + scalafmtConfig, + outputType, + packageName, + wildcardImport, + ruleName, + dialect, + patch, + removeNewFields, + initialExtractor, + ]); + + const r = main.convert( + inputScala, + format, + scalafmtConfig, + outputType, + packageName, + wildcardImport, + ruleName, + dialect, + patch, + removeNewFields, + initialExtractor, + ); + + let result = ""; + let info = ""; + let infoClass = ""; + + if (r.ast == null) { + info = r.errorString; + infoClass = "alert alert-danger"; + } else { + result = hljs.highlight(r.ast, { + language: "scala", + }).value; + info = `ast: ${r.astBuildMs} ms\nfmt: ${r.formatMs} ms`; + infoClass = "alert alert-success"; + console.log(r.ast); + + [ + ["source", inputScala], + ["scalafmt", scalafmtConfig], + ["package", packageName], + ["rule_name", ruleName], + ["dialect", dialect], + ["scalameta", scalameta], + ["patch", patch], + ["output_type", outputType], + + ["format", format], + ["wildcard_import", wildcardImport], + ["remove_new_fields", removeNewFields], + ["initial_extractor", initialExtractor], + ].forEach((xs) => { + localStorage.setItem(xs[0], xs[1]); }); - - run(); - }); -}); + } + + const disableScalafixRuleTemplateInput = + outputType === "raw" || outputType === "tokens"; + + return html`
+
+ ${summary} +
+
+
${info}
+
+
+
+
+ setFormat(e.target.checked)} + /> + +
+
+ setWildcardImport(e.target.checked)} + /> + +
+
+ setRemoveNewFields(e.target.checked)} + /> + +
+
+ setInitialExtractor(e.target.checked)} + /> + +
+
+
+

+
+
+
+ +
+
+ +
+
+ +
+
+
+
+
+
+ output type +
+ setOutputType("raw")} + /> + +
+
+ setOutputType("syntactic")} + /> + +
+
+ setOutputType("semantic")} + /> + +
+
+ setOutputType("tokens")} + /> + +
+
+
+
+
+ + +
+
+
+
+
+
+ + +
+
+
+
+ + setPackageName(x.target.value)} + /> +
+
+
+
+ + setRuleName(x.target.value)} + /> +
+
+
+
+ + +
+
+
+
+
+
+
+ +
+
+
+        
+        
+
+
+
+
+

+ scalafmt config +

+ +
+
+ +
`; +}; + +render(html`<${App} />`, document.getElementById("root"));