Skip to content

Commit 88a77b9

Browse files
author
Kapil Borle
committed
Fix extents of UseSingularNoun and UseApprovedVerbs
Fixes search logic to find the function name in a function definition. Prior to the fix, if the function definition started on the first line of a script, the extent would include the entire function definition. This commit fixes that bug.
1 parent e919621 commit 88a77b9

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

Engine/Helper.cs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -656,22 +656,46 @@ public IScriptExtent GetScriptExtentForFunctionName(FunctionDefinitionAst functi
656656
if (null == functionDefinitionAst)
657657
{
658658
return null;
659+
}
660+
var funcNameToken = Tokens.Where(
661+
token => ContainsExtent(functionDefinitionAst.Extent, token.Extent)
662+
&& token.Text.Equals(functionDefinitionAst.Name));
663+
if (funcNameToken.Any())
664+
{
665+
return funcNameToken.First().Extent;
659666
}
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();
665-
666-
if (funcTokenIndex > 0 && funcTokenIndex < Helper.Instance.Tokens.Count())
667+
else
667668
{
668-
// return the extent of the next token - this is the extent for the function name
669-
return Tokens[++funcTokenIndex].Extent;
669+
return null;
670670
}
671+
}
671672

672-
return null;
673+
/// <summary>
674+
/// Return true of subset is contained in set
675+
/// </summary>
676+
/// <param name="set"></param>
677+
/// <param name="subset"></param>
678+
/// <returns>True or False</returns>
679+
private bool ContainsExtent(IScriptExtent set, IScriptExtent subset)
680+
{
681+
if (set == null)
682+
{
683+
throw new ArgumentNullException("set");
684+
}
685+
if (subset == null)
686+
{
687+
throw new ArgumentNullException("subset");
688+
}
689+
if (set.StartOffset <= subset.StartOffset
690+
&& set.EndOffset >= subset.EndOffset)
691+
{
692+
return true;
693+
}
694+
else
695+
{
696+
return false;
697+
}
673698
}
674-
675699
private void FindClosingParenthesis(string keyword)
676700
{
677701
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)