Skip to content

Commit

Permalink
chore: fix deepsource issues
Browse files Browse the repository at this point in the history
  • Loading branch information
parth-deepsource committed Nov 24, 2023
1 parent 4e1f657 commit b1c8a17
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion analyzers/dart-analyze/CI/github.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
19 changes: 13 additions & 6 deletions analyzers/dart-analyze/utils/extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@

@dataclass
class Issue:
"""Represents a dart analyze rule."""

code: str
title: str
description: str
category: str


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.
Expand All @@ -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:
Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -158,11 +164,12 @@ 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"]

@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"]]
6 changes: 4 additions & 2 deletions analyzers/dart-analyze/utils/issue_gen.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import os
import tempfile
from typing import List

from constants import ISSUE_MD_TEMPLATE
from extractor import Issue, IssueExtractor
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,
Expand All @@ -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",
Expand Down
12 changes: 7 additions & 5 deletions analyzers/dart-analyze/utils/issue_map_gen.py
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -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:
{
Expand All @@ -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:
Expand Down

0 comments on commit b1c8a17

Please sign in to comment.