src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/NameOrder/NameOrderChecker.py

branch
eric7
changeset 11150
73d80859079c
parent 11145
d328a7b74fd8
diff -r fc45672fae42 -r 73d80859079c src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/NameOrder/NameOrderChecker.py
--- a/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/NameOrder/NameOrderChecker.py	Thu Feb 27 09:22:15 2025 +0100
+++ b/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/NameOrder/NameOrderChecker.py	Thu Feb 27 14:42:39 2025 +0100
@@ -8,11 +8,12 @@
 """
 
 import ast
-import copy
 import re
 
+from CodeStyleTopicChecker import CodeStyleTopicChecker
 
-class NameOrderChecker:
+
+class NameOrderChecker(CodeStyleTopicChecker):
     """
     Class implementing a checker for name ordering.
 
@@ -22,13 +23,13 @@
 
     Codes = [
         ## Imports order
-        "NO101",
-        "NO102",
-        "NO103",
-        "NO104",
-        "NO105",
+        "NO-101",
+        "NO-102",
+        "NO-103",
+        "NO-104",
+        "NO-105",
     ]
-    Prefix = "NO"
+    Category = "NO"
 
     def __init__(self, source, filename, tree, select, ignore, expected, repeat, args):
         """
@@ -51,18 +52,17 @@
         @param args dictionary of arguments for the various checks
         @type dict
         """
-        self.__select = tuple(
-            x for x in select if x.startswith(NameOrderChecker.Prefix)
-        )
-        self.__ignore = tuple(
-            x for x in ignore if x.startswith(NameOrderChecker.Prefix)
+        super().__init__(
+            NameOrderChecker.Category,
+            source,
+            filename,
+            tree,
+            select,
+            ignore,
+            expected,
+            repeat,
+            args,
         )
-        self.__expected = expected[:]
-        self.__repeat = repeat
-        self.__filename = filename
-        self.__source = source[:]
-        self.__tree = copy.deepcopy(tree)
-        self.__args = args
 
         # parameters for import sorting
         if args["SortOrder"] == "native":
@@ -72,86 +72,10 @@
             self.__sortingFunction = self.__naturally
         self.__sortCaseSensitive = args["SortCaseSensitive"]
 
-        # statistics counters
-        self.counters = {}
-
-        # collection of detected errors
-        self.errors = []
-
         checkersWithCodes = [
-            (self.__checkNameOrder, ("NO101", "NO102", "NO103", "NO104", "NO105")),
+            (self.__checkNameOrder, ("NO-101", "NO-102", "NO-103", "NO-104", "NO-105")),
         ]
-
-        self.__checkers = []
-        for checker, codes in checkersWithCodes:
-            if any(not (code and self.__ignoreCode(code)) for code in codes):
-                self.__checkers.append(checker)
-
-    def __ignoreCode(self, code):
-        """
-        Private method to check if the message code should be ignored.
-
-        @param code message code to check for
-        @type str
-        @return flag indicating to ignore the given code
-        @rtype bool
-        """
-        return code in self.__ignore or (
-            code.startswith(self.__ignore) and not code.startswith(self.__select)
-        )
-
-    def __error(self, lineNumber, offset, code, *args):
-        """
-        Private method to record an issue.
-
-        @param lineNumber line number of the issue
-        @type int
-        @param offset position within line of the issue
-        @type int
-        @param code message code
-        @type str
-        @param args arguments for the message
-        @type list
-        """
-        if self.__ignoreCode(code):
-            return
-
-        if code in self.counters:
-            self.counters[code] += 1
-        else:
-            self.counters[code] = 1
-
-        # Don't care about expected codes
-        if code in self.__expected:
-            return
-
-        if code and (self.counters[code] == 1 or self.__repeat):
-            # record the issue with one based line number
-            self.errors.append(
-                {
-                    "file": self.__filename,
-                    "line": lineNumber + 1,
-                    "offset": offset,
-                    "code": code,
-                    "args": args,
-                }
-            )
-
-    def run(self):
-        """
-        Public method to check the given source against miscellaneous
-        conditions.
-        """
-        if not self.__filename:
-            # don't do anything, if essential data is missing
-            return
-
-        if not self.__checkers:
-            # don't do anything, if no codes were selected
-            return
-
-        for check in self.__checkers:
-            check()
+        self._initializeCheckers(checkersWithCodes)
 
     #######################################################################
     ## Name Order
@@ -167,7 +91,7 @@
 
         errors = []
         imports = []
-        importNodes, aListNode, eListNodes = self.__findNodes(self.__tree)
+        importNodes, aListNode, eListNodes = self.__findNodes(self.tree)
 
         # check for an error in '__all__'
         allError = self.__findErrorInAll(aListNode)
@@ -183,11 +107,11 @@
 
             imports.append(
                 ImportNode(
-                    self.__args.get("ApplicationPackageNames", []),
+                    self.args.get("ApplicationPackageNames", []),
                     importNode,
                     self,
-                    self.__args.get("SortIgnoringStyle", False),
-                    self.__args.get("SortFromFirst", False),
+                    self.args.get("SortIgnoringStyle", False),
+                    self.args.get("SortFromFirst", False),
                 )
             )
 
@@ -203,21 +127,17 @@
                         errors.append(n.error)
 
                     if n == p:
-                        if self.__args.get("CombinedAsImports", False) or (
+                        if self.args.get("CombinedAsImports", False) or (
                             not n.asImport and not p.asImport
                         ):
-                            errors.append((n.node, "NO103", str(p), str(n)))
+                            errors.append((n.node, "NO-103", str(p), str(n)))
                     elif n < p:
-                        errors.append((n.node, "NO101", str(n), str(p)))
+                        errors.append((n.node, "NO-101", str(n), str(p)))
 
                     p = n
 
         for error in errors:
-            if not self.__ignoreCode(error[1]):
-                node = error[0]
-                reason = error[1]
-                args = error[2:]
-                self.__error(node.lineno - 1, node.col_offset, reason, *args)
+            self.addErrorFromNode(error[0], error[1], *error[2:])
 
     def __findExceptionListNodes(self, tree):
         """
@@ -277,7 +197,7 @@
 
         @param node reference to the '__all__' node
         @type ast.List or ast.Tuple
-        @return tuple containing a reference to the node an error code and the error
+        @return tuple containing a reference to the node, an error code and the error
             arguments
         @rtype tuple of (ast.List | ast.Tuple, str, str)
         """
@@ -295,7 +215,7 @@
                 key=lambda k: self.moduleKey(k, subImports=True),
             )
             if expectedList != actualList:
-                return (node, "NO104", ", ".join(expectedList))
+                return (node, "NO-104", ", ".join(expectedList))
 
         return None
 
@@ -331,7 +251,7 @@
 
             expectedList = self.sorted(actualList)
             if expectedList != actualList:
-                errors.append((node, "NO105", ", ".join(expectedList)))
+                errors.append((node, "NO-105", ", ".join(expectedList)))
 
         return errors
 

eric ide

mercurial