Skip to content

Commit 891955d

Browse files
committed
Merge pull request #448 from PowerShell/ExtentFixBranch
Emit accurate extents for non-approved verb violations
2 parents 0367f51 + 5f73a2c commit 891955d

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

Engine/Helper.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,28 @@ 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(FunctionDefinitionAst functionDefinitionAst)
650+
{
651+
if (null == functionDefinitionAst)
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.Extent.StartOffset == functionDefinitionAst.Extent.StartOffset)
659+
.Select(x => x.index).FirstOrDefault();
660+
661+
if (funcTokenIndex > 0 && funcTokenIndex < Helper.Instance.Tokens.Count())
662+
{
663+
// return the extent of the next token - this is the extent for the function name
664+
return Tokens[++funcTokenIndex].Extent;
665+
}
666+
667+
return null;
668+
}
669+
648670
private void FindClosingParenthesis(string keyword)
649671
{
650672
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(funcAst);
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
}

Rules/UseSingularNouns.cs

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

5757
if (!ps.IsSingular(noun) && ps.IsPlural(noun))
5858
{
59+
IScriptExtent extent = Helper.Instance.GetScriptExtentForFunctionName(funcAst);
60+
61+
if (null == extent)
62+
{
63+
extent = funcAst.Extent;
64+
}
65+
5966
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseSingularNounsError, funcAst.Name),
60-
funcAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
67+
extent, GetName(), DiagnosticSeverity.Warning, fileName);
6168
}
6269
}
6370
}

0 commit comments

Comments
 (0)