From 22537eb93685e1441550f4793a37ffdba9fd45e9 Mon Sep 17 00:00:00 2001 From: William Ballenthin Date: Fri, 26 Jun 2020 18:16:20 -0600 Subject: [PATCH] linter: learn to check filename to match rule name closes #7 --- scripts/lint.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/scripts/lint.py b/scripts/lint.py index 29e30b97..c6fa514b 100644 --- a/scripts/lint.py +++ b/scripts/lint.py @@ -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(), )