From b1c8a1708c97b9937ae7b07dc3f7fb2f21fa60c0 Mon Sep 17 00:00:00 2001 From: Parth <86726240+parth-deepsource@users.noreply.github.com> Date: Fri, 24 Nov 2023 12:52:09 +0530 Subject: [PATCH] chore: fix deepsource issues --- analyzers/dart-analyze/CI/github.yml | 2 +- analyzers/dart-analyze/utils/extractor.py | 19 +++++++++++++------ analyzers/dart-analyze/utils/issue_gen.py | 6 ++++-- analyzers/dart-analyze/utils/issue_map_gen.py | 12 +++++++----- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/analyzers/dart-analyze/CI/github.yml b/analyzers/dart-analyze/CI/github.yml index 13af89fd..970fda3c 100644 --- a/analyzers/dart-analyze/CI/github.yml +++ b/analyzers/dart-analyze/CI/github.yml @@ -1,4 +1,4 @@ -# A copy-paste Github Actions config to run kube-linter and report the artifact to DeepSource +# A copy-paste Github Actions config to run dart-analyze and report the artifact to DeepSource name: Scan with dart-analyze on: diff --git a/analyzers/dart-analyze/utils/extractor.py b/analyzers/dart-analyze/utils/extractor.py index 6c97529c..04912d3a 100644 --- a/analyzers/dart-analyze/utils/extractor.py +++ b/analyzers/dart-analyze/utils/extractor.py @@ -6,6 +6,8 @@ @dataclass class Issue: + """Represents a dart analyze rule.""" + code: str title: str description: str @@ -13,7 +15,9 @@ class Issue: class DiagnosticRulesParser: - def __init__(self, text: str): + """A simple wrapper that parses dart's diagnostic rules from a markdown file.""" + + def __init__(self, text: str) -> None: """ A simple wrapper that parses a diagnostic rules from a markdown file. @@ -25,19 +29,19 @@ def __init__(self, text: str): self.pos = 0 @property - def exhausted(self): + def exhausted(self) -> bool: """Whether the text content has been exhausted or not.""" return self.pos >= len(self.lines) - def next_line(self): + def next_line(self) -> str: """Returns the next line to be parsed.""" return self.lines[self.pos] - def consume(self): + def consume(self) -> None: """Consumes the current line and moves to the next line.""" self.pos += 1 - def get_issues(self) -> dict: + def get_issues(self) -> List[dict]: """Parses the the given text and extracts issue definitions.""" issues = [] while not self.exhausted: @@ -88,6 +92,8 @@ def get_issues(self) -> dict: class IssueExtractor: + """Extracts dart's diagnostic and linter rules.""" + LINTER_RULES_URL = ( "https://github.com/dart-lang/site-www/raw/main/src/_data/linter_rules.json" ) @@ -158,6 +164,7 @@ def get_linter_rule_title(cls, rule: dict) -> str: """Extract & sanitize the rule title.""" return rule["description"].rstrip(".").replace('"', r"\"") + @classmethod def get_linter_rule_description(cls, rule: dict) -> str: """Extracts the description from the rule""" return rule["details"] @@ -165,4 +172,4 @@ def get_linter_rule_description(cls, rule: dict) -> str: @classmethod def get_linter_rule_category(cls, rule: dict) -> str: """Extracts and returns the category mapped to the rule""" - return cls.GROUP_CATEGORY_MAP.get(rule["group"]) + return cls.GROUP_CATEGORY_MAP[rule["group"]] diff --git a/analyzers/dart-analyze/utils/issue_gen.py b/analyzers/dart-analyze/utils/issue_gen.py index 14fce9f7..eb27714c 100644 --- a/analyzers/dart-analyze/utils/issue_gen.py +++ b/analyzers/dart-analyze/utils/issue_gen.py @@ -1,5 +1,4 @@ import os -import tempfile from typing import List from constants import ISSUE_MD_TEMPLATE @@ -7,7 +6,9 @@ from issue_map_gen import generate_mapping -def get_issue_content(title, description, category, verbose_name) -> str: +def get_issue_content( + title: str, description: str, category: str, verbose_name: str +) -> str: """Return the content of the toml file.""" return ISSUE_MD_TEMPLATE.format( title=title, @@ -18,6 +19,7 @@ def get_issue_content(title, description, category, verbose_name) -> str: def get_issue_filepath(issue_code: str) -> str: + """Returns the file path of the given issue code.""" return os.path.join( os.path.dirname(os.path.dirname(__file__)), f".deepsource/issues/{issue_code}.md", diff --git a/analyzers/dart-analyze/utils/issue_map_gen.py b/analyzers/dart-analyze/utils/issue_map_gen.py index fa46d9fa..3b10de67 100644 --- a/analyzers/dart-analyze/utils/issue_map_gen.py +++ b/analyzers/dart-analyze/utils/issue_map_gen.py @@ -1,13 +1,12 @@ import itertools import json -import tempfile -from typing import Dict, List +from typing import Dict, Generator, List from constants import ISSUE_MAP_FILE, ISSUE_PREFIX from extractor import Issue, IssueExtractor -def get_code_generator(mapping) -> itertools.count: +def get_code_generator(mapping: Dict[str, Dict[str, str]]) -> Generator: """Return the next available issue code.""" num_issues = len(mapping.keys()) # get the number of issues already in the mapping next_code = 1001 + num_issues # issue code series starts from `1001` @@ -23,7 +22,7 @@ def get_issue_map() -> Dict[str, Dict[str, str]]: def generate_mapping(issues: List[Issue]) -> Dict[str, Dict[str, str]]: """ Generates and returns a dict mapping of DeepSource assigned - issue codes to Slither's detector names. + issue codes to dart analyzer rules. Example mapping: { @@ -38,7 +37,10 @@ def generate_mapping(issues: List[Issue]) -> Dict[str, Dict[str, str]]: # then generate the mapping only for the new detectors for issue in issues: if issue.code not in issue_map.keys(): - shortcode = f"{ISSUE_PREFIX}{next(code_generator)}" + try: + shortcode = f"{ISSUE_PREFIX}{next(code_generator)}" + except StopIteration: + continue issue_map[issue.code] = {"issue_code": shortcode} with open(ISSUE_MAP_FILE, "w") as f: