Skip to content

Latest commit

 

History

History
244 lines (174 loc) · 9.87 KB

README.md

File metadata and controls

244 lines (174 loc) · 9.87 KB

JS/TS Import/Export Sorter

Version Downloads Installs Ratings

Automatically format imports and exports for JavaScript and TypeScript in VSCode.

[7.6.x]

Added

Table of contents

Features

How to use

  • Auto format on save when autoFormat is set to onSave (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).

    1
  • Right click on editor content and select Sort Imports/Exports.

    image

Configuration

The extension reads configurations from the following sources (in precedence from high to low):

  • ESLint configuration if installed.
  • "importSorter" section in package.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, 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.

Vue.js Support

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.

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]