Socket
Socket
Sign inDemoInstall

@trivago/prettier-plugin-sort-imports

Package Overview
Dependencies
57
Maintainers
4
Versions
36
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @trivago/prettier-plugin-sort-imports

A prettier plugins to sort imports in provided RegEx order


Version published
Maintainers
4
Install size
28.6 MB
Created

Readme

Source

Prettier plugin sort imports

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

import order gif

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.

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

Maintainers

Ayush SharmaBehrang Yarahmadi
ayusharma@byara
@ayusharma_@behrang_y

Keywords

FAQs

Last updated on 12 Feb 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc