fluidattacks_core_serializers 12.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fluidattacks_core/serializers/__init__.py +0 -0
- fluidattacks_core/serializers/py.typed +0 -0
- fluidattacks_core/serializers/snippet.py +437 -0
- fluidattacks_core/serializers/syntax.py +577 -0
- fluidattacks_core_serializers-12.0.0.dist-info/METADATA +28 -0
- fluidattacks_core_serializers-12.0.0.dist-info/RECORD +7 -0
- fluidattacks_core_serializers-12.0.0.dist-info/WHEEL +4 -0
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
from collections.abc import Iterator
|
|
2
|
+
from contextlib import suppress
|
|
3
|
+
from operator import itemgetter
|
|
4
|
+
from typing import NamedTuple
|
|
5
|
+
|
|
6
|
+
from more_itertools import chunked
|
|
7
|
+
from tree_sitter import Node
|
|
8
|
+
|
|
9
|
+
from fluidattacks_core.serializers.syntax import (
|
|
10
|
+
TREE_SITTER_FUNCTION_DECLARATION_MAP,
|
|
11
|
+
TREE_SITTER_FUNCTION_IDENTIFIER,
|
|
12
|
+
TREE_SITTER_LANGUAGE_CONVERSION,
|
|
13
|
+
InvalidFileType,
|
|
14
|
+
extract_function_nodes,
|
|
15
|
+
extract_hierarchical_nodes,
|
|
16
|
+
get_language_from_path,
|
|
17
|
+
get_node_content,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
# Constants
|
|
21
|
+
SNIPPETS_CONTEXT: int = 10
|
|
22
|
+
SNIPPETS_COLUMNS: int = 500
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class SnippetViewport(NamedTuple):
|
|
26
|
+
line: int
|
|
27
|
+
column: int | None = None
|
|
28
|
+
|
|
29
|
+
columns_per_line: int = SNIPPETS_COLUMNS
|
|
30
|
+
line_context: int = SNIPPETS_CONTEXT
|
|
31
|
+
wrap: bool = False
|
|
32
|
+
show_line_numbers: bool = True
|
|
33
|
+
highlight_line_number: bool = True
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class Function(NamedTuple):
|
|
37
|
+
name: str | None = None
|
|
38
|
+
node_type: str | None = None
|
|
39
|
+
field_identifier_name: str | None = None
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class Snippet(NamedTuple):
|
|
43
|
+
"""A code snippet extracted from a source file.
|
|
44
|
+
|
|
45
|
+
Attributes:
|
|
46
|
+
content: The raw text of the extracted lines, joined by newlines.
|
|
47
|
+
offset: 0-indexed line number of the first line in *content*.
|
|
48
|
+
File line N is at ``content.splitlines()[N - offset - 1]``.
|
|
49
|
+
All write paths must store a 0-indexed value here; storing a
|
|
50
|
+
1-indexed value silently shifts every displayed line by one.
|
|
51
|
+
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
content: str
|
|
55
|
+
offset: int
|
|
56
|
+
line: int | None = None
|
|
57
|
+
column: int | None = None
|
|
58
|
+
columns_per_line: int = SNIPPETS_COLUMNS
|
|
59
|
+
line_context: int = SNIPPETS_CONTEXT
|
|
60
|
+
wrap: bool = False
|
|
61
|
+
show_line_numbers: bool = True
|
|
62
|
+
highlight_line_number: bool = True
|
|
63
|
+
start_point: tuple[int, int] | None = None
|
|
64
|
+
end_point: tuple[int, int] | None = None
|
|
65
|
+
is_function: bool = False
|
|
66
|
+
function: Function | None = None
|
|
67
|
+
|
|
68
|
+
def contains_line(self, target_line: int) -> bool:
|
|
69
|
+
"""Return True if target_line (1-indexed) falls within this snippet."""
|
|
70
|
+
if not self.content:
|
|
71
|
+
return False
|
|
72
|
+
line_count = len(self.content.splitlines())
|
|
73
|
+
return self.offset < target_line <= self.offset + line_count
|
|
74
|
+
|
|
75
|
+
def reshape(self, *, viewport: SnippetViewport) -> "Snippet":
|
|
76
|
+
"""Reshapes the given Snippet to the specified SnippetViewport.
|
|
77
|
+
|
|
78
|
+
The original focus line cannot be changed, but the content will be
|
|
79
|
+
adjusted to fit the viewport. The method calculates the vertical and
|
|
80
|
+
horizontal center of the snippet, and then slices the content to fit
|
|
81
|
+
the viewport. It also handles line numbers and highlighting as needed.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
viewport (SnippetViewport | None, optional): The viewport to
|
|
85
|
+
reshape the snippet to. Defaults to None.
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
Snippet: The reshaped Snippet.
|
|
89
|
+
|
|
90
|
+
"""
|
|
91
|
+
if not self.line:
|
|
92
|
+
return self
|
|
93
|
+
snippet = _make_snippet(
|
|
94
|
+
content=self.content,
|
|
95
|
+
viewport=viewport._replace(line=self.line),
|
|
96
|
+
offset=self.offset,
|
|
97
|
+
)
|
|
98
|
+
line_context = self.line_context - snippet.offset
|
|
99
|
+
return snippet._replace(
|
|
100
|
+
offset=self.offset + snippet.offset,
|
|
101
|
+
is_function=self.is_function
|
|
102
|
+
if len(snippet.content.splitlines()) == len(self.content.splitlines())
|
|
103
|
+
else False,
|
|
104
|
+
function=self.function,
|
|
105
|
+
line_context=line_context,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _make_snippet(
|
|
110
|
+
*,
|
|
111
|
+
content: str,
|
|
112
|
+
viewport: SnippetViewport | None = None,
|
|
113
|
+
offset: int | None = None,
|
|
114
|
+
) -> Snippet:
|
|
115
|
+
# Replace tab by spaces so 1 char renders as 1 symbol
|
|
116
|
+
lines_raw: list[str] = content.replace("\t", " ").splitlines()
|
|
117
|
+
offset = offset or 0
|
|
118
|
+
# Build a list of line numbers to line contents, handling wrapping
|
|
119
|
+
if viewport is not None:
|
|
120
|
+
lines = _get_lines(lines_raw, viewport, offset=offset)
|
|
121
|
+
# Find the vertical center of the snippet
|
|
122
|
+
viewport_center = next(
|
|
123
|
+
(index for index, (line_no, _) in enumerate(lines) if line_no == viewport.line),
|
|
124
|
+
0,
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
# Find the horizontal left of the snippet
|
|
128
|
+
# We'll place the center at 25% from the left border
|
|
129
|
+
viewport_left: int = (
|
|
130
|
+
max(viewport.column - viewport.columns_per_line // 4, 0)
|
|
131
|
+
if viewport.column is not None
|
|
132
|
+
else 0
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
if lines:
|
|
136
|
+
# How many chars do we need to write the line number
|
|
137
|
+
loc_width: int = len(str(lines[-1][0]))
|
|
138
|
+
modify_lines(lines, viewport, viewport_left, loc_width)
|
|
139
|
+
if viewport.line_context == -1:
|
|
140
|
+
offset = 0
|
|
141
|
+
elif viewport_center - viewport.line_context <= 0:
|
|
142
|
+
offset = 0
|
|
143
|
+
lines = lines[
|
|
144
|
+
slice(
|
|
145
|
+
0,
|
|
146
|
+
2 * viewport.line_context + 1,
|
|
147
|
+
)
|
|
148
|
+
]
|
|
149
|
+
else:
|
|
150
|
+
offset = (
|
|
151
|
+
max(viewport_center - viewport.line_context, 0)
|
|
152
|
+
if (viewport_center + viewport.line_context < len(lines))
|
|
153
|
+
else max(len(lines) - 2 * viewport.line_context - 1, 0)
|
|
154
|
+
)
|
|
155
|
+
lines = lines[
|
|
156
|
+
slice(
|
|
157
|
+
max(viewport_center - viewport.line_context, 0),
|
|
158
|
+
viewport_center + viewport.line_context + 1,
|
|
159
|
+
)
|
|
160
|
+
if (viewport_center + viewport.line_context < len(lines))
|
|
161
|
+
else slice(
|
|
162
|
+
max(len(lines) - 2 * viewport.line_context - 1, 0),
|
|
163
|
+
len(lines),
|
|
164
|
+
)
|
|
165
|
+
]
|
|
166
|
+
|
|
167
|
+
# Highlight the column if requested
|
|
168
|
+
|
|
169
|
+
if _must_highlight_line_number(viewport):
|
|
170
|
+
lines.append((0, f" {' ':>{loc_width}} ^ Col {viewport_left}"))
|
|
171
|
+
|
|
172
|
+
if viewport:
|
|
173
|
+
return Snippet(
|
|
174
|
+
content="\n".join(map(itemgetter(1), lines)),
|
|
175
|
+
offset=offset,
|
|
176
|
+
line=viewport.line,
|
|
177
|
+
column=viewport.column,
|
|
178
|
+
columns_per_line=viewport.columns_per_line,
|
|
179
|
+
line_context=viewport.line_context,
|
|
180
|
+
highlight_line_number=viewport.highlight_line_number,
|
|
181
|
+
show_line_numbers=viewport.show_line_numbers,
|
|
182
|
+
wrap=viewport.wrap,
|
|
183
|
+
start_point=None,
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
return Snippet(
|
|
187
|
+
content="\n".join(map(itemgetter(1), enumerate(lines_raw))),
|
|
188
|
+
offset=offset,
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def modify_lines(
|
|
193
|
+
lines: list[tuple[int, str]],
|
|
194
|
+
viewport: SnippetViewport,
|
|
195
|
+
viewport_left: int,
|
|
196
|
+
loc_width: int,
|
|
197
|
+
) -> None:
|
|
198
|
+
if not lines:
|
|
199
|
+
return
|
|
200
|
+
|
|
201
|
+
line_no_last: int | None = lines[-2][0] if len(lines) >= 2 else None
|
|
202
|
+
|
|
203
|
+
for index, (line_no, line) in enumerate(lines):
|
|
204
|
+
mark_symbol = _get_mark_symbol(line_no, line_no_last, viewport._replace())
|
|
205
|
+
line_no_str = "" if line_no == line_no_last else str(line_no)
|
|
206
|
+
line_no_last = line_no
|
|
207
|
+
|
|
208
|
+
# Apply horizontal viewport slicing
|
|
209
|
+
n_line = line[viewport_left : viewport_left + viewport.columns_per_line + 1]
|
|
210
|
+
line_prefix = f"{mark_symbol} {line_no_str:>{loc_width}} | "
|
|
211
|
+
|
|
212
|
+
lines[index] = (
|
|
213
|
+
line_no,
|
|
214
|
+
_format_line(n_line, line_prefix, viewport, lines[index][1]),
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def _format_line(
|
|
219
|
+
line: str,
|
|
220
|
+
line_prefix: str,
|
|
221
|
+
viewport: SnippetViewport,
|
|
222
|
+
original_line: str,
|
|
223
|
+
) -> str:
|
|
224
|
+
if not viewport.show_line_numbers:
|
|
225
|
+
if original_line.startswith(line_prefix.rstrip()):
|
|
226
|
+
line = line.replace(line_prefix, "").replace(line_prefix.rstrip(), "")
|
|
227
|
+
return line
|
|
228
|
+
|
|
229
|
+
if not line.startswith(line_prefix) and viewport.highlight_line_number:
|
|
230
|
+
return f"{line_prefix}{line}".rstrip(" ")
|
|
231
|
+
|
|
232
|
+
if original_line.startswith(line_prefix[0]):
|
|
233
|
+
line = line.replace(line_prefix, "")
|
|
234
|
+
return line
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _get_mark_symbol(
|
|
238
|
+
line_no: int,
|
|
239
|
+
line_no_last: int | None,
|
|
240
|
+
viewport: SnippetViewport,
|
|
241
|
+
) -> str:
|
|
242
|
+
if line_no == viewport.line and line_no != line_no_last:
|
|
243
|
+
return ">"
|
|
244
|
+
return " "
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _must_highlight_line_number(viewport: SnippetViewport) -> bool:
|
|
248
|
+
return bool(
|
|
249
|
+
viewport.column is not None
|
|
250
|
+
and viewport.highlight_line_number
|
|
251
|
+
and viewport.show_line_numbers,
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
def _get_lines(
|
|
256
|
+
lines_raw: list[str],
|
|
257
|
+
viewport: SnippetViewport,
|
|
258
|
+
offset: int | None = None,
|
|
259
|
+
) -> list[tuple[int, str]]:
|
|
260
|
+
offset = offset or 0
|
|
261
|
+
if viewport is not None and viewport.wrap:
|
|
262
|
+
return [
|
|
263
|
+
(line_no + offset, "".join(line_chunk))
|
|
264
|
+
for line_no, line in enumerate(lines_raw, start=1)
|
|
265
|
+
for line_chunk in _chunked(line, viewport.columns_per_line)
|
|
266
|
+
]
|
|
267
|
+
|
|
268
|
+
return [(line_no + offset, line) for line_no, line in enumerate(lines_raw, start=1)]
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def _chunked(line: str, chunk_size: int) -> Iterator[str]:
|
|
272
|
+
if line:
|
|
273
|
+
yield from chunked(list(line), n=chunk_size) # type: ignore # noqa: PGH003
|
|
274
|
+
else:
|
|
275
|
+
yield ""
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def find_function_name(func_nodes: list[Node], language: str) -> tuple[Node, str] | None:
|
|
279
|
+
for node in func_nodes:
|
|
280
|
+
if (field_name := TREE_SITTER_FUNCTION_IDENTIFIER.get(language, {}).get(node.type)) and (
|
|
281
|
+
(children := node.child_by_field_name(field_name))
|
|
282
|
+
or (children := next((x for x in node.children if x.type == field_name), None))
|
|
283
|
+
):
|
|
284
|
+
return children, node.type
|
|
285
|
+
return None
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def extract_snippet_function_from_nodes(
|
|
289
|
+
file_bytes: bytes,
|
|
290
|
+
func_nodes: list[Node],
|
|
291
|
+
desired_line: int,
|
|
292
|
+
*,
|
|
293
|
+
language: str,
|
|
294
|
+
) -> Snippet | None:
|
|
295
|
+
language = TREE_SITTER_LANGUAGE_CONVERSION.get(language) or language
|
|
296
|
+
|
|
297
|
+
try:
|
|
298
|
+
lines = file_bytes.decode(encoding="utf-8").split("\n")
|
|
299
|
+
start_line = func_nodes[0].start_point[0]
|
|
300
|
+
end_line = func_nodes[-1].end_point[0]
|
|
301
|
+
result = "\n".join(lines[start_line : end_line + 1])
|
|
302
|
+
identifier_ = find_function_name(func_nodes, language)
|
|
303
|
+
function_: Function | None = None
|
|
304
|
+
if identifier_:
|
|
305
|
+
identifier_node, node_type = identifier_
|
|
306
|
+
if identifier_node.text:
|
|
307
|
+
function_ = Function(
|
|
308
|
+
name=identifier_node.text.decode("utf-8"),
|
|
309
|
+
node_type=node_type,
|
|
310
|
+
field_identifier_name=identifier_node.type,
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
offset = func_nodes[0].start_point[0]
|
|
314
|
+
return Snippet(
|
|
315
|
+
content=result,
|
|
316
|
+
offset=offset,
|
|
317
|
+
line=desired_line,
|
|
318
|
+
show_line_numbers=False,
|
|
319
|
+
start_point=(func_nodes[0].start_point[0], 0),
|
|
320
|
+
end_point=(func_nodes[-1].end_point[0], len(lines[end_line])),
|
|
321
|
+
highlight_line_number=False,
|
|
322
|
+
line_context=(desired_line - offset) - 1,
|
|
323
|
+
is_function=True,
|
|
324
|
+
function=function_,
|
|
325
|
+
)
|
|
326
|
+
except IndexError:
|
|
327
|
+
return None
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def extract_function(
|
|
331
|
+
file_content: str,
|
|
332
|
+
desired_line: int,
|
|
333
|
+
*,
|
|
334
|
+
language: str,
|
|
335
|
+
) -> Snippet | None:
|
|
336
|
+
language = TREE_SITTER_LANGUAGE_CONVERSION.get(language) or language
|
|
337
|
+
|
|
338
|
+
if language not in TREE_SITTER_FUNCTION_DECLARATION_MAP:
|
|
339
|
+
raise InvalidFileType
|
|
340
|
+
file_bytes = file_content.encode("utf-8")
|
|
341
|
+
func_nodes = extract_function_nodes(file_bytes, desired_line, language=language)
|
|
342
|
+
|
|
343
|
+
return extract_snippet_function_from_nodes(
|
|
344
|
+
file_bytes,
|
|
345
|
+
func_nodes,
|
|
346
|
+
desired_line,
|
|
347
|
+
language=language,
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _make_snippet_function(
|
|
352
|
+
*,
|
|
353
|
+
file_content: str,
|
|
354
|
+
viewport: SnippetViewport,
|
|
355
|
+
file_path: str | None = None,
|
|
356
|
+
language: str | None = None,
|
|
357
|
+
) -> Snippet | None:
|
|
358
|
+
if not language and file_path:
|
|
359
|
+
language = get_language_from_path(file_path)
|
|
360
|
+
if not language:
|
|
361
|
+
return None
|
|
362
|
+
|
|
363
|
+
vulnerability_snippet: Snippet | None = None
|
|
364
|
+
with suppress(InvalidFileType, OSError, UnicodeEncodeError):
|
|
365
|
+
vulnerability_snippet = extract_function(
|
|
366
|
+
file_content,
|
|
367
|
+
desired_line=viewport.line,
|
|
368
|
+
language=language,
|
|
369
|
+
)
|
|
370
|
+
if vulnerability_snippet:
|
|
371
|
+
vulnerability_snippet = vulnerability_snippet.reshape(
|
|
372
|
+
viewport=viewport._replace(
|
|
373
|
+
line_context=-1,
|
|
374
|
+
),
|
|
375
|
+
)
|
|
376
|
+
if not vulnerability_snippet:
|
|
377
|
+
return None
|
|
378
|
+
|
|
379
|
+
return vulnerability_snippet
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def make_snippet_function(
|
|
383
|
+
*,
|
|
384
|
+
file_content: str,
|
|
385
|
+
viewport: SnippetViewport,
|
|
386
|
+
file_path: str | None = None,
|
|
387
|
+
language: str | None = None,
|
|
388
|
+
) -> Snippet:
|
|
389
|
+
snippet = _make_snippet_function(
|
|
390
|
+
file_content=file_content,
|
|
391
|
+
viewport=viewport,
|
|
392
|
+
file_path=file_path,
|
|
393
|
+
language=language,
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
return snippet or _make_snippet(content=file_content, viewport=viewport)
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def make_hierarchical_snippets(
|
|
400
|
+
source_code: str,
|
|
401
|
+
language: str,
|
|
402
|
+
vulnerability_line: int,
|
|
403
|
+
) -> list[Snippet]:
|
|
404
|
+
"""Generate hierarchical code snippets for source code context.
|
|
405
|
+
|
|
406
|
+
It function is util for declarative languages like html, xml, json, etc.
|
|
407
|
+
|
|
408
|
+
Returns:
|
|
409
|
+
List of Snippet objects ordered by context specificity
|
|
410
|
+
from most specific to most general
|
|
411
|
+
|
|
412
|
+
"""
|
|
413
|
+
nodes = extract_hierarchical_nodes(
|
|
414
|
+
source_code=source_code.encode("utf-8"),
|
|
415
|
+
language=language,
|
|
416
|
+
desired_line=vulnerability_line,
|
|
417
|
+
)
|
|
418
|
+
snippets = []
|
|
419
|
+
for node in nodes:
|
|
420
|
+
content = get_node_content(source_code=source_code.encode("utf-8"), node=node)
|
|
421
|
+
|
|
422
|
+
offset = node.start_point[0] + 1 # Start of the node content
|
|
423
|
+
|
|
424
|
+
snippet = Snippet(
|
|
425
|
+
content=content,
|
|
426
|
+
offset=offset,
|
|
427
|
+
line=vulnerability_line,
|
|
428
|
+
show_line_numbers=False,
|
|
429
|
+
start_point=(node.start_point[0], node.start_point[1]),
|
|
430
|
+
end_point=(node.end_point[0], node.end_point[1]),
|
|
431
|
+
highlight_line_number=False,
|
|
432
|
+
line_context=(vulnerability_line - offset) - 1,
|
|
433
|
+
is_function=False,
|
|
434
|
+
function=None,
|
|
435
|
+
)
|
|
436
|
+
snippets.append(snippet)
|
|
437
|
+
return snippets
|
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
import importlib
|
|
2
|
+
import os
|
|
3
|
+
import re
|
|
4
|
+
from ctypes import (
|
|
5
|
+
c_void_p,
|
|
6
|
+
cdll,
|
|
7
|
+
)
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import cast
|
|
10
|
+
|
|
11
|
+
import tree_sitter
|
|
12
|
+
from more_itertools import (
|
|
13
|
+
mark_ends,
|
|
14
|
+
)
|
|
15
|
+
from tree_sitter import (
|
|
16
|
+
Language,
|
|
17
|
+
Node,
|
|
18
|
+
Parser,
|
|
19
|
+
QueryCursor,
|
|
20
|
+
Tree,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
TREE_SITTER_PARSERS_PATH = os.environ.get("TREE_SITTER_PARSERS")
|
|
24
|
+
TREE_SITTER_LANGUAGE_CONVERSION = {
|
|
25
|
+
"c_sharp": "csharp",
|
|
26
|
+
"razor": "csharp",
|
|
27
|
+
"tsx": "typescript",
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
_TREE_SITTER_GRAMMAR_NAMES = {
|
|
31
|
+
"csharp": "c_sharp",
|
|
32
|
+
"razor": "c_sharp",
|
|
33
|
+
"tsx": "typescript",
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
TREE_SITTER_SUPPORTED_IMPERATIVE_LANGUAGES = {
|
|
37
|
+
".aspx": "csharp",
|
|
38
|
+
".cs": "csharp",
|
|
39
|
+
".cshtml": "razor",
|
|
40
|
+
".dart": "dart",
|
|
41
|
+
".go": "go",
|
|
42
|
+
".java": "java",
|
|
43
|
+
".js": "javascript",
|
|
44
|
+
".mjs": "javascript",
|
|
45
|
+
".jsp": "java",
|
|
46
|
+
".jsx": "javascript",
|
|
47
|
+
".kt": "kotlin",
|
|
48
|
+
".php": "php",
|
|
49
|
+
".py": "python",
|
|
50
|
+
".razor": "razor",
|
|
51
|
+
".rb": "ruby",
|
|
52
|
+
".scala": "scala",
|
|
53
|
+
".swift": "swift",
|
|
54
|
+
".ts": "typescript",
|
|
55
|
+
".tsx": "typescript",
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
TREE_SITTER_SUPPORTED_DECLARATIVE_LANGUAGES = {
|
|
59
|
+
".tf": "hcl",
|
|
60
|
+
".hcl": "hcl",
|
|
61
|
+
".html": "html",
|
|
62
|
+
".yaml": "yaml",
|
|
63
|
+
".yml": "yaml",
|
|
64
|
+
".xml": "xml",
|
|
65
|
+
"dockerfile": "dockerfile",
|
|
66
|
+
".json": "json",
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
TREE_SITTER_SUPPORTED_LANGUAGES = {
|
|
70
|
+
**TREE_SITTER_SUPPORTED_IMPERATIVE_LANGUAGES,
|
|
71
|
+
**TREE_SITTER_SUPPORTED_DECLARATIVE_LANGUAGES,
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
TREE_SITTER_IMPORTS_DECLARATIONS_MAP: dict[str, tuple[str, ...]] = {
|
|
76
|
+
"java": ("import_declaration",),
|
|
77
|
+
"csharp": ("using_directive",),
|
|
78
|
+
"dart": ("import_specification",),
|
|
79
|
+
"go": ("import_declaration",),
|
|
80
|
+
"javascript": ("import_statement",),
|
|
81
|
+
"kotlin": ("import",),
|
|
82
|
+
"php": ("use_declaration", "namespace_use_declaration"),
|
|
83
|
+
"python": ("import_statement", "import_from_statement"),
|
|
84
|
+
"scala": ("import_declaration",),
|
|
85
|
+
"swift": ("import_declaration",),
|
|
86
|
+
"typescript": ("import_statement",),
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
TREE_SITTER_FUNCTION_DECLARATION_MAP: dict[str, tuple[str, ...]] = {
|
|
90
|
+
"csharp": (
|
|
91
|
+
"constructor_declaration",
|
|
92
|
+
"conversion_operator_declaration",
|
|
93
|
+
"destructor_declaration",
|
|
94
|
+
"method_declaration",
|
|
95
|
+
),
|
|
96
|
+
"dart": (
|
|
97
|
+
"function_signature",
|
|
98
|
+
"function_body",
|
|
99
|
+
"getter_signature",
|
|
100
|
+
"setter_signature",
|
|
101
|
+
"method_signature",
|
|
102
|
+
),
|
|
103
|
+
"dockerfile": (
|
|
104
|
+
"from_instruction",
|
|
105
|
+
"workdir_instruction",
|
|
106
|
+
"comment",
|
|
107
|
+
"copy_instruction",
|
|
108
|
+
"run_instruction",
|
|
109
|
+
"expose_instruction",
|
|
110
|
+
"user_instruction",
|
|
111
|
+
"cmd_instruction",
|
|
112
|
+
),
|
|
113
|
+
"go": (
|
|
114
|
+
"function_declaration",
|
|
115
|
+
"method_declaration",
|
|
116
|
+
),
|
|
117
|
+
"html": (
|
|
118
|
+
"document",
|
|
119
|
+
"element",
|
|
120
|
+
"style_element",
|
|
121
|
+
"script_element",
|
|
122
|
+
),
|
|
123
|
+
"java": (
|
|
124
|
+
"constructor_declaration",
|
|
125
|
+
"method_declaration",
|
|
126
|
+
"record_declaration",
|
|
127
|
+
),
|
|
128
|
+
"javascript": (
|
|
129
|
+
"arrow_function",
|
|
130
|
+
"generator_function",
|
|
131
|
+
"generator_function_declaration",
|
|
132
|
+
"method_definition",
|
|
133
|
+
"function_expression",
|
|
134
|
+
"function_declaration",
|
|
135
|
+
),
|
|
136
|
+
"json": (
|
|
137
|
+
"object",
|
|
138
|
+
"array",
|
|
139
|
+
),
|
|
140
|
+
"kotlin": (
|
|
141
|
+
"function_declaration",
|
|
142
|
+
"primary_constructor",
|
|
143
|
+
"getter",
|
|
144
|
+
"setter",
|
|
145
|
+
),
|
|
146
|
+
"php": (
|
|
147
|
+
"function_definition",
|
|
148
|
+
"function_static_declaration",
|
|
149
|
+
"method_declaration",
|
|
150
|
+
),
|
|
151
|
+
"python": ("function_definition",),
|
|
152
|
+
"ruby": (
|
|
153
|
+
"method",
|
|
154
|
+
"singleton_method",
|
|
155
|
+
"setter",
|
|
156
|
+
),
|
|
157
|
+
"scala": (
|
|
158
|
+
"function_declaration",
|
|
159
|
+
"function_definition",
|
|
160
|
+
),
|
|
161
|
+
"swift": (
|
|
162
|
+
"function_declaration",
|
|
163
|
+
"protocol_function_declaration",
|
|
164
|
+
),
|
|
165
|
+
"typescript": (
|
|
166
|
+
"function_declaration",
|
|
167
|
+
"generator_function_declaration",
|
|
168
|
+
"method_definition",
|
|
169
|
+
"function_expression",
|
|
170
|
+
"arrow_function",
|
|
171
|
+
"function_signature",
|
|
172
|
+
"function_type",
|
|
173
|
+
),
|
|
174
|
+
"hcl": ("block",),
|
|
175
|
+
"yaml": (
|
|
176
|
+
"block_mapping_pair",
|
|
177
|
+
"block_mapping",
|
|
178
|
+
"block_sequence",
|
|
179
|
+
"block_sequence_item",
|
|
180
|
+
),
|
|
181
|
+
"yml": (
|
|
182
|
+
"block_mapping_pair",
|
|
183
|
+
"block_mapping",
|
|
184
|
+
"block_sequence",
|
|
185
|
+
"block_sequence_item",
|
|
186
|
+
),
|
|
187
|
+
"xml": ("element",),
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
TREE_SITTER_FUNCTION_IDENTIFIER = {
|
|
191
|
+
"python": {
|
|
192
|
+
"function_definition": "name",
|
|
193
|
+
},
|
|
194
|
+
"bash": {
|
|
195
|
+
"function_definition": "name",
|
|
196
|
+
},
|
|
197
|
+
"csharp": {
|
|
198
|
+
"constructor_declaration": "name",
|
|
199
|
+
"conversion_operator_declaration": "type",
|
|
200
|
+
"destructor_declaration": "name",
|
|
201
|
+
"method_declaration": "name",
|
|
202
|
+
},
|
|
203
|
+
"dart": {
|
|
204
|
+
"function_signature": "name",
|
|
205
|
+
"getter_signature": "name",
|
|
206
|
+
"setter_signature": "name",
|
|
207
|
+
"method_signature": "name",
|
|
208
|
+
},
|
|
209
|
+
"go": {
|
|
210
|
+
"function_declaration": "name",
|
|
211
|
+
"method_declaration": "name",
|
|
212
|
+
},
|
|
213
|
+
"java": {
|
|
214
|
+
"constructor_declaration": "name",
|
|
215
|
+
"method_declaration": "name",
|
|
216
|
+
"record_declaration": "name",
|
|
217
|
+
},
|
|
218
|
+
"javascript": {
|
|
219
|
+
"generator_function": "name",
|
|
220
|
+
"generator_function_declaration": "name",
|
|
221
|
+
"method_definition": "name",
|
|
222
|
+
"function_expression": "name",
|
|
223
|
+
"function_declaration": "name",
|
|
224
|
+
},
|
|
225
|
+
"kotlin": {
|
|
226
|
+
"function_declaration": "identifier",
|
|
227
|
+
},
|
|
228
|
+
"php": {
|
|
229
|
+
"function_definition": "name",
|
|
230
|
+
"function_static_declaration": "name",
|
|
231
|
+
"method_declaration": "name",
|
|
232
|
+
},
|
|
233
|
+
"ruby": {
|
|
234
|
+
"method": "name",
|
|
235
|
+
"singleton_method": "name",
|
|
236
|
+
"setter": "name",
|
|
237
|
+
},
|
|
238
|
+
"scala": {
|
|
239
|
+
"function_declaration": "name",
|
|
240
|
+
"function_definition": "name",
|
|
241
|
+
},
|
|
242
|
+
"swift": {
|
|
243
|
+
"function_declaration": "name",
|
|
244
|
+
"protocol_function_declaration": "name",
|
|
245
|
+
},
|
|
246
|
+
"typescript": {
|
|
247
|
+
"function_declaration": "name",
|
|
248
|
+
"generator_function_declaration": "name",
|
|
249
|
+
"method_definition": "name",
|
|
250
|
+
"function_expression": "name",
|
|
251
|
+
},
|
|
252
|
+
}
|
|
253
|
+
TREE_SITTER_REQUIRES_SEQUENCE = {
|
|
254
|
+
"dart": ("function_signature", "function_body"),
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
class InvalidFileType(Exception): # noqa: N818
|
|
259
|
+
"""Exception to control file type."""
|
|
260
|
+
|
|
261
|
+
def __init__(self, detail: str = "") -> None:
|
|
262
|
+
msg = "Exception - Invalid file type"
|
|
263
|
+
if detail:
|
|
264
|
+
msg += f": {detail}"
|
|
265
|
+
super().__init__(msg)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def ignore_advisories(where: str | None) -> str:
|
|
269
|
+
if where is not None:
|
|
270
|
+
where = re.sub(r"(\s+\(.*\))?(\s+\[.*\])?", "", where)
|
|
271
|
+
return str(where)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def get_node_content(source_code: bytes, node: Node) -> str:
|
|
275
|
+
"""Extract text content from a tree-sitter node."""
|
|
276
|
+
return source_code[node.start_byte : node.end_byte].decode("utf-8")
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def get_language_from_path(path: str) -> str | None:
|
|
280
|
+
path = ignore_advisories(path)
|
|
281
|
+
language = TREE_SITTER_SUPPORTED_LANGUAGES.get(Path(path).suffix, "")
|
|
282
|
+
language = language or TREE_SITTER_SUPPORTED_LANGUAGES.get(Path(path).name, "")
|
|
283
|
+
if not language:
|
|
284
|
+
return None
|
|
285
|
+
return language
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def get_language_for_tree(language: str) -> Language:
|
|
289
|
+
tree_sitter_lang = _TREE_SITTER_GRAMMAR_NAMES.get(language, language)
|
|
290
|
+
try:
|
|
291
|
+
parser_name = (
|
|
292
|
+
f"tree_sitter_{tree_sitter_lang}"
|
|
293
|
+
if tree_sitter_lang != "dart"
|
|
294
|
+
else "tree_sitter_dart_orchard"
|
|
295
|
+
)
|
|
296
|
+
language_module = importlib.import_module(parser_name)
|
|
297
|
+
function_name = {
|
|
298
|
+
"php": "language_php",
|
|
299
|
+
"tsx": "language_tsx",
|
|
300
|
+
"typescript": "language_tsx",
|
|
301
|
+
"xml": "language_xml",
|
|
302
|
+
}.get(tree_sitter_lang, "language")
|
|
303
|
+
language_so = Language(getattr(language_module, function_name)())
|
|
304
|
+
except (ImportError, ModuleNotFoundError) as exc:
|
|
305
|
+
so_library_path: str = os.path.join( # noqa: PTH118
|
|
306
|
+
TREE_SITTER_PARSERS_PATH or "", f"{tree_sitter_lang}.so"
|
|
307
|
+
)
|
|
308
|
+
if not Path(so_library_path).exists():
|
|
309
|
+
raise InvalidFileType(so_library_path) from exc
|
|
310
|
+
|
|
311
|
+
lib = cdll.LoadLibrary(os.fspath(so_library_path))
|
|
312
|
+
language_function = getattr(lib, f"tree_sitter_{tree_sitter_lang}")
|
|
313
|
+
language_function.restype = c_void_p
|
|
314
|
+
language_ptr = language_function()
|
|
315
|
+
language_so = Language(language_ptr)
|
|
316
|
+
|
|
317
|
+
return language_so
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def parse_content_tree_sitter(
|
|
321
|
+
content: bytes,
|
|
322
|
+
language: str,
|
|
323
|
+
) -> Tree:
|
|
324
|
+
language_so = get_language_for_tree(language)
|
|
325
|
+
|
|
326
|
+
parser: Parser = Parser(language_so)
|
|
327
|
+
return parser.parse(content)
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def query_nodes_by_language(
|
|
331
|
+
language: str,
|
|
332
|
+
tree: Tree,
|
|
333
|
+
queries_map: dict[str, tuple[str, ...]],
|
|
334
|
+
) -> dict[str, list[Node]]:
|
|
335
|
+
try:
|
|
336
|
+
language_parser = get_language_for_tree(language)
|
|
337
|
+
except OSError as exc:
|
|
338
|
+
raise InvalidFileType from exc
|
|
339
|
+
query = "\n".join(f"({item}) @{item}" for item in queries_map.get(language, []))
|
|
340
|
+
query_cursor = QueryCursor(tree_sitter.Query(language_parser, query))
|
|
341
|
+
return query_cursor.captures(tree.root_node)
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def query_function_by_name(
|
|
345
|
+
language: str,
|
|
346
|
+
tree: Tree,
|
|
347
|
+
function_node_type: str,
|
|
348
|
+
node_identifier_name: str,
|
|
349
|
+
function_name: str,
|
|
350
|
+
) -> list[Node]:
|
|
351
|
+
language_parser = get_language_for_tree(language)
|
|
352
|
+
query = f"""({function_node_type}
|
|
353
|
+
name: ({node_identifier_name}) @name
|
|
354
|
+
(#eq? @name "{function_name}")
|
|
355
|
+
) @{function_node_type}"""
|
|
356
|
+
result = QueryCursor(tree_sitter.Query(language_parser, query)).captures(tree.root_node)
|
|
357
|
+
return [node for key, nodes in result.items() for node in nodes if key == function_node_type]
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def find_function_expression_by_query(
|
|
361
|
+
language: str,
|
|
362
|
+
original_node: Node,
|
|
363
|
+
desired_line: int,
|
|
364
|
+
) -> Node | None:
|
|
365
|
+
language_parser = get_language_for_tree(language)
|
|
366
|
+
target_nodes: tuple[str, ...] = TREE_SITTER_FUNCTION_DECLARATION_MAP.get(language, ())
|
|
367
|
+
query = "([" + (" ".join(f"({item})" for item in target_nodes)) + "] @target )"
|
|
368
|
+
result = QueryCursor(tree_sitter.Query(language_parser, query)).captures(original_node)
|
|
369
|
+
|
|
370
|
+
nodes = [node for _, nodes in result.items() for node in nodes]
|
|
371
|
+
candidates = [
|
|
372
|
+
x
|
|
373
|
+
for x in sorted(
|
|
374
|
+
nodes,
|
|
375
|
+
key=lambda node: node.end_byte - node.start_byte,
|
|
376
|
+
)
|
|
377
|
+
if x.start_point[0] <= desired_line - 1 <= x.end_point[0]
|
|
378
|
+
]
|
|
379
|
+
if not candidates:
|
|
380
|
+
return None
|
|
381
|
+
|
|
382
|
+
final_candidate = candidates[0]
|
|
383
|
+
for node in candidates:
|
|
384
|
+
if (node.end_byte - node.start_byte) < 500:
|
|
385
|
+
final_candidate = node
|
|
386
|
+
continue
|
|
387
|
+
return node
|
|
388
|
+
|
|
389
|
+
return final_candidate
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
def find_function_expression(original_node: Node | None, language: str) -> Node | None:
|
|
393
|
+
target_nodes: tuple[str, ...] = TREE_SITTER_FUNCTION_DECLARATION_MAP.get(language, ())
|
|
394
|
+
node = original_node
|
|
395
|
+
while node:
|
|
396
|
+
for node_name in target_nodes:
|
|
397
|
+
if node_name == node.type:
|
|
398
|
+
return node
|
|
399
|
+
node = node.parent
|
|
400
|
+
|
|
401
|
+
return None
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def extract_function_nodes(
|
|
405
|
+
source_code: bytes,
|
|
406
|
+
desired_line: int,
|
|
407
|
+
*,
|
|
408
|
+
language: str,
|
|
409
|
+
) -> list[Node]:
|
|
410
|
+
language = TREE_SITTER_LANGUAGE_CONVERSION.get(language) or language
|
|
411
|
+
|
|
412
|
+
if language not in TREE_SITTER_FUNCTION_DECLARATION_MAP:
|
|
413
|
+
raise InvalidFileType
|
|
414
|
+
try:
|
|
415
|
+
tree = parse_content_tree_sitter(source_code, language)
|
|
416
|
+
except OSError:
|
|
417
|
+
return []
|
|
418
|
+
root_node = tree.root_node
|
|
419
|
+
|
|
420
|
+
captures = query_nodes_by_language(
|
|
421
|
+
language=language,
|
|
422
|
+
tree=tree,
|
|
423
|
+
queries_map=TREE_SITTER_FUNCTION_DECLARATION_MAP,
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
if language in TREE_SITTER_REQUIRES_SEQUENCE and captures:
|
|
427
|
+
return _process_sequence(captures, desired_line)
|
|
428
|
+
|
|
429
|
+
return _process_non_sequence(source_code, desired_line, root_node, language)
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def _process_sequence(
|
|
433
|
+
captures: dict[str, list[Node]],
|
|
434
|
+
desired_line: int,
|
|
435
|
+
) -> list[Node]:
|
|
436
|
+
sorted_lists = [sorted(nodes, key=lambda node: node.start_byte) for nodes in captures.values()]
|
|
437
|
+
for items in zip(*sorted_lists, strict=False):
|
|
438
|
+
if items[0].start_point[0] <= desired_line - 1 <= items[-1].end_point[0]:
|
|
439
|
+
return cast("list[Node]", list(items))
|
|
440
|
+
return []
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
def _process_non_sequence(
|
|
444
|
+
source_code: bytes,
|
|
445
|
+
desired_line: int,
|
|
446
|
+
root_node: Node,
|
|
447
|
+
language: str,
|
|
448
|
+
) -> list[Node]:
|
|
449
|
+
lines = source_code.splitlines()
|
|
450
|
+
if not lines:
|
|
451
|
+
return []
|
|
452
|
+
|
|
453
|
+
function_node = find_function_expression_by_query(language, root_node, desired_line)
|
|
454
|
+
|
|
455
|
+
return [function_node] if function_node else []
|
|
456
|
+
|
|
457
|
+
|
|
458
|
+
def _process_hierarchical_candidates(
|
|
459
|
+
language: str,
|
|
460
|
+
root_node: Node,
|
|
461
|
+
desired_line: int,
|
|
462
|
+
) -> list[Node]:
|
|
463
|
+
"""Find all nodes containing the target line, sorted by specificity.
|
|
464
|
+
|
|
465
|
+
Returns:
|
|
466
|
+
List of nodes ordered from most specific to most general
|
|
467
|
+
|
|
468
|
+
"""
|
|
469
|
+
language_parser = get_language_for_tree(language)
|
|
470
|
+
target_nodes: tuple[str, ...] = TREE_SITTER_FUNCTION_DECLARATION_MAP.get(language, ())
|
|
471
|
+
query = "([" + (" ".join(f"({item})" for item in target_nodes)) + "] @target )"
|
|
472
|
+
result = QueryCursor(tree_sitter.Query(language_parser, query)).captures(root_node)
|
|
473
|
+
|
|
474
|
+
nodes = [node for _, nodes in result.items() for node in nodes]
|
|
475
|
+
return [
|
|
476
|
+
node
|
|
477
|
+
for node in sorted(
|
|
478
|
+
nodes,
|
|
479
|
+
key=lambda node: node.end_byte - node.start_byte, # More specific first
|
|
480
|
+
)
|
|
481
|
+
if node.start_point[0] <= desired_line - 1 <= node.end_point[0]
|
|
482
|
+
]
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
def extract_hierarchical_nodes(
|
|
486
|
+
source_code: bytes,
|
|
487
|
+
desired_line: int,
|
|
488
|
+
language: str,
|
|
489
|
+
) -> list[Node]:
|
|
490
|
+
"""Extract hierarchical nodes containing the specified line.
|
|
491
|
+
|
|
492
|
+
Returns:
|
|
493
|
+
List of tree-sitter nodes in hierarchical order from most specific to most general
|
|
494
|
+
|
|
495
|
+
"""
|
|
496
|
+
language = TREE_SITTER_LANGUAGE_CONVERSION.get(language) or language
|
|
497
|
+
|
|
498
|
+
try:
|
|
499
|
+
tree = parse_content_tree_sitter(source_code, language)
|
|
500
|
+
except OSError:
|
|
501
|
+
return []
|
|
502
|
+
|
|
503
|
+
return _process_hierarchical_candidates(
|
|
504
|
+
language=language,
|
|
505
|
+
root_node=tree.root_node,
|
|
506
|
+
desired_line=desired_line,
|
|
507
|
+
)
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
def _calculate_positions(lines: list[bytes], desired_line: int) -> tuple[int | None, int | None]:
|
|
511
|
+
try:
|
|
512
|
+
start_position = 0
|
|
513
|
+
blank_spaces = 0
|
|
514
|
+
for _, last, index in mark_ends(range(desired_line)):
|
|
515
|
+
if last and (match := re.match(r"^ *", lines[index].decode())):
|
|
516
|
+
blank_spaces = len(match.group(0))
|
|
517
|
+
start_position += blank_spaces
|
|
518
|
+
else:
|
|
519
|
+
start_position += len(lines[index]) + 1
|
|
520
|
+
end_position = start_position + len(lines[desired_line - 1]) - blank_spaces
|
|
521
|
+
except IndexError:
|
|
522
|
+
return None, None
|
|
523
|
+
else:
|
|
524
|
+
return start_position, end_position
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
def extract_function_start_end_bytes(
|
|
528
|
+
source_code: bytes,
|
|
529
|
+
desired_line: int,
|
|
530
|
+
*,
|
|
531
|
+
language: str,
|
|
532
|
+
) -> tuple[int, int] | None:
|
|
533
|
+
language = TREE_SITTER_LANGUAGE_CONVERSION.get(language) or language
|
|
534
|
+
|
|
535
|
+
if language not in TREE_SITTER_FUNCTION_DECLARATION_MAP:
|
|
536
|
+
raise InvalidFileType
|
|
537
|
+
|
|
538
|
+
func_nodes = extract_function_nodes(source_code, desired_line, language=language)
|
|
539
|
+
if not func_nodes:
|
|
540
|
+
return None
|
|
541
|
+
|
|
542
|
+
return (func_nodes[0].start_byte, func_nodes[-1].end_byte)
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
def extract_imports(source_code: str, language: str) -> tuple[Node, ...]:
|
|
546
|
+
language = TREE_SITTER_LANGUAGE_CONVERSION.get(language) or language
|
|
547
|
+
if language not in TREE_SITTER_IMPORTS_DECLARATIONS_MAP:
|
|
548
|
+
return ()
|
|
549
|
+
|
|
550
|
+
tree = parse_content_tree_sitter(source_code.encode("utf-8"), language)
|
|
551
|
+
result = query_nodes_by_language(language, tree, TREE_SITTER_IMPORTS_DECLARATIONS_MAP)
|
|
552
|
+
return tuple(node for nodes in result.values() for node in nodes)
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
def indent_function(file_content_bytes: bytes, new_function: str, function_location: int) -> str:
|
|
556
|
+
indentation = (
|
|
557
|
+
file_content_bytes[
|
|
558
|
+
function_location
|
|
559
|
+
- next(
|
|
560
|
+
(
|
|
561
|
+
index
|
|
562
|
+
for index, character in enumerate(
|
|
563
|
+
reversed(file_content_bytes[:function_location]),
|
|
564
|
+
)
|
|
565
|
+
if character
|
|
566
|
+
not in (
|
|
567
|
+
10, # newline
|
|
568
|
+
32, # space
|
|
569
|
+
)
|
|
570
|
+
),
|
|
571
|
+
0,
|
|
572
|
+
) : function_location
|
|
573
|
+
]
|
|
574
|
+
.decode(encoding="utf-8")
|
|
575
|
+
.replace("\n", "")
|
|
576
|
+
)
|
|
577
|
+
return "\n".join([indentation + line if line else line for line in new_function.splitlines()])
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: fluidattacks_core_serializers
|
|
3
|
+
Version: 12.0.0
|
|
4
|
+
Summary: Fluid Attacks Core Serializers Library
|
|
5
|
+
Author-email: Development <development@fluidattacks.com>
|
|
6
|
+
License: MPL-2.0
|
|
7
|
+
Classifier: Development Status :: 1 - Planning
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
13
|
+
Requires-Python: >=3.11
|
|
14
|
+
Requires-Dist: more-itertools>=10.0.0
|
|
15
|
+
Requires-Dist: tree-sitter-c-sharp>=0.23.1
|
|
16
|
+
Requires-Dist: tree-sitter-dart-orchard>=0.3.2
|
|
17
|
+
Requires-Dist: tree-sitter-go>=0.23.4
|
|
18
|
+
Requires-Dist: tree-sitter-html>=0.23.2
|
|
19
|
+
Requires-Dist: tree-sitter-java>=0.23.4
|
|
20
|
+
Requires-Dist: tree-sitter-javascript>=0.23.1
|
|
21
|
+
Requires-Dist: tree-sitter-json>=0.24.8
|
|
22
|
+
Requires-Dist: tree-sitter-kotlin>=1.0.1
|
|
23
|
+
Requires-Dist: tree-sitter-php>=0.23.11
|
|
24
|
+
Requires-Dist: tree-sitter-python>=0.23.4
|
|
25
|
+
Requires-Dist: tree-sitter-typescript>=0.23.2
|
|
26
|
+
Requires-Dist: tree-sitter-xml>=0.7.0
|
|
27
|
+
Requires-Dist: tree-sitter-yaml>=0.7.0
|
|
28
|
+
Requires-Dist: tree-sitter>=0.25.2
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
fluidattacks_core/serializers/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
fluidattacks_core/serializers/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
fluidattacks_core/serializers/snippet.py,sha256=vze5-dAKBelYTEONCjQqZNUUfAshyaLqPwBrCVvuSS8,13988
|
|
4
|
+
fluidattacks_core/serializers/syntax.py,sha256=AbLy2jWKaqq24ee3Y3aGV4dwWDYHmXehRoVcba5qAaw,16377
|
|
5
|
+
fluidattacks_core_serializers-12.0.0.dist-info/METADATA,sha256=fFg2aYB0NEMIrV05vDrjZ2pYcUSxT1M0qo5OnaezLcU,1154
|
|
6
|
+
fluidattacks_core_serializers-12.0.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
|
|
7
|
+
fluidattacks_core_serializers-12.0.0.dist-info/RECORD,,
|