11import { parseSourceCodeDefinitionsForFile , setMinComponentLines } from ".."
22import * as fs from "fs/promises"
33import * as path from "path"
4- import Parser from "web-tree-sitter"
54import tsxQuery from "../queries/tsx"
6- // Mock setup
5+ import { Parser , Language } from "web-tree-sitter"
6+
77vi . mock ( "fs/promises" )
88export const mockedFs = vi . mocked ( fs )
99
@@ -26,39 +26,28 @@ export const debugLog = (message: string, ...args: any[]) => {
2626}
2727
2828// Store the initialized TreeSitter for reuse
29- let initializedTreeSitter : Parser | null = null
29+ let initializedTreeSitter : { Parser : typeof Parser ; Language : typeof Language } | null = null
3030
3131// Function to initialize tree-sitter
3232export async function initializeTreeSitter ( ) {
33- if ( initializedTreeSitter ) {
34- return initializedTreeSitter
35- }
33+ if ( ! initializedTreeSitter ) {
34+ // Initialize directly using the default export or the module itself
35+ await Parser . init ( )
3636
37- const TreeSitter = await initializeWorkingParser ( )
37+ // Override the Parser.Language.load to use dist directory
38+ const originalLoad = Language . load
3839
39- initializedTreeSitter = TreeSitter
40- return TreeSitter
41- }
40+ Language . load = async ( wasmPath : string ) => {
41+ const filename = path . basename ( wasmPath )
42+ const correctPath = path . join ( process . cwd ( ) , "dist" , filename )
43+ // console.log(`Redirecting WASM load from ${wasmPath} to ${correctPath}`)
44+ return originalLoad ( correctPath )
45+ }
4246
43- // Function to initialize a working parser with correct WASM path
44- // DO NOT CHANGE THIS FUNCTION
45- export async function initializeWorkingParser ( ) {
46- const TreeSitter = ( await vi . importActual ( "web-tree-sitter" ) ) as any
47-
48- // Initialize directly using the default export or the module itself
49- const ParserConstructor = TreeSitter . default || TreeSitter
50- await ParserConstructor . init ( )
51-
52- // Override the Parser.Language.load to use dist directory
53- const originalLoad = ParserConstructor . Language . load
54- ParserConstructor . Language . load = async ( wasmPath : string ) => {
55- const filename = path . basename ( wasmPath )
56- const correctPath = path . join ( process . cwd ( ) , "dist" , filename )
57- // console.log(`Redirecting WASM load from ${wasmPath} to ${correctPath}`)
58- return originalLoad ( correctPath )
47+ initializedTreeSitter = { Parser, Language }
5948 }
6049
61- return ParserConstructor
50+ return initializedTreeSitter
6251}
6352
6453// Test helper for parsing source code definitions
@@ -91,12 +80,12 @@ export async function testParseSourceCodeDefinitions(
9180 const mockedLoadRequiredLanguageParsers = loadRequiredLanguageParsers as any
9281
9382 // Initialize TreeSitter and create a real parser
94- const TreeSitter = await initializeTreeSitter ( )
95- const parser = new TreeSitter ( )
83+ const { Parser , Language } = await initializeTreeSitter ( )
84+ const parser = new Parser ( )
9685
9786 // Load language and configure parser
9887 const wasmPath = path . join ( process . cwd ( ) , `dist/${ wasmFile } ` )
99- const lang = await TreeSitter . Language . load ( wasmPath )
88+ const lang = await Language . load ( wasmPath )
10089 parser . setLanguage ( lang )
10190
10291 // Create a real query
@@ -122,16 +111,16 @@ export async function testParseSourceCodeDefinitions(
122111
123112// Helper function to inspect tree structure
124113export async function inspectTreeStructure ( content : string , language : string = "typescript" ) : Promise < string > {
125- const TreeSitter = await initializeTreeSitter ( )
126- const parser = new TreeSitter ( )
114+ const { Parser , Language } = await initializeTreeSitter ( )
115+ const parser = new Parser ( )
127116 const wasmPath = path . join ( process . cwd ( ) , `dist/tree-sitter-${ language } .wasm` )
128- const lang = await TreeSitter . Language . load ( wasmPath )
117+ const lang = await Language . load ( wasmPath )
129118 parser . setLanguage ( lang )
130119
131120 // Parse the content
132121 const tree = parser . parse ( content )
133122
134123 // Print the tree structure
135- debugLog ( `TREE STRUCTURE (${ language } ):\n${ tree . rootNode . toString ( ) } ` )
136- return tree . rootNode . toString ( )
124+ debugLog ( `TREE STRUCTURE (${ language } ):\n${ tree ? .rootNode . toString ( ) } ` )
125+ return tree ? .rootNode . toString ( ) || ""
137126}
0 commit comments