Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
@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,
}
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
.
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.
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 './';
There is a examples directory. The examples file can be formatted by using
npm run example
command.
npm run example examples/example.tsx
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 650,275 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
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.