Skip to content

Commit 86469ac

Browse files
committed
Emit accurate extents for non-approved verb violations
1 parent 0367f51 commit 86469ac

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Engine/Helper.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,27 @@ public bool SkipBlock(string keyword, Ast namedBlockAst)
645645
return false;
646646
}
647647

648+
// Obtain script extent for the function - just around the function name
649+
public IScriptExtent GetScriptExtentForFunctionName(string functionName)
650+
{
651+
if (String.IsNullOrEmpty(functionName))
652+
{
653+
return null;
654+
}
655+
656+
// Obtain the index where the function name is in Tokens
657+
int funcTokenIndex = Tokens.Select((s, index) => new { s, index })
658+
.Where(x => x.s.Text == functionName)
659+
.Select(x => x.index).FirstOrDefault();
660+
661+
if (funcTokenIndex > 0 && funcTokenIndex <= Helper.Instance.Tokens.Count())
662+
{
663+
return Tokens[funcTokenIndex].Extent;
664+
}
665+
666+
return null;
667+
}
668+
648669
private void FindClosingParenthesis(string keyword)
649670
{
650671
if (Tokens == null || Tokens.Length == 0)

Rules/UseApprovedVerbs.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,15 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
6363

6464
if (!approvedVerbs.Contains(verb, StringComparer.OrdinalIgnoreCase))
6565
{
66+
IScriptExtent extent = Helper.Instance.GetScriptExtentForFunctionName(funcName);
67+
68+
if (null == extent)
69+
{
70+
extent = funcAst.Extent;
71+
}
72+
6673
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseApprovedVerbsError, funcName),
67-
funcAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
74+
extent, GetName(), DiagnosticSeverity.Warning, fileName);
6875
}
6976
}
7077
}

0 commit comments

Comments
 (0)