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.
fix(organize-imports): Don't reorder shebang and block comment (#415)
Fixes #409. Fixes #412. Add shebang and js block comment to the excluded heading lines.
- Loading branch information
Showing
7 changed files
with
222 additions
and
3 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
Empty file.
50 changes: 50 additions & 0 deletions
50
test/tests/imports/__snapshots__/import-appender.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,50 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ImportAppender import-appender-file.ts should add a double normal named import 1`] = ` | ||
"import { MyClass, MyClass2 } from '../lib'; | ||
" | ||
`; | ||
|
||
exports[`ImportAppender import-appender-file.ts should add a normal default import 1`] = ` | ||
"import defaultDec from '../lib'; | ||
" | ||
`; | ||
|
||
exports[`ImportAppender import-appender-file.ts should add a normal named import 1`] = ` | ||
"import { MyClass } from '../lib'; | ||
" | ||
`; | ||
|
||
exports[`ImportAppender import-appender-file.ts should add an import below a block comment 1`] = ` | ||
"/* | ||
* copy right by me! | ||
*/ | ||
import { MyClass } from '../lib'; | ||
" | ||
`; | ||
|
||
exports[`ImportAppender import-appender-file.ts should add an import below a comment 1`] = ` | ||
"// my fancy comment | ||
import { MyClass } from '../lib'; | ||
" | ||
`; | ||
|
||
exports[`ImportAppender import-appender-file.ts should add an import below a jsdoc comment 1`] = ` | ||
"/** | ||
* js documentation! | ||
*/ | ||
import { MyClass } from '../lib'; | ||
" | ||
`; | ||
|
||
exports[`ImportAppender import-appender-file.ts should add an import below a shebang 1`] = ` | ||
"#! /usr/bin/env node | ||
import { MyClass } from '../lib'; | ||
" | ||
`; | ||
|
||
exports[`ImportAppender import-appender-file.ts should add an import below a use strict 1`] = ` | ||
"\\"use strict\\" | ||
import { MyClass } from '../lib'; | ||
" | ||
`; |
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,141 @@ | ||
import { join } from 'path'; | ||
import { ClassDeclaration, DeclarationInfo, DefaultDeclaration, File } from 'typescript-parser'; | ||
import { Position, Range, TextDocument, Uri, window, workspace } from 'vscode'; | ||
|
||
import { ImportAppender } from '../../../src/imports'; | ||
import ioc from '../../../src/ioc'; | ||
import iocSymbols from '../../../src/ioc-symbols'; | ||
import { expect } from '../setup'; | ||
|
||
describe('ImportAppender', () => { | ||
|
||
describe('import-appender-file.ts', () => { | ||
|
||
const rootPath = workspace.workspaceFolders![0].uri.fsPath; | ||
const file = Uri.file(join(rootPath, 'imports', 'import-appender-file.ts')); | ||
let document: TextDocument; | ||
let extension: { addImportToDocument(declaration: DeclarationInfo): Promise<boolean> }; | ||
|
||
before(async () => { | ||
document = await workspace.openTextDocument(file); | ||
await window.showTextDocument(document); | ||
|
||
extension = new ImportAppender( | ||
ioc.get(iocSymbols.extensionContext), | ||
ioc.get(iocSymbols.logger), | ||
ioc.get(iocSymbols.importManager), | ||
ioc.get(iocSymbols.declarationManager), | ||
ioc.get(iocSymbols.parser), | ||
) as any; | ||
}); | ||
|
||
afterEach(async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.delete(new Range( | ||
new Position(0, 0), | ||
document.lineAt(document.lineCount - 1).rangeIncludingLineBreak.end, | ||
)); | ||
}); | ||
}); | ||
|
||
it('should add a normal named import', async () => { | ||
await extension.addImportToDocument({ | ||
from: '/lib', | ||
declaration: new ClassDeclaration('MyClass', true, 0, 0), | ||
}); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
it('should add a double normal named import', async () => { | ||
await extension.addImportToDocument({ | ||
from: '/lib', | ||
declaration: new ClassDeclaration('MyClass', true, 0, 0), | ||
}); | ||
await extension.addImportToDocument({ | ||
from: '/lib', | ||
declaration: new ClassDeclaration('MyClass2', true, 0, 0), | ||
}); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
it('should add a normal default import', async () => { | ||
await extension.addImportToDocument({ | ||
from: '/lib', | ||
declaration: new DefaultDeclaration('defaultDec', new File(file.fsPath, rootPath, 0, 0), 0, 0), | ||
}); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
it('should add an import below a comment', async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.insert( | ||
new Position(0, 0), | ||
`// my fancy comment\n`, | ||
); | ||
}); | ||
await extension.addImportToDocument({ | ||
from: '/lib', | ||
declaration: new ClassDeclaration('MyClass', true, 0, 0), | ||
}); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
it('should add an import below a block comment', async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.insert( | ||
new Position(0, 0), | ||
`/* \n * copy right by me!\n */\n`, | ||
); | ||
}); | ||
await extension.addImportToDocument({ | ||
from: '/lib', | ||
declaration: new ClassDeclaration('MyClass', true, 0, 0), | ||
}); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
it('should add an import below a jsdoc comment', async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.insert( | ||
new Position(0, 0), | ||
`/** \n * js documentation!\n */\n`, | ||
); | ||
}); | ||
await extension.addImportToDocument({ | ||
from: '/lib', | ||
declaration: new ClassDeclaration('MyClass', true, 0, 0), | ||
}); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
it('should add an import below a shebang', async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.insert( | ||
new Position(0, 0), | ||
`#! /usr/bin/env node\n`, | ||
); | ||
}); | ||
await extension.addImportToDocument({ | ||
from: '/lib', | ||
declaration: new ClassDeclaration('MyClass', true, 0, 0), | ||
}); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
it('should add an import below a use strict', async () => { | ||
await window.activeTextEditor!.edit((builder) => { | ||
builder.insert( | ||
new Position(0, 0), | ||
`"use strict"\n`, | ||
); | ||
}); | ||
await extension.addImportToDocument({ | ||
from: '/lib', | ||
declaration: new ClassDeclaration('MyClass', true, 0, 0), | ||
}); | ||
expect(window.activeTextEditor!.document.getText()).to.matchSnapshot(); | ||
}); | ||
|
||
}); | ||
|
||
}); |
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