3.2.4 • Published 5 months ago

prettier-plugin-organize-imports v3.2.4

Weekly downloads
79,513
License
MIT
Repository
github
Last release
5 months ago

Tests

Prettier Plugin: Organize Imports

Make sure that your import statements stay consistent no matter who writes them and what their preferences are.

A plugin that makes Prettier organize your imports (i. e. sorts, combines and removes unused ones) using the organizeImports feature of the TypeScript language service API. This is the same as using the "Organize Imports" action in VS Code.

Features

  • 👌 Dependency-free (just peer-dependencies you probably already have).
  • 💪 Supports .js, .jsx, .ts, .tsx and .vue files.
  • 🚀 Zero config.
  • 🤓 No more weird diffs or annoying merge conflicts in PRs caused by import statements.
  • 🤯 If your editor supports auto-imports, you'll stop thinking about your imports so much that you won't even care about their order anymore.

Caveat

This plugin inherits, extends, and then overrides the built-in Prettier parsers for babel, babel-ts, typescript and vue. This means that it is incompatible with other plugins that do the same; only the last loaded plugin that exports one of those parsers will function.

Installation

npm install --save-dev prettier-plugin-organize-imports

Note that prettier and typescript are peer dependencies, so make sure you have those installed in your project.

Usage

Prettier 3

Automatic plugin discovery has been removed. Thus you need to configure Prettier to use the plugin according to the Plugins docs, for example by adding it to the plugins config option:

{
  "plugins": ["prettier-plugin-organize-imports"]
}

Prettier 2

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

Note that automatic plugin discovery is not supported with some package managers, e. g. Yarn PnP (see https://github.com/prettier/prettier/issues/8474). In that case follow the instructions for Prettier 3 above.

Configuration

Skip Files

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

Skip Destructive Code Actions

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

{
  "organizeImportsSkipDestructiveCodeActions": true
}

Compatibility

ESLint

For compatibility with ESLint or other linters, see "Integrating with Linters" in the Prettier docs. You should have any import order rules/plugins disabled.

React

Depending on your configuration, if you need the React import to stay even if it's "unused" (i.e. only needed for the JSX factory), make sure to have the jsx option set to react in your tsconfig.json. For more details click here.

Vue.js

Make sure that you have the optional peer dependency @volar/vue-typescript installed.

npm i --save-dev @volar/vue-typescript

If you're using Vue.js with Pug templates, you'll also need to install @volar/vue-language-plugin-pug as a dev dependency, and configure it in vueCompilerOptions (see usage).

Debug Logs

If something doesn't work, you can try to prefix your prettier command with DEBUG=true which will enable this plugin to print some logs.

Rationale/Disclaimer

This plugin acts outside of Prettier's scope because "Prettier only prints code. It does not transform it.", and technically sorting is a code transformation because it changes the AST (this plugin even removes code, i. e. unused imports). In my opinion however, the import statements are not really part of the code, they are merely directives that instruct the module system where to find the code (only true as long as your imports are side-effects free regarding the global scope, i. e. import order doesn't matter), comparable with using directives in C# or #include preprocessing directives in C. Therefore the practical benefits outweigh sticking with the philosophy in this case.

Changelog

Version 3.2.4 implements a fix to skip when formatting ranges (i.e. if the plugin is requested to format a range, it doesn't do anything because it would lack the full file context).

Version 3.2.3 updates the readme with instructions for Prettier 3.

Version 3.2.2 fixes a performance regression introduced in 3.2.0.

Version 3.2.1 fixes the implementation of the language service host's getCurrentDirectory method to return the directory containing the tsconfig, rather than using ts.sys.getCurrentDirectory (which returns process.cwd()). This should prevent issues with resolving compiler plugins with Volar (which is used for Vue support).

Version 3.2.0 adds and fixes support for pug templates in Vue files (via @volar/vue-language-plugin-pug). Please be aware that you'll need to update your version of the @volar/vue-typescript peer dependency from 0.x to 1.x.

Version 3.1.0 adds an option to skip destructive code actions like removing unused imports.

Version 3.0.3 fixes a performance regression introduced in 3.0.2.

Version 3.0.2 fixes a regression introduced by adding some file-system related methods to the language service host (to fix a bug), which revealed that another method's implementation was incorrect.

Version 3.0.1 bumps the @volar/vue-typescript version to fix more edge cases, e. g. not removing imports when a component is used via kebab-case naming. @volar/vue-typescript is now defined as an optional peer dependency and you'll need to install version 0.39 or later. Furthermore a fix has been added that should help support more module resolution algorithms.

Version 3.0.0 switches to a different package for Vue support, which fixes some more issues, e. g. support for setup scripts. No breaking changes otherwise.

Version 2.3.4 fixes an issue with Vue v2 files.

Version 2.3.3 fixes a bug where default imports were removed erroneously.

Version 2.3.1 adds debug logs and fixes Vue.js support.

Version 2.2.0 adds a compiler options cache to improve performance.

Version 2.1.0 adds support for Vue.js (.vue files).

Version 2.0.0 adds support for the parsers babel (i. e. JavaScript) and babel-ts which are only available since Prettier v2 (and thus the peer dependency has received a major bump).

License

MIT.

@caddijp/prettier-config@lintdog/caddi-core-ui@lintdog/eslint-config@lintdog/prettier-config@fellwork/prettier-configprettier-config-ljlx@everything-registry/sub-chunk-2470@thatdev/lib-dev@beardeddudes-dev/prettier-config@amplicode/codegen@arietta-studio/lint@arthurfiorette/prettier-config@alita/codemod@20i/eslint-config@21epub/configs@ayahub/lint@beardeddudes/prettier-config@baltpeter/prettier-config@d-gs/prettier-config@bjoluc/eslint-config-xo-next@brevity-builder/cli@bonfhir/prettier-config@bubkoo/prettier-configcaddi-bill-eslint-config@builder.io/mitosis@bn-digital/prettier-config@baic/prettier-config-yolk@baiyun/yun-tools@bazuka5801/prettier-config@baqhub/cli@calpa/ui@canale/eslint-config-typescript@genzy.io/cli@genzy.io/generator@germinal-io/eslint-config@gmahechas/common-devtools-ts-becy-stylepullcodeprettier-config-usagisahprettier-config-devjskitprettier-config-sexy-moderemotion-templatexiaohuoniumix-pro-componentszenesis-schematokriptthat-dev-librarytldraw-without-keyboard-shortcutstldrawligninsmart-order-routersuperplate-cliswr-codegenstorybook-vue-addonrn-tokriptschema2typeboxsetup-prettier-editorconfig-eslintsolid-fabric@charrue/prettier@col0ring/prettier-config@comfortdelgro/prettier-config@cosmonic/celestial-alignment@clevercanyon/dev-depsclay-generator-formatter-prettier@enchanterjs/enchanter@epaypool/eslint-config-base@faustt/prettier-config@fcodes/lint@gadgetinc/prettier-config@frenco/prettier-config@hbwow/lints@0xforkitall/shared-config@0xouzm/smart-order-router@eejit/prettier-config@dxsixpc/configs@dt-frames/prettier-config@fibre/prettier-config@fadeaway-ai/cli@fast-snail/rule-config@eliduty/prettier-config@clear-image/eslint-config-preset-prettier@devoxa/prettier-configcreate-mongez-app@grafit/swagger-api@grafit/swagger-to-api@layer5/rtk-query-codegen@juln/fabric@jupiterone/typescript-tools@kecoyo/prettier-config@kngsthvs/prettier-config@lego/eslint-config-typescript@magijs/prettier@merlin-gql/cli@megabytelabs/prettier-config@modyqyw/fabric@lyra-network/prettier-config@n1mbly/cli@nola/dev-scaff@nolanrigo/prettier-config@nordicsemiconductor/asset-tracker-cloud-code-style
3.2.4

5 months ago

3.2.3

9 months ago

3.2.2

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

3.1.1

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.3.4

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.3.3

3 years ago

2.3.0

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago