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

Commit

Permalink
fix(organize-imports): Don't reorder shebang and block comment (#415)
Browse files Browse the repository at this point in the history
Fixes #409. Fixes #412. Add shebang and js block comment
to the excluded heading lines.
  • Loading branch information
buehler committed Mar 6, 2018
1 parent 6dcc852 commit a83c410
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Expand Up @@ -50,7 +50,7 @@
"env": {
"EXT_DEBUG": "true",
"LOCAL_TEST": "true",
"COVERAGE": "true",
"COVERAGE": "",
"CHAI_JEST_SNAPSHOT_UPDATE_ALL": ""
},
"stopOnEntry": false,
Expand All @@ -74,7 +74,7 @@
"env": {
"EXT_DEBUG": "true",
"LOCAL_TEST": "true",
"COVERAGE": "true",
"COVERAGE": "",
"CHAI_JEST_SNAPSHOT_UPDATE_ALL": ""
},
"stopOnEntry": false,
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/utility-functions.ts
Expand Up @@ -320,7 +320,7 @@ export function getRelativeLibraryName(library: string, actualFilePath: string,
return toPosix(relativePath);
}

const REGEX_IGNORED_LINE = /^\s*(?:\/\/|\/\*\*|\*\/|\*|(['"])use strict\1)/;
const REGEX_IGNORED_LINE = /^\s*(?:\/\/|\/\*|\*\/|\*|#!|(['"])use strict\1)/;

/**
* Calculate the position, where a new import should be inserted.
Expand Down
Empty file.
50 changes: 50 additions & 0 deletions test/tests/imports/__snapshots__/import-appender.test.ts.snap
@@ -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';
"
`;
141 changes: 141 additions & 0 deletions test/tests/imports/import-appender.test.ts
@@ -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();
});

});

});
14 changes: 14 additions & 0 deletions test/tests/utilities/__snapshots__/utility-functions.test.ts.snap
Expand Up @@ -7,6 +7,13 @@ Object {
}
`;

exports[`utility functions getImportInsertPosition() should return correct position for js block comment open 1`] = `
Object {
"character": 0,
"line": 1,
}
`;

exports[`utility functions getImportInsertPosition() should return correct position for jsdoc comment close 1`] = `
Object {
"character": 0,
Expand All @@ -28,6 +35,13 @@ Object {
}
`;

exports[`utility functions getImportInsertPosition() should return correct position for shebang (#!) 1`] = `
Object {
"character": 0,
"line": 1,
}
`;

exports[`utility functions getImportInsertPosition() should return correct position for use strict 1`] = `
Object {
"character": 0,
Expand Down
14 changes: 14 additions & 0 deletions test/tests/utilities/utility-functions.test.ts
Expand Up @@ -57,6 +57,13 @@ describe('utility functions', () => {
expect(pos).to.matchSnapshot();
});

it('should return correct position for js block comment open', () => {
const pos = getImportInsertPosition({
document: new MockDocument('/* yay\n'),
} as any);
expect(pos).to.matchSnapshot();
});

it('should return correct position for jsdoc comment line', () => {
const pos = getImportInsertPosition({
document: new MockDocument(' * jsdoc line\n'),
Expand All @@ -71,6 +78,13 @@ describe('utility functions', () => {
expect(pos).to.matchSnapshot();
});

it('should return correct position for shebang (#!)', () => {
const pos = getImportInsertPosition({
document: new MockDocument('#!\n'),
} as any);
expect(pos).to.matchSnapshot();
});

});

describe('importGroupSortForPrecedence', () => {
Expand Down

0 comments on commit a83c410

Please sign in to comment.