scalacenter / scalafix

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

Reproduce lexical scope to resolve name to symbol at a given position #502

Closed olafurpg closed 4 years ago

olafurpg commented 6 years ago

To properly implement ExplicitResultTypes #324 scalafix needs a way to confidently be able to insert a type name like Future knowing it will resolve to scala.concurrent.Future instead of some other com.bar.Future that has been imported in the enclosing scope.

With scalameta v2.1.2, Denotation.members: List[Signature] is populated for wildcard imports and Template parent constructors with scalacOption -P:semanticdb:members:all. This information should be sufficient used to reproduce lexical scoping rules of the Scala spec.

The most immediate application of this issue in scalafix would be to remove the ExplicitResultTypes.unsafeShortenName: Boolean option in favor of inserting short names by default as long as they resolve to the correct symbol. The place this can be accomplished is here https://github.com/scalacenter/scalafix/blob/e4a5c35d676aee196edd645a9f860330606ebfe7/scalafix-core/shared/src/main/scala/scalafix/internal/util/TypeSyntax.scala#L44-L55 Instead of blindly shortening names if unsafeShortenNames is enabled, we lookup the name in the scope and shorten if the name resolves to the correct symbol OR the name does not resolve to any symbol. In the future, we can accommodate more advanced functionality such a configuring that mutable collections should be prefixed with mutable or when a package should be renamed on import, etc.

olafurpg commented 4 years ago

Fixed in #989