scalacenter / scalafix

Refactoring and linting tool for Scala
https://scalacenter.github.io/scalafix/
BSD 3-Clause "New" or "Revised" License
830 stars 186 forks source link

turn wildcard imports into qualified imports #182

Open cvogt opened 7 years ago

cvogt commented 7 years ago

not sure how easy that is

olafurpg commented 7 years ago

This would be a really nice rewrite to have. However, it's a tricky one since

Assuming we fix those issues, it should be possible to implement a def usedSymbol(Importee.Wildcard): Seq[Symbol] method that can be used to produce a patch. I'm happy to give more pointers if someone is interested.

olafurpg commented 7 years ago

Example diff produced by this rule

- import com.foo._
+ import com.foo.{A, B}

assuming that only com.foo.{A,B} are the only symbols used from the wildcard import. This is blocked by https://github.com/scalameta/scalameta/issues/1075

gabro commented 7 years ago

What would be the rewrite strategy for this?

olafurpg commented 7 years ago

We don't have the available data in semanticdb to resolve which symbols are imported from the wildcard.

gabro commented 7 years ago

And that would be #1075? Isn't that issue closed?

olafurpg commented 7 years ago

I just reopened the issue with a more detailed motivation

olafurpg commented 6 years ago

A quick update: synthetics now have attached symbols, which means scalafix knows symbols that are used by implicits. We also visit both macro expandees and expanders, however we could miss some used symbols in pathological cases, causing some symbols to be missing from the expanded wildcard import. I'm happy to give more pointers if someone is motivated to give this a try :)

olafurpg commented 6 years ago

This is a good idea. This should be significantly simpler now thanks to the new synthetics introduced in https://github.com/scalacenter/scalafix/pull/828. After the PR https://github.com/scalacenter/scalafix/pull/827, we are moving most built-in rules outside of the scalacenter/scalafix repository so that they can evolve at a different pace than Scalafix APIs. As long as a custom rule meets a certain level of quality, it can still

Ping us on Gitter if you believe your rule is good enough for inclusion. Don't hesitate to ask for help around code review or general guidance on writing/sharing custom rules.

Jacke commented 4 years ago

Any progress so far?

vitalii-unzer commented 2 years ago

Would be nice to expand wildcard imports into fully qualified imports.

PawelLipski commented 2 months ago

Hey @olafurpg... is it available as a ready rule in Scalafix already? or at least more easily doable? 🤔

tgodzik commented 2 months ago

I don't think this was ever worked on, it would require some time put into it. Most blockers should be resolved aside from macro issue perhaps?

bjaglin commented 2 months ago

This clearly falls under the scope of OrganizeImports, which is now a built-in rule, so I am re-opening that. Not because there is active work planned on it, but because it should probably be done as a PR here if someone is willing to try.

The behavior could honor coalesceToWildcardImportThreshold to avoid expanding too many qualified imports.