Skip to content
This repository has been archived by the owner on Oct 10, 2018. It is now read-only.

Commit

Permalink
feat(import management): Merge imports from the same package (#392)
Browse files Browse the repository at this point in the history
Does automatically merge imports from the same package into one.
  • Loading branch information
shobhitg authored and buehler committed Feb 6, 2018
1 parent 9feb1e3 commit abc45f1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"editor.tabSize": 4
"editor.tabSize": 2
}
13 changes: 12 additions & 1 deletion src/import-organizer/import-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,20 @@ export default class ImportManager {
.filter(o => this._parsedDocument.nonLocalUsages.indexOf(o.alias || o.specifier) > -1)
.sort(specifierSort);
const defaultSpec = actImport.defaultAlias;
const libraryAlreadyImported = keep.find(d => d.libraryName === actImport.libraryName);
if (actImport.specifiers.length ||
(!!defaultSpec && this._parsedDocument.nonLocalUsages.indexOf(defaultSpec) >= 0)) {
keep.push(actImport);
if (libraryAlreadyImported) {
if (actImport.defaultAlias) {
(<NamedImport>libraryAlreadyImported).defaultAlias = actImport.defaultAlias;
}
(<NamedImport>libraryAlreadyImported).specifiers = [
...(<NamedImport>libraryAlreadyImported).specifiers,
...actImport.specifiers,
];
} else {
keep.push(actImport);
}
}
} else if (actImport instanceof StringImport) {
keep.push(actImport);
Expand Down
6 changes: 4 additions & 2 deletions test/etc/workspace_1/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import iocSymbols from './ioc-symbols';
import { Foo } from './server/indices/foobar';
import { Loggee } from './utilities/Logger';
import winstonLogger, { Logger } from './utilities/Logger';

import iocSymbols from './ioc-symbols';

function foo() {
console.log(winstonLogger, Logger, iocSymbols);
console.log(winstonLogger, Logger, Loggee, iocSymbols);
}

Foo

0 comments on commit abc45f1

Please sign in to comment.