Skip to content

Commit

Permalink
expose & fix scalameta deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
bjaglin committed Jun 2, 2023
1 parent 0501523 commit 5e77b05
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 66 deletions.
12 changes: 6 additions & 6 deletions docs/developers/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ")
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(_) =>
// ....
Expand Down Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion project/ScalafixBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ object ScalafixBuild extends AutoPlugin with GhpagesKeys {
"-encoding",
"UTF-8",
"-feature",
"-unchecked"
"-unchecked",
"-Wconf:cat=deprecation&origin=scala\\.meta\\..*:error",
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import scalafix.v0._

object DenotationOps {
val defaultDialect: Dialect =
dialects.Scala212.copy(allowTypeLambdas = true)
dialects.Scala212.withAllowTypeLambdas(true)

def resultType(
symbol: Symbol,
denot: Denotation,
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, _)
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,21 @@ 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)
)
}
Expand Down Expand Up @@ -267,7 +271,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,
Expand All @@ -279,7 +283,8 @@ class PrettyType private (
!i.isVarSetter
) toStat(i)
else Nil
}
},
Nil
)
)
case k.OBJECT =>
Expand All @@ -290,7 +295,8 @@ class PrettyType private (
Nil,
inits,
Self(Name(""), None),
objectDecls
objectDecls,
Nil
)
)
case k.PACKAGE_OBJECT =>
Expand All @@ -301,35 +307,36 @@ class PrettyType private (
Nil,
inits,
Self(Name(""), None),
objectDecls
objectDecls,
Nil
)
)
case k.CLASS =>
val ctor: Ctor.Primary = declarations
.collectFirst {
case i if i.kind.isConstructor && i.is(p.PRIMARY) =>
toTree(i) match {
case ctor @ Ctor.Primary(_, _, Nil :: Nil)
case ctor @ Ctor.Primary.After_4_6_0(_, _, 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)

Defn.Class(
toMods(info),
Type.Name(info.displayName),
tparams.smap(toTypeParam),
Type.ParamClause(tparams.smap(toTypeParam)),
ctor,
Template(
Nil,
Expand All @@ -342,7 +349,8 @@ class PrettyType private (
!isSyntheticMember(i)
) toStat(i)
else Nil
}
},
Nil
)
)
case _ =>
Expand All @@ -353,14 +361,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)
)
}
Expand Down Expand Up @@ -389,7 +397,7 @@ class PrettyType private (
case _ =>
tpe
}
Init(
Init.After_4_6_0(
toType(fixed),
Name.Anonymous(),
// Can't support term arguments
Expand Down Expand Up @@ -518,14 +526,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 _ =>
Expand All @@ -548,7 +556,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) =>
Expand Down Expand Up @@ -634,7 +642,7 @@ class PrettyType private (
Defn.Type(
Nil,
universalName,
typeParameters.smap(toTypeParam),
Type.ParamClause(typeParameters.smap(toTypeParam)),
toType(underlying)
) :: Nil
),
Expand Down Expand Up @@ -681,7 +689,7 @@ class PrettyType private (
Type.Param(
Nil,
Name(""),
Nil,
Type.ParamClause(Nil),
Type.Bounds(None, None),
Nil,
Nil
Expand All @@ -698,7 +706,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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, Nil, _) =>
List(Position.Range(lhs.pos.input, lhs.pos.start, op.pos.end))
case _ =>
List()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions scalafix-core/src/main/scala/scalafix/util/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Loading

0 comments on commit 5e77b05

Please sign in to comment.