Добавьте файлы проекта.
This commit is contained in:
46
SQLLinter/Infrastructure/Rules/CaseSensitiveVariablesRule.cs
Normal file
46
SQLLinter/Infrastructure/Rules/CaseSensitiveVariablesRule.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Microsoft.SqlServer.TransactSql.ScriptDom;
|
||||
using SQLLinter.Common;
|
||||
|
||||
namespace SQLLinter.Infrastructure.Rules;
|
||||
|
||||
/// <summary>
|
||||
/// Имена переменных будут использовать общий регистр
|
||||
/// </summary>
|
||||
public class CaseSensitiveVariablesRule : BaseRuleVisitor, IRule
|
||||
{
|
||||
private readonly List<string> variableNames;
|
||||
|
||||
public CaseSensitiveVariablesRule()
|
||||
{
|
||||
this.variableNames = new List<string>();
|
||||
}
|
||||
|
||||
public override string Text => "Ожидается, что имена переменных будут использовать один регистр: {0} (DECLARE {1})";
|
||||
|
||||
public override void Visit(TSqlBatch node)
|
||||
{
|
||||
variableNames.Clear();
|
||||
}
|
||||
|
||||
public override void Visit(DeclareVariableStatement node)
|
||||
{
|
||||
foreach (var declaration in node.Declarations)
|
||||
{
|
||||
variableNames.Add(declaration.VariableName.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Visit(VariableReference node)
|
||||
{
|
||||
var variableName = node.Name;
|
||||
var caseInsensitiveMatch = variableNames.Where(v => v.ToUpper() == variableName.ToUpper());
|
||||
|
||||
if (!caseInsensitiveMatch.Any()) return;
|
||||
|
||||
var declareName = caseInsensitiveMatch.First();
|
||||
|
||||
if (declareName == variableName) return;
|
||||
|
||||
AddViolation(node, variableName, declareName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user