Import Polisher
Automatically beautify and reorganize import statements while guaranteeing zero changes to runtime behavior.

Features
- Import Sorting — Sorts imports by length (shorter first) or alphabetically
- Named Import Sorting — Sorts members inside
{ } braces
- Multi-Language Support — TypeScript, JavaScript, Python, Go, Rust, Java, Kotlin, Swift, Dart, C#, PHP, C/C++, Ruby, Scala, Elixir
- AST-Based Parsing — Uses syntax trees for accurate import detection (falls back to regex if AST fails)
- Comment Preservation — Comments attached to imports remain attached after reordering
- Barrier Preservation — Non-import code between imports (e.g.,
export const ...) is preserved in place
- Zero Runtime Changes — Never modifies executable code
- Deterministic Output — Running multiple times produces the same result
- Format on Save — Automatically beautifies imports when you save (Ctrl+S)
- Format on Paste — Optionally beautifies imports after pasting (Ctrl+V)
Examples
Length Sort (default)
Set sortOrder: "length" — shorter module names first:
Before:
import { z, a, m } from 'zod';
import fs from 'fs';
import { sortImports } from './utils';
import path from 'path';
import express from 'express';
import { helper } from '@/helpers';
import './polyfills';
After:
import fs from 'fs';
import './polyfills';
import path from 'path';
import { a, m, z } from 'zod';
import express from 'express';
import { helper } from '@/helpers';
import { sortImports } from './utils';
Alphabetical Sort
Set sortOrder: "alphabetical" — A-Z ordering:
Before:
import { zebra, apple, mango } from 'fruits';
import express from 'express';
import path from 'path';
After:
import express from 'express';
import path from 'path';
import { apple, mango, zebra } from 'fruits';
Case-Insensitive Sort
Set sortOrder: "case-insensitive" — ignores case when comparing:
Before:
import { Berry } from './berry';
import apple from 'apple';
import { Cherry } from './cherry';
After:
import apple from 'apple';
import { Berry } from './berry';
import { Cherry } from './cherry';
Named Import Sorting
Set sortNamedImports: true (default) — sorts members inside { }:
Before:
import { zebra, apple, mango } from 'fruits';
After:
import { apple, mango, zebra } from 'fruits';
Python
Before:
from mypackage.utils import helper, formatter
import json
from .models import User
import sys
from typing import Optional, List, Dict
After:
import json
import sys
from mypackage.utils import formatter, helper
from typing import Dict, List, Optional
from .models import User
Barrier Preservation
Non-import code between imports is preserved in place. Imports above and below the barrier are sorted independently:
Before:
import moment from 'moment';
import { MatDialog } from '@angular/material/dialog';
export const MY_FORMATS = {
parse: { dateInput: 'DD MMM YYYY' },
};
import { Store } from '@ngxs/store';
import { z } from './z';
After:
import moment from 'moment';
import { MatDialog } from '@angular/material/dialog';
export const MY_FORMATS = {
parse: { dateInput: 'DD MMM YYYY' },
};
import { z } from './z';
import { Store } from '@ngxs/store';
What changed:
- Imports above
MY_FORMATS sorted by length (moment before MatDialog)
MY_FORMATS stays exactly where it was
- Imports below
MY_FORMATS sorted by length (z before Store)
Configuration Options
| Setting |
Type |
Default |
Description |
importPolisher.formatOnSave |
boolean |
true |
Auto-format when saving a file |
importPolisher.formatOnPaste |
boolean |
false |
Auto-format after pasting code |
importPolisher.sortOrder |
string |
"length" |
Sort algorithm for imports |
importPolisher.sortNamedImports |
boolean |
true |
Sort { a, b } → { a, b } |
importPolisher.useAST |
boolean |
true |
Use AST parsing (more accurate) |
importPolisher.enabledLanguages |
string[] |
[] |
Limit to specific languages |
importPolisher.debug |
boolean |
false |
Enable debug logging |
Settings JSON
Add to your settings.json:
{
"importPolisher.sortOrder": "length",
"importPolisher.sortNamedImports": true,
"importPolisher.useAST": true,
"importPolisher.debug": false,
"importPolisher.enabledLanguages": []
}
Usage
Keyboard Shortcuts
| Action |
Windows/Linux |
Mac |
| Beautify Imports |
Ctrl+Alt+I |
Cmd+Alt+I |
| Beautify Workspace |
Ctrl+Alt+Shift+I |
Cmd+Alt+Shift+I |
| Save & Beautify |
Ctrl+S |
Cmd+S |
Commands
Open the Command Palette (Ctrl+Shift+P) and type:
| Command |
Description |
Import Polisher: Beautify Imports |
Beautify imports in the current editor |
Import Polisher: Beautify Current File |
Beautify the active file |
Import Polisher: Beautify Workspace |
Beautify all supported files in the workspace |
Import Polisher: Toggle Format on Save |
Enable/disable auto-format on save |
Import Polisher: Toggle Format on Paste |
Enable/disable auto-format on paste |
Supported Languages
| Language |
Parser |
Type Imports |
| TypeScript |
AST (TypeScript Compiler API) |
Yes |
| JavaScript |
AST (TypeScript Compiler API) |
No |
| TSX |
AST (TypeScript Compiler API) |
Yes |
| JSX |
AST (TypeScript Compiler API) |
No |
| Python |
Regex-based |
No |
| Go |
Regex-based |
No |
| Rust |
Regex-based |
No |
| Java |
Regex-based |
No |
| Kotlin |
Regex-based |
No |
| Swift |
Regex-based |
No |
| Dart |
Regex-based |
No |
| C# |
Regex-based |
No |
| PHP |
Regex-based |
No |
| C/C++ |
Regex-based |
No |
| Ruby |
Regex-based |
No |
| Scala |
Regex-based |
No |
| Elixir |
Regex-based |
No |
Installation
From .vsix
code --install-extension import-polisher-0.0.3.vsix
From Source
git clone https://github.com/your-username/import-polisher.git
cd import-polisher
npm install
npm run build-extension
code --install-extension import-polisher-0.0.3.vsix
Or press F5 in VS Code to launch the Extension Development Host.
Architecture
src/
├── extension.ts # Extension entry point
├── models/ # Data models and interfaces
│ └── import.ts # Import types and config
├── parsers/ # Language-specific parsers
│ ├── typescript-parser.ts # TS/JS/TSX/JSX (AST)
│ ├── python-parser.ts # Python (regex)
│ └── generic-parser.ts # Other languages (regex)
├── sorters/ # Import and specifier sorting
│ └── import-sorter.ts # Sorting algorithms
├── languages/ # Language registry
│ └── registry.ts # Language configuration
├── services/ # Core services
│ └── formatter.ts # Main formatting logic
├── settings/ # Configuration management
│ └── settings.ts # VS Code settings reader
└── utils/ # Utility functions
└── logger.ts # Debug logging
Development
npm install
npm run compile # Compile once
npm run watch # Watch mode (auto-recompile)
npm run lint # Lint source files
npm run build-extension # Package as .vsix
After packaging, install the built extension locally:
code --install-extension import-polisher-0.0.3.vsix
Contributing
We welcome contributions! For detailed instructions on development setup, testing, debugging, and publishing, see docs/CONTRIBUTING.md.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature)
- Commit your changes (
git commit -m 'Add amazing feature')
- Push to the branch (
git push origin feature/amazing-feature)
- Open a Pull Request
License
MIT License - see LICENSE for details.