Изменено формирование ошибок на темплейты
This commit is contained in:
@@ -55,14 +55,14 @@ public abstract class BaseRuleVisitor : TSqlFragmentVisitor, IRule
|
|||||||
_violations.Add(violation);
|
_violations.Add(violation);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void AddViolation(string RuleName, string Message, int Line, int Column)
|
protected void AddViolation(string RuleName, string Template, int Line, int Column, params string[] param)
|
||||||
{
|
{
|
||||||
_violations.Add(new(RuleName, Message, Line, Column));
|
_violations.Add(new(RuleName, Template, Line, Column, param));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void AddViolation(TSqlFragment node, params string[] param)
|
protected void AddViolation(TSqlFragment node, params string[] param)
|
||||||
{
|
{
|
||||||
AddViolation(Name, this.GetText(param), GetLineNumber(node), GetColumnNumber(node));
|
_violations.Add(new(this.Name, this.Text, GetLineNumber(node), GetColumnNumber(node), param));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected string GetText(params string[] param)
|
protected string GetText(params string[] param)
|
||||||
|
|||||||
@@ -4,5 +4,5 @@ public interface IReporter : IBaseReporter
|
|||||||
{
|
{
|
||||||
void ReportViolation(IRuleViolation violation);
|
void ReportViolation(IRuleViolation violation);
|
||||||
|
|
||||||
void ReportViolation(string fileName, int line, int column, RuleViolationSeverity severity, string ruleName, string violationText);
|
void ReportViolation(string fileName, int line, int column, RuleViolationSeverity severity, string ruleName, string template, params string[] param);
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace SQLLinter.Common
|
namespace SQLLinter.Common
|
||||||
{
|
{
|
||||||
public record Violation(string RuleName, string Message, int Line, int Column);
|
public record Violation(string RuleName, string Template, int Line, int Column, string[] Params);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ public class SqlRuleVisitor : IRuleVisitor
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
violations.ForEach(t => _reporter.ReportViolation(filePath, t.Line, t.Column, rule.Severity, t.RuleName, t.Message));
|
violations.ForEach(t => _reporter.ReportViolation(filePath, t.Line, t.Column, rule.Severity, t.RuleName, t.Template, t.Params));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool VisitorIsBlackListedForDynamicSql(IRule visitor)
|
private static bool VisitorIsBlackListedForDynamicSql(IRule visitor)
|
||||||
@@ -109,7 +109,15 @@ public class SqlRuleVisitor : IRuleVisitor
|
|||||||
|
|
||||||
if (!globalRulesOnLine.Any())
|
if (!globalRulesOnLine.Any())
|
||||||
{
|
{
|
||||||
_reporter.ReportViolation(new RuleViolation(sqlPath, "invalid-syntax", error.Message, error.Line, error.Column, RuleViolationSeverity.Critical));
|
_reporter.ReportViolation(new RuleViolation()
|
||||||
|
{
|
||||||
|
FileName = sqlPath,
|
||||||
|
RuleName = "invalid-syntax",
|
||||||
|
Text = error.Message,
|
||||||
|
Line = error.Line,
|
||||||
|
Column = error.Column,
|
||||||
|
Severity = RuleViolationSeverity.Critical
|
||||||
|
});
|
||||||
if (updatedExitCode)
|
if (updatedExitCode)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -7,15 +7,22 @@ namespace SQLLinter.Infrastructure.Reporters;
|
|||||||
public class Reporter : IReporter
|
public class Reporter : IReporter
|
||||||
{
|
{
|
||||||
private readonly List<string> _log = new();
|
private readonly List<string> _log = new();
|
||||||
|
private readonly bool _useLogging;
|
||||||
|
|
||||||
public int? FixedCount { get; set; }
|
public int? FixedCount { get; set; }
|
||||||
|
|
||||||
private readonly ConcurrentBag<IRuleViolation> ruleViolations = new();
|
private readonly ConcurrentBag<IRuleViolation> ruleViolations = new();
|
||||||
|
|
||||||
public List<IRuleViolation> Violations => ruleViolations.ToList();
|
public List<IRuleViolation> Violations => ruleViolations.ToList();
|
||||||
|
|
||||||
|
public Reporter(bool useLogging = false)
|
||||||
|
{
|
||||||
|
_useLogging = useLogging;
|
||||||
|
}
|
||||||
|
|
||||||
public virtual void Report(string message)
|
public virtual void Report(string message)
|
||||||
{
|
{
|
||||||
_log.Add(message);
|
if (_useLogging) _log.Add(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<string> GetLog() => _log;
|
public List<string> GetLog() => _log;
|
||||||
@@ -34,8 +41,17 @@ public class Reporter : IReporter
|
|||||||
Report(violation.ToString());
|
Report(violation.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ReportViolation(string fileName, int line, int column, RuleViolationSeverity severity, string ruleName, string violationText)
|
public void ReportViolation(string fileName, int line, int column, RuleViolationSeverity severity, string ruleName, string template, params string[] param)
|
||||||
{
|
{
|
||||||
ReportViolation(new RuleViolation(fileName, ruleName, violationText, line, column, severity));
|
ReportViolation(new RuleTemplateViolation()
|
||||||
|
{
|
||||||
|
FileName = fileName,
|
||||||
|
RuleName = ruleName,
|
||||||
|
RuleTemplate = template,
|
||||||
|
Line = line,
|
||||||
|
Column = column,
|
||||||
|
Severity = severity,
|
||||||
|
Params = param.ToList(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class ConditionalBeginEndRule : BaseRuleVisitor, IRule
|
|||||||
{
|
{
|
||||||
if (node.ThenStatement is not BeginEndBlockStatement)
|
if (node.ThenStatement is not BeginEndBlockStatement)
|
||||||
{
|
{
|
||||||
AddViolation(Name, Text, GetLineNumber(node), GetColumnNumber(node));
|
AddViolation(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node.ElseStatement != null && node.ElseStatement is not BeginEndBlockStatement && node.ElseStatement is not IfStatement)
|
if (node.ElseStatement != null && node.ElseStatement is not BeginEndBlockStatement && node.ElseStatement is not IfStatement)
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ public class IndexHintRule : BaseRuleVisitor
|
|||||||
{
|
{
|
||||||
if (node.HintKind == TableHintKind.Index)
|
if (node.HintKind == TableHintKind.Index)
|
||||||
{
|
{
|
||||||
AddViolation(Name, Text, GetLineNumber(node), GetColumnNumber(node));
|
AddViolation(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public class InsertValuesInsteadOfSelectRule : BaseRuleVisitor
|
|||||||
// Если в SELECT нет таблиц (т.е. просто SELECT 1,2,3)
|
// Если в SELECT нет таблиц (т.е. просто SELECT 1,2,3)
|
||||||
if (query.FromClause == null || query.FromClause.TableReferences.Count == 0)
|
if (query.FromClause == null || query.FromClause.TableReferences.Count == 0)
|
||||||
{
|
{
|
||||||
AddViolation(Name, Text, GetLineNumber(node), GetColumnNumber(node));
|
AddViolation(node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ public class KeywordCapitalizationRule : BaseRuleVisitor, IRule
|
|||||||
var tabsOnLine = ColumnNumberCalculator.CountTabsBeforeToken(token.Line, index, node.ScriptTokenStream);
|
var tabsOnLine = ColumnNumberCalculator.CountTabsBeforeToken(token.Line, index, node.ScriptTokenStream);
|
||||||
var column = ColumnNumberCalculator.GetColumnNumberBeforeToken(tabsOnLine, token);
|
var column = ColumnNumberCalculator.GetColumnNumberBeforeToken(tabsOnLine, token);
|
||||||
|
|
||||||
AddViolation(Name, GetText(token.Text), GetLineNumber(token), column + dynamicSQLAdjustment);
|
AddViolation(Name, Text, GetLineNumber(token), column + dynamicSQLAdjustment, token.Text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public class MultiTableAliasRule : BaseRuleVisitor, IRule
|
|||||||
tableName = SQLHelpers.ObjectGetFullName(namedTable.SchemaObject);
|
tableName = SQLHelpers.ObjectGetFullName(namedTable.SchemaObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
AddViolation(Name, GetText(tableName), GetLineNumber(childNode), column + dynamicSqlAdjustment);
|
AddViolation(Name, Text, GetLineNumber(childNode), column + dynamicSqlAdjustment, tableName);
|
||||||
}
|
}
|
||||||
|
|
||||||
var childTableJoinVisitor = new ChildTableJoinVisitor();
|
var childTableJoinVisitor = new ChildTableJoinVisitor();
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ public class ProcedureLoggingReturnRule : BaseRuleVisitor
|
|||||||
|
|
||||||
if ((hasDebugLog || hasLabelFinish) || hasReturn)
|
if ((hasDebugLog || hasLabelFinish) || hasReturn)
|
||||||
{
|
{
|
||||||
returnPositions.ForEach(t => AddViolation(Name, GetText(name), t.Line, t.Column));
|
returnPositions.ForEach(t => AddViolation(Name, Text, t.Line, t.Column, name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,46 +4,25 @@ namespace SQLLinter.Infrastructure.Rules.RuleViolations
|
|||||||
{
|
{
|
||||||
public class RuleViolation : IRuleViolation
|
public class RuleViolation : IRuleViolation
|
||||||
{
|
{
|
||||||
public RuleViolation(string fileName, string ruleName, string text, int startLine, int startColumn, RuleViolationSeverity severity)
|
required public string FileName { get; init; }
|
||||||
{
|
|
||||||
FileName = fileName;
|
required public int Column { get; set; }
|
||||||
RuleName = ruleName;
|
|
||||||
Text = text;
|
required public int Line { get; set; }
|
||||||
Line = startLine;
|
|
||||||
Column = startColumn;
|
required public string RuleName { get; init; }
|
||||||
Severity = severity;
|
|
||||||
|
required public RuleViolationSeverity Severity { get; init; }
|
||||||
|
|
||||||
|
virtual public string Text { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public RuleViolation(string fileName, string ruleName, int startLine, int startColumn)
|
public class RuleTemplateViolation : RuleViolation
|
||||||
{
|
{
|
||||||
FileName = fileName;
|
override public string Text => string.Format(RuleTemplate, Params.ToArray());
|
||||||
RuleName = ruleName;
|
|
||||||
Line = startLine;
|
|
||||||
Column = startColumn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public RuleViolation(string ruleName, int startLine, int startColumn)
|
required public string RuleTemplate { get; init; }
|
||||||
{
|
|
||||||
RuleName = ruleName;
|
|
||||||
Line = startLine;
|
|
||||||
Column = startColumn;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int Column { get; set; }
|
public List<string> Params { get; set; } = new();
|
||||||
|
|
||||||
public string FileName { get; set; }
|
|
||||||
|
|
||||||
public int Line { get; set; }
|
|
||||||
|
|
||||||
public string RuleName { get; set; }
|
|
||||||
|
|
||||||
public RuleViolationSeverity Severity { get; set; }
|
|
||||||
|
|
||||||
public string Text { get; set; }
|
|
||||||
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
return $@"{Severity.ToString().ToUpper()}: L{Line} C{Column} {FileName} ""{Text}""";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user