diff --git a/core/src/main/scala-latest/scalameta_ast/AddDefaultParam.scala b/core/src/main/scala-latest/scalameta_ast/AddDefaultParam.scala new file mode 100644 index 0000000..61cc717 --- /dev/null +++ b/core/src/main/scala-latest/scalameta_ast/AddDefaultParam.scala @@ -0,0 +1,39 @@ +package scalameta_ast + +import scala.annotation.tailrec +import scala.meta.Position +import scala.meta.Term + +object AddDefaultParam { + + def addDefaultParam(parsed: Term, str: String): String = { + val values = parsed.collect { + case Term.Apply.After_4_6_0( + Term.Select(Term.Name("Term"), Term.Name("ParamClause" | "ArgClause")), + Term.ArgClause( + List(a), + _ + ) + ) => + a.pos + } + + @tailrec + def loop(acc: List[String], code: String, consumed: Int, src: List[Position]): List[String] = { + src match { + case head :: tail => + val (x1, x2) = code.splitAt(head.end - consumed) + loop( + s", None" :: x1 :: acc, + x2, + x1.length + consumed, + tail + ) + case Nil => + (code :: acc).reverse + } + } + loop(Nil, str, 0, values).mkString + } + +} diff --git a/core/src/main/scala-scalafix_compat-js/scalameta_ast/AddDefaultParam.scala b/core/src/main/scala-scalafix_compat-js/scalameta_ast/AddDefaultParam.scala new file mode 100644 index 0000000..1a7ef68 --- /dev/null +++ b/core/src/main/scala-scalafix_compat-js/scalameta_ast/AddDefaultParam.scala @@ -0,0 +1,9 @@ +package scalameta_ast + +import scala.meta.Term + +object AddDefaultParam { + + def addDefaultParam(parsed: Term, str: String): String = str + +} diff --git a/core/src/main/scala/scalameta_ast/ScalametaAST.scala b/core/src/main/scala/scalameta_ast/ScalametaAST.scala index 87ef6e3..5dfe85c 100644 --- a/core/src/main/scala/scalameta_ast/ScalametaAST.scala +++ b/core/src/main/scala/scalameta_ast/ScalametaAST.scala @@ -254,7 +254,12 @@ class ScalametaAST { } if (AfterExtractor.enable) { - addExtractor(parsed = parsed, str = str, identity) -> parsedOpt + val str2 = addExtractor(parsed = parsed, str = str, identity) + val parsed2 = implicitly[Parse[Term]].apply(Input.String(str2), scala.meta.dialects.Scala3).get + val parsedOpt2 = PartialFunction.condOpt(args) { case x: ScalafixRule => + ParsedValue(() => parsed2, x) + } + AddDefaultParam.addDefaultParam(parsed = parsed2, str = str2) -> parsedOpt2 } else { if (a.removeNewFields) { val str2 = RemoveNewFields.remove(tree = tree, parsed = parsed, str = str) 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 06ca0ee..3f46d3a 100644 --- a/core/src/test/scala-latest-js/scalameta_ast/ScalametaASTSpec2.scala +++ b/core/src/test/scala-latest-js/scalameta_ast/ScalametaASTSpec2.scala @@ -11,6 +11,25 @@ class ScalametaASTSpec2 extends AnyFreeSpec { "ScalametaAST" - { val main = new ScalametaAST + "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)))""" + val result = main.convert( + src = "def a(b: C) = d(e)", + outputType = "", + packageName = None, + wildcardImport = false, + ruleNameOption = None, + dialect = None, + patch = None, + removeNewFields = false, + initialExtractor = false, + explanation = true, + pathFilter = false, + ) + assert(result.result == expect) + } + "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)"""