All files / providers issuesProvider.ts

56.11% Statements 78/139
49.47% Branches 47/95
69.56% Functions 16/23
58.59% Lines 75/128

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336      1x 1x 1x 1x 1x     1x                                                                         11x     11x   11x 11x 11x 11x     9x 9x       7x 7x       2x 2x       3x 3x       1x       6x     6x 4x       6x 2x 2x 8x           6x       11x   4x   3x   2x   2x             6x   6x 6x 1x                       5x                   5x                   5x 2x 2x 8x   5x         8x       8x     8x 8x 8x 8x   3x                             3x                                   3x     2x                                                     3x 6x 6x         6x   6x   6x       6x     6x 6x 6x     6x 6x                                 6x         2x           2x 2x 5x 5x 5x               2x                                                                
import * as vscode from 'vscode';
 
// Inlined from @aiready/core to avoid bundling the entire package in the VSIX
export enum Severity {
  Critical = 'critical',
  Major = 'major',
  Minor = 'minor',
  Info = 'info',
}
 
export const FRIENDLY_TOOL_NAMES: Record<string, string> = {
  'pattern-detect': 'Semantic Duplicates',
  'context-analyzer': 'Context Fragmentation',
  'naming-consistency': 'Naming Consistency',
  'ai-signal-clarity': 'AI Signal Clarity',
  'agent-grounding': 'Agent Grounding',
  'testability-index': 'Testability Index',
  'doc-drift': 'Documentation Health',
  'dependency-health': 'Dependency Health',
  'change-amplification': 'Change Amplification',
  'cognitive-load': 'Cognitive Load',
  'pattern-entropy': 'Pattern Entropy',
  'concept-cohesion': 'Concept Cohesion',
  'semantic-distance': 'Semantic Distance',
};
 
export interface Issue {
  message: string;
  severity: Severity | 'critical' | 'major' | 'minor' | 'info';
  tool?: string;
  location?: {
    file: string;
    line?: number;
  };
}
 
export type GroupBy = 'severity' | 'tool' | 'file' | 'none';
export type SeverityFilter =
  | 'all'
  | Severity
  | 'critical'
  | 'major'
  | 'minor'
  | 'info';
 
// Tree item for issues view
export class AIReadyIssuesProvider implements vscode.TreeDataProvider<vscode.TreeItem> {
  private _onDidChangeTreeData = new vscode.EventEmitter<
    vscode.TreeItem | undefined | null | void
  >();
  readonly onDidChangeTreeData = this._onDidChangeTreeData.event;
 
  private issues: Issue[] = [];
  private groupBy: GroupBy = 'severity';
  private severityFilter: SeverityFilter = 'all';
  private searchQuery: string = '';
 
  refresh(issues: Issue[] = []): void {
    this.issues = issues;
    this._onDidChangeTreeData.fire();
  }
 
  setGroupBy(groupBy: GroupBy): void {
    this.groupBy = groupBy;
    this._onDidChangeTreeData.fire();
  }
 
  setSeverityFilter(filter: SeverityFilter): void {
    this.severityFilter = filter;
    this._onDidChangeTreeData.fire();
  }
 
  setSearchQuery(query: string): void {
    this.searchQuery = query;
    this._onDidChangeTreeData.fire();
  }
 
  getTreeItem(element: vscode.TreeItem): vscode.TreeItem {
    return element;
  }
 
  private getFilteredIssues(): Issue[] {
    let filtered = [...this.issues];
 
    // Apply severity filter
    if (this.severityFilter !== 'all') {
      filtered = filtered.filter((i) => i.severity === this.severityFilter);
    }
 
    // Apply search filter
    if (this.searchQuery) {
      const query = this.searchQuery.toLowerCase();
      filtered = filtered.filter(
        (i) =>
          (i.message || '').toLowerCase().includes(query) ||
          (i.location?.file || '').toLowerCase().includes(query)
      );
    }
 
    return filtered;
  }
 
  private getSeverityIcon(severity: string): string {
    switch (severity) {
      case Severity.Critical:
        return 'error';
      case Severity.Major:
        return 'warning';
      case Severity.Minor:
        return 'info';
      default:
        return 'circle-outline';
    }
  }
 
  getChildren(
    element?: vscode.TreeItem
  ): vscode.ProviderResult<vscode.TreeItem[]> {
    const filteredIssues = this.getFilteredIssues();
 
    Eif (!element) {
      if (filteredIssues.length === 0 && this.issues.length === 0) {
        return [
          {
            label: '$(play) Run Scan',
            iconPath: new vscode.ThemeIcon('play'),
            command: {
              command: 'aiready.scan',
              title: 'Run Scan',
            },
          } as vscode.TreeItem,
        ];
      }
 
      Iif (filteredIssues.length === 0) {
        return [
          {
            label: 'No issues match current filters',
            iconPath: new vscode.ThemeIcon('filter'),
          } as vscode.TreeItem,
        ];
      }
 
      // Show filter controls as first items
      const controls: vscode.TreeItem[] = [
        {
          label: `📊 ${filteredIssues.length} issues`,
          iconPath: new vscode.ThemeIcon('list-filter'),
          description: `Grouped by ${this.groupBy} | Filter: ${this.severityFilter}`,
          contextValue: 'controls',
        } as vscode.TreeItem,
      ];
 
      // Group issues
      if (this.groupBy === 'severity') {
        const groups = this.groupBySeverity(filteredIssues);
        for (const [severity, issues] of Object.entries(groups)) {
          if (issues.length === 0) continue; // Skip empty groups
          const icon =
            severity === Severity.Critical || severity === 'critical'
              ? '🔴'
              : severity === Severity.Major || severity === 'major'
                ? '🟡'
                : '🔵';
          const groupItem = new vscode.TreeItem(
            `${icon} ${severity.toUpperCase()} (${issues.length})`,
            vscode.TreeItemCollapsibleState.Collapsed
          );
          groupItem.iconPath = new vscode.ThemeIcon(
            this.getSeverityIcon(severity)
          );
          groupItem.contextValue = 'group';
          (groupItem as any).groupKey = severity;
          (groupItem as any).groupType = 'severity';
          controls.push(groupItem);
        }
      I} else if (this.groupBy === 'tool') {
        const groups = this.groupByTool(filteredIssues);
        for (const [tool, issues] of Object.entries(groups)) {
          if (issues.length === 0) continue;
          const label = (FRIENDLY_TOOL_NAMES as any)[tool] || tool;
          const groupItem = new vscode.TreeItem(
            `🔧 ${label} (${issues.length})`,
            vscode.TreeItemCollapsibleState.Collapsed
          );
          groupItem.iconPath = new vscode.ThemeIcon('symbol-property');
          groupItem.contextValue = 'group';
          (groupItem as any).groupKey = tool;
          (groupItem as any).groupType = 'tool';
          controls.push(groupItem);
        }
      I} else if (this.groupBy === 'file') {
        const groups = this.groupByFile(filteredIssues);
        for (const [file, issues] of Object.entries(groups)) {
          if (issues.length === 0) continue;
          const shortFile = file.split('/').pop() || file;
          const groupItem = new vscode.TreeItem(
            `📁 ${shortFile} (${issues.length})`,
            vscode.TreeItemCollapsibleState.Collapsed
          );
          groupItem.iconPath = new vscode.ThemeIcon('file');
          groupItem.description = file;
          groupItem.contextValue = 'group';
          (groupItem as any).groupKey = file;
          (groupItem as any).groupType = 'file';
          controls.push(groupItem);
        }
      } else {
        // No grouping - show flat list
        return controls.concat(this.createIssueItems(filteredIssues));
      }
 
      return controls;
    }
 
    // Return issues for a group
    if (element.contextValue === 'group') {
      const groupType = (element as any).groupType;
      const groupKey = (element as any).groupKey;
 
      if (groupType === 'severity') {
        return this.createIssueItems(
          filteredIssues.filter((i) => i.severity === groupKey)
        );
      } else if (groupType === 'tool') {
        return this.createIssueItems(
          filteredIssues.filter((i) => i.tool === groupKey)
        );
      } else if (groupType === 'file') {
        return this.createIssueItems(
          filteredIssues.filter((i) => i.location?.file === groupKey)
        );
      }
    }
 
    return [];
  }
 
  private createIssueItems(issues: Issue[]): vscode.TreeItem[] {
    return issues.map((issue) => {
      const location = issue.location;
      const locationStr = location
        ? `${location.file}${location.line ? `:${location.line}` : ''}`
        : '';
 
      // Handle undefined message
      const message = issue.message || 'Unknown issue';
      const truncatedMessage =
        message.substring(0, 80) + (message.length > 80 ? '...' : '');
 
      const item = new vscode.TreeItem(
        truncatedMessage,
        vscode.TreeItemCollapsibleState.None
      );
      item.iconPath = new vscode.ThemeIcon(
        this.getSeverityIcon(issue.severity as string)
      );
      item.description = locationStr;
      item.tooltip = `${issue.message}\n\n${locationStr}`;
      item.contextValue = 'issue';
 
      // Add command to jump to issue
      Eif (location) {
        item.command = {
          command: 'vscode.open',
          arguments: [
            vscode.Uri.file(location.file),
            {
              selection: new vscode.Range(
                (location.line || 1) - 1,
                0,
                (location.line || 1) - 1,
                0
              ),
            },
          ],
          title: 'Go to Issue',
        };
      }
 
      return item;
    });
  }
 
  private groupBySeverity(issues: Issue[]): Record<string, Issue[]> {
    const groups: Record<string, Issue[]> = {
      [Severity.Critical]: [],
      [Severity.Major]: [],
      [Severity.Minor]: [],
      [Severity.Info]: [],
    };
    Eif (Array.isArray(issues)) {
      issues.forEach((issue) => {
        const sev = (issue.severity as string).toLowerCase();
        if (groups[sev]) {
          groups[sev].push(issue);
        E} else if (sev === 'critical') groups[Severity.Critical].push(issue);
        else if (sev === 'major') groups[Severity.Major].push(issue);
        else if (sev === 'minor') groups[Severity.Minor].push(issue);
        else if (sev === 'info') groups[Severity.Info].push(issue);
      });
    }
    // Sort by severity order
    return {
      [Severity.Critical]: groups[Severity.Critical],
      [Severity.Major]: groups[Severity.Major],
      [Severity.Minor]: groups[Severity.Minor],
      [Severity.Info]: groups[Severity.Info],
    };
  }
 
  private groupByTool(issues: Issue[]): Record<string, Issue[]> {
    const groups: Record<string, Issue[]> = {};
    if (Array.isArray(issues)) {
      issues.forEach((issue) => {
        const tool = issue.tool || 'unknown';
        if (!groups[tool]) groups[tool] = [];
        groups[tool].push(issue);
      });
    }
    return groups;
  }
 
  private groupByFile(issues: Issue[]): Record<string, Issue[]> {
    const groups: Record<string, Issue[]> = {};
    if (Array.isArray(issues)) {
      issues.forEach((issue) => {
        const file = issue.location?.file || 'unknown';
        if (!groups[file]) groups[file] = [];
        groups[file].push(issue);
      });
    }
    return groups;
  }
}