-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4488 from comintern/validref
Add new inspection for Excel UDFs hidden by cells.
- Loading branch information
Showing
14 changed files
with
799 additions
and
921 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
Rubberduck.CodeAnalysis/Inspections/Concrete/ExcelUdfNameIsValidCellReferenceInspection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using Rubberduck.Inspections.Abstract; | ||
using Rubberduck.Inspections.Results; | ||
using Rubberduck.Parsing.Inspections; | ||
using Rubberduck.Parsing.Inspections.Abstract; | ||
using Rubberduck.Parsing.Symbols; | ||
using Rubberduck.Parsing.VBA; | ||
using Rubberduck.Resources.Inspections; | ||
|
||
namespace Rubberduck.Inspections.Inspections.Concrete | ||
{ | ||
[RequiredLibrary("Excel")] | ||
public class ExcelUdfNameIsValidCellReferenceInspection : InspectionBase | ||
{ | ||
public ExcelUdfNameIsValidCellReferenceInspection(RubberduckParserState state) : base(state) { } | ||
|
||
private static readonly Regex ValidCellIdRegex = | ||
new Regex(@"^([a-z]|[a-z]{2}|[a-w][a-z]{2}|x([a-e][a-z]|f[a-d]))(?<Row>\d+)$", | ||
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); | ||
|
||
private static readonly HashSet<Accessibility> VisibleAsUdf = new HashSet<Accessibility> { Accessibility.Public, Accessibility.Implicit }; | ||
|
||
private const uint MaximumExcelRows = 1048576; | ||
|
||
protected override IEnumerable<IInspectionResult> DoGetInspectionResults() | ||
{ | ||
var excel = State.DeclarationFinder.Projects.SingleOrDefault(item => !item.IsUserDefined && item.IdentifierName == "Excel"); | ||
if (excel == null) | ||
{ | ||
return Enumerable.Empty<IInspectionResult>(); | ||
} | ||
|
||
var candidates = UserDeclarations.OfType<FunctionDeclaration>().Where(decl => | ||
decl.ParentScopeDeclaration.DeclarationType == DeclarationType.ProceduralModule && | ||
VisibleAsUdf.Contains(decl.Accessibility)); | ||
|
||
return (from function in candidates.Where(decl => ValidCellIdRegex.IsMatch(decl.IdentifierName)) | ||
let row = Convert.ToUInt32(ValidCellIdRegex.Matches(function.IdentifierName)[0].Groups["Row"].Value) | ||
where row > 0 && row <= MaximumExcelRows && !IsIgnoringInspectionResultFor(function, AnnotationName) | ||
select new DeclarationInspectionResult(this, | ||
string.Format(InspectionResults.ExcelUdfNameIsValidCellReferenceInspection, function.IdentifierName), | ||
function)) | ||
.Cast<IInspectionResult>().ToList(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
Rubberduck.Resources/Inspections/InspectionResults.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
128 changes: 128 additions & 0 deletions
128
RubberduckTests/Inspections/ExcelUdfNameIsValidCellReferenceInspectionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using System.Linq; | ||
using System.Threading; | ||
using NUnit.Framework; | ||
using Rubberduck.Inspections.Inspections.Concrete; | ||
using Rubberduck.VBEditor.SafeComWrappers; | ||
using RubberduckTests.Mocks; | ||
|
||
namespace RubberduckTests.Inspections | ||
{ | ||
[TestFixture] | ||
public class ExcelUdfNameIsValidCellReferenceInspectionTests | ||
{ | ||
[TestCase("a1")] | ||
[TestCase("A1")] | ||
[TestCase("AA1")] | ||
[TestCase("ZZ1")] | ||
[TestCase("XFD1")] | ||
[TestCase("XEZ1")] | ||
[TestCase("WZZ1")] | ||
[TestCase("Foo42")] | ||
[TestCase("XFD1048576")] | ||
[Category("Inspections")] | ||
public void ExcelUdfNameIsValidCellReferenceInspection_ReturnsResult_ValidCells(string identifier) | ||
{ | ||
const string codeTemplate = | ||
@"Public Function {0}() As Long | ||
{0} = 42 | ||
End Function | ||
"; | ||
|
||
Assert.AreEqual(1, InspectionResultCount(string.Format(codeTemplate, identifier), ComponentType.StandardModule)); | ||
} | ||
|
||
[TestCase("Foo")] | ||
[TestCase("XXX69")] | ||
[TestCase("XKCD42")] | ||
[TestCase("AAA1234567")] | ||
[Category("Inspections")] | ||
public void ExcelUdfNameIsValidCellReferenceInspection_ReturnsNoResult_InvalidAsCell(string identifier) | ||
{ | ||
const string codeTemplate = | ||
@"Public Function {0}() As Long | ||
{0} = 42 | ||
End Function | ||
"; | ||
|
||
Assert.AreEqual(0, InspectionResultCount(string.Format(codeTemplate, identifier), ComponentType.StandardModule)); | ||
} | ||
|
||
[TestCase(ComponentType.ClassModule)] | ||
[TestCase(ComponentType.UserForm)] | ||
[TestCase(ComponentType.DocObject)] | ||
[Category("Inspections")] | ||
public void ExcelUdfNameIsValidCellReferenceInspection_ReturnsNoResult_NonStandardModule(ComponentType moduleType) | ||
{ | ||
const string code = | ||
@"Public Function A1() As Long | ||
A1 = 42 | ||
End Function | ||
"; | ||
|
||
Assert.AreEqual(0, InspectionResultCount(code, moduleType)); | ||
} | ||
|
||
[Test] | ||
[Category("Inspections")] | ||
public void ExcelUdfNameIsValidCellReferenceInspection_ReturnsNoResult_Ignored() | ||
{ | ||
const string code = | ||
@"'@Ignore ExcelUdfNameIsValidCellReference | ||
Public Function A1() As Long | ||
A1 = 42 | ||
End Function | ||
"; | ||
|
||
Assert.AreEqual(0, InspectionResultCount(code, ComponentType.StandardModule)); | ||
} | ||
|
||
[Test] | ||
[Category("Inspections")] | ||
public void ExcelUdfNameIsValidCellReferenceInspection_ReturnsNoResult_PrivateFunction() | ||
{ | ||
const string code = | ||
@"Private Function A1() As Long | ||
A1 = 42 | ||
End Function | ||
"; | ||
|
||
Assert.AreEqual(0, InspectionResultCount(code, ComponentType.StandardModule)); | ||
} | ||
|
||
[TestCase("Sub A1()", "Sub")] | ||
[TestCase("Property Get A1() As Long", "Property")] | ||
[TestCase("Property Let A1(foo As Long)", "Property")] | ||
[TestCase("Property Set A1(foo As Variant)", "Property")] | ||
[Category("Inspections")] | ||
public void ExcelUdfNameIsValidCellReferenceInspection_ReturnsNoResult_NonFunction(string signature, string ending) | ||
{ | ||
const string codeTemplate = | ||
@"{0} | ||
A1 = 42 | ||
End {1} | ||
"; | ||
|
||
Assert.AreEqual(0, InspectionResultCount(string.Format(codeTemplate, signature, ending), ComponentType.StandardModule)); | ||
} | ||
|
||
private static int InspectionResultCount(string inputCode, ComponentType moduleType) | ||
{ | ||
var builder = new MockVbeBuilder(); | ||
var project = builder.ProjectBuilder("VBAProject", ProjectProtection.Unprotected) | ||
.AddComponent("UnderTest", moduleType, inputCode) | ||
.AddReference("Excel", MockVbeBuilder.LibraryPathMsExcel, 1, 8, true) | ||
.Build(); | ||
|
||
var vbe = builder.AddProject(project).Build(); | ||
|
||
using (var state = MockParser.CreateAndParse(vbe.Object)) | ||
{ | ||
|
||
var inspection = new ExcelUdfNameIsValidCellReferenceInspection(state); | ||
var inspectionResults = inspection.GetInspectionResults(CancellationToken.None); | ||
|
||
return inspectionResults.Count(); | ||
} | ||
} | ||
} | ||
} |