remove_unused_imports
Automatically remove unused import statements to clean up JavaScript/TypeScript code. Detects unused imports safely to reduce bundle size and improve code quality.
Instructions
Automatically remove unused import statements to clean up code. Safely detects which imports are actually used.
Examples: • Bundle size optimization: remove_unused_imports() to reduce bundle size • Code cleanup: remove_unused_imports() after refactoring • Linting compliance: remove_unused_imports() to fix ESLint warnings • Before deployment: remove_unused_imports({preview: true}) to see what will be removed • Legacy cleanup: remove_unused_imports() after removing old code • Development workflow: remove_unused_imports() during feature development
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| preview | No | Return preview only without applying changes (default: false). Use to see what will be removed. |
Implementation Reference
- src/index.ts:830-877 (handler)The core handler function that executes the remove_unused_imports tool logic. It uses the tree-hugger-js library's transform().removeUnusedImports() method to safely remove unused imports from the current AST, with optional preview mode and state updates.private async removeUnusedImports(args: { preview?: boolean }) { if (!this.currentAST) { return { content: [{ type: "text", text: "No AST loaded. Please use parse_code first.", }], isError: true, }; } try { const transformed = this.currentAST.tree.transform() .removeUnusedImports(); const result = transformed.toString(); if (!args.preview) { this.currentAST.sourceCode = result; this.currentAST.tree = parse(result); this.currentAST.timestamp = new Date(); } const transformResult: TransformResult = { operation: "remove_unused_imports", parameters: {}, preview: result.slice(0, 500) + (result.length > 500 ? '...' : ''), timestamp: new Date(), }; this.transformHistory.push(transformResult); return { content: [{ type: "text", text: `${args.preview ? 'Preview: ' : ''}Removed unused imports\n\n${args.preview ? 'Preview:\n' : 'Result:\n'}${result}`, }], }; } catch (error) { return { content: [{ type: "text", text: `Error removing unused imports: ${error instanceof Error ? error.message : String(error)}`, }], isError: true, }; } }
- src/index.ts:434-435 (registration)Registration of the tool in the CallToolRequestSchema switch statement, dispatching to the handler method.case "remove_unused_imports": return await this.removeUnusedImports(args as { preview?: boolean });
- src/index.ts:303-314 (schema)Tool registration in ListToolsRequestSchema including name, description, and input schema for validation.{ name: "remove_unused_imports", description: "Automatically remove unused import statements to clean up code. Safely detects which imports are actually used.\n\nExamples:\n• Bundle size optimization: remove_unused_imports() to reduce bundle size\n• Code cleanup: remove_unused_imports() after refactoring\n• Linting compliance: remove_unused_imports() to fix ESLint warnings\n• Before deployment: remove_unused_imports({preview: true}) to see what will be removed\n• Legacy cleanup: remove_unused_imports() after removing old code\n• Development workflow: remove_unused_imports() during feature development", inputSchema: { type: "object", properties: { preview: { type: "boolean", description: "Return preview only without applying changes (default: false). Use to see what will be removed." } }, },
- src/index.ts:901-902 (helper)Helper usage of removeUnusedImports transformer method within the composite transform_code tool.case "removeUnusedImports": transformer = transformer.removeUnusedImports();
- src/index.ts:843-843 (helper)Direct call to the tree-hugger-js library's removeUnusedImports() method in the handler..removeUnusedImports();