Skip to content

Commit 6b6fdba

Browse files
DimitriPapadopoulosadrienverge
authored andcommitted
linter: pre-compile disable/enable rules regexes
Not only this should improve performance, but I find the code more readable.
1 parent 8683506 commit 6b6fdba

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

yamllint/linter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
'error': 2,
3030
}
3131

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

3336
class LintProblem(object):
3437
"""Represents a linting problem found by yamllint."""
@@ -82,7 +85,7 @@ def __init__(self):
8285
def process_comment(self, comment):
8386
comment = str(comment)
8487

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

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

0 commit comments

Comments
 (0)