find_pattern
Locate specific JavaScript/TypeScript code patterns using intuitive syntax for targeted searches in code analysis.
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"]' |
Implementation Reference
- src/index.ts:518-567 (handler)The handler function that executes the 'find_pattern' tool. It checks if AST is loaded, uses tree.find(pattern) to locate the first matching node, extracts metadata, and returns formatted node information or error.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 a required 'pattern' string parameter.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)Registration in the CallToolRequestSchema switch statement that dispatches to the findPattern handler.case "find_pattern": return await this.findPattern(args as { pattern: string });
- src/index.ts:202-215 (registration)Tool registration in the ListToolsRequestSchema handler, including name, description, and input schema.{ 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"], }, },