This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(organize-imports): Don't remove exported elements and react calle…
…d functions (#413) Closes #380. Closes #364.
- Loading branch information
Showing
10 changed files
with
530 additions
and
301 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
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
Empty file.
Empty file.
74 changes: 74 additions & 0 deletions
74
test/tests/imports/__snapshots__/import-organizer.test.ts.snap
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,74 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ImportOrganizer import-organizer-file.ts should not remove default exported default imports 1`] = ` | ||
"import Barbaz from './foo'; | ||
export default Barbaz; | ||
" | ||
`; | ||
|
||
exports[`ImportOrganizer import-organizer-file.ts should not remove default exported default imports 2`] = ` | ||
"import Barbaz from './foo'; | ||
export default Barbaz; | ||
" | ||
`; | ||
|
||
exports[`ImportOrganizer import-organizer-file.ts should not remove directly exported default imports 1`] = ` | ||
"import Barbaz from './foo'; | ||
export { Barbaz } | ||
" | ||
`; | ||
|
||
exports[`ImportOrganizer import-organizer-file.ts should not remove directly exported default imports 2`] = ` | ||
"import Barbaz from './foo'; | ||
export { Barbaz } | ||
" | ||
`; | ||
|
||
exports[`ImportOrganizer import-organizer-file.ts should not remove directly exported imports 1`] = ` | ||
"import * as Foobar from './lol'; | ||
import * as Barbaz from './foo'; | ||
export { Foobar, Barbaz } | ||
" | ||
`; | ||
|
||
exports[`ImportOrganizer import-organizer-file.ts should not remove directly exported imports 2`] = ` | ||
"import * as Barbaz from './foo'; | ||
import * as Foobar from './lol'; | ||
export { Foobar, Barbaz } | ||
" | ||
`; | ||
|
||
exports[`ImportOrganizer import-organizer-file.tsx should not remove function that is used in tsx 1`] = ` | ||
"import { f } from './somewhere'; | ||
import * as React from 'react'; | ||
export class Comp extends React.Component { | ||
render() { | ||
return ( | ||
<div>{f()}</div> | ||
); | ||
} | ||
} | ||
" | ||
`; | ||
|
||
exports[`ImportOrganizer import-organizer-file.tsx should not remove function that is used in tsx 2`] = ` | ||
"import * as React from 'react'; | ||
import { f } from './somewhere'; | ||
export class Comp extends React.Component { | ||
render() { | ||
return ( | ||
<div>{f()}</div> | ||
); | ||
} | ||
} | ||
" | ||
`; |
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,143 @@ | ||
import { join } from 'path'; | ||
import { Position, Range, TextDocument, Uri, window, workspace } from 'vscode'; | ||
|
||
import { ImportOrganizer } from '../../../src/imports'; | ||
import ioc from '../../../src/ioc'; | ||
import iocSymbols from '../../../src/ioc-symbols'; | ||
import { expect } from '../setup'; | ||
|
||
describe('ImportOrganizer', () => { | ||
|
||
describe('import-organizer-file.ts', () => { | ||
|
||
const rootPath = workspace.workspaceFolders![0].uri.fsPath; | ||
const file = Uri.file(join(rootPath, 'imports', 'import-organizer-file.ts')); | ||
let document: TextDocument; | ||
let extension: any; | ||
|
||
before(async () => { | ||
document = await workspace.openTextDocument(file); | ||
await window.showTextDocument(document); | ||
|
||
extension = new ImportOrganizer( | ||
ioc.get(iocSymbols.extensionContext), | ||
ioc.get(iocSymbols.logger), | ||
ioc.get(iocSymbols.configuration), | ||
ioc.get(iocSymbols.importManager), | ||
); | ||
}); | ||
|
||
afterEach(async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.delete(new Range( | ||
new Position(0, 0), | ||
document.lineAt(document.lineCount - 1).rangeIncludingLineBreak.end, | ||
)); | ||
}); | ||
}); | ||
|
||
it('should not remove directly exported imports', async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.insert( | ||
new Position(0, 0), | ||
`import * as Foobar from './lol'; | ||
import * as Barbaz from './foo'; | ||
export { Foobar, Barbaz } | ||
`, | ||
); | ||
}); | ||
|
||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
await extension.organizeImports(); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
it('should not remove directly exported default imports', async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.insert( | ||
new Position(0, 0), | ||
`import Barbaz from './foo'; | ||
export { Barbaz } | ||
`, | ||
); | ||
}); | ||
|
||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
await extension.organizeImports(); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
it('should not remove default exported default imports', async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.insert( | ||
new Position(0, 0), | ||
`import Barbaz from './foo'; | ||
export default Barbaz; | ||
`, | ||
); | ||
}); | ||
|
||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
await extension.organizeImports(); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
}); | ||
|
||
describe('import-organizer-file.tsx', () => { | ||
|
||
const rootPath = workspace.workspaceFolders![0].uri.fsPath; | ||
const file = Uri.file(join(rootPath, 'imports', 'import-organizer-file.tsx')); | ||
let document: TextDocument; | ||
let extension: any; | ||
|
||
before(async () => { | ||
document = await workspace.openTextDocument(file); | ||
await window.showTextDocument(document); | ||
|
||
extension = new ImportOrganizer( | ||
ioc.get(iocSymbols.extensionContext), | ||
ioc.get(iocSymbols.logger), | ||
ioc.get(iocSymbols.configuration), | ||
ioc.get(iocSymbols.importManager), | ||
); | ||
}); | ||
|
||
afterEach(async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.delete(new Range( | ||
new Position(0, 0), | ||
document.lineAt(document.lineCount - 1).rangeIncludingLineBreak.end, | ||
)); | ||
}); | ||
}); | ||
|
||
it('should not remove function that is used in tsx', async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.insert( | ||
new Position(0, 0), | ||
`import { f } from './somewhere'; | ||
import * as React from 'react'; | ||
export class Comp extends React.Component { | ||
render() { | ||
return ( | ||
<div>{f()}</div> | ||
); | ||
} | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
await extension.organizeImports(); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
}); | ||
|
||
}); |
Oops, something went wrong.