Skip to content

Commit

Permalink
Change: to default sorting in fix in spaces_before_dots
Browse files Browse the repository at this point in the history
  • Loading branch information
NiklasHargarter authored and mbrinkhoff committed Oct 1, 2024
1 parent 93c0afa commit a48398c
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions troubadix/plugins/spaces_before_dots.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# SPDX-FileCopyrightText: 2024 Greenbone AG
import re
from collections.abc import Iterator
from operator import itemgetter
from pathlib import Path

from troubadix.helper import CURRENT_ENCODING
Expand Down Expand Up @@ -33,8 +32,8 @@
PATTERN = re.compile(r"\s+\.(\s|$)")
IGNORE = [
# 21.04 and 22.04 are generated and should not be touched manually
"21.04",
"22.04",
"21.04/",
"22.04/",
# uses dots for beginning of entry in enumeration
"common/2008/debian/deb_246.nasl",
"common/2008/debian/deb_266.nasl",
Expand Down Expand Up @@ -85,29 +84,31 @@ def check_content(
for tag in TAGS:
pattern = get_script_tag_pattern(tag)
match = pattern.search(file_content)
if match:
value = match.group("value")
value_start = match.start("value")
if not match:
continue

for excess_match in PATTERN.finditer(value):
whitespace_pos = excess_match.start() + value_start
self.matches.append((whitespace_pos, excess_match.group()))
fullmatch = match.group()
yield LinterWarning(
f"value of script_tag {match.group('name')} has at least"
" one occurence of excess whitespace before a dot:"
f"\n '{fullmatch}'",
file=nasl_file,
plugin=self.name,
)
value = match.group("value")
value_start = match.start("value")

for excess_match in PATTERN.finditer(value):
whitespace_pos = excess_match.start() + value_start
self.matches.append((whitespace_pos, excess_match.group()))
fullmatch = match.group()
yield LinterWarning(
f"value of script_tag {match.group('name')} has at least"
" one occurence of excess whitespace before a dot:"
f"\n '{fullmatch}'",
file=nasl_file,
plugin=self.name,
)

def fix(self) -> Iterator[LinterResult]:

if not self.matches:
return

# Sort matches by position, descending order to avoid messing up indices during replacement
self.matches.sort(reverse=True, key=itemgetter(0))
self.matches.sort(reverse=True)

file_content = self.context.file_content
for pos, match_str in self.matches:
Expand Down

0 comments on commit a48398c

Please sign in to comment.