Skip to content

Commit

Permalink
feat(cli): --min-severity option
Browse files Browse the repository at this point in the history
  • Loading branch information
jubnzv committed Sep 26, 2024
1 parent c60fd28 commit 79e1a84
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- ANSI escape sequences to colorize output and the `--no-colors` CLI option to disable it
- Driver in a single-contract mode tries to copy all the .tact and .fc files to resolve imports
- Short CLI options. See: https://nowarp.io/tools/misti/docs/next/tutorial/cli
- CLI: `--min-severity/-m` option

### Changed
- Warnings now have more comprehensive descriptions and are sorted by severity
Expand Down
22 changes: 15 additions & 7 deletions src/cli/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { CompilationUnit, ProjectName } from "../internals/ir";
import { createIR } from "../internals/ir/builders/tactIRBuilder";
import { GraphvizDumper, JSONDumper } from "../internals/irDump";
import { Logger } from "../internals/logger";
import { MistiTactWarning, severityToString } from "../internals/warnings";
import {
MistiTactWarning,
severityToString,
Severity,
} from "../internals/warnings";
import fs from "fs";
import JSONbig from "json-bigint";
import path from "path";
Expand Down Expand Up @@ -43,6 +47,8 @@ export class Driver {
private dumpConfig: boolean;
private colorizeOutput: boolean;
private tactConfigPath: string;
/** Minimum severity level to report warnings. */
private minSeverity: Severity;

private constructor(tactPath: string, options: CLIOptions) {
const singleContract = tactPath.endsWith(".tact");
Expand All @@ -68,6 +74,7 @@ export class Driver {
this.dumpOutput = options.dumpOutput || DUMP_STDOUT_PATH;
this.dumpConfig = options.dumpConfig ?? false;
this.colorizeOutput = options.colors ?? true;
this.minSeverity = options.minSeverity ?? Severity.INFO;
}

/**
Expand Down Expand Up @@ -272,20 +279,20 @@ export class Driver {
const projectWarnings: MistiTactWarning[] = Array.from(
detectorsMap.values(),
).flat();
projectWarnings.forEach((err) => {
if (!reported.has(err.msg)) {
acc.push(err);
reported.add(err.msg);
projectWarnings.forEach((warn) => {
if (!reported.has(warn.msg) && warn.severity >= this.minSeverity) {
acc.push(warn);
reported.add(warn.msg);
}
});
return acc;
}, []);
const sortedWarnings = collectedWarnings.sort(
(a, b) => b.severity - a.severity,
);
const formattedWarnings = sortedWarnings.reduce((acc, err, index) => {
const formattedWarnings = sortedWarnings.reduce((acc, warn, index) => {
const isLastWarning = index === sortedWarnings.length - 1;
acc.push(this.formatWarning(err, !isLastWarning));
acc.push(this.formatWarning(warn, !isLastWarning));
return acc;
}, [] as string[]);
return {
Expand Down Expand Up @@ -429,6 +436,7 @@ export class Runner {
tactStdlibPath: undefined,
verbose: false,
quiet: false,
minSeverity: undefined,
detectors: undefined,
suppress: undefined,
allDetectors: false,
Expand Down
8 changes: 8 additions & 0 deletions src/cli/options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Severity, parseSeverity } from "../internals/warnings";
import { Option } from "commander";

export const DUMP_STDOUT_PATH = "-";
Expand All @@ -15,6 +16,7 @@ export interface CLIOptions {
tactStdlibPath?: string;
verbose?: boolean;
quiet?: boolean;
minSeverity?: Severity;
detectors?: string[];
suppress?: string[];
allDetectors?: boolean;
Expand Down Expand Up @@ -59,6 +61,12 @@ export const cliOptions = [
new Option("--tact-stdlib-path <PATH>", "Path to the Tact standard library."),
new Option("-v, --verbose", "Enable verbose output.").default(false),
new Option("-q, --quiet", "Suppress output.").default(false),
new Option(
"-m, --min-severity <info|low|medium|high|critical>",
"Minimum level of severity to report.",
)
.default(undefined)
.argParser(parseSeverity),
new Option(
"-D, --detectors <name|path:name>",
"A comma-separated list of detectors to enable.",
Expand Down
9 changes: 8 additions & 1 deletion src/internals/warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import * as path from "path";
* Enumerates the levels of severity that can be assigned to detected findings.
*/
export enum Severity {
INFO,
INFO = 1,
LOW,
MEDIUM,
HIGH,
CRITICAL,
}

/**
* Parses string input to corresponding Severity enum value.
*/
export function parseSeverity(value: string): Severity {
return Severity[value.toUpperCase() as keyof typeof Severity];
}

/**
* Returns string representation of `Severity` optionally wrapped in ANSI escape
* sequences making them colorful for visual overload.
Expand Down

0 comments on commit 79e1a84

Please sign in to comment.