A personal opinionated EcmaScript import sorting extension for VSCode.
Thanks to Renke Grunwald's nice work, it really save me a lot of time so that I can easily make this.
No any more features, no configuration, just sort!
Firstly, NodeJS built-in module imports have most priority, and then, imports are grouped by the module hierarchy. Imports farther away from the current directory have higher priority.
Be attention, the absolute imports are always farther than relative imports.
Before:
import foo from "./foo";
import _ from "lodash";
import foobar from "../foobar";
import fs from "fs";
After:
import fs from "fs"; // built-in module
import _ from "lodash"; // absolute module
import foobar from "../foobar"; // farther module
import foo from "./foo";
modules having same hierarchy compose a group. For each group, the order is:
- imports which have only namespace member
eg:import * as fs from 'fs'
- imports which have only default member
eg:import fs from 'fs'
- imports which have both default and namespace member
eg:import foo, * as bar from 'foobar'
- imports which have both default and named member
eg:import foo, { bar } from 'foobar'
- imports which have only named member
eg:import { foo, bar } from 'foobar'
If the above rule didn't determine the order, then use the first member's name (not alias) to compare. For namespace member, the alias is regarded as its name. The comparison rule is:
- literal that starts with non-alphanumeric has higher priority
- then follow the alphabetical order(in unicode)
For imports having no member, sort them by module name with same rule.
This didn't be published to extension marketplace since it doesn't suitable for most people. So you have to install manually.
- download VSIX file from Github release
- in vscode,
Ctrl+Shift+P
and input "Install from VSIX..." - select downloaded VSIX file, then install
- enjoy
clone this repo:
git clone https://github.com/mattuylee/vsc-sort-import
change into repo directory and install dependencies:
cd vsc-sort-import && npm install
package VSIX file:
npx vsce package
After install dependencies, just press F5
to start debugging. If some error occurs, it will be printed onto the output panel.
See CHANGELOG.md