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

Commit

Permalink
Merge pull request #322 from buehler/develop
Browse files Browse the repository at this point in the history
Release 2017-10-17
  • Loading branch information
buehler committed Oct 17, 2017
2 parents 4b85cf7 + 8019e80 commit d057db6
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 736 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -90,6 +90,7 @@ The following settings do have the prefix `resolver`. So an example setting coul
| multiLineWrapThreshold | The threshold, when imports are converted into multiline imports |
| multiLineTrailingComma | When multiline imports are created, `true` inserts a trailing comma to the last line |
| disableImportSorting | Disable sorting during organize imports action |
| disableImportRemovalOnOrganize | Disable removal unsed imports during organize imports action |
| importGroups | The groups that are used for sorting the imports (description below) |
| ignoreImportsForOrganize | Imports that are never removed during organize import (e.g. react) |
| resolverMode | Which files should be considered to index for TypeScript Hero |
Expand Down
25 changes: 15 additions & 10 deletions package.json
Expand Up @@ -91,30 +91,30 @@
"devDependencies": {
"@types/chai": "^4.0.4",
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.28",
"@types/node": "^8.0.31",
"@types/reflect-metadata": "0.0.5",
"@types/sinon": "^2.3.4",
"@types/sinon": "^2.3.5",
"@types/sinon-chai": "^2.7.29",
"chai": "^4.1.2",
"del-cli": "^1.1.0",
"filewalker": "^0.1.3",
"mocha-testdata": "^1.2.0",
"semantic-release": "^8.0.0",
"semantic-release": "^8.0.3",
"semantic-release-visualstudio-marketplace-version": "^1.0.0",
"sinon": "^3.2.1",
"sinon-chai": "^2.13.0",
"sinon": "^4.0.0",
"sinon-chai": "^2.14.0",
"tslint": "^5.7.0",
"tslint-config-airbnb": "^5.3.0-beta",
"tsutils": "^2.8.2",
"tslint-config-airbnb": "^5.3.0",
"tsutils": "^2.11.1",
"vscode": "^1.1.5"
},
"dependencies": {
"inversify": "^4.3.0",
"inversify-inject-decorators": "^3.0.1",
"reflect-metadata": "^0.1.10",
"tslib": "^1.7.1",
"typescript": "~2.4.2",
"typescript-parser": "^1.3.3"
"typescript": "~2.5.3",
"typescript-parser": "^2.0.0"
},
"activationEvents": [
"onLanguage:typescript",
Expand Down Expand Up @@ -257,9 +257,14 @@
"default": false,
"description": "Defines if sorting is disable during organize imports."
},
"typescriptHero.resolver.disableImportRemovalOnOrganize": {
"type": "boolean",
"default": false,
"description": "Defines, if removal unsed imports is obligatory during organize imports"
},
"typescriptHero.resolver.organizeOnSave": {
"type": "boolean",
"default": true,
"default": false,
"description": "Defines if the imports should be organized on save."
},
"typescriptHero.resolver.ignoreImportsForOrganize": {
Expand Down
8 changes: 8 additions & 0 deletions src/common/config/ResolverConfig.ts
Expand Up @@ -89,6 +89,14 @@ export interface ResolverConfig {
*/
disableImportSorting: boolean;

/**
* Disables removal of the unsed imports on organize.
*
* @type {boolean}
* @memberof ResolverConfig
*/
disableImportRemovalOnOrganize: boolean;

/**
* List of import libraries ("from" part) which are ignored during the organize import function.
*
Expand Down
25 changes: 10 additions & 15 deletions src/common/helpers/DeclarationIndexHelpers.ts
@@ -1,18 +1,10 @@
import { join, normalize, parse, relative } from 'path';
import {
DeclarationInfo,
DefaultDeclaration,
DefaultImport,
ExternalModuleImport,
Import,
NamedImport,
NamespaceImport,
} from 'typescript-parser';
import { DeclarationInfo, ExternalModuleImport, Import, NamedImport, NamespaceImport } from 'typescript-parser';

/**
* Calculates a list of declarationInfos filtered by the already imported ones in the given document.
* The result is a list of declarations that are not already imported by the document.
*
*
* @export
* @param {ResolveIndex} resolveIndex
* @param {string} documentPath
Expand All @@ -35,11 +27,14 @@ export function getDeclarationsFilteredByImports(
declarations = declarations
.filter(o => o.from !== importedLib || !(tsImport as NamedImport).specifiers
.some(s => s.specifier === o.declaration.name));
// if (tsImport.defaultAlias) {
// else if (tsImport instanceof DefaultImport) {
// declarations = declarations
// .filter(o => (!(o.declaration instanceof DefaultDeclaration) || importedLib !== o.from));
// }
// }
} else if (tsImport instanceof NamespaceImport || tsImport instanceof ExternalModuleImport) {
declarations = declarations.filter(o => o.from !== tsImport.libraryName);
} else if (tsImport instanceof DefaultImport) {
declarations = declarations
.filter(o => (!(o.declaration instanceof DefaultDeclaration) || importedLib !== o.from));
}
}

Expand All @@ -51,7 +46,7 @@ export function getDeclarationsFilteredByImports(
* If the library is a node module or a typings module, the name
* is returned. If the "lib" is in the local workspace, then the
* absolut path from the workspace root is returned.
*
*
* @param {string} library Name of the library
* @param {string} actualFilePath Filepath of the actually open file
* @param {string} [rootPath] Root path of the workspace
Expand All @@ -72,7 +67,7 @@ export function getAbsolutLibraryName(library: string, actualFilePath: string, r
* If the library is a node module or a typings module, the name
* is returned. If the "lib" is in the local workspace, then the
* relative path from the actual file is returned.
*
*
* @param {string} library Name of the library
* @param {string} actualFilePath Filepath of the actually open file
* @param {string} [rootPath] Root path of the workspace
Expand Down
12 changes: 12 additions & 0 deletions src/extension/VscodeExtensionConfig.ts
Expand Up @@ -174,6 +174,18 @@ class VscodeResolverConfig implements ResolverConfig {
return value !== undefined ? value : false;
}

/**
* Defines, if removal unsed is obligatory during organize imports
*
* @readonly
* @type {boolean}
* @memberof ResolverConfig
*/
public get disableImportRemovalOnOrganize(): boolean {
const value = this.workspaceSection.get<boolean>('resolver.disableImportRemovalOnOrganize');
return value !== undefined ? value : false;
}

/**
* Returns the tab size that is configured in vscode.
*
Expand Down
24 changes: 3 additions & 21 deletions src/extension/extension.ts
@@ -1,12 +1,11 @@
import 'reflect-metadata';

import { DefaultImport, GENERATORS, NamedImport, SymbolSpecifier, TypescriptCodeGenerator } from 'typescript-parser';
import { GENERATORS, TypescriptCodeGenerator } from 'typescript-parser';
import { Disposable, ExtensionContext } from 'vscode';

import { KeywordImportGroup, RegexImportGroup, RemainImportGroup } from './import-grouping';
import { Container } from './IoC';
import { iocSymbols } from './IoCSymbols';
import { ImportProxy } from './proxy-objects/ImportProxy';
import { TypeScriptHero } from './TypeScriptHero';

let extension: Disposable;
Expand All @@ -25,28 +24,11 @@ function extendGenerator(generator: TypescriptCodeGenerator): void {
GENERATORS[KeywordImportGroup.name] = simpleGenerator;
GENERATORS[RegexImportGroup.name] = simpleGenerator;
GENERATORS[RemainImportGroup.name] = simpleGenerator;
GENERATORS[ImportProxy.name] = (proxy: ImportProxy) => {
if (proxy.specifiers.length <= 0 && (proxy.defaultAlias || proxy.defaultPurposal)) {
return generator.generate(
new DefaultImport(
proxy.libraryName, (proxy.defaultAlias || proxy.defaultPurposal)!, proxy.start, proxy.end,
),
);
}
if (proxy.defaultAlias) {
proxy.specifiers.push(new SymbolSpecifier('default', proxy.defaultAlias));
}
const named = new NamedImport(
proxy.libraryName, proxy.start, proxy.end,
);
named.specifiers = proxy.specifiers;
return generator.generate(named);
};
}

/**
* Activates TypeScript Hero
*
*
* @export
* @param {ExtensionContext} context
*/
Expand All @@ -61,7 +43,7 @@ export async function activate(context: ExtensionContext): Promise<void> {

/**
* Deactivates TypeScript Hero
*
*
* @export
*/
export function deactivate(): void {
Expand Down

0 comments on commit d057db6

Please sign in to comment.