-
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.
Add new inspection for Excel UDFs hidden by cells.
- Loading branch information
Showing
14 changed files
with
279 additions
and
179 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
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
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
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
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
Oops, something went wrong.