Skip to content

Commit f54c056

Browse files
author
Eric Wheeler
committed
refactor: move helper functions to dedicated file
Move test helper functions from parseSourceCodeDefinitions.test.ts to a new helpers.ts file. Rename test file to parseSourceCodeDefinitions.tsx.test.ts to indicate it's for TSX tests. This improves code organization by separating test helpers from test cases. Signed-off-by: Eric Wheeler <[email protected]>
1 parent 1bc0213 commit f54c056

File tree

2 files changed

+184
-197
lines changed

2 files changed

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

0 commit comments

Comments
 (0)