Skip to content

Commit 6bb533c

Browse files
author
Tamar Weisskopf
committed
extract normalization logic into helper function
1 parent 5587ef3 commit 6bb533c

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

src/vuln_analysis/utils/functions_parsers/javascript_functions_parser.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ def _split_into_statements(text: str) -> list[str]:
236236
- Function/class declarations (including multiline parameter lists)
237237
- Braces { and }
238238
"""
239+
def normalize(line: str) -> str:
240+
"""Normalize whitespace: collapse multiple spaces/newlines into single space and strip."""
241+
return re.sub(r'\s+', ' ', line).strip()
242+
239243
parts = []
240244
current = ''
241245
in_string = False
@@ -281,42 +285,40 @@ def _split_into_statements(text: str) -> list[str]:
281285
current += char
282286

283287
if just_closed_block_comment and char == '/':
284-
normalized = re.sub(r'\s+', ' ', current).strip()
288+
normalized = normalize(current)
285289
if normalized.startswith('/*') and normalized.endswith('*/'):
286290
parts.append(normalized)
287291
current = ''
288292
just_closed_block_comment = False
289293

290294
if char == '\n' and in_line_comment:
291295
in_line_comment = False
292-
current = re.sub(r'\s+', ' ', current).strip()
293-
parts.append(current)
296+
parts.append(normalize(current))
294297
current = ''
295298

296299
elif char == ';' and not in_string and not in_block_comment and not in_line_comment:
297-
current = re.sub(r'\s+', ' ', current).strip()
298-
parts.append(current)
300+
parts.append(normalize(current))
299301
current = ''
300302

301303
# Split on opening brace { for function/method body
302304
elif char == '{' and not in_string and not in_block_comment and not in_line_comment:
303-
normalized = re.sub(r'\s+', ' ', current).strip()
305+
normalized = normalize(current)
304306
if just_closed_paren or any(normalized.startswith(kw) for kw in ['class ', 'export class ']):
305307
if normalized:
306308
parts.append(normalized)
307309
current = ''
308310
just_closed_paren = False
309311

310312
elif char == '\n' and not in_string and not in_block_comment:
311-
normalized = re.sub(r'\s+', ' ', current).strip()
313+
normalized = normalize(current)
312314
if normalized:
313315
if normalized == '}':
314316
parts.append(normalized)
315317
current = ''
316318

317319
# Add remaining content
318320
if current:
319-
normalized = re.sub(r'\s+', ' ', current).strip()
321+
normalized = normalize(current)
320322
if normalized:
321323
parts.append(normalized)
322324

0 commit comments

Comments
 (0)