This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Option to allow first-specifier/alias import ordering (#336)
Many users (e.g. me, or people at #316) like their imports sorted not by library path, but by first specifier/alias name. This adds a `typescriptHero.resolver.organizeSortsByFirstSpecifier` boolean option, defaulting to `false`, that enables just such a behavior. Import grouping is unaffected: sorting happens within groups. Sorting is locale-aware, by the way, so not just lexicographical. When looking for specifier-ordering, we usually are in a "human language" mindset, so case shifts and diacritics should work in natural order.
- Loading branch information
Showing
12 changed files
with
226 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.tabSize": 4 | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
6 changes: 6 additions & 0 deletions
6
test/_workspace/extension/utilities/importsForSpecifierSort.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import 'coolEffectLib'; | ||
import './workspaceSideEffectLib'; | ||
import { Foobar, Genero } from './myFile'; | ||
import { AnotherFoobar } from './anotherFile'; | ||
import ModuleFoobar from 'myLib'; | ||
import { AnotherModuleFoo as MuchFurtherSorted } from 'anotherLib'; |
This file contains 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
This file contains 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
65 changes: 48 additions & 17 deletions
65
test/single-workspace-tests/extension/utilities/utilityFunctions.test.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,56 @@ | ||
import * as chai from 'chai'; | ||
chai.should(); | ||
import { Container } from '../../../../src/extension/IoC'; | ||
import { TypescriptParser } from 'typescript-parser'; | ||
import { iocSymbols } from '../../../../src/extension/IoCSymbols'; | ||
import { join } from 'path'; | ||
import { workspace } from 'vscode'; | ||
|
||
import { KeywordImportGroup, ImportGroupKeyword, RegexImportGroup } from '../../../../src/extension/import-grouping'; | ||
import { importGroupSortForPrecedence } from '../../../../src/extension/utilities/utilityFunctions'; | ||
import { importGroupSortForPrecedence, importSortByFirstSpecifier } from '../../../../src/extension/utilities/utilityFunctions'; | ||
|
||
chai.should(); | ||
|
||
describe('utilityFunctions', () => { | ||
describe('importGroupSortForPrecedence', () => { | ||
it('should prioritize regexes, leaving original order untouched besides that', () => { | ||
const initialList = [ | ||
new KeywordImportGroup(ImportGroupKeyword.Modules), | ||
new KeywordImportGroup(ImportGroupKeyword.Plains), | ||
new RegexImportGroup("/cool-library/"), | ||
new RegexImportGroup("/cooler-library/"), | ||
new KeywordImportGroup(ImportGroupKeyword.Workspace), | ||
] | ||
const expectedList = initialList.slice(2, 4) | ||
.concat(initialList.slice(0, 2)) | ||
.concat(initialList.slice(4)) | ||
describe('importGroupSortForPrecedence', () => { | ||
it('should prioritize regexes, leaving original order untouched besides that', () => { | ||
const initialList = [ | ||
new KeywordImportGroup(ImportGroupKeyword.Modules), | ||
new KeywordImportGroup(ImportGroupKeyword.Plains), | ||
new RegexImportGroup("/cool-library/"), | ||
new RegexImportGroup("/cooler-library/"), | ||
new KeywordImportGroup(ImportGroupKeyword.Workspace), | ||
] | ||
const expectedList = initialList.slice(2, 4) | ||
.concat(initialList.slice(0, 2)) | ||
.concat(initialList.slice(4)) | ||
|
||
importGroupSortForPrecedence(initialList).should.deep.equal(expectedList, | ||
'Regex Import Groups should appear first (and that should be the only change)') | ||
}); | ||
}); | ||
|
||
describe('importSortByFirstSpecifier', () => { | ||
const parser = Container.get<TypescriptParser>(iocSymbols.typescriptParser); | ||
const rootPath = workspace.workspaceFolders![0].uri.fsPath; | ||
|
||
it('should sort according to first specifier/alias, falling back to module path', async () => { | ||
const file = await parser.parseFile( | ||
join( | ||
rootPath, | ||
'extension/utilities/importsForSpecifierSort.ts', | ||
), | ||
rootPath, | ||
); | ||
|
||
importGroupSortForPrecedence(initialList).should.deep.equal(expectedList, | ||
'Regex Import Groups should appear first (and that should be the only change)') | ||
const result = [...file.imports].sort(importSortByFirstSpecifier); | ||
result.map((i) => i.libraryName).should.deep.equal([ | ||
'./anotherFile', // { AnotherFoobar } | ||
'coolEffectLib', // 'coolEffectLib' | ||
'./myFile', // { Foobar, Genero } | ||
'myLib', // ModuleFoobar | ||
'anotherLib', // { AnotherModuleFoo as MuchFurtherSorted } | ||
'./workspaceSideEffectLib', // './workspaceSideEffectLib | ||
]); | ||
}); | ||
}); | ||
}); | ||
}); |