linter: learn to check filename to match rule name

closes #7
This commit is contained in:
William Ballenthin
2020-06-26 18:16:20 -06:00
parent 5de0884dd2
commit 22537eb936

View File

@@ -6,6 +6,7 @@ Usage:
$ python scripts/lint.py rules/
'''
import os
import os.path
import sys
import string
import hashlib
@@ -39,6 +40,25 @@ class NameCasing(Lint):
rule.name[1] not in string.ascii_uppercase)
class FilenameDoesntMatchRuleName(Lint):
name = 'filename doesn\'t match the rule name'
recommendation = 'Rename rule file to match the rule name'
def check_rule(self, ctx, rule):
expected = rule.name
expected = expected.lower()
expected = expected.replace(" ", "-")
expected = expected.replace("(", "")
expected = expected.replace(")", "")
expected = expected.replace("+", "")
expected = expected.replace("/", "")
expected = expected + ".yml"
found = os.path.basename(rule.meta['capa/path'])
return expected != found
class MissingNamespace(Lint):
name = 'missing rule namespace'
recommendation = 'Add meta.namespace so that the rule is emitted correctly'
@@ -172,6 +192,7 @@ def run_feature_lints(lints, ctx, features):
NAME_LINTS = (
NameCasing(),
FilenameDoesntMatchRuleName(),
)