This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
Do not throw exception when imports are collapsed to wildcard import #140
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It is kind of difficult to describe why this exception is thrown and the
fact that it took me an entire working day to fix it should speak for
itself. But I will try nevertheless to give a description:
The problem starts in the class
OrganizeImports.AlwaysUseWildcards
. Ifthe "collapse to wildcard import" option is enabled, this class replaces
all imports that are matched by a wildcard import with such a wildcard
import. Even though the wildcard import is generated it has positions
since the position of the first matching import are set to it. This
means that
ReusingPrinter
is used to print the import. However, inorder to print import selectors the refactoring logic needs to generate
its own trees, called
ImportSelectorTree
. This is necessary, sinceimport selectors are not trees by default and therefore it wouldn't be
easily possible to print them through the current tree printing
algorithm. Since the
ImportSelectorTree
is synthetic, it doesn't havepositions and is therefore forwarded to
PrettyPrinter
, thereforePrettyPrinter
contains the bug fix.I didn't add the same logic to ReusingPrinter, since I don't see how
such an implementation could ever be called there (there can't be a
reusing part in a synthetic tree). Furthermore, the fix is more
powerful, than what the test can cover. In theory it can even print
rename imports but I was not able to come up with a test case that would
invoke this logic, therefore we have no guarantee that this part of the
implementation is correct or even necessary.
Fixes #1002654