src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyChecker.py

branch
eric7
changeset 11150
73d80859079c
parent 11147
dee6e106b4d3
--- a/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyChecker.py	Thu Feb 27 09:22:15 2025 +0100
+++ b/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyChecker.py	Thu Feb 27 14:42:39 2025 +0100
@@ -8,12 +8,13 @@
 """
 
 import ast
-import copy
+
+from CodeStyleTopicChecker import CodeStyleTopicChecker
 
 from .SimplifyNodeVisitor import SimplifyNodeVisitor
 
 
-class SimplifyChecker:
+class SimplifyChecker(CodeStyleTopicChecker):
     """
     Class implementing a checker for to help simplifying Python code.
     """
@@ -79,6 +80,7 @@
         "Y-910",
         "Y-911",
     ]
+    Category = "Y"
 
     def __init__(self, source, filename, tree, selected, ignored, expected, repeat):
         """
@@ -99,92 +101,88 @@
         @param repeat flag indicating to report each occurrence of a code
         @type bool
         """
-        self.__select = tuple(selected)
-        self.__ignore = tuple(ignored)
-        self.__expected = expected[:]
-        self.__repeat = repeat
-        self.__filename = filename
-        self.__source = source[:]
-        self.__tree = copy.deepcopy(tree)
-
-        # statistics counters
-        self.counters = {}
-
-        # collection of detected errors
-        self.errors = []
-
-        self.__checkCodes = (code for code in self.Codes if not self.__ignoreCode(code))
-
-    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)
+        super().__init__(
+            SimplifyChecker.Category,
+            source,
+            filename,
+            tree,
+            selected,
+            ignored,
+            expected,
+            repeat,
+            [],
         )
 
-    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
-
-        # record the issue with one based line number
-        errorInfo = {
-            "file": self.__filename,
-            "line": lineNumber + 1,
-            "offset": offset,
-            "code": code,
-            "args": args,
-        }
+        checkersWithCodes = [
+            (
+                self.__checkCodeSimplifications,
+                (
+                    "Y-101",
+                    "Y-102",
+                    "Y-103",
+                    "Y-104",
+                    "Y-105",
+                    "Y-106",
+                    "Y-107",
+                    "Y-108",
+                    "Y-109",
+                    "Y-110",
+                    "Y-111",
+                    "Y-112",
+                    "Y-113",
+                    "Y-114",
+                    "Y-115",
+                    "Y-116",
+                    "Y-117",
+                    "Y-118",
+                    "Y-119",
+                    "Y-120",
+                    "Y-121",
+                    "Y-122",
+                    "Y-123",
+                    "Y-181",
+                    "Y-182",
+                    "Y-201",
+                    "Y-202",
+                    "Y-203",
+                    "Y-204",
+                    "Y-205",
+                    "Y-206",
+                    "Y-207",
+                    "Y-208",
+                    "Y-211",
+                    "Y-212",
+                    "Y-213",
+                    "Y-221",
+                    "Y-222",
+                    "Y-223",
+                    "Y-224",
+                    "Y-301",
+                    "Y-401",
+                    "Y-402",
+                    "Y-411",
+                    "Y-901",
+                    "Y-904",
+                    "Y-905",
+                    "Y-906",
+                    "Y-907",
+                    "Y-909",
+                    "Y-910",
+                    "Y-911",
+                ),
+            ),
+        ]
+        self._initializeCheckers(checkersWithCodes)
 
-        if errorInfo not in self.errors:
-            # this issue was not seen before
-            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):
-                self.errors.append(errorInfo)
-
-    def run(self):
+    def __checkCodeSimplifications(self):
+        """
+        Private method to check for code simplifications.
         """
-        Public method to check the given source against functions
-        to be replaced by 'pathlib' equivalents.
-        """
-        if not self.__filename:
-            # don't do anything, if essential data is missing
-            return
+        # Add parent information
+        self.__addMeta(self.tree)
 
-        if not self.__checkCodes:
-            # don't do anything, if no codes were selected
-            return
-
-        # Add parent information
-        self.__addMeta(self.__tree)
-
-        visitor = SimplifyNodeVisitor(self.__error)
-        visitor.visit(self.__tree)
+        visitor = SimplifyNodeVisitor(self.addErrorFromNode)
+        visitor.visit(self.tree)
 
     def __addMeta(self, root, level=0):
         """

eric ide

mercurial