Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@trivago/prettier-plugin-sort-imports
Advanced tools
A prettier plugins to sort imports in provided RegEx order
@trivago/prettier-plugin-sort-imports is a Prettier plugin that automatically sorts your import statements in a consistent and customizable manner. It helps maintain a clean and organized codebase by ensuring that import statements are ordered according to specified rules.
Sort imports alphabetically
This feature sorts the import statements alphabetically by module name.
import React from 'react';
import { useState } from 'react';
import { render } from 'react-dom';
Group imports by type
This feature groups import statements by their type, such as external libraries, internal modules, and styles.
import React from 'react';
import { useState } from 'react';
import { render } from 'react-dom';
import './styles.css';
import utils from './utils';
Customizable sorting order
This feature allows you to define a custom sorting order for your import statements, such as placing React imports at the top, followed by other external libraries, and then internal modules.
import React from 'react';
import { useState } from 'react';
import { render } from 'react-dom';
import utils from './utils';
import './styles.css';
eslint-plugin-import is an ESLint plugin that helps validate proper imports and enforce import order. It provides a wide range of rules for managing import statements, including sorting them. Unlike @trivago/prettier-plugin-sort-imports, which is a Prettier plugin, eslint-plugin-import is used with ESLint.
import-sort is a tool and library for sorting ES6 imports. It can be used as a standalone tool or integrated with various editors and build tools. It offers customizable sorting configurations similar to @trivago/prettier-plugin-sort-imports but is not tied to Prettier.
prettier-plugin-organize-imports is a Prettier plugin that uses the TypeScript language service to organize imports. It automatically removes unused imports and sorts the remaining ones. It is similar to @trivago/prettier-plugin-sort-imports but focuses on TypeScript projects.
A prettier plugin to sort import declarations by provided RegEx order.
npm
npm install --save-dev @trivago/prettier-plugin-sort-imports
or, using yarn
yarn add --dev @trivago/prettier-plugin-sort-imports
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,
"importOrderCaseInsensitive": true,
}
importOrder
A collection of:
new RegExp
to evaluate regular expression. E.g. node.source.value.match(new RegExp(val))
Here, val
is the string provided in import order.<THIRD_PARTY_MODULES>
- marks the position between RegExps, where the not matched imports will be placed. By default, it's the top position (above all the groups in importOrder
).importOrderCaseInsensitive
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';
importOrderSeparation
A boolean value to enable or disable the new line separation
between sorted import declarations. The separation takes place according to importOrder
.
importOrderSortSpecifiers
A boolean value to enable or disable sorting of the imports module declarations. It is disabled by default
importOrderParserPlugins
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.
Since prettier options are limited to strings, you can pass plugins with options as a JSON string of the plugin array: "[\"plugin-name\", { \"pluginOption\": true }]"
.
module.exports = {
"printWidth": 80,
"tabWidth": 4,
"trailingComma": "all",
"singleQuote": true,
"jsxBracketSameLine": true,
"semi": true,
"importOrder": ["^@core/(.*)$", "^@server/(.*)$", "^@ui/(.*)$", "^[./]"],
"importOrderSeparation": true,
"importOrderCaseInsensitive": true,
"importOrderParserPlugins" : ["jsx", "typescript", "[\"decorators\", { \"decoratorsBeforeExport\": true }]"]
}
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
In the end, the plugin returns final imports with 3rd party imports on top and local imports at the end.
The 3rd party imports position (it's top by default) can be overrided using the <THIRD_PARTY_MODULES>
special word.
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 './';
react
and classnames
to place between my RegEx imports without hardcoding?You can define the <THIRD_PARTY_MODULES>
special word in the importOrder
. For example above, the importOrder
would be like ["^@ui/(.*)$", "^@server/(.*)$", "<THIRD_PARTY_MODULES>", '^[./]']
.
Now, the final output would be as follows:
import p from '@ui/p';
import q from '@ui/q';
import a from '@server/a';
import z from '@server/z';
import classnames from 'classnames';
import React from 'react';
import s from './';
There is a examples directory. The examples file can be formatted by using
npm run example
command.
npm run example examples/example.tsx
*.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.
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'
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 importOrderParserPlugins
in your .prettierrc
and pass an array of plugin names to be used.
Framework | Supported | Note |
---|---|---|
JS with ES Modules | ✅ Everything | - |
NodeJS with ES Modules | ✅ Everything | - |
React | ✅ Everything | - |
Angular | 🔜 Experimental features are not supported | To be supported in v3.x.x, coming soon 😉 |
Vue | ❌ Not supported | Any contribution is welcome. |
Svelte | ❌ Not supported | Any contribution is welcome. |
For more information regarding contribution, please check the CONTRIBUTING.
This plugin modifies the AST which is against the rules of prettier.
Ayush Sharma | Behrang Yarahmadi |
---|---|
@ayusharma_ | @behrang_y |
FAQs
A prettier plugins to sort imports in provided RegEx order
The npm package @trivago/prettier-plugin-sort-imports receives a total of 684,651 weekly downloads. As such, @trivago/prettier-plugin-sort-imports popularity was classified as popular.
We found that @trivago/prettier-plugin-sort-imports demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
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.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.