Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose option to skip destructive code actions #69

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,25 @@ const withOrganizeImportsPreprocess = (parser) => {
};
};

exports.parsers = {
babel: withOrganizeImportsPreprocess(babelParsers.babel),
'babel-ts': withOrganizeImportsPreprocess(babelParsers['babel-ts']),
typescript: withOrganizeImportsPreprocess(typescriptParsers.typescript),
vue: withOrganizeImportsPreprocess(htmlParsers.vue),
/**
* @type {import('prettier').Plugin}
*/
const plugin = {
options: {
organizeImportsSkipDestructiveCodeActions: {
type: 'boolean',
default: false,
category: 'OrganizeImports',
description: 'Skip destructive code actions like removing unused imports.',
since: '2.0.0',
},
},
parsers: {
babel: withOrganizeImportsPreprocess(babelParsers.babel),
'babel-ts': withOrganizeImportsPreprocess(babelParsers['babel-ts']),
typescript: withOrganizeImportsPreprocess(typescriptParsers.typescript),
vue: withOrganizeImportsPreprocess(htmlParsers.vue),
},
};

module.exports = plugin;
11 changes: 9 additions & 2 deletions lib/organize.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const { getLanguageServiceHost, getVueLanguageServiceHost } = require('./service
* @param {string} code
* @param {import('prettier').ParserOptions} options
*/
module.exports.organize = (code, { filepath = 'file.ts', parentParser, parser }) => {
module.exports.organize = (
code,
{ filepath = 'file.ts', organizeImportsSkipDestructiveCodeActions, parentParser, parser },
) => {
if (parentParser === 'vue') {
return code; // we do the preprocessing from the `vue` parent parser instead, so we skip the child parsers
}
Expand All @@ -23,7 +26,11 @@ module.exports.organize = (code, { filepath = 'file.ts', parentParser, parser })
languageService = require('typescript').createLanguageService(getLanguageServiceHost(filepath, code));
}

const fileChanges = languageService.organizeImports({ type: 'file', fileName: filepath }, {}, {})[0];
const fileChanges = languageService.organizeImports(
{ type: 'file', fileName: filepath, skipDestructiveCodeActions: organizeImportsSkipDestructiveCodeActions },
{},
{},
)[0];

return fileChanges ? applyTextChanges(code, fileChanges.textChanges) : code;
};
8 changes: 8 additions & 0 deletions prettier.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export declare module 'prettier' {
interface Options {
organizeImportsSkipDestructiveCodeActions?: boolean;
}
interface ParserOptions {
organizeImportsSkipDestructiveCodeActions?: boolean;
}
}
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ 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.

If you don't want destructive code actions (like removing unused imports), you can enable the option `organizeImportsSkipDestructiveCodeActions` via your Prettier config.

```js
// prettierrc.js

module.exports = {
// ...
organizeImportsSkipDestructiveCodeActions: true,
};
```

**Notes:**

- Automatic plugin discovery is not supported with some package managers (e. g. [Yarn 2](https://github.com/prettier/prettier/issues/8474)). Check the [Prettier documentation](https://prettier.io/docs/en/plugins.html) for alternatives to manually load plugins in that case.
Expand Down
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,12 @@ import { NDivider } from "naive-ui";

t.is(formattedCode, code);
});

test('does not remove unused imports with `organizeImportsSkipDestructiveCodeActions` enabled', (t) => {
const code = `import { foo } from "./bar";
`;

const formattedCode = prettify(code, { organizeImportsSkipDestructiveCodeActions: true });

t.is(formattedCode, code);
});