Skip to content

Fix extents of UseSingularNoun and UseApprovedVerbs #495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions Engine/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
/// <summary>
/// Return true if subset is contained in set
/// </summary>
/// <param name="set"></param>
/// <param name="subset"></param>
/// <returns>True or False</returns>
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)
Expand Down
2 changes: 0 additions & 2 deletions Rules/UseApprovedVerbs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@ public IEnumerable<DiagnosticRecord> 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);
}
Expand Down
8 changes: 8 additions & 0 deletions Tests/Rules/UseSingularNounsReservedVerbs.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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" {
Expand Down