Skip to content

Commit

Permalink
feat: add support for // organize-imports-ignore comments (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorn0 authored Jun 19, 2020
1 parent e1c0beb commit 039c178
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const ts = require('typescript');
* @param {import('prettier').Options} options
*/
const organizeImports = (text, options) => {
if (text.includes('// organize-imports-ignore') || text.includes('// tslint:disable:ordered-imports')) {
return text;
}

const fileName = options.filepath || 'file.ts';

const languageService = ts.createLanguageService(new ServiceHost(fileName, text));
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ _`prettier` and `typescript` are peer dependencies, so make sure you have those

The plugin will be loaded by Prettier automatically. No configuration needed.

Files containing the substring `// organize-imports-ignore` or `// tslint:disable:ordered-imports` are skipped.

## License

[MIT](/license).
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,16 @@ test('works without a filepath', (t) => {

t.is(formattedCode.split('\n')[0], 'import { bar, foo } from "foobar";');
});

test('files with `// organize-imports-ignore` are skipped', (t) => {
const code = `
// organize-imports-ignore
import { foo, bar } from "foobar"
export const foobar = foo + bar
`;

const formattedCode = prettify(code);

t.is(formattedCode.split('\n')[1], 'import { foo, bar } from "foobar";');
});

0 comments on commit 039c178

Please sign in to comment.