[Use fewer imports]: combine field imports for same imported type, class, etc. #1133#1654
Draft
Morabbin wants to merge 2 commits intondmitchell:masterfrom
Draft
[Use fewer imports]: combine field imports for same imported type, class, etc. #1133#1654Morabbin wants to merge 2 commits intondmitchell:masterfrom
Morabbin wants to merge 2 commits intondmitchell:masterfrom
Conversation
…, class ndmitchell#1133 # Summary Combine multiple fields for any imported type, constructor, or class (this is called a "thing" in the GHC API). Suppose a type, constructor, or class with fields will appear in at most one non-hiding import declaration (as guaranteed by `simplify`). The "use fewer imports" hint will combine multiple field imports into a single import list element. Hiding imports are left untouched. For example: ```haskell import A (Foo(A,B), Bar(C,D), Foo(E), Foo(C,D)) import A (Foo(A,F), Baz(D,A)) ``` would first `simplify` to: ```haskell import A (Foo(A,B), Bar(C,D), Foo(E), Foo(C,D), Foo(A,F), Baz(D,A)) ``` and then `simplifyFields` would yield: ```haskell import A (Foo(A,B,C,D,E,F), Bar(C,D), Baz(D,A)) ``` # Test Plan ## Unit tests Added several cases to the <TEST> section in Hint.Import: ```sh ~/Work/hlint $ cabal run -- hlint --test ... Testing (with refactoring) Source annotations .................... Input/outputs .......................................................................................................................................................................... Hint names ........ Hint annotations ........ Tests passed (975) ``` ## Running this hlint on hlint src ```sh ~/Work/hlint $ cabal install exe:hlint --overwrite-policy=always Resolving dependencies... Build profile: -w ghc-9.12.2 -O1 In order, the following will be built (use -v for more details): - hlint-3.10 (lib) (requires build) - hlint-3.10 (exe:hlint) (requires build) Starting hlint-3.10 (lib) ... Completed hlint-3.10 (exe:hlint) Symlinking 'hlint' to '/Users/aam/.cabal/bin/hlint' ~/Work/hlint $ cabal run hlint -- src --report Writing report to report.html ... No hints ```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Combine multiple fields for any imported type, constructor, or class (this is called a "thing" in the GHC API).
Suppose a type, constructor, or class with fields will appear in at most one non-hiding import declaration (as guaranteed by
simplify). The "use fewer imports" hint will combine multiple field imports into a single import list element.Hiding imports are left untouched.
For example:
would first
simplifyto:and then
simplifyFieldswould yield:To sort or not to sort?
Note that sorting occurs only when field imports are combined.
The import list itself preserves the order of the types, etc. in the original imports. It also leaves field imports unsorted if there's only one import list element for that type, class, or constructor. The latter is easy (just don't change it), but the former requires a kind of stable nub.
We could use a similar stable nub to preserve the field import order from the original too.
Is that worth doing?
Test Plan
Unit tests
Added several cases to the section in Hint.Import:
~/Work/hlint $ cabal run -- hlint --test ... Testing (with refactoring) Source annotations .................... Input/outputs .......................................................................................................................................................................... Hint names ........ Hint annotations ........ Tests passed (975)Running this hlint on hlint src
~/Work/hlint $ cabal run hlint -- src --report Writing report to report.html ... No hints