Automatically format imports and exports for JavaScript and TypeScript in VSCode.
- Support Vue.js.
- Support use server
- Support URL as module identifier.
- Support Import Attributes.
- Features
- How to use
- Configuration
- ESLint Compatibility
- Monorepo Support
- Vue.js Support
- Contribution
- Thanks
- License
- Auto format imports and exports on save or code actions, or manually from command, shortcut or context menu.
- Group and sort imports by configurable rules, including sort by paths or names.
- Remove duplicated and unused names with exceptions.
- Ignore files or declarations by config or inline comments.
- Respect ESLint and eslint-plugin-import rules.
- Respect configs from Prettier, EditorConfig and VS Code editor settings.
- Preserve
'use strict/client/server'
,///
directives, shebang (#!
) and comments. - Support Type-Only imports/exports, Type Modifier on names, Import Attributes and satisfies operator.
- Support multi-root projects.
- Support Vue.js.
-
Auto format on save when
autoFormat
is set toonSave
(this is the default). -
Auto format via Code Actions on Save (VSCode 1.44+)
"editor.codeActionsOnSave": ["source.organizeImports.sortImports"]
or
"editor.codeActionsOnSave": {"source.organizeImports.sortImports": true}
-
Press shortcut keys, default to
Alt+Shift+S
. -
Use
Sort Imports/Exports
command in the Command Palette (Ctrl+Shift+P
). -
Right click on editor content and select
Sort Imports/Exports
.
The extension reads configurations from the following sources (in precedence from high to low):
- ESLint configuration if installed.
"importSorter"
section inpackage.json
import-sorter.json
(File name is configurable)- Prettier configuration if installed
.editorconfig
- VS Code
"editor"
and"files"
settings - VS Code
"tsImportSorter"
settings
The following settings (and default values) are available in VSCode User Settings for this extension:
// Configuration file name.
// Not available in 'import-sorter.json' or 'package.json'.
"tsImportSorter.configuration.configurationFileName": "import-sorter.json",
// When to auto format imports/exports. Valid values are 'off' or 'onSave'.
"tsImportSorter.configuration.autoFormat": "onSave",
// Whether to format exports as well.
"tsImportSorter.configuration.formatExports": true,
// Disable formatting for files matching regular expressions.
"tsImportSorter.configuration.exclude": ["node_modules"],
// Disable formatting for files matching glob patterns.
"tsImportSorter.configuration.excludeGlob": [],
// Sort import declarations by paths or first names. Valid values are 'paths' or 'names'.
"tsImportSorter.sortImportsBy": "paths",
// Grouping rules for path patterns for imports. {} is the fall-back group.
// Wiki: https://github.com/daidodo/format-imports/wiki/Grouping-Rules
"tsImportSorter.configuration.groupRules": [
"^react(-dom)?$",
"^@angular/",
"^vue$",
"^node:",
{},
"^[@]",
"^[.]"
],
// Global Sorting rules. Valid values are 'none' or an object.
// Wiki: https://github.com/daidodo/format-imports/wiki/Sorting-Rules
"tsImportSorter.configuration.sortRules": {
// Sorting rule for import paths. Valid values are 'none' or an array.
"paths": ["_", "aA"],
// Sorting rule for imported/exported names. Valid values are 'none' or an array.
"names": ["_", "aA"]
}
// By default all unused imports are removed. Keep some or all them around with this
// setting via a list of regular expressions if you need.
// e.g. In order to keep all unused imports, use [".*"]
"tsImportSorter.configuration.keepUnused": [],
// Line wrapping style. Valid values are 'prettier' or an object.
// Wiki: https://github.com/daidodo/format-imports/wiki/Line-Wrapping-Style
"tsImportSorter.configuration.wrappingStyle": {
// Max binding names per line before wrapping for imports. 0 for no limit.
"maxBindingNamesPerLine": 1,
// Max default and binding names per line before wrapping for imports. 0 for no limit.
"maxDefaultAndBindingNamesPerLine": 2,
// Max binding names per line before wrapping for exports. 0 for no limit.
"maxExportNamesPerLine": 0,
// Max names on wrapped lines. 0 for no limit.
"maxNamesPerWrappedLine": 1,
// Whether to ignore trailing comments when counting line length.
"ignoreComments": false
}
// Number of empty lines between groups (NOT sub-groups).
"tsImportSorter.configuration.emptyLinesBetweenGroups": 1,
// Number of empty lines after the last import declaration.
"tsImportSorter.configuration.emptyLinesAfterAllImports": 1,
// Whether to remove the last slash when normalizing paths.
"tsImportSorter.configuration.removeLastSlashInPath": false,
// Whether to remove the last 'index' when normalizing paths.
"tsImportSorter.configuration.removeLastIndexInPath": false,
// Whether to enable debug mode and write detailed logs to VSCode output channel.
// Not available in 'import-sorter.json' or 'package.json'.
"tsImportSorter.configuration.development.enableDebug": false,
Most of the above settings, plus more in Configuration are also available in import-sorter.json
or package.json
under "importSorter
section.
For examples:
import-sorter.json:
{
"autoFormat": "onSave",
"quoteMark": "single",
"wrappingStyle": {
"maxBindingNamesPerLine": 2
}
}
Or
package.json:
{
"importSorter": {
"autoFormat": "onSave",
"quoteMark": "single",
"wrappingStyle": {
"maxBindingNamesPerLine": 2
}
}
}
If installed, ESLint and plugins rules will be detected and consulted, so that the result code will comply to the lint rules.
For how it works, please check the ESLint Compatibility wiki.
When reading config from import-sorter.json
or package.json
, the extention will automatically look for them in the directory of the file to be formatted, and in successive parent directories all the way up to the root directory of the filesystem (unless "root": true
is specified).
Multiple import-sorter.json
or package.json
files can be useful when you want different configurations for different sub projects of your monorepo, while common settings are kept in the root import-sorter.json
or package.json
. When there is a conflict, the sub project (more localized) config will take precedence.
From v7.2.0, the extension supports formatting <script>
in .vue files. But removing unused imports is disabled due to an issue. You'll need to remove them manually.
This is an open source project so your contribution will be well appreciated.
Please refer to CONTRIBUTING.md for more information.
The inspiration came from import-sorter.
MIT © Zhao DAI [email protected]