|
| 1 | +import { jest } from "@jest/globals" |
| 2 | +import { parseSourceCodeDefinitionsForFile } from ".." |
| 3 | +import * as fs from "fs/promises" |
| 4 | +import * as path from "path" |
| 5 | +import Parser from "web-tree-sitter" |
| 6 | +import tsxQuery from "../queries/tsx" |
| 7 | + |
| 8 | +// Global debug flag - set to 0 to disable debug logging |
| 9 | +export const DEBUG = 0 |
| 10 | + |
| 11 | +// Debug function to conditionally log messages |
| 12 | +export const debugLog = (message: string, ...args: any[]) => { |
| 13 | + if (DEBUG) { |
| 14 | + console.debug(message, ...args) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +// Mock fs module |
| 19 | +const mockedFs = jest.mocked(fs) |
| 20 | + |
| 21 | +// Store the initialized TreeSitter for reuse |
| 22 | +let initializedTreeSitter: Parser | null = null |
| 23 | + |
| 24 | +// Function to initialize tree-sitter |
| 25 | +export async function initializeTreeSitter() { |
| 26 | + if (initializedTreeSitter) { |
| 27 | + return initializedTreeSitter |
| 28 | + } |
| 29 | + |
| 30 | + const TreeSitter = await initializeWorkingParser() |
| 31 | + const wasmPath = path.join(process.cwd(), "dist/tree-sitter-tsx.wasm") |
| 32 | + const tsxLang = await TreeSitter.Language.load(wasmPath) |
| 33 | + |
| 34 | + initializedTreeSitter = TreeSitter |
| 35 | + return TreeSitter |
| 36 | +} |
| 37 | + |
| 38 | +// Function to initialize a working parser with correct WASM path |
| 39 | +// DO NOT CHANGE THIS FUNCTION |
| 40 | +export async function initializeWorkingParser() { |
| 41 | + const TreeSitter = jest.requireActual("web-tree-sitter") as any |
| 42 | + |
| 43 | + // Initialize directly using the default export or the module itself |
| 44 | + const ParserConstructor = TreeSitter.default || TreeSitter |
| 45 | + await ParserConstructor.init() |
| 46 | + |
| 47 | + // Override the Parser.Language.load to use dist directory |
| 48 | + const originalLoad = TreeSitter.Language.load |
| 49 | + TreeSitter.Language.load = async (wasmPath: string) => { |
| 50 | + const filename = path.basename(wasmPath) |
| 51 | + const correctPath = path.join(process.cwd(), "dist", filename) |
| 52 | + // console.log(`Redirecting WASM load from ${wasmPath} to ${correctPath}`) |
| 53 | + return originalLoad(correctPath) |
| 54 | + } |
| 55 | + |
| 56 | + return TreeSitter |
| 57 | +} |
| 58 | + |
| 59 | +// Test helper for parsing source code definitions |
| 60 | +export async function testParseSourceCodeDefinitions( |
| 61 | + testFilePath: string, |
| 62 | + content: string, |
| 63 | + options: { |
| 64 | + language?: string |
| 65 | + wasmFile?: string |
| 66 | + queryString?: string |
| 67 | + extKey?: string |
| 68 | + } = {}, |
| 69 | +): Promise<string | undefined> { |
| 70 | + // Set default options |
| 71 | + const language = options.language || "tsx" |
| 72 | + const wasmFile = options.wasmFile || "tree-sitter-tsx.wasm" |
| 73 | + const queryString = options.queryString || tsxQuery |
| 74 | + const extKey = options.extKey || "tsx" |
| 75 | + |
| 76 | + // Clear any previous mocks |
| 77 | + jest.clearAllMocks() |
| 78 | + |
| 79 | + // Mock fs.readFile to return our sample content |
| 80 | + mockedFs.readFile.mockResolvedValue(content) |
| 81 | + |
| 82 | + // Get the mock function |
| 83 | + const mockedLoadRequiredLanguageParsers = require("../languageParser").loadRequiredLanguageParsers |
| 84 | + |
| 85 | + // Initialize TreeSitter and create a real parser |
| 86 | + const TreeSitter = await initializeTreeSitter() |
| 87 | + const parser = new TreeSitter() |
| 88 | + |
| 89 | + // Load language and configure parser |
| 90 | + const wasmPath = path.join(process.cwd(), `dist/${wasmFile}`) |
| 91 | + const lang = await TreeSitter.Language.load(wasmPath) |
| 92 | + parser.setLanguage(lang) |
| 93 | + |
| 94 | + // Create a real query |
| 95 | + const query = lang.query(queryString) |
| 96 | + |
| 97 | + // Set up our language parser with real parser and query |
| 98 | + const mockLanguageParser: any = {} |
| 99 | + mockLanguageParser[extKey] = { parser, query } |
| 100 | + |
| 101 | + // Configure the mock to return our parser |
| 102 | + mockedLoadRequiredLanguageParsers.mockResolvedValue(mockLanguageParser) |
| 103 | + |
| 104 | + // Call the function under test |
| 105 | + const result = await parseSourceCodeDefinitionsForFile(testFilePath) |
| 106 | + |
| 107 | + // Verify loadRequiredLanguageParsers was called with the expected file path |
| 108 | + expect(mockedLoadRequiredLanguageParsers).toHaveBeenCalledWith([testFilePath]) |
| 109 | + expect(mockedLoadRequiredLanguageParsers).toHaveBeenCalled() |
| 110 | + |
| 111 | + debugLog(`content:\n${content}\n\nResult:\n${result}`) |
| 112 | + return result |
| 113 | +} |
| 114 | + |
| 115 | +// Helper function to inspect tree structure |
| 116 | +export async function inspectTreeStructure(content: string, language: string = "typescript"): Promise<void> { |
| 117 | + const TreeSitter = await initializeTreeSitter() |
| 118 | + const parser = new TreeSitter() |
| 119 | + const wasmPath = path.join(process.cwd(), `dist/tree-sitter-${language}.wasm`) |
| 120 | + const lang = await TreeSitter.Language.load(wasmPath) |
| 121 | + parser.setLanguage(lang) |
| 122 | + |
| 123 | + // Parse the content |
| 124 | + const tree = parser.parse(content) |
| 125 | + |
| 126 | + // Print the tree structure |
| 127 | + debugLog(`TREE STRUCTURE (${language}):\n${tree.rootNode.toString()}`) |
| 128 | + |
| 129 | + // Add more detailed debug information |
| 130 | + debugLog("\nDETAILED NODE INSPECTION:") |
| 131 | + |
| 132 | + // Function to recursively print node details |
| 133 | + const printNodeDetails = (node: Parser.SyntaxNode, depth: number = 0) => { |
| 134 | + const indent = " ".repeat(depth) |
| 135 | + debugLog( |
| 136 | + `${indent}Node Type: ${node.type}, Start: ${node.startPosition.row}:${node.startPosition.column}, End: ${node.endPosition.row}:${node.endPosition.column}`, |
| 137 | + ) |
| 138 | + |
| 139 | + // Print children |
| 140 | + for (let i = 0; i < node.childCount; i++) { |
| 141 | + const child = node.child(i) |
| 142 | + if (child) { |
| 143 | + // For type_alias_declaration nodes, print more details |
| 144 | + if (node.type === "type_alias_declaration") { |
| 145 | + debugLog(`${indent} TYPE ALIAS: ${node.text}`) |
| 146 | + } |
| 147 | + |
| 148 | + // For conditional_type nodes, print more details |
| 149 | + if (node.type === "conditional_type" || child.type === "conditional_type") { |
| 150 | + debugLog(`${indent} CONDITIONAL TYPE FOUND: ${child.text}`) |
| 151 | + } |
| 152 | + |
| 153 | + // For infer_type nodes, print more details |
| 154 | + if (node.type === "infer_type" || child.type === "infer_type") { |
| 155 | + debugLog(`${indent} INFER TYPE FOUND: ${child.text}`) |
| 156 | + } |
| 157 | + |
| 158 | + printNodeDetails(child, depth + 1) |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // Start recursive printing from the root node |
| 164 | + printNodeDetails(tree.rootNode) |
| 165 | +} |
0 commit comments