Skip to content

Commit

Permalink
linter: pre-compile disable/enable rules regexes
Browse files Browse the repository at this point in the history
Not only this should improve performance, but I find the code more
readable.
  • Loading branch information
DimitriPapadopoulos authored and adrienverge committed Aug 6, 2022
1 parent 8683506 commit 6b6fdba
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions yamllint/linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
'error': 2,
}

DISABLE_RULE_PATTERN = re.compile(r'^# yamllint disable( rule:\S+)*\s*$')
ENABLE_RULE_PATTERN = re.compile(r'^# yamllint enable( rule:\S+)*\s*$')


class LintProblem(object):
"""Represents a linting problem found by yamllint."""
Expand Down Expand Up @@ -82,7 +85,7 @@ def __init__(self):
def process_comment(self, comment):
comment = str(comment)

if re.match(r'^# yamllint disable( rule:\S+)*\s*$', comment):
if DISABLE_RULE_PATTERN.match(comment):
items = comment[18:].rstrip().split(' ')
rules = [item[5:] for item in items][1:]
if len(rules) == 0:
Expand All @@ -92,7 +95,7 @@ def process_comment(self, comment):
if id in self.all_rules:
self.rules.add(id)

elif re.match(r'^# yamllint enable( rule:\S+)*\s*$', comment):
elif ENABLE_RULE_PATTERN.match(comment):
items = comment[17:].rstrip().split(' ')
rules = [item[5:] for item in items][1:]
if len(rules) == 0:
Expand Down

0 comments on commit 6b6fdba

Please sign in to comment.