diff --git a/core/src/main/scala-latest-js/scalameta_ast/RemoveNewFields.scala b/core/src/main/scala-latest-js/scalameta_ast/RemoveNewFields.scala new file mode 100644 index 0000000..6086694 --- /dev/null +++ b/core/src/main/scala-latest-js/scalameta_ast/RemoveNewFields.scala @@ -0,0 +1,9 @@ +package scalameta_ast + +import scala.annotation.unused +import scala.meta.Term +import scala.meta.Tree + +object RemoveNewFields { + def remove(@unused tree: Tree, @unused parsed: Term, str: String): String = str +} diff --git a/core/src/main/scala-scalafix_compat-js/scalameta_ast/RemoveNewFields.scala b/core/src/main/scala-scalafix_compat-js/scalameta_ast/RemoveNewFields.scala new file mode 100644 index 0000000..7dda8f3 --- /dev/null +++ b/core/src/main/scala-scalafix_compat-js/scalameta_ast/RemoveNewFields.scala @@ -0,0 +1,67 @@ +package scalameta_ast + +import scala.meta.Defn +import scala.meta.Template +import scala.meta.Term +import scala.meta.Tree +import scala.meta.contrib._ +import scala.meta.tokens.Token + +object RemoveNewFields { + def remove(tree: Tree, parsed: Term, str: String): String = { + if ( + tree.collectFirst { + case _: Term.If => () + case _: Term.Match => () + case _: Defn.Type => () + case _: Template => () + }.nonEmpty + ) { + val positions = parsed.collect { + case x @ Term.Apply( + Term.Select( + Term.Name("Term"), + Term.Name("If") + ), + _ :: _ :: _ :: last :: Nil + ) => + (x.tokens, last) + case x @ Term.Apply( + Term.Select( + Term.Name("Term"), + Term.Name("Match") + ), + _ :: _ :: last :: Nil + ) => + (x.tokens, last) + case x @ Term.Apply( + Term.Select( + Term.Name("Defn"), + Term.Name("Type") + ), + _ :: _ :: _ :: _ :: last :: Nil + ) => + (x.tokens, last) + case x @ Term.Apply( + Term.Name("Template"), + _ :: _ :: _ :: _ :: last :: Nil + ) => + (x.tokens, last) + }.flatMap { case (tokens, toRemove) => + val startOpt = + tokens.reverseIterator.filter(_.is[Token.Comma]).find(_.pos.start < toRemove.pos.start).map(_.start) + val endOpt = + tokens.reverseIterator.find(_.is[Token.RightParen]).map(_.pos.end - 1) + startOpt.zip(endOpt) + } + + str.zipWithIndex.flatMap { case (char, pos) => + Option.unless(positions.exists { case (start, end) => start <= pos && pos < end }) { + char + } + }.mkString + } else { + str + } + } +} diff --git a/core/src/main/scala/scalameta_ast/ScalametaAST.scala b/core/src/main/scala/scalameta_ast/ScalametaAST.scala index 246cd21..c44762c 100644 --- a/core/src/main/scala/scalameta_ast/ScalametaAST.scala +++ b/core/src/main/scala/scalameta_ast/ScalametaAST.scala @@ -4,7 +4,6 @@ import java.util.Date import scala.annotation.tailrec import scala.meta._ import scala.meta.common.Convert -import scala.meta.contrib.XtensionTreeOps import scala.meta.tokens.Token import scala.meta.parsers.Parse import scala.meta.parsers.Parsed @@ -186,63 +185,6 @@ class ScalametaAST { loop(Nil, str, 0, values).mkString } - private def removeModsFields(tree: Tree, parsed: Term, str: String): String = { - if ( - tree.collectFirst { - case _: Term.If => () - case _: Term.Match => () - case _: Defn.Type => () - case _: Template => () - }.nonEmpty - ) { - val positions = parsed.collect { - case x @ Term.Apply( - Term.Select( - Term.Name("Term"), - Term.Name("If") - ), - _ :: _ :: _ :: last :: Nil - ) => - (x.tokens, last) - case x @ Term.Apply( - Term.Select( - Term.Name("Term"), - Term.Name("Match") - ), - _ :: _ :: last :: Nil - ) => - (x.tokens, last) - case x @ Term.Apply( - Term.Select( - Term.Name("Defn"), - Term.Name("Type") - ), - _ :: _ :: _ :: _ :: last :: Nil - ) => - (x.tokens, last) - case x @ Term.Apply( - Term.Name("Template"), - _ :: _ :: _ :: _ :: last :: Nil - ) => - (x.tokens, last) - }.flatMap { case (tokens, toRemove) => - val startOpt = - tokens.reverseIterator.filter(_.is[Token.Comma]).find(_.pos.start < toRemove.pos.start).map(_.start) - val endOpt = - tokens.reverseIterator.find(_.is[Token.RightParen]).map(_.pos.end - 1) - startOpt.zip(endOpt) - } - - str.zipWithIndex.flatMap { case (char, pos) => - Option.unless(positions.exists { case (start, end) => start <= pos && pos < end }) { - char - } - }.mkString - } else { - str - } - } - def convert( src: String, outputType: String, @@ -371,7 +313,7 @@ class ScalametaAST { addExtractor(parsed = parsed, str = str, identity) -> parsedOpt } else { if (a.removeNewFields) { - val str2 = removeModsFields(tree = tree, parsed = parsed, str = str) + val str2 = RemoveNewFields.remove(tree = tree, parsed = parsed, str = str) if (a.initialExtractor) { val parsed2 = implicitly[Parse[Term]].apply(Input.String(str2), scala.meta.dialects.Scala3).get val parsedOpt2 = PartialFunction.condOpt(args) { case x: ScalafixRule => diff --git a/core/src/main/scalajvm/scalameta_ast/RemoveNewFields.scala b/core/src/main/scalajvm/scalameta_ast/RemoveNewFields.scala new file mode 100644 index 0000000..6086694 --- /dev/null +++ b/core/src/main/scalajvm/scalameta_ast/RemoveNewFields.scala @@ -0,0 +1,9 @@ +package scalameta_ast + +import scala.annotation.unused +import scala.meta.Term +import scala.meta.Tree + +object RemoveNewFields { + def remove(@unused tree: Tree, @unused parsed: Term, str: String): String = str +}