Skip to content

Commit

Permalink
Fix RemoveUnusedImportProvider
Browse files Browse the repository at this point in the history
Fix its behaviour in regards to removal from import lists contaning constructors, e.g. `Sum(..)`
  • Loading branch information
EduardSergeev committed May 18, 2024
1 parent 7d70145 commit bbe5701
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions input/after/UnusedImportProvider.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Data.List (sort)
import Data.Monoid (Sum(..))

foo :: Ord a => [a] -> [a]
foo xs =
sort xs

bar :: Sum a -> a
bar (Sum a) = a
4 changes: 4 additions & 0 deletions input/before/UnusedImportProvider.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Data.List (sort, tails)
import Data.Maybe
import Data.Monoid (All(..), Any(..), Sum(..))

foo :: Ord a => [a] -> [a]
foo xs =
sort xs

bar :: Sum a -> a
bar (Sum a) = a
2 changes: 1 addition & 1 deletion src/features/importProvider/importDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class ImportDeclaration {

public removeElement(elem: string) {
const before = this.importElements;
const index = this.importNames.findIndex(oldElem => oldElem === elem);
const index = this.importNames.findIndex(oldElem => oldElem === elem || oldElem == `${elem}(..)`);
this._importElements.splice(index, 1);
if(index === 0 && this._importElements.length > 0) {
this._importElements[0] = this._importElements[0].trimLeft();
Expand Down
2 changes: 1 addition & 1 deletion src/features/removeUnusedImportProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default class RemoveUnusedImportProvider implements CodeActionProvider {
return diagnostics
.map(d => [
d.range,
d.message.match(/The (qualified )?import of (?:[`‘](.+?)['’]\s+from module )?[`‘](.+?)['’] is redundant/m)
d.message.match(/The (qualified )?import of (?:[`‘]([\s\S]+?)['’]\s+from module )?[`‘](.+?)['’] is redundant/m)
] as const)
.filter(([,m]) => m)
.map(([range, match]) => [
Expand Down
2 changes: 1 addition & 1 deletion src/test/providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ suite('', () => {
});

test('Remove unused imports', () => {
return runQuickfixTest('UnusedImportProvider.hs', 2);
return runQuickfixTest('UnusedImportProvider.hs', 3);
});

test('Add missing extension', () => {
Expand Down

0 comments on commit bbe5701

Please sign in to comment.