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 support to disable symbol via regex #669

Merged
merged 24 commits into from
Apr 11, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,32 @@ final case class Disable(index: SemanticdbIndex, config: DisableConfig)
case _ => blockedSymbols
}

def skipTermSelect(term: Term): Boolean = term match {
case _: Term.Name => true
case Term.Select(q, _) => skipTermSelect(q)
case _ => false
}

def handleName(t: Name, blockedSymbols: List[DisabledSymbol])
: Either[LintMessage, List[DisabledSymbol]] = {
val isBlocked = new DisableSymbolMatcher(blockedSymbols)
ctx.index.symbol(t) match {
case Some(isBlocked(s: Symbol.Global, disabled)) =>
SymbolOps.normalize(s) match {
case g: Symbol.Global if g.signature.name != "<init>" =>
Left(createLintMessage(g, disabled, t.pos))
case _ => Right(blockedSymbols)
}
case _ => Right(blockedSymbols)
}
}

new ContextTraverser(config.allDisabledSymbols)({
case (_: Import, _) => Right(List.empty)
case (Term.Select(q, name), blockedSymbols) if skipTermSelect(q) =>
handleName(name, blockedSymbols)
case (Type.Select(q, name), blockedSymbols) if skipTermSelect(q) =>
handleName(name, blockedSymbols)
case (
Term
.Apply(Term.Select(block @ safeBlock(_, _), Term.Name("apply")), _),
Expand All @@ -129,16 +153,7 @@ final case class Disable(index: SemanticdbIndex, config: DisableConfig)
case (_: Term.Function, _) =>
Right(config.allDisabledSymbols) // reset blocked symbols in (...) => (...)
case (t: Name, blockedSymbols) =>
val isBlocked = new DisableSymbolMatcher(blockedSymbols)
ctx.index.symbol(t) match {
case Some(isBlocked(s: Symbol.Global, disabled)) =>
SymbolOps.normalize(s) match {
case g: Symbol.Global =>
Left(createLintMessage(s, disabled, t.pos))
case _ => Right(blockedSymbols)
}
case _ => Right(blockedSymbols)
}
handleName(t, blockedSymbols)
}).result(ctx.tree)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scalafix.SemanticdbIndex
import scalafix.internal.config.ExplicitResultTypesConfig
import scalafix.internal.config.MemberKind
import scalafix.internal.config.MemberVisibility
import scalafix.internal.util.{TreeOps, TypeSyntax}
import scalafix.internal.util.TypeSyntax
import scalafix.rule.Rule
import scalafix.rule.RuleCtx
import scalafix.rule.RuleName
Expand Down Expand Up @@ -51,6 +51,12 @@ case class ExplicitResultTypes(
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
}

def visibility(mods: Traversable[Mod]): MemberVisibility =
mods
.collectFirst {
Expand All @@ -68,10 +74,17 @@ case class ExplicitResultTypes(
override def fix(ctx: RuleCtx): Patch = {
def defnType(defn: Defn): Option[(Type, Patch)] =
for {
name <- TreeOps.defnName(defn)
symbol <- name.symbol
typ <- symbol.resultType
} yield TypeSyntax.prettify(typ, ctx, config.unsafeShortenNames)
name <- defnName(defn)
denot <- name.denotation
tpe <- denot.tpeInternal
// Skip existential types for now since they cause problems.
if !tpe.tag.isExistentialType
result <- TypeSyntax.prettify(
tpe,
ctx,
config.unsafeShortenNames,
name.pos)
} yield result
import scala.meta._
def fix(defn: Defn, body: Term): Patch = {
val lst = ctx.tokenList
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions scalafix-tests/input/src/main/scala/test/DisableRegex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ object DisableRegex {
import scala.collection.mutable // ok

@SuppressWarnings(Array("Disable.ListBuffer"))
@SuppressWarnings(Array("Disable.<init>"))
val buffer = new ListBuffer[Int]()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you have a more complex qualifier in a select?

a.b(1).c

and a and c are banned? I would expect there to be two lint errors

a.b(1).c
^

a.b(1).c
       ^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Colombo: one more thing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened #679 and assigned the ticket to you @vovapolu . I will go ahead and merge to unblock a release, but please be sure to follow up on that ticket as I think the current change results in surprising behavior.


@SuppressWarnings(Array("Disable.mutable"))
@SuppressWarnings(Array("Disable.HashMap"))
@SuppressWarnings(Array("Disable.<init>"))
val abc = new mutable.HashMap[String, String]()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intuitively, there is only one symbol being referenced here HashMap.<init> so I think it's a bit weird that three errors are reported.

I think it would make sense to skip the qualifier of a Term.Select if it only contains Term.Select or Term.Name.

a.b.C // only report on C
d.E // only report on E
f.g(1).h // report on h and g

It should be simple to do this check with something like

def skipQualifier(qual: Term) Boolean = qual match {
  case _: Term.Name => true
  case Term.Select(q, _) => skipQualifier(q)
}


object IO { // IO we deserve
Expand Down