Skip to content

Commit

Permalink
Merge pull request #184 from xuwei-k/ParamClause-ArgClause-None
Browse files Browse the repository at this point in the history
add explicit `None` for `Term.ParamClause` and `Term.ArgClause`
  • Loading branch information
xuwei-k authored Sep 29, 2024
2 parents 6a86195 + 9beb4f2 commit ba2c57d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
39 changes: 39 additions & 0 deletions core/src/main/scala-latest/scalameta_ast/AddDefaultParam.scala
Original file line number Diff line number Diff line change
@@ -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
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scalameta_ast

import scala.meta.Term

object AddDefaultParam {

def addDefaultParam(parsed: Term, str: String): String = str

}
7 changes: 6 additions & 1 deletion core/src/main/scala/scalameta_ast/ScalametaAST.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)"""
Expand Down

0 comments on commit ba2c57d

Please sign in to comment.