-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathjavascript.py
More file actions
470 lines (418 loc) · 19.7 KB
/
Copy pathjavascript.py
File metadata and controls
470 lines (418 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
from tree_sitter import Language, Parser, Query
import tree_sitter_javascript as tsjs
from .base import LanguagePlugin, _matches, _fill_docs_from_siblings
_LANGUAGE = Language(tsjs.language())
_PARSER = Parser(_LANGUAGE)
def _parse(source: bytes):
return _PARSER.parse(source)
def _arrow_params(fn_node) -> str:
"""Extract params text from an arrow_function or function_expression node.
Handles three forms:
(a, b) => ... → formal_parameters node → "(a, b)"
a => ... → bare identifier → "(a)"
() => ... → formal_parameters node → "()"
"""
for child in fn_node.children:
if child.type == "formal_parameters":
return child.text.decode("utf-8", errors="replace")
if child.type == "identifier":
return f"({child.text.decode('utf-8', errors='replace')})"
return "()"
class JavaScriptPlugin(LanguagePlugin):
extensions = (".js", ".jsx")
_lang = _LANGUAGE
_parser = _PARSER
def _get_language(self):
return self._lang
def _get_parser(self):
return self._parser
def extract_skeleton(self, source: bytes) -> list[dict]:
lang = self._get_language()
tree = self._get_parser().parse(source)
results = []
# Top-level classes — plain and exported (export class Foo {})
for q_str in [
"(program (class_declaration name: (identifier) @name) @def)",
"(program (export_statement (class_declaration name: (identifier) @name) @def))",
]:
for _, m in _matches(Query(lang, q_str), tree.root_node):
results.append({
"type": "class",
"name": m["name"].text.decode("utf-8", errors="replace"),
"line": m["name"].start_point[0] + 1,
"parent": None,
"params": "",
})
# Methods inside classes
q = Query(lang, """
(class_declaration
name: (identifier) @class_name
body: (class_body
(method_definition
name: (property_identifier) @method_name
parameters: (formal_parameters) @params)))
""")
for _, m in _matches(q, tree.root_node):
results.append({
"type": "method",
"name": m["method_name"].text.decode("utf-8", errors="replace"),
"line": m["method_name"].start_point[0] + 1,
"parent": m["class_name"].text.decode("utf-8", errors="replace"),
"params": m["params"].text.decode("utf-8", errors="replace"),
})
# Top-level function declarations: function foo() {}
q = Query(lang, """
(program (function_declaration
name: (identifier) @name
parameters: (formal_parameters) @params))
""")
for _, m in _matches(q, tree.root_node):
results.append({
"type": "function",
"name": m["name"].text.decode("utf-8", errors="replace"),
"line": m["name"].start_point[0] + 1,
"parent": None,
"params": m["params"].text.decode("utf-8", errors="replace"),
})
# export default/named function foo() {} and export function* gen() {}
for q_str in [
"""(program (export_statement
(function_declaration
name: (identifier) @name
parameters: (formal_parameters) @params) @def))""",
"""(program (export_statement
(generator_function_declaration
name: (identifier) @name
parameters: (formal_parameters) @params) @def))""",
]:
for _, m in _matches(Query(lang, q_str), tree.root_node):
results.append({
"type": "function",
"name": m["name"].text.decode("utf-8", errors="replace"),
"line": m["name"].start_point[0] + 1,
"parent": None,
"params": m["params"].text.decode("utf-8", errors="replace"),
})
# Generator functions: function* gen() {}
q = Query(lang, """
(program (generator_function_declaration
name: (identifier) @name
parameters: (formal_parameters) @params))
""")
for _, m in _matches(q, tree.root_node):
results.append({
"type": "function",
"name": m["name"].text.decode("utf-8", errors="replace"),
"line": m["name"].start_point[0] + 1,
"parent": None,
"params": m["params"].text.decode("utf-8", errors="replace"),
})
# const/let foo = () => {} and const/let foo = function() {}
# Both at module level and exported (export const foo = ...)
for q_str in [
"""(program (lexical_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) @fn (function_expression) @fn])))""",
"""(program (export_statement (lexical_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) @fn (function_expression) @fn]))))""",
]:
for _, m in _matches(Query(lang, q_str), tree.root_node):
fn_node = m.get("fn")
results.append({
"type": "function",
"name": m["name"].text.decode("utf-8", errors="replace"),
"line": m["name"].start_point[0] + 1,
"parent": None,
"params": _arrow_params(fn_node) if fn_node else "()",
})
# Fill doc fields from preceding comments
for item in results:
item.setdefault("doc", "")
_fill_docs_from_siblings(results, tree.root_node, lang, [
"(function_declaration name: (identifier) @name) @def",
"(class_declaration name: (identifier) @name) @def",
"(export_statement (function_declaration name: (identifier) @name) @def)",
"(export_statement (class_declaration name: (identifier) @name) @def)",
])
# Deduplicate by (name, line)
seen = set()
deduped = []
for item in results:
key = (item["name"], item["line"])
if key not in seen:
seen.add(key)
deduped.append(item)
deduped.sort(key=lambda x: x["line"])
return deduped
def extract_symbol_source(self, source: bytes, name: str) -> tuple[str, int] | None:
lang = self._get_language()
tree = self._get_parser().parse(source)
# function/class/generator declarations (plain and exported)
for q_str in [
"(function_declaration name: (identifier) @name) @def",
"(class_declaration name: (identifier) @name) @def",
"(generator_function_declaration name: (identifier) @name) @def",
"(export_statement (function_declaration name: (identifier) @name) @def)",
"(export_statement (class_declaration name: (identifier) @name) @def)",
"(export_statement (generator_function_declaration name: (identifier) @name) @def)",
]:
for _, m in _matches(Query(lang, q_str), tree.root_node):
if m["name"].text.decode("utf-8", errors="replace") == name:
node = m["def"]
return (
source[node.start_byte:node.end_byte].decode("utf-8", errors="replace"),
node.start_point[0] + 1,
)
# const/let foo = () => {} (plain and exported) — return full lexical_declaration
for q_str in [
"""(lexical_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) (function_expression)])) @def""",
"""(export_statement (lexical_declaration
(variable_declarator
name: (identifier) @name
value: [(arrow_function) (function_expression)])) @def)""",
]:
for _, m in _matches(Query(lang, q_str), tree.root_node):
if m["name"].text.decode("utf-8", errors="replace") == name:
node = m["def"]
return (
source[node.start_byte:node.end_byte].decode("utf-8", errors="replace"),
node.start_point[0] + 1,
)
# Methods inside classes (method_definition)
q = Query(lang, "(method_definition name: (property_identifier) @name) @def")
for _, m in _matches(q, tree.root_node):
if m["name"].text.decode("utf-8", errors="replace") == name:
node = m["def"]
return (
source[node.start_byte:node.end_byte].decode("utf-8", errors="replace"),
node.start_point[0] + 1,
)
return None
def extract_calls_in_function(self, source: bytes, fn_name: str) -> list[str]:
lang = self._get_language()
tree = self._get_parser().parse(source)
fn_node = None
# function_declaration and generator_function_declaration (plain and exported)
for q_str in [
"(function_declaration name: (identifier) @name) @def",
"(generator_function_declaration name: (identifier) @name) @def",
"(export_statement (function_declaration name: (identifier) @name) @def)",
"(export_statement (generator_function_declaration name: (identifier) @name) @def)",
]:
for _, m in _matches(Query(lang, q_str), tree.root_node):
if m["name"].text.decode("utf-8", errors="replace") == fn_name:
fn_node = m["def"]
break
if fn_node:
break
# const/let foo = () => {} (plain and exported)
if fn_node is None:
for q_str in [
"""(variable_declarator
name: (identifier) @name
value: [(arrow_function) @def (function_expression) @def])""",
]:
for _, m in _matches(Query(lang, q_str), tree.root_node):
if m["name"].text.decode("utf-8", errors="replace") == fn_name:
fn_node = m["def"]
break
if fn_node:
break
# Methods inside classes
if fn_node is None:
q = Query(lang, "(method_definition name: (property_identifier) @name) @def")
for _, m in _matches(q, tree.root_node):
if m["name"].text.decode("utf-8", errors="replace") == fn_name:
fn_node = m["def"]
break
if fn_node is None:
return []
q_call = Query(lang, """
(call_expression function: [
(identifier) @called
(member_expression property: (property_identifier) @called)
])
""")
q_new = Query(lang, "(new_expression constructor: (identifier) @called)")
calls = set()
for _, m in _matches(q_call, fn_node):
calls.add(m["called"].text.decode("utf-8", errors="replace"))
for _, m in _matches(q_new, fn_node):
calls.add(m["called"].text.decode("utf-8", errors="replace"))
return sorted(calls)
def extract_symbol_usages(self, source: bytes, name: str) -> list[dict]:
lang = self._get_language()
tree = self._get_parser().parse(source)
q = Query(lang, f'((identifier) @name (#eq? @name "{name}"))')
usages = []
for _, m in _matches(q, tree.root_node):
node = m["name"]
usages.append({"line": node.start_point[0] + 1, "col": node.start_point[1]})
return usages
def extract_imports(self, source: bytes) -> list[dict]:
lang = self._get_language()
tree = self._get_parser().parse(source)
results = []
q = Query(lang, "(program (import_statement) @imp)")
for _, m in _matches(q, tree.root_node):
node = m["imp"]
results.append({
"line": node.start_point[0] + 1,
"text": node.text.decode("utf-8", errors="replace").strip(),
})
results.sort(key=lambda x: x["line"])
return results
def compute_complexity(self, source: bytes, fn_name: str) -> dict | None:
lang = self._get_language()
tree = self._get_parser().parse(source)
fn_node = None
for q_str in [
"(function_declaration name: (identifier) @name) @def",
"(generator_function_declaration name: (identifier) @name) @def",
"(export_statement (function_declaration name: (identifier) @name) @def)",
"(export_statement (generator_function_declaration name: (identifier) @name) @def)",
]:
for _, m in _matches(Query(lang, q_str), tree.root_node):
if m["name"].text.decode("utf-8", errors="replace") == fn_name:
fn_node = m["def"]
break
if fn_node:
break
if fn_node is None:
for q_str in [
"""(variable_declarator
name: (identifier) @name
value: [(arrow_function) @def (function_expression) @def])""",
]:
for _, m in _matches(Query(lang, q_str), tree.root_node):
if m["name"].text.decode("utf-8", errors="replace") == fn_name:
fn_node = m["def"]
break
if fn_node:
break
if fn_node is None:
q = Query(lang, "(method_definition name: (property_identifier) @name) @def")
for _, m in _matches(q, tree.root_node):
if m["name"].text.decode("utf-8", errors="replace") == fn_name:
fn_node = m["def"]
break
if fn_node is None:
return None
branch_map = {
"if_statement": "if",
"for_statement": "for",
"for_in_statement": "for_in",
"while_statement": "while",
"do_statement": "do_while",
"switch_case": "case",
"catch_clause": "catch",
"ternary_expression": "ternary",
}
counts: dict[str, int] = {}
def walk(node):
if node.type in branch_map:
label = branch_map[node.type]
counts[label] = counts.get(label, 0) + 1
elif node.type == "binary_expression":
for child in node.children:
if child.type in ("&&", "||"):
counts[child.type] = counts.get(child.type, 0) + 1
for child in node.children:
walk(child)
walk(fn_node)
total = 1 + sum(counts.values())
return {"total": total, "breakdown": counts}
def extract_variables(self, source: bytes, fn_name: str) -> list[dict]:
lang = self._get_language()
tree = self._get_parser().parse(source)
# Find the function node by name
fn_node = None
for q_str in [
"(export_statement declaration: (function_declaration name: (identifier) @name) @def)",
"(function_declaration name: (identifier) @name) @def",
"(export_statement declaration: (generator_function_declaration name: (identifier) @name) @def)",
"(generator_function_declaration name: (identifier) @name) @def",
"(method_definition name: (property_identifier) @name) @def",
]:
for _, m in _matches(Query(lang, q_str), tree.root_node):
if m["name"].text.decode("utf-8", errors="replace") == fn_name:
fn_node = m["def"]
break
if fn_node:
break
# Also look for arrow/function expression: const foo = () => {}
if fn_node is None:
q_str = "(variable_declarator name: (identifier) @name value: [(arrow_function) @def (function_expression) @def])"
for _, m in _matches(Query(lang, q_str), tree.root_node):
if m["name"].text.decode("utf-8", errors="replace") == fn_name:
fn_node = m["def"]
break
if fn_node is None:
return []
results = []
seen = set()
def _add(name, line, var_type="", kind="local"):
if name not in seen:
seen.add(name)
results.append({"name": name, "line": line, "type": var_type, "kind": kind})
# Extract parameters from formal_parameters
for child in fn_node.children:
if child.type == "formal_parameters":
for param in child.children:
if param.type == "identifier":
_add(param.text.decode("utf-8", errors="replace"),
param.start_point[0] + 1, kind="parameter")
elif param.type == "assignment_pattern":
# default params: x = default
for sub in param.children:
if sub.type == "identifier":
_add(sub.text.decode("utf-8", errors="replace"),
sub.start_point[0] + 1, kind="parameter")
break
break
# Walk the function body
def walk(node):
if node.type in ("lexical_declaration", "variable_declaration"):
for child in node.children:
if child.type == "variable_declarator":
for sub in child.children:
if sub.type == "identifier":
name = sub.text.decode("utf-8", errors="replace")
# Look for type annotation (TS only, but harmless here)
var_type = ""
for sib in child.children:
if sib.type == "type_annotation":
t = sib.text.decode("utf-8", errors="replace")
# Strip leading ": " prefix
if t.startswith(": "):
t = t[2:]
elif t.startswith(":"):
t = t[1:].strip()
var_type = t
break
_add(name, sub.start_point[0] + 1, var_type=var_type)
break
elif node.type == "for_in_statement":
# for (const item of data) or for (const key in obj)
# The loop variable identifier is a direct child of for_in_statement
for child in node.children:
if child.type == "identifier":
_add(child.text.decode("utf-8", errors="replace"),
child.start_point[0] + 1, kind="loop_var")
break
for child in node.children:
walk(child)
# Find the body (statement_block)
for child in fn_node.children:
if child.type == "statement_block":
walk(child)
break
return results
def check_syntax(self, source: bytes) -> bool:
return self._get_parser().parse(source).root_node.has_error