insert_code
Add or modify code in JavaScript/TypeScript files with precise node targeting and automatic formatting. Use patterns to insert logging, validation, error handling, or comments before or after specific code blocks.
Instructions
Insert code before or after nodes with smart formatting. Professional-quality code insertion with proper indentation.
Examples: • Add logging: insert_code('function_declaration', 'console.log("Function started");', 'after') • Add validation: insert_code('method_definition[name="save"]', 'if (!this.isValid()) return;', 'after') • Add comments: insert_code('class_declaration', '// Main user management class', 'before') • Add error handling: insert_code('function[async]', 'try {', 'after') + insert_code('function[async]', '} catch(e) { console.error(e); }', 'after') • Add metrics: insert_code('function[name*="api"]', 'performance.mark("api-start");', 'after') • Debug mode: insert_code('call[text*="fetch"]', 'console.log("API call:", url);', 'before')
Input Schema
Name | Required | Description | Default |
---|---|---|---|
code | Yes | Code to insert. Will be formatted with proper indentation automatically. | |
pattern | Yes | Pattern to match: 'function_declaration', 'class[name="MyClass"]', 'method_definition[async]' | |
position | Yes | Insert position: 'before' (above) or 'after' (below) the matched nodes | |
preview | No | Return preview only without applying changes (default: false). Always preview first! |