Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add explicit None for Term.ParamClause and Term.ArgClause #184

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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