remove_unused_imports
Cleans up JavaScript/TypeScript code by automatically detecting and removing unused import statements. Helps optimize bundle size, comply with linting rules, and streamline code maintenance during development or refactoring.
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. |
Input Schema (JSON Schema)
Implementation Reference
- src/index.ts:830-877 (handler)The primary handler function for the 'remove_unused_imports' tool. It checks if AST is loaded, applies the transformation using tree-hugger-js to remove unused imports, updates the AST if not preview, logs to history, and returns the result or preview.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:303-315 (registration)Registration of the tool in the ListToolsRequestSchema handler, providing the tool name, description, and input schema for MCP tool discovery.{ 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:434-435 (registration)Dispatch case in CallToolRequestSchema handler that routes incoming tool calls to the removeUnusedImports handler method.case "remove_unused_imports": return await this.removeUnusedImports(args as { preview?: boolean });
- src/index.ts:306-314 (schema)Input schema definition for the remove_unused_imports tool, specifying the optional preview parameter.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 the removeUnusedImports transformer method within the batch transform_code tool.case "removeUnusedImports": transformer = transformer.removeUnusedImports();