1
1
const TreeSitter = require ( 'tree-sitter' ) ;
2
2
const Python = require ( 'tree-sitter-python' ) ;
3
+ const TypeScript = require ( 'tree-sitter-typescript' ) ;
4
+ const JavaScript = require ( 'tree-sitter-javascript' ) ;
3
5
const fs = require ( 'fs' ) ;
4
6
5
7
const args = JSON . parse ( process . argv [ 2 ] )
6
8
7
9
// Load the Python parser
8
10
const parser = new TreeSitter ( ) ;
9
11
// 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
+
11
31
// Read the code file content
12
32
const codeContent = fs . readFileSync ( args . path , 'utf8' ) ;
13
33
// Parse the code using the chosen parser
14
34
const parsed = parser . parse ( codeContent ) ;
15
35
16
36
const line_to_grab = args . line
17
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
+ }
47
+
18
48
// Look for node where node.startPosition.row and node.endPosition.row are equal to line_to_grab
19
49
const search_node = ( node ) => {
20
- if ( node . startPosition . row === line_to_grab && node . endPosition . row === line_to_grab ) {
50
+ if ( node . startPosition . row === line_to_grab - 1 && node . endPosition . row === line_to_grab - 1 ) {
21
51
return node
22
52
}
53
+
23
54
for ( const child of node . children ) {
24
55
const result = search_node ( child )
25
56
if ( result ) {
@@ -31,19 +62,52 @@ const search_node = (node) => {
31
62
32
63
const line_node = search_node ( parsed . rootNode )
33
64
34
- const parent = line_node . parent
65
+ if ( ! line_node ) {
66
+ console . log ( 'No node found for line' , line_to_grab )
67
+ console . log ( 'Code:' , codeContent . split ( '\n' ) . slice ( line_to_grab - 5 , line_to_grab + 5 ) . join ( '\n' ) )
68
+ process . exit ( 1 )
69
+ }
35
70
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' )
71
+ const parseParent = ( childNode ) => {
72
+ const parent = childNode . parent
73
+ if ( ! parent ) {
74
+ return childNode
75
+ }
76
+ const MIN_CONTEXT_LENGTH = 700
77
+ const MAX_CONTEXT_LENGTH = 1500
78
+ if ( parent . text . length > MAX_CONTEXT_LENGTH ) {
79
+ console . log ( 'Parent node text is too long, truncating' )
80
+ const index = parent . text . indexOf ( line_node . text )
81
+ const start = index - MAX_CONTEXT_LENGTH / 2
82
+ const end = index + MAX_CONTEXT_LENGTH / 2
83
+ parent . text = parent . text . slice ( start , end )
84
+ return parent
85
+ }
86
+ if ( parent . text . length < MIN_CONTEXT_LENGTH ) {
87
+ if ( parent . parent ) {
88
+ return parseParent ( parent . parent )
89
+ }
90
+ else {
91
+ return parent
92
+ }
93
+ }
94
+ return parent
42
95
}
43
96
97
+ const ancestor = parseParent ( line_node )
98
+
99
+
100
+ const siblings = line_node . parent ? line_node . parent . children : [ line_node ]
101
+
102
+ const offending_line = siblings . map ( sibling => {
103
+ if ( sibling . startPosition . row === line_to_grab - 1 && sibling . endPosition . row === line_to_grab - 1 ) {
104
+ return sibling . text
105
+ }
106
+ } ) . join ( '' )
107
+
44
108
console . log ( {
45
- offending_line : line_node . text ,
46
- line_node : line_node ,
47
- parent : parent ,
48
- parent_text : parent . text
109
+ offending_line,
110
+ line_node,
111
+ ancestor ,
112
+ ancestor_text : ancestor . text
49
113
} )
0 commit comments