Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

prettier-plugin-better-sort-imports

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prettier-plugin-better-sort-imports

A better prettier plugins to sort imports in provided RegEx order

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
90
decreased by-14.29%
Maintainers
1
Weekly downloads
 
Created
Source

Prettier plugin better sort imports

A prettier plugin to sort import declarations by provided Regular Expression order.Inspired by @trivago/prettier-plugin-sort-imports

Input

import React, {
    FC,
    useEffect,
    useRef,
    ChangeEvent,
    KeyboardEvent,
} from 'react';
import { logger } from '@core/logger';
import { reduce, debounce } from 'lodash';
import { Message } from '../Message';
import { createServer } from '@server/node';
import { Alert } from '@ui/Alert';
import { repeat, filter, add } from '../utils';
import { initializeApp } from '@core/app';
import { Popup } from '@ui/Popup';
import { createConnection } from '@server/database';

Output

import { debounce, reduce } from 'lodash';
import React, {
    ChangeEvent,
    FC,
    KeyboardEvent,
    useEffect,
    useRef,
} from 'react';

import { createConnection } from '@server/database';
import { createServer } from '@server/node';

import { initializeApp } from '@core/app';
import { logger } from '@core/logger';

import { Alert } from '@ui/Alert';
import { Popup } from '@ui/Popup';

import { Message } from '../Message';
import { add, filter, repeat } from '../utils';

Install

npm

npm install --save-dev prettier-plugin-better-sort-imports

or, using yarn

yarn add --dev prettier-plugin-better-sort-imports

Usage

Add an order in prettier config file.

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

APIs

importOrder

type: Array<string>

A collection of Regular expressions in string format.

"importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],

Default behavior: The plugin moves the third party imports to the top which are not part of the importOrder list. To move the third party imports at desired place, you can use <THIRD_PARTY_MODULES> to assign third party imports to the appropriate position:

"importOrder": ["^@core/(.*)$", "<THIRD_PARTY_MODULES>", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],

Notice

<THIRD_PARTY_MODULES> in @trivago/prettier-plugin-sort-imports are all third-party modules. When sorting, the dependencies will be sorted in alphabetical order, users can't specific sort orders for third packages, just like below:

//	before sort
import React from 'react'
import { render } from 'react-dom'
import { v4 } from 'uuid'
import { message } from 'antd'
import { CoolModule } from 'coll-package'

//	sorted
import { message } from 'antd'
import { CoolModule } from 'coll-package'
import React from 'react'
import { render } from 'react-dom'
import { v4 } from 'uuid'

In prettier-plugin-better-sort-imports, users can specify the order of specific third-party packages, and <THIRD_PARTY_MODULES> will be sorted after the specified third-party packages, just like below:

"importOrder": ["react", "react-dom", "<THIRD_PARTY_MODULES>", "eth."],
//	before sort
import React from 'react'
import { render } from 'react-dom'
import { v4 } from 'uuid'
import { CoolModule } from 'coll-package'
import { message } from 'antd'

//	sorted
import React from 'react'
import { render } from 'react-dom'
import { message } from 'antd'
import { CoolModule } from 'coll-package'
import { v4 } from 'uuid'
importOrderSeparation

type: boolean

default value: false

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

"importOrderSeparation": true,
importOrderSortSpecifiers

type: boolean

default value: false

A boolean value to enable or disable sorting of the specifiers in an import declarations.

importOrderGroupNamespaceSpecifiers

type: boolean

default value: false

A boolean value to enable or disable sorting the namespace specifiers to the top of the import group.

importOrderCaseInsensitive

type: boolean

default value: false

A boolean value to enable case-insensitivity in the sorting algorithm used to order imports within each match group.

For example, when false (or not specified):

import ExampleView from './ExampleView';
import ExamplesList from './ExamplesList';

compared with "importOrderCaseInsensitive": true:

import ExamplesList from './ExamplesList';
import ExampleView from './ExampleView';
importOrderParserPlugins

type: Array<string>

default value: ["typescript", "jsx"]

Previously known as experimentalBabelParserPluginsList.

A collection of plugins 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 parser needs.

To pass the plugins to babel parser:

  "importOrderParserPlugins" : ["classProperties", "decorators-legacy"]

To pass the options to the babel parser plugins: Since prettier options are limited to string, you can pass plugins with options as a JSON string of the plugin array: "[\"plugin-name\", { \"pluginOption\": true }]".

  "importOrderParserPlugins" : ["classProperties", "[\"decorators\", { \"decoratorsBeforeExport\": true }]"]

To disable default plugins for babel parser, pass an empty array:

importOrderParserPlugins: []

Compatibility

FrameworkSupportedNote
JS with ES Modules✅ Everything-
NodeJS with ES Modules✅ Everything-
React✅ Everything-
Angular✅ EverythingSupported through importOrderParserPlugins API
VueComing soonAny contribution is welcome.
SvelteComing soonAny contribution is welcome.

Disclaimer

This plugin modifies the AST which is against the rules of prettier.

Keywords

FAQs

Package last updated on 03 Mar 2022

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc