From 498b37594248575a8e1be9a4416fb19221126709 Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Sat, 3 Jun 2023 11:30:19 +0200 Subject: [PATCH] expose & fix scalameta deprecations --- docs/developers/tutorial.md | 12 +-- project/ScalafixBuild.scala | 3 +- .../scalafix/internal/patch/EscapeHatch.scala | 28 ++++++- .../internal/patch/ReplaceSymbolOps.scala | 2 +- .../internal/util/DenotationOps.scala | 15 ++-- .../scalafix/internal/util/PrettyType.scala | 68 +++++++++++------ .../scalafix/internal/v1/PositionSearch.scala | 3 +- .../scala/scalafix/internal/v1/TreePos.scala | 2 +- .../main/scala/scalafix/util/TreeOps.scala | 22 +++--- .../reflect/RuleInstrumentation.scala | 74 +++++++++++++++---- .../internal/rule/CompilerTypePrinter.scala | 54 +++++++------- .../internal/rule/ExplicitResultTypes.scala | 14 ++-- .../internal/rule/DisableSyntax.scala | 35 ++++++--- .../rule/LeakingImplicitClassVal.scala | 16 +++- .../internal/rule/NoAutoTupling.scala | 4 +- .../scalafix/internal/rule/RemoveUnused.scala | 23 ++++-- .../scalafix/tests/cli/CliSuppressSuite.scala | 2 +- .../scalafix/tests/core/PrettyTypeSuite.scala | 10 ++- .../scala/scalafix/tests/v1/SymbolSuite.scala | 8 +- .../tests/util/ScalametaStructureSuite.scala | 14 ++-- 20 files changed, 271 insertions(+), 138 deletions(-) diff --git a/docs/developers/tutorial.md b/docs/developers/tutorial.md index 9c41e9546..2dcd81315 100644 --- a/docs/developers/tutorial.md +++ b/docs/developers/tutorial.md @@ -181,7 +181,7 @@ To fix this bug, we first match function call nodes `Term.Apply` and pattern match only `Lit.Boolean` that appear in argument position ```scala -case Term.Apply(_, args) => +case Term.Apply.After_4_6_0(_, args) => args.collect { case t @ Lit.Boolean(_) => Patch.addLeft(t, "isSuccess = ") @@ -206,8 +206,8 @@ is to produce `finish(isError = true)`. To fix this bug, we start by capturing the called method into a variable `fun` ```diff -- case Term.Apply(_, args) => -+ case Term.Apply(fun, args) => +- case Term.Apply.After_4_6_0(_, args) => ++ case Term.Apply.After_4_6_0(fun, args) => ``` We update the call to `args.collect` to include the index of the argument @@ -284,7 +284,7 @@ class NamedLiteralArguments extends SemanticRule("NamedLiteralArguments") { override def fix(implicit doc: SemanticDocument): Patch = { doc.tree.collect { - case Term.Apply(fun, args) => + case Term.Apply.After_4_6_0(fun, args) => args.zipWithIndex.collect { case (t @ Lit.Boolean(_), i) => fun.symbol.info match { @@ -384,7 +384,7 @@ Next, we write the same pattern matching logic as in `NamedLiteralArguments` ```scala doc.tree.collect { - case Term.Apply(_, args) => + case Term.Apply.After_4_6_0(_, args) => args.collect { case t @ Lit.Boolean(_) => // .... @@ -581,7 +581,7 @@ class NoLiteralArguments(config: NoLiteralArgumentsConfig) override def fix(implicit doc: SyntacticDocument): Patch = { doc.tree .collect { - case Term.Apply(_, args) => + case Term.Apply.After_4_6_0(_, args) => args.collect { case t: Lit if config.isDisabled(t) => Patch.lint(LiteralArgument(t)) diff --git a/project/ScalafixBuild.scala b/project/ScalafixBuild.scala index ff82e4a44..2df78941c 100644 --- a/project/ScalafixBuild.scala +++ b/project/ScalafixBuild.scala @@ -86,7 +86,8 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys { "-encoding", "UTF-8", "-feature", - "-unchecked" + "-unchecked", + "-Wconf:cat=deprecation&origin=scala\\.meta\\..*:error" ) ) diff --git a/scalafix-core/src/main/scala/scalafix/internal/patch/EscapeHatch.scala b/scalafix-core/src/main/scala/scalafix/internal/patch/EscapeHatch.scala index d940db3d3..6eed74624 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/patch/EscapeHatch.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/patch/EscapeHatch.scala @@ -242,19 +242,39 @@ object EscapeHatch { def unapply(mods: List[Mod]): Option[List[Term]] = mods.collectFirst { case Mod.Annot( - Init( + Init.After_4_6_0( Type.Name(SuppressWarnings), _, - List(Term.Apply(Term.Name("Array"), args) :: Nil) + List( + Term.ArgClause( + List( + Term.Apply.After_4_6_0( + Term.Name("Array"), + Term.ArgClause(args, None) + ) + ), + None + ) + ) ) ) => args case Mod.Annot( - Init( + Init.After_4_6_0( Type.Select(_, Type.Name(SuppressWarnings)), _, - List(Term.Apply(Term.Name("Array"), args) :: Nil) + List( + Term.ArgClause( + List( + Term.Apply.After_4_6_0( + Term.Name("Array"), + Term.ArgClause(args, None) + ) + ), + None + ) + ) ) ) => args diff --git a/scalafix-core/src/main/scala/scalafix/internal/patch/ReplaceSymbolOps.scala b/scalafix-core/src/main/scala/scalafix/internal/patch/ReplaceSymbolOps.scala index bdc10bb79..27972205b 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/patch/ReplaceSymbolOps.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/patch/ReplaceSymbolOps.scala @@ -88,7 +88,7 @@ object ReplaceSymbolOps { object Identifier { def unapply(tree: Tree): Option[(Name, Symbol)] = tree match { case n: Name => n.symbol.map(s => n -> s) - case Init(n: Name, _, _) => n.symbol.map(s => n -> s) + case Init.After_4_6_0(n: Name, _, _) => n.symbol.map(s => n -> s) case _ => None } } diff --git a/scalafix-core/src/main/scala/scalafix/internal/util/DenotationOps.scala b/scalafix-core/src/main/scala/scalafix/internal/util/DenotationOps.scala index acdf756c1..3997ac357 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/util/DenotationOps.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/util/DenotationOps.scala @@ -7,7 +7,7 @@ import scalafix.v0._ object DenotationOps { val defaultDialect: Dialect = - dialects.Scala212.copy(allowTypeLambdas = true) + dialects.Scala212.withAllowTypeLambdas(true) def resultType( symbol: Symbol, @@ -15,10 +15,15 @@ object DenotationOps { dialect: Dialect ): Option[Type] = { def getDeclType(tpe: Type): Type = tpe match { - case Type.Method(_, tpe) if denot.isMethod => tpe - case Type.Lambda(_, tpe) if denot.isMethod => getDeclType(tpe) - case Type.Method((Term.Param(_, _, Some(tpe), _) :: Nil) :: Nil, _) - if denot.isVar => + case Type.Method.After_4_6_0(_, tpe) if denot.isMethod => tpe + case Type.Lambda.After_4_6_0(_, tpe) if denot.isMethod => getDeclType(tpe) + case Type.Method.After_4_6_0( + Term.ParamClause( + Term.Param(_, _, Some(tpe), _) :: Nil, + None + ) :: Nil, + _ + ) if denot.isVar => // Workaround for https://github.com/scalameta/scalameta/issues/1100 tpe case x => diff --git a/scalafix-core/src/main/scala/scalafix/internal/util/PrettyType.scala b/scalafix-core/src/main/scala/scalafix/internal/util/PrettyType.scala index 7672108b3..7d0a2f3fa 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/util/PrettyType.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/util/PrettyType.scala @@ -216,17 +216,25 @@ class PrettyType private ( toMods(info), Name(""), paramss.iterator - .map(params => params.symbols.smap(toTermParam)) + .map(params => + Term.ParamClause(params.symbols.smap(toTermParam)) + ) .toList ) } else { Decl.Def( toMods(info), Term.Name(info.displayName), - tparams.smap(toTypeParam), - paramss.iterator - .map(params => params.symbols.smap(toTermParam)) - .toList, + List( + Member.ParamClauseGroup( + tparams.smap(toTypeParam), + paramss.iterator + .map(params => + Term.ParamClause(params.symbols.smap(toTermParam)) + ) + .toList + ) + ), toType(ret) ) } @@ -267,7 +275,7 @@ class PrettyType private ( Defn.Trait( toMods(info), Type.Name(info.displayName), - tparams.smap(toTypeParam), + Type.ParamClause(tparams.smap(toTypeParam)), Ctor.Primary(Nil, Name(""), Seq.empty[Term.ParamClause]), Template( Nil, @@ -279,7 +287,8 @@ class PrettyType private ( !i.isVarSetter ) toStat(i) else Nil - } + }, + Nil ) ) case k.OBJECT => @@ -290,7 +299,8 @@ class PrettyType private ( Nil, inits, Self(Name(""), None), - objectDecls + objectDecls, + Nil ) ) case k.PACKAGE_OBJECT => @@ -301,7 +311,8 @@ class PrettyType private ( Nil, inits, Self(Name(""), None), - objectDecls + objectDecls, + Nil ) ) case k.CLASS => @@ -309,19 +320,27 @@ class PrettyType private ( .collectFirst { case i if i.kind.isConstructor && i.is(p.PRIMARY) => toTree(i) match { - case ctor @ Ctor.Primary(_, _, Nil :: Nil) - if !info.is(p.CASE) => + case ctor @ Ctor.Primary.After_4_6_0( + _, + _, + Term.ParamClause(Nil, _) :: Nil + ) if !info.is(p.CASE) => // Remove redudant () for non-case classes: class Foo ctor.copy(paramss = Nil) case e: Ctor.Primary => e } } .getOrElse { - Ctor.Primary(Nil, Name(""), Seq.empty[Term.ParamClause]) + Ctor.Primary.After_4_6_0( + Nil, + Name(""), + Seq.empty[Term.ParamClause] + ) } // FIXME: Workaround for https://github.com/scalameta/scalameta/issues/1492 - val isCtorName = ctor.paramss.flatMap(_.map(_.name.value)).toSet + val isCtorName = + ctor.paramClauses.flatMap(_.values).map(_.name.value).toSet def isSyntheticMember(m: s.SymbolInformation): Boolean = (isCaseClass && isCaseClassMethod(m.displayName)) || isCtorName(m.displayName) @@ -329,7 +348,7 @@ class PrettyType private ( Defn.Class( toMods(info), Type.Name(info.displayName), - tparams.smap(toTypeParam), + Type.ParamClause(tparams.smap(toTypeParam)), ctor, Template( Nil, @@ -342,7 +361,8 @@ class PrettyType private ( !isSyntheticMember(i) ) toStat(i) else Nil - } + }, + Nil ) ) case _ => @@ -353,14 +373,14 @@ class PrettyType private ( Defn.Type( toMods(info), Type.Name(info.displayName), - typeParameters.smap(toTypeParam), + Type.ParamClause(typeParameters.smap(toTypeParam)), toType(lo) ) } else { Decl.Type( toMods(info), Type.Name(info.displayName), - typeParameters.smap(toTypeParam), + Type.ParamClause(typeParameters.smap(toTypeParam)), toTypeBounds(lo, hi) ) } @@ -389,7 +409,7 @@ class PrettyType private ( case _ => tpe } - Init( + Init.After_4_6_0( toType(fixed), Name.Anonymous(), // Can't support term arguments @@ -518,14 +538,14 @@ class PrettyType private ( def targs: List[Type] = typeArguments.iterator.map { case TypeExtractors.Wildcard() => - Type.Placeholder(Type.Bounds(None, None)) + Type.Wildcard(Type.Bounds(None, None)) case targ => toType(targ) }.toList symbol match { case TypeExtractors.FunctionN() if typeArguments.lengthCompare(0) > 0 => val params :+ res = targs - Type.Function(params, res) + Type.Function(Type.FuncParamClause(params), res) case TypeExtractors.TupleN() if typeArguments.lengthCompare(1) > 0 => Type.Tuple(targs) case _ => @@ -548,7 +568,7 @@ class PrettyType private ( case (name: Type.Name, Seq(lhs, rhs)) if !Character.isJavaIdentifierPart(name.value.head) => Type.ApplyInfix(lhs, name, rhs) - case (q, targs) => Type.Apply(q, targs) + case (q, targs) => Type.Apply(q, Type.ArgClause(targs)) } } case s.SingleType(_, symbol) => @@ -634,7 +654,7 @@ class PrettyType private ( Defn.Type( Nil, universalName, - typeParameters.smap(toTypeParam), + Type.ParamClause(typeParameters.smap(toTypeParam)), toType(underlying) ) :: Nil ), @@ -681,7 +701,7 @@ class PrettyType private ( Type.Param( Nil, Name(""), - Nil, + Type.ParamClause(Nil), Type.Bounds(None, None), Nil, Nil @@ -698,7 +718,7 @@ class PrettyType private ( Type.Param( toMods(info), name = Type.Name(info.displayName), - tparams = tparams, + tparamClause = Type.ParamClause(tparams), tbounds = bounds, // TODO: re-sugar context and view bounds https://github.com/scalacenter/scalafix/issues/759 vbounds = Nil, diff --git a/scalafix-core/src/main/scala/scalafix/internal/v1/PositionSearch.scala b/scalafix-core/src/main/scala/scalafix/internal/v1/PositionSearch.scala index 9d69ec0f4..142709b04 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/v1/PositionSearch.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/v1/PositionSearch.scala @@ -3,13 +3,14 @@ package scalafix.internal.v1 import scala.meta.Position import scala.meta.Term import scala.meta.Tree +import scala.meta.Type import scalafix.internal.util.PositionSyntax._ object PositionSearch { def find(tree: Tree, pos: Position): Option[Tree] = { val extrapos = tree match { - case Term.ApplyInfix(lhs, op, Nil, _) => + case Term.ApplyInfix.After_4_6_0(lhs, op, Type.ArgClause(Nil), _) => List(Position.Range(lhs.pos.input, lhs.pos.start, op.pos.end)) case _ => List() diff --git a/scalafix-core/src/main/scala/scalafix/internal/v1/TreePos.scala b/scalafix-core/src/main/scala/scalafix/internal/v1/TreePos.scala index 7a7811476..cfe7db165 100644 --- a/scalafix-core/src/main/scala/scalafix/internal/v1/TreePos.scala +++ b/scalafix-core/src/main/scala/scalafix/internal/v1/TreePos.scala @@ -61,7 +61,7 @@ object TreePos { case t: Pat.Interpolate => symbolImpl(t.prefix) case Defn.Val(_, p :: Nil, _, _) => symbolImpl(p) case Decl.Val(_, p :: Nil, _) => symbolImpl(p) - case Defn.Var(_, p :: Nil, _, _) => symbolImpl(p) + case Defn.Var.After_4_7_2(_, p :: Nil, _, _) => symbolImpl(p) case Decl.Var(_, p :: Nil, _) => symbolImpl(p) case t: Importee.Rename => symbolImpl(t.name) case t: Importee.Name => symbolImpl(t.name) diff --git a/scalafix-core/src/main/scala/scalafix/util/TreeOps.scala b/scalafix-core/src/main/scala/scalafix/util/TreeOps.scala index a4c8ce1cc..7f8401965 100644 --- a/scalafix-core/src/main/scala/scalafix/util/TreeOps.scala +++ b/scalafix-core/src/main/scala/scalafix/util/TreeOps.scala @@ -24,23 +24,23 @@ object TreeExtractors { object Mods { def unapply(tree: Tree): Option[List[Mod]] = tree match { - case Ctor.Primary(mods, _, _) => Some(mods) - case Ctor.Secondary(mods, _, _, _, _) => Some(mods) - case Decl.Def(mods, _, _, _, _) => Some(mods) - case Decl.Type(mods, _, _, _) => Some(mods) + case Ctor.Primary.After_4_6_0(mods, _, _) => Some(mods) + case Ctor.Secondary.After_4_6_0(mods, _, _, _, _) => Some(mods) + case Decl.Def.After_4_6_0(mods, _, _, _) => Some(mods) + case Decl.Type.After_4_6_0(mods, _, _, _) => Some(mods) case Decl.Val(mods, _, _) => Some(mods) case Decl.Var(mods, _, _) => Some(mods) - case Defn.Class(mods, _, _, _, _) => Some(mods) - case Defn.Def(mods, _, _, _, _, _) => Some(mods) - case Defn.Macro(mods, _, _, _, _, _) => Some(mods) + case Defn.Class.After_4_6_0(mods, _, _, _, _) => Some(mods) + case Defn.Def.After_4_7_3(mods, _, _, _, _) => Some(mods) + case Defn.Macro.After_4_7_3(mods, _, _, _, _) => Some(mods) case Defn.Object(mods, _, _) => Some(mods) - case Defn.Trait(mods, _, _, _, _) => Some(mods) - case Defn.Type(mods, _, _, _) => Some(mods) + case Defn.Trait.After_4_6_0(mods, _, _, _, _) => Some(mods) + case Defn.Type.After_4_6_0(mods, _, _, _, _) => Some(mods) case Defn.Val(mods, _, _, _) => Some(mods) - case Defn.Var(mods, _, _, _) => Some(mods) + case Defn.Var.After_4_7_2(mods, _, _, _) => Some(mods) case Pkg.Object(mods, _, _) => Some(mods) case Term.Param(mods, _, _, _) => Some(mods) - case Type.Param(mods, _, _, _, _, _) => Some(mods) + case Type.Param.After_4_6_0(mods, _, _, _, _, _) => Some(mods) case _ => None } } diff --git a/scalafix-reflect/src/main/scala/scalafix/internal/reflect/RuleInstrumentation.scala b/scalafix-reflect/src/main/scala/scalafix/internal/reflect/RuleInstrumentation.scala index 2d5f420a6..72050bb8b 100644 --- a/scalafix-reflect/src/main/scala/scalafix/internal/reflect/RuleInstrumentation.scala +++ b/scalafix-reflect/src/main/scala/scalafix/internal/reflect/RuleInstrumentation.scala @@ -15,54 +15,96 @@ object RuleInstrumentation { def unapply(templ: Template): Boolean = templ match { // v0 - case Template(_, Init(Type.Name("Rewrite"), _, Nil) :: _, _, _) => true - case Template( + case Template.After_4_4_0( + _, + Init.After_4_6_0(Type.Name("Rewrite"), _, Nil) :: _, _, - Init(Type.Name("Rule"), _, List(List(_))) :: _, _, _ ) => true - case Template( + case Template.After_4_4_0( + _, + Init.After_4_6_0( + Type.Name("Rule"), + _, + Term.ArgClause(List(_), None) :: Nil + ) :: _, _, - Init(Type.Name("SemanticRewrite"), _, List(List(_))) :: _, _, _ ) => true - case Template( + case Template.After_4_4_0( + _, + Init.After_4_6_0( + Type.Name("SemanticRewrite"), + _, + Term.ArgClause(List(_), None) :: Nil + ) :: _, + _, + _, + _ + ) => + true + case Template.After_4_4_0( + _, + Init.After_4_6_0( + Type.Name("SemanticRule"), + _, + Term.ArgClause(List(_), None) :: Nil + ) :: _, _, - Init(Type.Name("SemanticRule"), _, List(List(_, _))) :: _, _, _ ) => true // v1 - case Template( + case Template.After_4_4_0( + _, + Init.After_4_6_0( + Type.Name("SemanticRule"), + _, + Term.ArgClause(List(_), None) :: Nil + ) :: _, _, - Init(Type.Name("SemanticRule"), _, List(List(_))) :: _, _, _ ) => true - case Template( + case Template.After_4_4_0( + _, + Init.After_4_6_0( + Type.Name("v1.SemanticRule"), + _, + Term.ArgClause(List(_), None) :: Nil + ) :: _, _, - Init(Type.Name("v1.SemanticRule"), _, List(List(_))) :: _, _, _ ) => true - case Template( + case Template.After_4_4_0( + _, + Init.After_4_6_0( + Type.Name("SyntacticRule"), + _, + Term.ArgClause(List(_), None) :: Nil + ) :: _, _, - Init(Type.Name("SyntacticRule"), _, List(List(_))) :: _, _, _ ) => true - case Template( + case Template.After_4_4_0( + _, + Init.After_4_6_0( + Type.Name("v1.SyntacticRule"), + _, + Term.ArgClause(List(_), None) :: Nil + ) :: _, _, - Init(Type.Name("v1.SyntacticRule"), _, List(List(_))) :: _, _, _ ) => @@ -86,7 +128,7 @@ object RuleInstrumentation { stats.foreach(s => loop(prefix :+ ref.syntax, s)) case Defn.Object(_, name, ExtendsRule()) => add(prefix :+ name.syntax) - case Defn.Class(_, name, _, _, ExtendsRule()) => + case Defn.Class.After_4_6_0(_, name, _, _, ExtendsRule()) => add(prefix :+ name.syntax) case _ => tree.children.foreach(s => loop(prefix, s)) diff --git a/scalafix-rules/src/main/scala-2/scalafix/internal/rule/CompilerTypePrinter.scala b/scalafix-rules/src/main/scala-2/scalafix/internal/rule/CompilerTypePrinter.scala index a33054a8f..07bec2687 100644 --- a/scalafix-rules/src/main/scala-2/scalafix/internal/rule/CompilerTypePrinter.scala +++ b/scalafix-rules/src/main/scala-2/scalafix/internal/rule/CompilerTypePrinter.scala @@ -238,7 +238,7 @@ class CompilerTypePrinter(g: ScalafixGlobal, config: ExplicitResultTypesConfig)( decls.filterNot(_.isOverridingSymbol).nonEmpty => val body: Option[m.Term] = defn match { case t: m.Defn.Val => Some(t.rhs) - case t: m.Defn.Var => t.rhs + case t: m.Defn.Var => Some(t.body) case t: m.Defn.Def => Some(t.body) case _ => None } @@ -255,37 +255,37 @@ class CompilerTypePrinter(g: ScalafixGlobal, config: ExplicitResultTypesConfig)( } .get isInsertedClass += name - val paramDefnSuffix = defn match { - case d: m.Defn.Def => - d.paramss - .map(_.map(_.syntax).mkString(", ")) - .mkString("(", ")(", ")") - case _ => "" - } - val tparamDefn = defn match { - case d: m.Defn.Def if d.tparams.nonEmpty => - d.tparams.map(_.syntax).mkString("[", ", ", "]") - case _ => "" - } - val tparamCall = defn match { - case d: m.Defn.Def if d.tparams.nonEmpty => - d.tparams.map(_.name.syntax).mkString("[", ", ", "]") - case _ => "" - } - val paramCallSuffix = defn match { - case d: m.Defn.Def => - d.paramss - .map(_.map(_.name.syntax).mkString(", ")) - .mkString("(", ")(", ")") - case _ => "" - } + // val paramDefnSuffix = defn match { + // case d: m.Defn.Def => + // d.paramss + // .map(_.map(_.syntax).mkString(", ")) + // .mkString("(", ")(", ")") + // case _ => "" + // } + // val tparamDefn = defn match { + // case d: m.Defn.Def if d.tparams.nonEmpty => + // d.tparams.map(_.syntax).mkString("[", ", ", "]") + // case _ => "" + // } + // val tparamCall = defn match { + // case d: m.Defn.Def if d.tparams.nonEmpty => + // d.tparams.map(_.name.syntax).mkString("[", ", ", "]") + // case _ => "" + // } + // val paramCallSuffix = defn match { + // case d: m.Defn.Def => + // d.paramss + // .map(_.map(_.name.syntax).mkString(", ")) + // .mkString("(", ")(", ")") + // case _ => "" + // } val indent = " " * defn.pos.startColumn Some( ( - new PrettyType(name + tparamCall), + new PrettyType(name), // + tparamCall), v1.Patch.addRight( body.tokens.head, - s" ${name}${tparamCall}${paramCallSuffix}\n${indent}class ${name}${tparamDefn}${paramDefnSuffix} extends" + "" // s" ${name}${tparamCall}${paramCallSuffix}\n${indent}class ${name}${tparamDefn}${paramDefnSuffix} extends" ) ) ) diff --git a/scalafix-rules/src/main/scala-2/scalafix/internal/rule/ExplicitResultTypes.scala b/scalafix-rules/src/main/scala-2/scalafix/internal/rule/ExplicitResultTypes.scala index 0c981ba07..92b1bba97 100644 --- a/scalafix-rules/src/main/scala-2/scalafix/internal/rule/ExplicitResultTypes.scala +++ b/scalafix-rules/src/main/scala-2/scalafix/internal/rule/ExplicitResultTypes.scala @@ -98,11 +98,11 @@ final class ExplicitResultTypes( if isRuleCandidate(t, name, mods, body) => fixDefinition(t, body, types) - case t @ Defn.Var(mods, Pat.Var(name) :: Nil, None, Some(body)) + case t @ Defn.Var.After_4_7_2(mods, Pat.Var(name) :: Nil, None, body) if isRuleCandidate(t, name, mods, body) => fixDefinition(t, body, types) - case t @ Defn.Def(mods, name, _, _, None, body) + case t @ Defn.Def.After_4_7_3(mods, name, _, None, body) if isRuleCandidate(t, name, mods, body) => fixDefinition(t, body, types) }.asPatch @@ -115,20 +115,20 @@ final class ExplicitResultTypes( // this a common trick employed implicit-heavy code to workaround SI-2712. // Context: https://gitter.im/typelevel/cats?at=584573151eb3d648695b4a50 private def isImplicitly(term: Term): Boolean = term match { - case Term.ApplyType(Term.Name("implicitly"), _) => true + case Term.ApplyType.After_4_6_0(Term.Name("implicitly"), _) => true case _ => false } def defnName(defn: Defn): Option[Name] = Option(defn).collect { case Defn.Val(_, Pat.Var(name) :: Nil, _, _) => name - case Defn.Var(_, Pat.Var(name) :: Nil, _, _) => name - case Defn.Def(_, name, _, _, _, _) => name + case Defn.Var.After_4_7_2(_, Pat.Var(name) :: Nil, _, _) => name + case Defn.Def.After_4_7_3(_, name, _, _, _) => name } def defnBody(defn: Defn): Option[Term] = Option(defn).collect { case Defn.Val(_, _, _, term) => term - case Defn.Var(_, _, _, Some(term)) => term - case Defn.Def(_, _, _, _, _, term) => term + case Defn.Var.After_4_7_2(_, _, _, term) => term + case Defn.Def.After_4_7_3(_, _, _, _, term) => term } def visibility(mods: Iterable[Mod]): MemberVisibility = diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala index 4d35ccba7..5d6eea934 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/DisableSyntax.scala @@ -132,10 +132,10 @@ final class DisableSyntax(config: DisableSyntaxConfig) object AbstractWithVals { def unapply(t: Tree): Option[List[Defn.Val]] = { val stats = t match { - case Defn.Class(mods, _, _, _, templ) + case Defn.Class.After_4_6_0(mods, _, _, _, templ) if mods.exists(_.is[Mod.Abstract]) => templ.stats - case Defn.Trait(_, _, _, _, templ) => templ.stats + case Defn.Trait.After_4_6_0(_, _, _, _, templ) => templ.stats case _ => List.empty } val vals = stats.flatMap { @@ -152,7 +152,8 @@ final class DisableSyntax(config: DisableSyntaxConfig) case d: Defn.Def => { val defaults = for { - params <- d.paramss + groups <- d.paramClauseGroups + params <- groups.paramClauses param <- params default <- param.default.toList } yield default @@ -181,8 +182,11 @@ final class DisableSyntax(config: DisableSyntaxConfig) } } - def hasNonImplicitParam(d: Defn.Def): Boolean = - d.paramss.exists(_.exists(_.mods.forall(!_.is[Mod.Implicit]))) + def hasNonImplicitParam(d: Defn.Def): Boolean = { + d.paramClauseGroups.exists( + _.paramClauses.exists(_.exists(_.mods.forall(!_.is[Mod.Implicit]))) + ) + } val DefaultMatcher: PartialFunction[Tree, Seq[Diagnostic]] = { case Defn.Val(mods, _, _, _) @@ -225,7 +229,7 @@ final class DisableSyntax(config: DisableSyntaxConfig) t.pos ) ) - case t @ Defn.Def(mods, _, _, paramss, _, _) + case t @ Defn.Def.After_4_7_3(mods, _, _, _, _) if mods.exists(_.is[Mod.Implicit]) && hasNonImplicitParam(t) && config.noImplicitConversion => @@ -245,16 +249,16 @@ final class DisableSyntax(config: DisableSyntaxConfig) m.pos ) } - case Term.ApplyInfix(_, t @ Term.Name("=="), _, _) + case Term.ApplyInfix.After_4_6_0(_, t @ Term.Name("=="), _, _) if config.noUniversalEquality => Seq(noUniversalEqualityDiagnostic("==", t)) - case Term.Apply(Term.Select(_, t @ Term.Name("==")), _) + case Term.Apply.After_4_6_0(Term.Select(_, t @ Term.Name("==")), _) if config.noUniversalEquality => Seq(noUniversalEqualityDiagnostic("==", t)) - case Term.ApplyInfix(_, t @ Term.Name("!="), _, _) + case Term.ApplyInfix.After_4_6_0(_, t @ Term.Name("!="), _, _) if config.noUniversalEquality => Seq(noUniversalEqualityDiagnostic("!=", t)) - case Term.Apply(Term.Select(_, t @ Term.Name("!=")), _) + case Term.Apply.After_4_6_0(Term.Select(_, t @ Term.Name("!=")), _) if config.noUniversalEquality => Seq(noUniversalEqualityDiagnostic("!=", t)) } @@ -297,7 +301,16 @@ object DisableSyntax { |overriding finalize incurs a performance penalty""".stripMargin def FinalizeMatcher(id: String): PartialFunction[Tree, List[Diagnostic]] = { - case Defn.Def(_, name @ Term.Name("finalize"), _, Nil | Nil :: Nil, _, _) => + case Defn.Def.After_4_7_3( + _, + name @ Term.Name("finalize"), + Member.ParamClauseGroup( + Type.ParamClause(Nil), + Term.ParamClause(Nil, None) :: Nil + ) :: Nil, + _, + _ + ) => Diagnostic( id, "finalize should not be used", diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/LeakingImplicitClassVal.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/LeakingImplicitClassVal.scala index 41e949b6a..45a3a16a6 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/LeakingImplicitClassVal.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/LeakingImplicitClassVal.scala @@ -12,12 +12,22 @@ class LeakingImplicitClassVal extends SyntacticRule("LeakingImplicitClassVal") { override def fix(implicit doc: SyntacticDocument): Patch = { doc.tree.collect { - case Defn.Class( + case Defn.Class.After_4_6_0( cMods, _, _, - Ctor.Primary(_, _, (Term.Param(pMods, _, _, _) :: Nil) :: Nil), - Template(_, Init(Type.Name("AnyVal"), _, _) :: Nil, _, _) + Ctor.Primary.After_4_6_0( + _, + _, + Term.ParamClause(Term.Param(pMods, _, _, _), _) :: Nil + ), + Template.After_4_4_0( + _, + Init.After_4_6_0(Type.Name("AnyVal"), _, _) :: Nil, + _, + _, + _ + ) ) if cMods.exists(_.is[Mod.Implicit]) => val optPatch = for { anchorMod <- pMods.find(!_.is[Mod.Annot]) diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala index 336273300..474b96662 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/NoAutoTupling.scala @@ -44,9 +44,9 @@ class NoAutoTupling extends SemanticRule("NoAutoTupling") { doc.tree .collect { case t: Term.Apply if tupleAdaptations.contains(t.pos) => - addWrappingParens(t.args) + addWrappingParens(t.argClause.values) case t: Term.Apply - if t.args.isEmpty && unitAdaptations.contains(t.pos) => + if t.argClause.values.isEmpty && unitAdaptations.contains(t.pos) => insertUnit(t) } .map(_.atomic) diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/RemoveUnused.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/RemoveUnused.scala index 2583ddc6c..67543e9fa 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/RemoveUnused.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/RemoveUnused.scala @@ -146,16 +146,19 @@ class RemoveUnused(config: RemoveUnusedConfig) case i @ Defn.Val(_, List(pat), _, _) if isUnusedTerm.exists(p => p.start == pat.pos.start) => defnTokensToRemove(i).map(Patch.removeTokens).asPatch.atomic - case i @ Defn.Var(_, List(pat), _, _) + case i @ Defn.Var.After_4_7_2(_, List(pat), _, _) if isUnusedTerm.exists(p => p.start == pat.pos.start) => defnTokensToRemove(i).map(Patch.removeTokens).asPatch.atomic - case Term.Match(_, cases) => + case Term.Match.After_4_4_5(_, cases, _) => patchPatVarsIn(cases) case Term.PartialFunction(cases) => patchPatVarsIn(cases) case Term.Try(_, cases, _) => patchPatVarsIn(cases) - case Term.Function(List(param @ Term.Param(_, name, None, _)), _) + case Term.Function.After_4_6_0( + Term.ParamClause(param @ Term.Param(_, name, None, _) :: Nil, _), + _ + ) if isUnusedParam( // diagnostic does not include the implicit prefix (supported only // on 1-arity function without explicit type) captured in param, @@ -164,13 +167,19 @@ class RemoveUnused(config: RemoveUnusedConfig) ) => // drop the entire param so that the implicit prefix (if it exists) is removed Patch.replaceTree(param, "_") - case Term.Function(List(param @ Term.Param(_, name, Some(_), _)), _) + case Term.Function.After_4_6_0( + Term.ParamClause( + param @ Term.Param(_, name, Some(_), _) :: Nil, + _ + ), + _ + ) if isUnusedParam( // diagnostic does not include the wrapping parens captured in single param posExclParens(param) ) => Patch.replaceTree(name, "_") - case Term.Function(params, _) => + case Term.Function.After_4_6_0(Term.ParamClause(params, _), _) => params.collect { case param @ Term.Param(_, name, _, _) if isUnusedParam(param.pos) => @@ -195,8 +204,8 @@ class RemoveUnused(config: RemoveUnusedConfig) private def defnTokensToRemove(defn: Defn): Option[Tokens] = defn match { case i @ Defn.Val(_, _, _, Lit(_)) => Some(i.tokens) case i @ Defn.Val(_, _, _, rhs) => Some(leftTokens(i, rhs)) - case i @ Defn.Var(_, _, _, Some(Lit(_))) => Some(i.tokens) - case i @ Defn.Var(_, _, _, rhs) => rhs.map(leftTokens(i, _)) + case i @ Defn.Var.After_4_7_2(_, _, _, Lit(_)) => Some(i.tokens) + case i @ Defn.Var.After_4_7_2(_, _, _, body) => Some(leftTokens(i, body)) case i: Defn.Def => Some(i.tokens) case _ => None } diff --git a/scalafix-tests/integration/src/test/scala-2/scalafix/tests/cli/CliSuppressSuite.scala b/scalafix-tests/integration/src/test/scala-2/scalafix/tests/cli/CliSuppressSuite.scala index c41446843..8d2ee8fd0 100644 --- a/scalafix-tests/integration/src/test/scala-2/scalafix/tests/cli/CliSuppressSuite.scala +++ b/scalafix-tests/integration/src/test/scala-2/scalafix/tests/cli/CliSuppressSuite.scala @@ -11,7 +11,7 @@ import scalafix.v1.SyntacticRule class NoVars extends SyntacticRule("NoVars") { override def fix(implicit doc: SyntacticDocument): Patch = { val error = LintCategory.error("No vars!") - doc.tree.collect { case v @ Defn.Var(_, _, _, _) => + doc.tree.collect { case v @ Defn.Var.After_4_7_2(_, _, _, _) => Patch.lint(error.at("no vars", v.pos)) }.asPatch } diff --git a/scalafix-tests/integration/src/test/scala-2/scalafix/tests/core/PrettyTypeSuite.scala b/scalafix-tests/integration/src/test/scala-2/scalafix/tests/core/PrettyTypeSuite.scala index ef89d3615..9e4234490 100644 --- a/scalafix-tests/integration/src/test/scala-2/scalafix/tests/core/PrettyTypeSuite.scala +++ b/scalafix-tests/integration/src/test/scala-2/scalafix/tests/core/PrettyTypeSuite.scala @@ -41,8 +41,14 @@ class PrettyTypeSuite extends BasePrettyTypeSuite { val m.Source(m.Pkg(_, stats) :: Nil) = source.transform { // Remove bodies from methods like `private def foo: Unit` that can't be abstract. - case m.Defn.Def(mods, name, tparams, paramss, Some(decltpe), _) => - m.Decl.Def(mods, name, tparams, paramss, decltpe) + case m.Defn.Def.After_4_6_0( + mods, + name, + paramClauseGroups, + Some(decltpe), + _ + ) => + m.Decl.Def.After_4_6_0(mods, name, paramClauseGroups, decltpe) } // ignoring Functor[C[_]] because of a regression with scalac 2.13.7, see https://github.com/scalacenter/scalafix/pull/1493 diff --git a/scalafix-tests/integration/src/test/scala/scalafix/tests/v1/SymbolSuite.scala b/scalafix-tests/integration/src/test/scala/scalafix/tests/v1/SymbolSuite.scala index bf176bea1..a5fdabdee 100644 --- a/scalafix-tests/integration/src/test/scala/scalafix/tests/v1/SymbolSuite.scala +++ b/scalafix-tests/integration/src/test/scala/scalafix/tests/v1/SymbolSuite.scala @@ -19,7 +19,13 @@ class SymbolSuite extends munit.FunSuite { test("fromTextDocument") { val arg :: Nil = doc.tree.collect { - case Term.ApplyInfix(_, Term.Name("shouldBe"), _, arg :: Nil) => arg + case Term.ApplyInfix.After_4_6_0( + _, + Term.Name("shouldBe"), + _, + Term.ArgClause(arg :: Nil, None) + ) => + arg } assertNotEquals(arg.symbol, Symbol.None) diff --git a/scalafix-tests/unit/src/test/scala/scalafix/tests/util/ScalametaStructureSuite.scala b/scalafix-tests/unit/src/test/scala/scalafix/tests/util/ScalametaStructureSuite.scala index 700bd6140..180e43e57 100644 --- a/scalafix-tests/unit/src/test/scala/scalafix/tests/util/ScalametaStructureSuite.scala +++ b/scalafix-tests/unit/src/test/scala/scalafix/tests/util/ScalametaStructureSuite.scala @@ -56,12 +56,11 @@ class ScalametaStructureSuite extends AnyFunSuite with DiffAssertions { test("option") { assertNoDiff( - Defn - .Def( + Defn.Def + .After_4_7_3( List(), Term.Name("foo"), - List(), - List(List()), + Member.ParamClauseGroup(Type.ParamClause(Nil), Nil) :: Nil, Some(Type.Name("A")), Term.Name("???") ) @@ -76,9 +75,10 @@ class ScalametaStructureSuite extends AnyFunSuite with DiffAssertions { test("list") { assertNoDiff( // NOTE(olafur): need downcast because List is no longer a Product in 2.13. - Term - .Apply(Term.Name("foo"), List(Term.Name("a"))) - .args + Term.Apply + .After_4_6_0(Term.Name("foo"), Term.ArgClause(List(Term.Name("a")))) + .argClause + .values .asInstanceOf[Product] .structureWidth(1), """|List(