diff --git a/Engine/Helper.cs b/Engine/Helper.cs index a15e5166b..cfd75a937 100644 --- a/Engine/Helper.cs +++ b/Engine/Helper.cs @@ -656,22 +656,30 @@ public IScriptExtent GetScriptExtentForFunctionName(FunctionDefinitionAst functi if (null == functionDefinitionAst) { return null; - } - - // Obtain the index where the function name is in Tokens - int funcTokenIndex = Tokens.Select((s, index) => new { s, index }) - .Where(x => x.s.Extent.StartOffset == functionDefinitionAst.Extent.StartOffset) - .Select(x => x.index).FirstOrDefault(); + } + var funcNameTokens = Tokens.Where( + token => + ContainsExtent(functionDefinitionAst.Extent, token.Extent) + && token.Text.Equals(functionDefinitionAst.Name)); + var funcNameToken = funcNameTokens.FirstOrDefault(); + return funcNameToken == null ? null : funcNameToken.Extent; + } - if (funcTokenIndex > 0 && funcTokenIndex < Helper.Instance.Tokens.Count()) + /// + /// Return true if subset is contained in set + /// + /// + /// + /// True or False + private bool ContainsExtent(IScriptExtent set, IScriptExtent subset) + { + if (set == null || subset == null) { - // return the extent of the next token - this is the extent for the function name - return Tokens[++funcTokenIndex].Extent; + return false; } - - return null; + return set.StartOffset <= subset.StartOffset + && set.EndOffset >= subset.EndOffset; } - private void FindClosingParenthesis(string keyword) { if (Tokens == null || Tokens.Length == 0) diff --git a/Rules/UseApprovedVerbs.cs b/Rules/UseApprovedVerbs.cs index e0ac84b98..93c883c31 100644 --- a/Rules/UseApprovedVerbs.cs +++ b/Rules/UseApprovedVerbs.cs @@ -64,12 +64,10 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) { if (!approvedVerbs.Contains(verb, StringComparer.OrdinalIgnoreCase)) { IScriptExtent extent = Helper.Instance.GetScriptExtentForFunctionName(funcAst); - if (null == extent) { extent = funcAst.Extent; } - yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseApprovedVerbsError, funcName), extent, GetName(), DiagnosticSeverity.Warning, fileName); } diff --git a/Tests/Rules/UseSingularNounsReservedVerbs.tests.ps1 b/Tests/Rules/UseSingularNounsReservedVerbs.tests.ps1 index 25e30b445..6685bd0d4 100644 --- a/Tests/Rules/UseSingularNounsReservedVerbs.tests.ps1 +++ b/Tests/Rules/UseSingularNounsReservedVerbs.tests.ps1 @@ -20,6 +20,10 @@ Describe "UseSingularNouns" { It "has the correct description message" { $nounViolations[0].Message | Should Match $nounViolationMessage } + + It "has the correct extent" { + $nounViolations[0].Extent.Text | Should be "Verb-Files" + } } Context "When there are no violations" { @@ -38,6 +42,10 @@ Describe "UseApprovedVerbs" { It "has the correct description message" { $verbViolations[0].Message | Should Match $verbViolationMessage } + + It "has the correct extent" { + $verbViolations[0].Extent.Text | Should be "Verb-Files" + } } Context "When there are no violations" {