Skip to content

Commit cfe5cd4

Browse files
author
Kapil Borle
committed
Merge pull request #495 from PowerShell/FixUseApprovedVerbsExtent
Fix extents of UseSingularNoun and UseApprovedVerbs
2 parents e919621 + 1b7bfb0 commit cfe5cd4

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

Engine/Helper.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -656,22 +656,30 @@ public IScriptExtent GetScriptExtentForFunctionName(FunctionDefinitionAst functi
656656
if (null == functionDefinitionAst)
657657
{
658658
return null;
659-
}
660-
661-
// Obtain the index where the function name is in Tokens
662-
int funcTokenIndex = Tokens.Select((s, index) => new { s, index })
663-
.Where(x => x.s.Extent.StartOffset == functionDefinitionAst.Extent.StartOffset)
664-
.Select(x => x.index).FirstOrDefault();
659+
}
660+
var funcNameTokens = Tokens.Where(
661+
token =>
662+
ContainsExtent(functionDefinitionAst.Extent, token.Extent)
663+
&& token.Text.Equals(functionDefinitionAst.Name));
664+
var funcNameToken = funcNameTokens.FirstOrDefault();
665+
return funcNameToken == null ? null : funcNameToken.Extent;
666+
}
665667

666-
if (funcTokenIndex > 0 && funcTokenIndex < Helper.Instance.Tokens.Count())
668+
/// <summary>
669+
/// Return true if subset is contained in set
670+
/// </summary>
671+
/// <param name="set"></param>
672+
/// <param name="subset"></param>
673+
/// <returns>True or False</returns>
674+
private bool ContainsExtent(IScriptExtent set, IScriptExtent subset)
675+
{
676+
if (set == null || subset == null)
667677
{
668-
// return the extent of the next token - this is the extent for the function name
669-
return Tokens[++funcTokenIndex].Extent;
678+
return false;
670679
}
671-
672-
return null;
680+
return set.StartOffset <= subset.StartOffset
681+
&& set.EndOffset >= subset.EndOffset;
673682
}
674-
675683
private void FindClosingParenthesis(string keyword)
676684
{
677685
if (Tokens == null || Tokens.Length == 0)

Rules/UseApprovedVerbs.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,10 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
6464
if (!approvedVerbs.Contains(verb, StringComparer.OrdinalIgnoreCase))
6565
{
6666
IScriptExtent extent = Helper.Instance.GetScriptExtentForFunctionName(funcAst);
67-
6867
if (null == extent)
6968
{
7069
extent = funcAst.Extent;
7170
}
72-
7371
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseApprovedVerbsError, funcName),
7472
extent, GetName(), DiagnosticSeverity.Warning, fileName);
7573
}

Tests/Rules/UseSingularNounsReservedVerbs.tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Describe "UseSingularNouns" {
2020
It "has the correct description message" {
2121
$nounViolations[0].Message | Should Match $nounViolationMessage
2222
}
23+
24+
It "has the correct extent" {
25+
$nounViolations[0].Extent.Text | Should be "Verb-Files"
26+
}
2327
}
2428

2529
Context "When there are no violations" {
@@ -38,6 +42,10 @@ Describe "UseApprovedVerbs" {
3842
It "has the correct description message" {
3943
$verbViolations[0].Message | Should Match $verbViolationMessage
4044
}
45+
46+
It "has the correct extent" {
47+
$verbViolations[0].Extent.Text | Should be "Verb-Files"
48+
}
4149
}
4250

4351
Context "When there are no violations" {

0 commit comments

Comments
 (0)