format-imports-vscode
format-imports-vscode copied to clipboard
Format imports and exports for JavaScript and TypeScript in VS Code.
JS/TS Import/Export Sorter
Automatically format imports and exports for JavaScript and TypeScript in VSCode.
[7.4.x]
Added
- Support formatting Import Assertions.
- Add
root
config to support monorepo projects. - Add
preserve
option forinsertFinalNewline
config. - Add
ignoreESLintRules
config to ignore specific ESLint rules. - Add
always
option fortrailingComma
config. - Support new ESLint rules:
- Support
type
modifiers on import names
Changed
- Update default
GroupRules
to groupnode:
imports separately. - Fix Prettier option
"trailingComma": "es5"
.
Table of contents
- Features
- How to use
-
Configuration
- VSCode Settings
- Config Files
- ESLint Compatibility
- Monorepo Support
- Contribution
- Thanks
- License
Features
- Auto format imports and exports on save or code actions, or manually from command, shortcut or context menu.
- Group and sort imports by custom rules, including sort by paths or names.
- Remove duplicated and unused names with configurable 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'
,///
directives, shebang (#!
) and comments. - Support Type-Only imports/exports, Type Modifier on names and Import Assertions.
- Support multi-root projects.
How to use
-
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
.
Configuration
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
VSCode 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,
Config Files
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
}
}
}
ESLint Compatibility
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.
Monorepo Support
When reading config from import-sorter.json
or package.json
, Format-Imports 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.
Contribution
This is an open source project so your contribution will be well appreciated.
Please refer to CONTRIBUTING.md for more information.
Thanks
The inspiration came from import-sorter.
License
MIT © Zhao DAI [email protected]