Skip to content

Commit

Permalink
Check if bad pattern applies to filename via regex
Browse files Browse the repository at this point in the history
Previously, we only used file extensions to check whether a
given bad pattern applies.
  • Loading branch information
falko17 committed Aug 15, 2023
1 parent 4cbd80b commit 42a7192
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions GitScripts/check_for_bad_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class Level(str, Enum):
ERROR = ":x:"


# Extensions that a pattern will be applied to by default.
DEFAULT_EXTENSIONS = ("cs",)
# Filenames that a pattern will be applied to by default.
DEFAULT_FILENAMES = (r".*\.cs$",)

# List of matches to be printed as a JSON array at the end of the script.
collected_matches: List[Dict[str, Union[str, int]]] = []
Expand All @@ -42,15 +42,15 @@ def __init__(
self,
regex,
message,
extensions=DEFAULT_EXTENSIONS,
filenames=DEFAULT_FILENAMES,
suggestion=None,
level=Level.INFO,
see_only=True,
):
"""
Takes a compiled regular expression `regex` that is checked against
every line within changed files having an extension contained in
`extensions`, a `message` that shall be displayed to the user in case
every line within changed files having a filename matched by a regex contained in
`filenames`, a `message` that shall be displayed to the user in case
a match has been found, a regex substitution `suggestion` for a found
bad pattern, and a severity `level`.
If `see_only` is set to `True`, the pattern will only be applied to
Expand All @@ -62,7 +62,7 @@ def __init__(
regex = re.compile(r".*")
self.regex = regex
self.message = message
self.extensions = extensions
self.filenames = [re.compile(x) for x in filenames]
self.suggestion = suggestion
self.level = level
self.see_only = see_only
Expand All @@ -71,14 +71,13 @@ def applies_to(self, filename: str, line: str = "") -> bool:
"""
Returns whether the given pattern applies to the given filename.
A pattern applies to a filename if:
* The filename has an extension contained in `extensions`.
* The filename matches one of the regexes in `filenames`.
* The filename starts with `Assets/SEE/` if `see_only` is set to `True`.
* The line matches the regular expression `regex`.
"""
extension = filename.rsplit(".", 1)[1] if "." in filename else ""
return (
(not self.see_only or filename.startswith("Assets/SEE/"))
and extension in self.extensions
and any(x.match(filename) is not None for x in self.filenames)
and self.regex.match(line) is not None
)

Expand Down Expand Up @@ -126,7 +125,7 @@ def to_json(
This happens on Linux systems automatically, but Windows systems will change this back.
We should just leave it as a backslash.""",
suggestion=r"\1\SteamVR\actions.json\2",
extensions=["asset"],
filenames=[r".*\.asset$"],
level=Level.WARN,
see_only=False,
),
Expand Down

0 comments on commit 42a7192

Please sign in to comment.