All Projects → trivago → Prettier Plugin Sort Imports

trivago / Prettier Plugin Sort Imports

Licence: apache-2.0
A prettier plugin to sort imports in typescript and javascript files by the provided RegEx order.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Prettier Plugin Sort Imports

Eslint Import Resolver Jest
🃏 Jest import resolution plugin for eslint-plugin-import
Stars: ✭ 29 (-85.85%)
Mutual labels:  import, plugin
S2s
Coding time Compile. A tool to write code fastest.
Stars: ✭ 254 (+23.9%)
Mutual labels:  prettier, plugin
Snowonder
🔮 Magical import declarations formatter for Xcode
Stars: ✭ 100 (-51.22%)
Mutual labels:  sort, import
eslint-config-welly
😎 ⚙️ ESLint configuration for React projects that I do. Feel free to use this!
Stars: ✭ 21 (-89.76%)
Mutual labels:  prettier, import
Keepassbrowserimporter
KeePass 2.x plugin which imports credentials from various browsers.
Stars: ✭ 139 (-32.2%)
Mutual labels:  import, plugin
Dont Break
Checks if the current version of your package would break dependent projects
Stars: ✭ 200 (-2.44%)
Mutual labels:  plugin
Prettier Chrome
🎨 An extension that can run Prettier in the browser
Stars: ✭ 207 (+0.98%)
Mutual labels:  prettier
Pluginframework
Everything is a Plugin in .NET
Stars: ✭ 197 (-3.9%)
Mutual labels:  plugin
Vlc Bittorrent
A bittorrent plugin for VLC.
Stars: ✭ 198 (-3.41%)
Mutual labels:  plugin
Postgraphile Plugin Connection Filter
Advanced filtering of Connections in PostGraphile
Stars: ✭ 210 (+2.44%)
Mutual labels:  plugin
Artist
An artist creates views. Artist is a Gradle plugin that codegens a base set of Android Views.
Stars: ✭ 208 (+1.46%)
Mutual labels:  plugin
Vue Editor Js
editor.js for Vue users
Stars: ✭ 205 (+0%)
Mutual labels:  plugin
Network Media Library
Network Media Library plugin for WordPress Multisite
Stars: ✭ 203 (-0.98%)
Mutual labels:  plugin
Argusapm
Powerful, comprehensive (Android) application performance management platform. 360线上移动性能检测平台
Stars: ✭ 2,452 (+1096.1%)
Mutual labels:  plugin
Obs Ios Camera Source
Use your iPhone camera as a video source in OBS Studio and stream high quality video from your iPhone's camera over USB
Stars: ✭ 199 (-2.93%)
Mutual labels:  plugin
Qtfirebase
An effort to bring Google's Firebase C++ API to Qt + QML
Stars: ✭ 208 (+1.46%)
Mutual labels:  plugin
Assistant
✏️ Generate human quality code from your design. supports flutter, react, vue, figma, sketch, xd
Stars: ✭ 199 (-2.93%)
Mutual labels:  plugin
Plugins
Plugins for Flutter maintained by the Flutter team
Stars: ✭ 14,956 (+7195.61%)
Mutual labels:  plugin
Technicalnote
Repository to store what we have studied. 📖 We want everyone to get a job through TechnicalNote.
Stars: ✭ 206 (+0.49%)
Mutual labels:  sort
Wechatplugin Macos
No description or website provided.
Stars: ✭ 13,280 (+6378.05%)
Mutual labels:  plugin

Prettier plugin sort imports

A prettier plugin to sort import declarations by provided RegEx order.

Input

input

Output

output

Install

npm

npm install --save-dev @trivago/prettier-plugin-sort-imports

or, using yarn

yarn add --dev @trivago/prettier-plugin-sort-imports

Usage

Add an order in prettier config file.

module.exports = {
  "printWidth": 80,
  "tabWidth": 4,
  "trailingComma": "all",
  "singleQuote": true,
  "jsxBracketSameLine": true,
  "semi": true,
  "importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
  "importOrderSeparation": true,
}

APIs

importOrder

A collection of regular expressions in string format. The plugin uses new RegExp to evaluate regular expression. E.g. node.source.value.match(new RegExp(val)) Here, val is the string provided in import order.

importOrderSeparation

A boolean value to enable or disable the new line separation between sorted import declarations. The separation takes place according to importOrder.

experimentalBabelParserPluginsList

A collection of parser names for babel parser. The plugin passes this list to babel parser so it can understand the syntaxes used in the file being formatted. The plugin uses prettier itself to figure out the parser it needs to use but if that fails, you can use this field to enforce the usage of the plugins babel needs.

module.exports = {
  "printWidth": 80,
  "tabWidth": 4,
  "trailingComma": "all",
  "singleQuote": true,
  "jsxBracketSameLine": true,
  "semi": true,
  "importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
  "importOrderSeparation": true,
  "experimentalBabelParserPluginsList" : ["jsx", "typescript"]
}

How does import sort work ?

The plugin extracts the imports which are defined in importOrder. These imports are local imports. The imports which are not part of the importOrder is considered as 3rd party imports.

After, the plugin sorts the local imports and 3rd party imports using natural sort algorithm. In the end, the plugin returns final imports with 3rd party imports on top and local imports at the end.

FAQ / Troubleshooting

Q. How can I add the RegEx imports in the importOrder list ?

You can define the RegEx in the importOrder. For example if you want to sort the following imports:

import React from 'react';
import classnames from 'classnames';
import z from '@server/z';
import a from '@server/a';
import s from './';
import p from '@ui/p';
import q from '@ui/q';

then the importOrder would be ["^@ui/(.*)$","^@server/(.*)$", '^[./]']. Now, the final output would be as follows:

import classnames from 'classnames';
import React from 'react';
import p from '@ui/p';
import q from '@ui/q';
import a from '@server/a';
import z from '@server/z';
import s from './';

Q. How can I run examples in this project ?

There is a examples directory. The examples file can be formatted by using npm run example command.

npm run example examples/example.tsx

Q. How to use the plugin with *.d.ts files ?

The plugin automatically ignores the *.d.ts files. We encourage you to declare the *.d.ts files in .prettierignore. (Read more here)[https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore].

Q. How does the plugin handle the first comment in the file.

The plugin keeps the first comment as it is in the file. The plugin also removes the new lines in between first comment and the first import. input:

// comment

import a from 'a'

output:

// comment
import a from 'a'

Q. I'm getting error about experimental syntax.

If you are using some experimental syntax and the plugin has trouble parsing your files, you might getting errors similar to this:

SyntaxError: This experimental syntax requires enabling one of the following parser plugin(s): ...

To solve this issue, you can use the new option experimentalBabelParserPluginsList in your .prettierrc and pass an array of plugin names to be used.

Maintainers

Ayush Sharma Behrang Yarahmadi
ayusharma @byara
@ayusharma_ @behrang_y
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].