-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathLintCommand.cs
More file actions
68 lines (60 loc) · 2.1 KB
/
Copy pathLintCommand.cs
File metadata and controls
68 lines (60 loc) · 2.1 KB
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
using System.Linq;
using ManyConsole;
namespace SIL.Machine.Morphology.HermitCrab;
/// <summary>
/// Thin CLI wrapper around <see cref="GrammarAnalyzer.Analyze"/> (complexity-cap.md §6.3) — lets
/// machine.py users and CI-style grammar validation run the static lint outside FLEx.
/// </summary>
internal class LintCommand : ConsoleCommand
{
private readonly HCContext _context;
private string _severity;
public LintCommand(HCContext context)
{
_context = context;
IsCommand("lint", "Runs static grammar analysis and reports diagnostics (see complexity-cap.md).");
SkipsCommandSummaryBeforeRunning();
HasOption(
"s|severity=",
"minimum severity to report: info, warning, or error (default: info)",
o => _severity = o
);
}
public override int Run(string[] remainingArguments)
{
DiagnosticSeverity minSeverity = ParseSeverity(_severity);
var diagnostics = GrammarAnalyzer
.Analyze(_context.Language)
.Where(d => d.Severity >= minSeverity)
.OrderBy(d => d.Code)
.ToList();
if (diagnostics.Count == 0)
{
_context.Out.WriteLine("No grammar diagnostics found.");
}
else
{
foreach (GrammarDiagnostic diagnostic in diagnostics)
{
_context.Out.WriteLine("{0} [{1}] {2}", diagnostic.Code, diagnostic.Severity, diagnostic.Message);
_context.Out.WriteLine(" Suggestion: {0}", diagnostic.Suggestion);
}
_context.Out.WriteLine();
_context.Out.WriteLine("{0} diagnostic(s).", diagnostics.Count);
}
_context.Out.WriteLine();
return 0;
}
private static DiagnosticSeverity ParseSeverity(string severity)
{
switch (severity?.ToLowerInvariant())
{
case "warning":
return DiagnosticSeverity.Warning;
case "error":
return DiagnosticSeverity.Error;
default:
return DiagnosticSeverity.Info;
}
}
}