52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using Microsoft.SqlServer.TransactSql.ScriptDom;
|
|
using SQLLinter.Common;
|
|
using SQLLinter.Common.Helpers;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace SQLLinter.Infrastructure.Rules;
|
|
|
|
public class DuplicateEmptyLineRule : BaseRuleVisitor, IRule
|
|
{
|
|
private static readonly Regex EmptyLineRegex = new(@"^\s*$", RegexOptions.Compiled);
|
|
|
|
|
|
public override string Text => "Обнаружен дубликат новой строки";
|
|
|
|
public override void Visit(TSqlScript node)
|
|
{
|
|
var isEmptyLine = false;
|
|
var fileLines = FixHelpers.GetString(node)
|
|
.Split('\n')
|
|
.ToList();
|
|
|
|
for (var i = 0; i < fileLines.Count; i++)
|
|
{
|
|
if (EmptyLineRegex.IsMatch(fileLines[i]))
|
|
{
|
|
if (isEmptyLine)
|
|
{
|
|
AddViolation(Name, Text, i + 1, 1);
|
|
}
|
|
|
|
isEmptyLine = true;
|
|
}
|
|
else
|
|
{
|
|
isEmptyLine = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void FixViolation(List<string> fileLines, IRuleViolation ruleViolation, FileLineActions actions)
|
|
{
|
|
if (ruleViolation.Line - 1 == fileLines.Count)
|
|
{
|
|
actions.RemoveAt(ruleViolation.Line - 2);
|
|
}
|
|
else
|
|
{
|
|
actions.RemoveAt(ruleViolation.Line - 1);
|
|
}
|
|
}
|
|
}
|