find_pattern
Locate the first node in JavaScript/TypeScript code matching a specific pattern using intuitive syntax. Ideal for pinpointing functions, classes, JSX components, or debug calls efficiently.
Instructions
Find first node matching the specified pattern using tree-hugger-js intuitive syntax. Use for targeted searches when you need one specific match.
Examples: • Find main function: find_pattern('function[name="main"]') • Find React component: find_pattern('function[name="UserProfile"]') • Find async functions: find_pattern('function[async]') • Find specific class: find_pattern('class[name="UserManager"]') • Find error handling: find_pattern('call[text*="catch"]') • Find JSX with props: find_pattern('jsx:has(jsx-attribute[name="className"])') • Debug specific calls: find_pattern('call[text*="console.log"]')
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | Yes | Pattern using intuitive syntax: 'function', 'class[name="MyClass"]', 'function[async]', 'call[text*="fetch"]' |
Input Schema (JSON Schema)
Implementation Reference
- src/index.ts:518-567 (handler)The core handler function for the 'find_pattern' tool. It checks if an AST is loaded, uses tree-hugger-js's find method to locate the first matching node based on the pattern, extracts node metadata, and returns formatted details or appropriate error messages.private async findPattern(args: { pattern: string }) { if (!this.currentAST) { return { content: [{ type: "text", text: "No AST loaded. Please use parse_code first.", }], isError: true, }; } try { const result = this.currentAST.tree.find(args.pattern); if (!result) { return { content: [{ type: "text", text: `No match found for pattern: ${args.pattern}`, }], }; } const nodeInfo = { type: result.type, text: result.text.length > 200 ? result.text.slice(0, 200) + '...' : result.text, line: result.line, column: result.column, name: result.name, startPosition: result.startPosition, endPosition: result.endPosition, childrenCount: result.children.length, }; return { content: [{ type: "text", text: `Found match for pattern "${args.pattern}":\n${JSON.stringify(nodeInfo, null, 2)}`, }], }; } catch (error) { return { content: [{ type: "text", text: `Error finding pattern: ${error instanceof Error ? error.message : String(error)}`, }], isError: true, }; } }
- src/index.ts:205-214 (schema)Input schema definition for the 'find_pattern' tool, specifying an object with a required 'pattern' string property using tree-hugger-js pattern syntax.inputSchema: { type: "object", properties: { pattern: { type: "string", description: "Pattern using intuitive syntax: 'function', 'class[name=\"MyClass\"]', 'function[async]', 'call[text*=\"fetch\"]'" } }, required: ["pattern"], },
- src/index.ts:202-215 (registration)Tool registration in ListToolsRequestSchema handler, declaring name, description, and input schema for 'find_pattern'.{ name: "find_pattern", description: "Find first node matching the specified pattern using tree-hugger-js intuitive syntax. Use for targeted searches when you need one specific match.\n\nExamples:\n• Find main function: find_pattern('function[name=\"main\"]')\n• Find React component: find_pattern('function[name=\"UserProfile\"]')\n• Find async functions: find_pattern('function[async]')\n• Find specific class: find_pattern('class[name=\"UserManager\"]')\n• Find error handling: find_pattern('call[text*=\"catch\"]')\n• Find JSX with props: find_pattern('jsx:has(jsx-attribute[name=\"className\"])')\n• Debug specific calls: find_pattern('call[text*=\"console.log\"]')", inputSchema: { type: "object", properties: { pattern: { type: "string", description: "Pattern using intuitive syntax: 'function', 'class[name=\"MyClass\"]', 'function[async]', 'call[text*=\"fetch\"]'" } }, required: ["pattern"], }, },
- src/index.ts:416-417 (registration)Dispatcher registration in CallToolRequestSchema switch statement, routing 'find_pattern' calls to the findPattern handler method.case "find_pattern": return await this.findPattern(args as { pattern: string });