From 2519a1bb8989cab4097a7e21149f01aa01e79536 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Wed, 15 Jan 2025 10:26:57 +0900 Subject: [PATCH] scalafix 0.14 --- build.sbt | 6 +- .../scala/scalameta_ast/AfterExtractor.scala | 1 + .../scalameta_ast/ScalametaASTSpec2.scala | 225 +++++++++++++++++- .../scalameta_ast/ScalametaASTSpec2.scala | 49 ++++ .../scalameta_ast/ScalametaASTSpec.scala | 101 -------- sources/main.js | 2 +- 6 files changed, 273 insertions(+), 111 deletions(-) diff --git a/build.sbt b/build.sbt index d12ad89..a9a0ded 100644 --- a/build.sbt +++ b/build.sbt @@ -46,8 +46,8 @@ lazy val `scalameta-ast` = projectMatrix metaVersion := (LocalProject("scalameta-ast-latestJS") / metaVersion).value, testBuildInfo, commonLatest, - libraryDependencies += "org.scalameta" %%% "scalameta" % "4.10.2", - libraryDependencies += "org.scalameta" %%% "scalafmt-core" % "3.8.3", + libraryDependencies += "org.scalameta" %%% "scalameta" % "4.12.4.1", + libraryDependencies += "org.scalameta" %%% "scalafmt-core" % "3.8.4", libraryDependencies += "com.google.inject" % "guice" % "7.0.0" % Test, Test / resourceGenerators += Def.task { val v1 = (LocalProject("scalameta-ast-latestJS") / metaTreesSource).value @@ -77,7 +77,7 @@ lazy val `scalameta-ast` = projectMatrix jsProjectSettings, commonLatest, libraryDependencies += "org.ekrich" %%% "sconfig" % "1.8.1", - libraryDependencies += ("com.github.xuwei-k" %%% "scalafmt-core" % "3.8.3-fork-2").withSources(), + libraryDependencies += ("com.github.xuwei-k" %%% "scalafmt-core" % "3.8.4-fork-1").withSources(), ) ) diff --git a/core/src/main/scala/scalameta_ast/AfterExtractor.scala b/core/src/main/scala/scalameta_ast/AfterExtractor.scala index 1db0334..85b4bb1 100644 --- a/core/src/main/scala/scalameta_ast/AfterExtractor.scala +++ b/core/src/main/scala/scalameta_ast/AfterExtractor.scala @@ -51,6 +51,7 @@ object AfterExtractor extends AfterExtractorEnable { E2("Term", "PolyFunction", "After_4_6_0"), E2("Term", "Try", "After_4_9_9"), E2("Type", "Apply", "After_4_6_0"), + E2("Type", "Bounds", "After_4_12_3"), E2("Type", "ContextFunction", "After_4_6_0"), E2("Type", "Existential", "After_4_9_9"), E2("Type", "Function", "After_4_6_0"), diff --git a/core/src/test/scala-latest-js/scalameta_ast/ScalametaASTSpec2.scala b/core/src/test/scala-latest-js/scalameta_ast/ScalametaASTSpec2.scala index 3f46d3a..bbaaadc 100644 --- a/core/src/test/scala-latest-js/scalameta_ast/ScalametaASTSpec2.scala +++ b/core/src/test/scala-latest-js/scalameta_ast/ScalametaASTSpec2.scala @@ -11,9 +11,167 @@ class ScalametaASTSpec2 extends AnyFreeSpec { "ScalametaAST" - { val main = new ScalametaAST + "convert" in { + val result = main.convert( + src = "val x = ((), null)", + outputType = "syntactic", + packageName = Option("package_name"), + wildcardImport = false, + ruleNameOption = None, + dialect = None, + patch = None, + removeNewFields = true, + initialExtractor = false, + explanation = true, + pathFilter = false, + ) + val expect = + s"""package package_name + | + |import scala.meta.Defn + |import scala.meta.Lit + |import scala.meta.Pat + |import scala.meta.Term + |import scala.meta.transversers._ + |import scalafix.Patch + |import scalafix.lint.Diagnostic + |import scalafix.lint.LintSeverity + |import scalafix.v1.SyntacticDocument + |import scalafix.v1.SyntacticRule + |import scalafix.v1.XtensionSeqPatch + | + |class Example extends SyntacticRule("Example") { + | override def fix(implicit doc: SyntacticDocument): Patch = { + | doc.tree.collect { + | case t @ Defn.Val( + | Nil, + | List( + | Pat.Var(Term.Name("x")) + | ), + | None, + | Term.Tuple(List( + | Lit.Unit(), + | Lit.Null() + | )) + |) => + | Patch.lint( + | Diagnostic( + | id = "", + | message = "", + | position = t.pos, + | explanation = "", + | severity = LintSeverity.Warning + | ) + | ) + | }.asPatch + | } + |} + |""".stripMargin + assert(result.result == expect) + } + + "import" in { + val result = main.convert( + src = ( + "Case", + "Ctor", + "Decl", + "Defn", + "Defn", + "Export", + "Import", + "Importee", + "Init", + "Member", + "Mod", + "Pkg", + "Ref", + "Self", + "Stat", + "Template", + "Type" + ).toString, + outputType = "syntactic", + packageName = Option("package_name"), + wildcardImport = false, + ruleNameOption = None, + dialect = None, + patch = Some("empty"), + removeNewFields = true, + initialExtractor = false, + explanation = true, + pathFilter = false, + ) + val expect = + s"""package package_name + | + |import scala.meta.Term + |import scala.meta.transversers._ + |import scalafix.Patch + |import scalafix.v1.SyntacticDocument + |import scalafix.v1.SyntacticRule + |import scalafix.v1.XtensionSeqPatch + | + |class Example extends SyntacticRule("Example") { + | override def fix(implicit doc: SyntacticDocument): Patch = { + | doc.tree.collect { + | case t @ Term.Tuple(List( + | Term.Name("Case"), + | Term.Name("Ctor"), + | Term.Name("Decl"), + | Term.Name("Defn"), + | Term.Name("Defn"), + | Term.Name("Export"), + | Term.Name("Import"), + | Term.Name("Importee"), + | Term.Name("Init"), + | Term.Name("Member"), + | Term.Name("Mod"), + | Term.Name("Pkg"), + | Term.Name("Ref"), + | Term.Name("Self"), + | Term.Name("Stat"), + | Term.Name("Template"), + | Term.Name("Type") + |)) => + | Patch.empty + | }.asPatch + | } + |} + |""".stripMargin + assert(result.result == expect) + } + "Term.ParamClause and ArgClause" in { val expect = - """Defn.Def.After_4_7_3(Nil, Term.Name("a"), List(Member.ParamClauseGroup(Type.ParamClause(Nil), List(Term.ParamClause(List(Term.Param(Nil, Term.Name("b"), Some(Type.Name("C")), None)), None)))), None, Term.Apply.After_4_6_0(Term.Name("d"), Term.ArgClause(List(Term.Name("e")), None)))""" + """Defn.Def.After_4_7_3( + | Nil, + | Term.Name("a"), + | List( + | Member.ParamClauseGroup( + | Type.ParamClause(Nil), + | List( + | Term.ParamClause(List( + | Term.Param( + | Nil, + | Term.Name("b"), + | Some( + | Type.Name("C") + | ), + | None + | ) + | ), None) + | ) + | ) + | ), + | None, + | Term.Apply.After_4_6_0( + | Term.Name("d"), + | Term.ArgClause(List( + | Term.Name("e") + | ), None) + | ) + |)""".stripMargin val result = main.convert( src = "def a(b: C) = d(e)", outputType = "", @@ -32,7 +190,20 @@ class ScalametaASTSpec2 extends AnyFreeSpec { "Term.If" in { val expect = - """Term.If.After_4_4_0(Term.Name("a"), Term.Name("b"), Term.If.After_4_4_0(Term.Name("x"), Term.Name("y"), Term.Select(Term.Name("z"), Term.Name("f")), Nil), Nil)""" + """Term.If.After_4_4_0( + | Term.Name("a"), + | Term.Name("b"), + | Term.If.After_4_4_0( + | Term.Name("x"), + | Term.Name("y"), + | Term.Select( + | Term.Name("z"), + | Term.Name("f") + | ), + | Nil + | ), + | Nil + |)""".stripMargin val result = main.convert( src = """if a then b else (if(x) y else z.f)""", outputType = "", @@ -51,7 +222,19 @@ class ScalametaASTSpec2 extends AnyFreeSpec { "Term.Match" in { val expect = - """Term.Match.After_4_9_9(Term.Name("a"), Term.CasesBlock(List(Case(Pat.Var(Term.Name("b")), None, Term.Block(Nil)))), List(Mod.Inline()))""" + """Term.Match.After_4_9_9( + | Term.Name("a"), + | Term.CasesBlock(List( + | Case( + | Pat.Var(Term.Name("b")), + | None, + | Term.Block(Nil) + | ) + | )), + | List( + | Mod.Inline() + | ) + |)""".stripMargin val result = main.convert( src = """inline a match { case b => }""", outputType = "", @@ -84,7 +267,18 @@ class ScalametaASTSpec2 extends AnyFreeSpec { ) val expect = - """Defn.Type.After_4_6_0(Nil, Type.Name("A"), Type.ParamClause(Nil), Type.Name("B"), Type.Bounds(None, None))""" + """Defn.Type.After_4_6_0( + | Nil, + | Type.Name("A"), + | Type.ParamClause(Nil), + | Type.Name("B"), + | Type.Bounds.After_4_12_3( + | None, + | None, + | Nil, + | Nil + | ) + |)""".stripMargin assert(result.result == expect) } @@ -104,8 +298,27 @@ class ScalametaASTSpec2 extends AnyFreeSpec { ) val expect = - """Defn.Class.After_4_6_0(Nil, Type.Name("A"), Type.ParamClause(Nil), Ctor.Primary.After_4_6_0(Nil, Name.Anonymous(), Nil), Template.After_4_9_9(None, Nil, Template.Body(None, Nil), List(Type.Name("B"))))""" - + """Defn.Class.After_4_6_0( + | Nil, + | Type.Name("A"), + | Type.ParamClause(Nil), + | Ctor.Primary.After_4_6_0( + | Nil, + | Name.Anonymous(), + | Nil + | ), + | Template.After_4_9_9( + | None, + | Nil, + | Template.Body( + | None, + | Nil + | ), + | List( + | Type.Name("B") + | ) + | ) + |)""".stripMargin assert(result.result == expect) } diff --git a/core/src/test/scala-scalafix_compat-js/scalameta_ast/ScalametaASTSpec2.scala b/core/src/test/scala-scalafix_compat-js/scalameta_ast/ScalametaASTSpec2.scala index 4798571..f17ffe2 100644 --- a/core/src/test/scala-scalafix_compat-js/scalameta_ast/ScalametaASTSpec2.scala +++ b/core/src/test/scala-scalafix_compat-js/scalameta_ast/ScalametaASTSpec2.scala @@ -13,6 +13,55 @@ class ScalametaASTSpec2 extends AnyFreeSpec { "ScalametaAST" - { val main = new ScalametaAST + "convert" in { + val result = main.convert( + src = "val x = ((), null)", + outputType = "syntactic", + packageName = Option("package_name"), + wildcardImport = false, + ruleNameOption = None, + dialect = None, + patch = None, + removeNewFields = true, + initialExtractor = false, + explanation = true, + pathFilter = false, + ) + val expect = + s"""package package_name + | + |import scala.meta.Defn + |import scala.meta.Lit + |import scala.meta.Pat + |import scala.meta.Term + |import scala.meta.transversers._ + |import scalafix.Patch + |import scalafix.lint.Diagnostic + |import scalafix.lint.LintSeverity + |import scalafix.v1.SyntacticDocument + |import scalafix.v1.SyntacticRule + |import scalafix.v1.XtensionSeqPatch + | + |class Example extends SyntacticRule("Example") { + | override def fix(implicit doc: SyntacticDocument): Patch = { + | doc.tree.collect { + | case t @ Defn.Val(Nil, List(Pat.Var(Term.Name("x"))), None, Term.Tuple(List(Lit.Unit(), Lit.Null()))) => + | Patch.lint( + | Diagnostic( + | id = "", + | message = "", + | position = t.pos, + | explanation = "", + | severity = LintSeverity.Warning + | ) + | ) + | }.asPatch + | } + |} + |""".stripMargin + assert(result.result == expect) + } + "Term.If" in { Seq[(String, List[TestArg])]( """Term.If.Initial(Term.Name("a"), Term.Name("b"), Term.If.Initial(Term.Name("x"), Term.Name("y"), Term.Select(Term.Name("z"), Term.Name("f"))))""" diff --git a/core/src/test/scala/scalameta_ast/ScalametaASTSpec.scala b/core/src/test/scala/scalameta_ast/ScalametaASTSpec.scala index b1279be..7d2ee56 100644 --- a/core/src/test/scala/scalameta_ast/ScalametaASTSpec.scala +++ b/core/src/test/scala/scalameta_ast/ScalametaASTSpec.scala @@ -5,107 +5,6 @@ import org.scalatest.freespec.AnyFreeSpec class ScalametaASTSpec extends AnyFreeSpec { "ScalametaAST" - { val main = new ScalametaAST - "convert" in { - val result = main.convert( - src = "val x = ((), null)", - outputType = "syntactic", - packageName = Option("package_name"), - wildcardImport = false, - ruleNameOption = None, - dialect = None, - patch = None, - removeNewFields = true, - initialExtractor = false, - explanation = true, - pathFilter = false, - ) - val expect = s"""package package_name - | - |import scala.meta.Defn - |import scala.meta.Lit - |import scala.meta.Pat - |import scala.meta.Term - |import scala.meta.transversers._ - |import scalafix.Patch - |import scalafix.lint.Diagnostic - |import scalafix.lint.LintSeverity - |import scalafix.v1.SyntacticDocument - |import scalafix.v1.SyntacticRule - |import scalafix.v1.XtensionSeqPatch - | - |class Example extends SyntacticRule("Example") { - | override def fix(implicit doc: SyntacticDocument): Patch = { - | doc.tree.collect { - | case t @ Defn.Val(Nil, List(Pat.Var(Term.Name("x"))), None, Term.Tuple(List(Lit.Unit(), Lit.Null()))) => - | Patch.lint( - | Diagnostic( - | id = "", - | message = "", - | position = t.pos, - | explanation = "", - | severity = LintSeverity.Warning - | ) - | ) - | }.asPatch - | } - |} - |""".stripMargin - assert(result.result == expect) - } - - "import" in { - val result = main.convert( - src = ( - "Case", - "Ctor", - "Decl", - "Defn", - "Defn", - "Export", - "Import", - "Importee", - "Init", - "Member", - "Mod", - "Pkg", - "Ref", - "Self", - "Stat", - "Template", - "Type" - ).toString, - outputType = "syntactic", - packageName = Option("package_name"), - wildcardImport = false, - ruleNameOption = None, - dialect = None, - patch = Some("empty"), - removeNewFields = true, - initialExtractor = false, - explanation = true, - pathFilter = false, - ) - val expect = - s"""package package_name - | - |import scala.meta.Term - |import scala.meta.transversers._ - |import scalafix.Patch - |import scalafix.v1.SyntacticDocument - |import scalafix.v1.SyntacticRule - |import scalafix.v1.XtensionSeqPatch - | - |class Example extends SyntacticRule("Example") { - | override def fix(implicit doc: SyntacticDocument): Patch = { - | doc.tree.collect { - | case t @ Term.Tuple(List(Term.Name("Case"), Term.Name("Ctor"), Term.Name("Decl"), Term.Name("Defn"), Term.Name("Defn"), Term.Name("Export"), Term.Name("Import"), Term.Name("Importee"), Term.Name("Init"), Term.Name("Member"), Term.Name("Mod"), Term.Name("Pkg"), Term.Name("Ref"), Term.Name("Self"), Term.Name("Stat"), Term.Name("Template"), Term.Name("Type"))) => - | Patch.empty - | }.asPatch - | } - |} - |""".stripMargin - assert(result.result == expect) - } "invalid tree" in { val result = main.convert( diff --git a/sources/main.js b/sources/main.js index d024272..6e96786 100644 --- a/sources/main.js +++ b/sources/main.js @@ -236,7 +236,7 @@ const App = () => { result = hljs.highlight(r.ast, { language: "scala", }).value; - const scalafixVersion = scalameta == "latest" ? "0.13.0" : "0.10.4"; + const scalafixVersion = scalameta == "latest" ? "0.14.0" : "0.10.4"; const scalafixUrl = (s) => `https://github.com/scalacenter/scalafix/blob/v${scalafixVersion}/scalafix-core/src/main/scala/scalafix/${s}.scala`;