11const TreeSitter = require ( 'tree-sitter' ) ;
22const Python = require ( 'tree-sitter-python' ) ;
3+ const TypeScript = require ( 'tree-sitter-typescript' ) ;
4+ const JavaScript = require ( 'tree-sitter-javascript' ) ;
35const fs = require ( 'fs' ) ;
46
57const args = JSON . parse ( process . argv [ 2 ] )
68
79// Load the Python parser
810const parser = new TreeSitter ( ) ;
911// Set the language to the parser
10- parser . setLanguage ( Python ) ;
12+ const ext = args . path . split ( '.' ) . pop ( ) ;
13+
14+ let language
15+
16+ if ( ext === 'py' ) {
17+ language = Python
18+ }
19+ else if ( ext === 'ts' ) {
20+ language = TypeScript . typescript
21+ }
22+ else if ( ext === 'tsx' ) {
23+ language = TypeScript . tsx
24+ }
25+ else if ( ext === 'js' || ext === 'jsx' ) {
26+ language = JavaScript
27+ }
28+
29+ parser . setLanguage ( language ) ;
30+
1131// Read the code file content
1232const codeContent = fs . readFileSync ( args . path , 'utf8' ) ;
1333// Parse the code using the chosen parser
1434const parsed = parser . parse ( codeContent ) ;
1535
16- const line_to_grab = args . line
36+ const line_to_grab = args . line + 1
37+
38+ if ( line_to_grab === undefined ) {
39+ console . log ( 'No line number provided' )
40+ process . exit ( 1 )
41+ }
42+
43+ if ( line_to_grab > codeContent . split ( '\n' ) . length ) {
44+ console . log ( 'Line number provided is greater than the number of lines in the file' )
45+ process . exit ( 1 )
46+ }
1747
1848// Look for node where node.startPosition.row and node.endPosition.row are equal to line_to_grab
1949const search_node = ( node ) => {
@@ -31,16 +61,23 @@ const search_node = (node) => {
3161
3262const line_node = search_node ( parsed . rootNode )
3363
64+ if ( ! line_node ) {
65+ console . log ( 'No node found for line' , line_to_grab )
66+ console . log ( 'Code:' , codeContent . split ( '\n' ) . slice ( line_to_grab - 5 , line_to_grab + 5 ) . join ( '\n' ) )
67+ process . exit ( 1 )
68+ }
69+
3470const parent = line_node . parent
3571
36- if ( parent ) {
37- const start_line = parent . startPosition . row
38- const end_line = parent . endPosition . row
39- // Return codeContent from start_line to end_line
40- const lines = codeContent . split ( '\n' ) . slice ( start_line , end_line + 1 )
41- parent . content = lines . join ( '\n' )
72+ if ( parent . text . length > 256 ) {
73+ console . log ( 'Parent node text is too long, truncating' )
74+ const index = parent . text . indexOf ( line_node . text )
75+ const start = index - 128
76+ const end = index + 128
77+ parent . text = parent . text . slice ( start , end )
4278}
4379
80+
4481console . log ( {
4582 offending_line : line_node . text ,
4683 line_node : line_node ,
0 commit comments