linter/lint-finding.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/text/stringstream"; | ||
| 5 | |||
| 6 | // Own imports | ||
| 7 | import "@/reader/code-loc"; | ||
| 8 | |||
| 9 | public type LintSeverity enum { | ||
| 10 | INFO, | ||
| 11 | WARNING, | ||
| 12 | ERROR | ||
| 13 | } | ||
| 14 | |||
| 15 | /** | ||
| 16 | * A single diagnostic produced by a lint rule, e.g. a naming convention violation. | ||
| 17 | */ | ||
| 18 | 40 | public type LintFinding struct { | |
| 19 | public String ruleId | ||
| 20 | public LintSeverity severity | ||
| 21 | public String findingMessage | ||
| 22 | } | ||
| 23 | |||
| 24 | /** | ||
| 25 | * Constructor | ||
| 26 | * | ||
| 27 | * @param codeLoc Code location, where the lint finding occurred | ||
| 28 | * @param ruleId Id of the rule that produced this finding, e.g. "naming-convention" | ||
| 29 | * @param severity Severity of the finding | ||
| 30 | * @param message Finding message suffix | ||
| 31 | */ | ||
| 32 | 8 | public p LintFinding.ctor(const CodeLoc& codeLoc, const string ruleId, const LintSeverity severity, const String& message) { | |
| 33 | 8 | this.ruleId = String(ruleId); | |
| 34 | 8 | this.severity = severity; | |
| 35 | 8 | StringStream msg; | |
| 36 | 8 | msg << "[" << getSeverityLabel(severity) << "] " << codeLoc.toPrettyString() << ": " << this.ruleId << ": " << message; | |
| 37 | 8 | this.findingMessage = msg.str(); | |
| 38 | 8 | } | |
| 39 | |||
| 40 | /** | ||
| 41 | * Print the lint finding to standard output | ||
| 42 | */ | ||
| 43 | 8 | public p LintFinding.print() { | |
| 44 | 8 | string color = "\033[36m"; // INFO: cyan | |
| 45 | 8 | if this.severity == LintSeverity::WARNING { | |
| 46 | 8 | color = "\033[33m"; // yellow | |
| 47 | 8 | } else if this.severity == LintSeverity::ERROR { | |
| 48 | ✗ | color = "\033[31m"; // red | |
| 49 | ✗ | } | |
| 50 | 8 | printf("%s%s\033[0m\n", color, this.findingMessage); | |
| 51 | 8 | } | |
| 52 | |||
| 53 | /** | ||
| 54 | * Get the human-readable label for a lint severity | ||
| 55 | * | ||
| 56 | * @param severity Severity of the finding | ||
| 57 | * @return Label string for the severity | ||
| 58 | */ | ||
| 59 | 8 | f<string> getSeverityLabel(const LintSeverity severity) { | |
| 60 | 8 | switch severity { | |
| 61 | ✗ | case LintSeverity::INFO: { return "Info"; } | |
| 62 | 8 | case LintSeverity::WARNING: { return "Lint"; } | |
| 63 | ✗ | case LintSeverity::ERROR: { return "Lint error"; } | |
| 64 | ✗ | default: { panic(Error("Unknown lint severity")); } | |
| 65 | } | ||
| 66 | ✗ | } | |
| 67 |