Skip to content

Commit 599e852

Browse files
committed
Merge branch 'next' of https://github.com/rubberduck-vba/Rubberduck into CleanComSafeLeakRound2
# Conflicts: # Rubberduck.CodeAnalysis/QuickFixes/IgnoreOnceQuickFix.cs # Rubberduck.Resources/Rubberduck.Resources.csproj
2 parents 869f783 + fb0f9b7 commit 599e852

File tree

223 files changed

+6236
-7550
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

223 files changed

+6236
-7550
lines changed

Rubberduck.API/Rubberduck.API.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Project Sdk="Microsoft.NET.Sdk">
2+
<Project Sdk="Sunburst.NET.Sdk.WPF/1.0.47">
33
<PropertyGroup>
44
<Product>Rubberduck.API</Product>
55
<Description>Rubberduck Reflection API</Description>

Rubberduck.API/VBA/Parser.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ internal Parser(object vbe) : this()
9696
var projectRepository = new ProjectsRepository(_vbe);
9797
_state = new RubberduckParserState(_vbe, projectRepository, declarationFinderFactory, _vbeEvents);
9898
_state.StateChanged += _state_StateChanged;
99-
100-
var sourceFileHandler = _vbe.TempSourceFileHandler;
10199
var vbeVersion = double.Parse(_vbe.Version, CultureInfo.InvariantCulture);
102100
var predefinedCompilationConstants = new VBAPredefinedCompilationConstants(vbeVersion);
103101
var typeLibProvider = new TypeLibWrapperProvider(projectRepository);
@@ -110,7 +108,6 @@ internal Parser(object vbe) : this()
110108
var mainTokenStreamParser = new VBATokenStreamParser(mainParseErrorListenerFactory, mainParseErrorListenerFactory);
111109
var tokenStreamProvider = new SimpleVBAModuleTokenStreamProvider();
112110
var stringParser = new TokenStreamParserStringParserAdapterWithPreprocessing(tokenStreamProvider, mainTokenStreamParser, preprocessor);
113-
var attributesSourceCodeHandler = new SourceFileHandlerSourceCodeHandlerAdapter(sourceFileHandler, projectRepository);
114111
var projectManager = new RepositoryProjectManager(projectRepository);
115112
var moduleToModuleReferenceManager = new ModuleToModuleReferenceManager();
116113
var parserStateManager = new ParserStateManager(_state);
@@ -131,14 +128,12 @@ internal Parser(object vbe) : this()
131128
}
132129
);
133130
var codePaneSourceCodeHandler = new CodePaneSourceCodeHandler(projectRepository);
134-
var moduleRewriterFactory = new ModuleRewriterFactory(
135-
codePaneSourceCodeHandler,
136-
attributesSourceCodeHandler);
131+
var sourceFileHandler = _vbe.TempSourceFileHandler;
132+
var attributesSourceCodeHandler = new SourceFileHandlerSourceCodeHandlerAdapter(sourceFileHandler, projectRepository);
137133
var moduleParser = new ModuleParser(
138134
codePaneSourceCodeHandler,
139135
attributesSourceCodeHandler,
140-
stringParser,
141-
moduleRewriterFactory);
136+
stringParser);
142137
var parseRunner = new ParseRunner(
143138
_state,
144139
parserStateManager,

Rubberduck.CodeAnalysis/Inspections/Abstract/QuickFixBase.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
using System.Linq;
44
using NLog;
55
using Rubberduck.Parsing.Inspections.Abstract;
6-
using Rubberduck.Parsing.VBA;
6+
using Rubberduck.Parsing.Rewriter;
77
using Rubberduck.Parsing.VBA.Extensions;
8+
using Rubberduck.Parsing.VBA.Parsing;
89

910
namespace Rubberduck.Inspections.Abstract
1011
{
@@ -39,7 +40,9 @@ public void RemoveInspections(params Type[] inspections)
3940
_supportedInspections = _supportedInspections.Except(inspections).ToHashSet();
4041
}
4142

42-
public abstract void Fix(IInspectionResult result);
43+
public virtual CodeKind TargetCodeKind => CodeKind.CodePaneCode;
44+
45+
public abstract void Fix(IInspectionResult result, IRewriteSession rewriteSession);
4346
public abstract string Description(IInspectionResult result);
4447

4548
public abstract bool CanFixInProcedure { get; }

Rubberduck.CodeAnalysis/Inspections/Concrete/AssignmentNotUsedInspection.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Rubberduck.Inspections.CodePathAnalysis.Extensions;
88
using System.Linq;
99
using Rubberduck.Inspections.Results;
10+
using Rubberduck.Parsing;
11+
using Rubberduck.Parsing.Grammar;
1012

1113
namespace Rubberduck.Inspections.Concrete
1214
{
@@ -21,7 +23,9 @@ public AssignmentNotUsedInspection(RubberduckParserState state, Walker walker)
2123

2224
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
2325
{
24-
var variables = State.DeclarationFinder.UserDeclarations(DeclarationType.Variable);
26+
var variables = State.DeclarationFinder
27+
.UserDeclarations(DeclarationType.Variable)
28+
.Where(d => !d.IsArray);
2529

2630
var nodes = new List<IdentifierReference>();
2731
foreach (var variable in variables)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text.RegularExpressions;
5+
using Rubberduck.Inspections.Abstract;
6+
using Rubberduck.Inspections.Results;
7+
using Rubberduck.Parsing.Inspections;
8+
using Rubberduck.Parsing.Inspections.Abstract;
9+
using Rubberduck.Parsing.Symbols;
10+
using Rubberduck.Parsing.VBA;
11+
using Rubberduck.Resources.Inspections;
12+
13+
namespace Rubberduck.Inspections.Inspections.Concrete
14+
{
15+
[RequiredLibrary("Excel")]
16+
public class ExcelUdfNameIsValidCellReferenceInspection : InspectionBase
17+
{
18+
public ExcelUdfNameIsValidCellReferenceInspection(RubberduckParserState state) : base(state) { }
19+
20+
private static readonly Regex ValidCellIdRegex =
21+
new Regex(@"^([a-z]|[a-z]{2}|[a-w][a-z]{2}|x([a-e][a-z]|f[a-d]))(?<Row>\d+)$",
22+
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
23+
24+
private static readonly HashSet<Accessibility> VisibleAsUdf = new HashSet<Accessibility> { Accessibility.Public, Accessibility.Implicit };
25+
26+
private const uint MaximumExcelRows = 1048576;
27+
28+
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
29+
{
30+
var excel = State.DeclarationFinder.Projects.SingleOrDefault(item => !item.IsUserDefined && item.IdentifierName == "Excel");
31+
if (excel == null)
32+
{
33+
return Enumerable.Empty<IInspectionResult>();
34+
}
35+
36+
var candidates = UserDeclarations.OfType<FunctionDeclaration>().Where(decl =>
37+
decl.ParentScopeDeclaration.DeclarationType == DeclarationType.ProceduralModule &&
38+
VisibleAsUdf.Contains(decl.Accessibility));
39+
40+
return (from function in candidates.Where(decl => ValidCellIdRegex.IsMatch(decl.IdentifierName))
41+
let row = Convert.ToUInt32(ValidCellIdRegex.Matches(function.IdentifierName)[0].Groups["Row"].Value)
42+
where row > 0 && row <= MaximumExcelRows && !IsIgnoringInspectionResultFor(function, AnnotationName)
43+
select new DeclarationInspectionResult(this,
44+
string.Format(InspectionResults.ExcelUdfNameIsValidCellReferenceInspection, function.IdentifierName),
45+
function))
46+
.Cast<IInspectionResult>().ToList();
47+
}
48+
}
49+
}

Rubberduck.CodeAnalysis/Inspections/Concrete/FunctionReturnValueNotUsedInspection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
2828
var interfaceImplementationMembers = State.DeclarationFinder.FindAllInterfaceImplementingMembers();
2929
var functions = State.DeclarationFinder
3030
.UserDeclarations(DeclarationType.Function)
31-
.Where(item => !IsIgnoringInspectionResultFor(item, AnnotationName))
31+
.Where(item => !IsIgnoringInspectionResultFor(item, AnnotationName) &&
32+
item.References.Any(r => !IsReturnStatement(item, r) && !r.IsAssignment))
3233
.ToList();
3334
var interfaceMemberIssues = GetInterfaceMemberIssues(interfaceMembers);
3435
var nonInterfaceFunctions = functions.Except(interfaceMembers.Union(interfaceImplementationMembers));

Rubberduck.CodeAnalysis/Inspections/Concrete/UnassignedVariableUsageInspection.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Rubberduck.Inspections.Abstract;
66
using Rubberduck.Inspections.Results;
77
using Rubberduck.Parsing;
8+
using Rubberduck.Parsing.Grammar;
89
using Rubberduck.Parsing.Inspections.Abstract;
910
using Rubberduck.Resources.Inspections;
1011
using Rubberduck.Parsing.Symbols;
@@ -30,7 +31,7 @@ public UnassignedVariableUsageInspection(RubberduckParserState state)
3031
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
3132
{
3233
var declarations = State.DeclarationFinder.UserDeclarations(DeclarationType.Variable)
33-
.Where(declaration =>
34+
.Where(declaration => !declaration.IsArray &&
3435
State.DeclarationFinder.MatchName(declaration.AsTypeName)
3536
.All(d => d.DeclarationType != DeclarationType.UserDefinedType)
3637
&& !declaration.IsSelfAssigned
@@ -43,12 +44,22 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
4344
.SelectMany(d => d.References)
4445
.Distinct()
4546
.Where(r => !r.IsIgnoringInspectionResultFor(AnnotationName))
47+
.Where(r => !r.Context.TryGetAncestor<VBAParser.RedimStmtContext>(out _) && !IsArraySubscriptAssignment(r))
4648
.Select(r => new IdentifierReferenceInspectionResult(this,
4749
string.Format(InspectionResults.UnassignedVariableUsageInspection, r.IdentifierName),
4850
State,
4951
r)).ToList();
5052
}
5153

54+
private static bool IsArraySubscriptAssignment(IdentifierReference reference)
55+
{
56+
var isLetAssignment = reference.Context.TryGetAncestor<VBAParser.LetStmtContext>(out var letStmt);
57+
var isSetAssignment = reference.Context.TryGetAncestor<VBAParser.SetStmtContext>(out var setStmt);
58+
59+
return isLetAssignment && letStmt.lExpression() is VBAParser.IndexExprContext ||
60+
isSetAssignment && setStmt.lExpression() is VBAParser.IndexExprContext;
61+
}
62+
5263
private static bool DeclarationReferencesContainsReference(Declaration parentDeclaration, Declaration target)
5364
{
5465
foreach (var targetReference in target.References)

Rubberduck.CodeAnalysis/Inspections/Concrete/VariableRequiresSetAssignmentEvaluator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public static class VariableRequiresSetAssignmentEvaluator
1313
/// Determines whether the 'Set' keyword is required (whether it's present or not) for the specified identifier reference.
1414
/// </summary>
1515
/// <param name="reference">The identifier reference to analyze</param>
16-
/// <param name="state">The parser state</param>
17-
public static bool RequiresSetAssignment(IdentifierReference reference, RubberduckParserState state)
16+
/// <param name="declarationFinderProvider">The parser state</param>
17+
public static bool RequiresSetAssignment(IdentifierReference reference, IDeclarationFinderProvider declarationFinderProvider)
1818
{
1919
if (!reference.IsAssignment)
2020
{
@@ -89,7 +89,7 @@ public static bool RequiresSetAssignment(IdentifierReference reference, Rubberdu
8989

9090
// todo resolve expression return type
9191

92-
var memberRefs = state.DeclarationFinder.IdentifierReferences(reference.ParentScoping.QualifiedName);
92+
var memberRefs = declarationFinderProvider.DeclarationFinder.IdentifierReferences(reference.ParentScoping.QualifiedName);
9393
var lastRef = memberRefs.LastOrDefault(r => !Equals(r, reference) && r.Context.GetAncestor<VBAParser.LetStmtContext>() == letStmtContext);
9494
if (lastRef?.Declaration.AsTypeDeclaration?.DeclarationType.HasFlag(DeclarationType.ClassModule) ?? false)
9595
{
@@ -104,7 +104,7 @@ public static bool RequiresSetAssignment(IdentifierReference reference, Rubberdu
104104
// is the reference referring to something else in scope that's a object?
105105
var project = Declaration.GetProjectParent(reference.ParentScoping);
106106
var module = Declaration.GetModuleParent(reference.ParentScoping);
107-
return state.DeclarationFinder.MatchName(expression.GetText().ToLowerInvariant())
107+
return declarationFinderProvider.DeclarationFinder.MatchName(expression.GetText().ToLowerInvariant())
108108
.Any(decl => (decl.DeclarationType.HasFlag(DeclarationType.ClassModule) || Tokens.Object.Equals(decl.AsTypeName))
109109
&& AccessibilityCheck.IsAccessible(project, module, reference.ParentScoping, decl));
110110
}

Rubberduck.CodeAnalysis/QuickFixes/AccessSheetUsingCodeNameQuickFix.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,26 @@
66
using Rubberduck.Parsing;
77
using Rubberduck.Parsing.Grammar;
88
using Rubberduck.Parsing.Inspections.Abstract;
9+
using Rubberduck.Parsing.Rewriter;
910
using Rubberduck.Parsing.VBA;
1011

1112
namespace Rubberduck.Inspections.QuickFixes
1213
{
13-
public class AccessSheetUsingCodeNameQuickFix : QuickFixBase
14+
public sealed class AccessSheetUsingCodeNameQuickFix : QuickFixBase
1415
{
15-
private readonly RubberduckParserState _state;
16+
private readonly IDeclarationFinderProvider _declarationFinderProvider;
1617

17-
public AccessSheetUsingCodeNameQuickFix(RubberduckParserState state)
18+
public AccessSheetUsingCodeNameQuickFix(IDeclarationFinderProvider declarationFinderProvider)
1819
: base(typeof(SheetAccessedUsingStringInspection))
1920
{
20-
_state = state;
21+
_declarationFinderProvider = declarationFinderProvider;
2122
}
2223

23-
public override void Fix(IInspectionResult result)
24+
public override void Fix(IInspectionResult result, IRewriteSession rewriteSession)
2425
{
2526
var referenceResult = (IdentifierReferenceInspectionResult)result;
2627

27-
var rewriter = _state.GetRewriter(referenceResult.QualifiedName);
28+
var rewriter = rewriteSession.CheckOutModuleRewriter(referenceResult.QualifiedName);
2829

2930
var setStatement = referenceResult.Context.GetAncestor<VBAParser.SetStmtContext>();
3031
var isArgument = referenceResult.Context.GetAncestor<VBAParser.ArgumentContext>() != null;
@@ -36,14 +37,14 @@ public override void Fix(IInspectionResult result)
3637
var indexExprContext = referenceResult.Context.Parent.Parent as VBAParser.IndexExprContext ??
3738
referenceResult.Context.Parent as VBAParser.IndexExprContext;
3839

39-
rewriter.Replace(indexExprContext, referenceResult.Properties.CodeName);
40+
rewriter.Replace(indexExprContext, (string)referenceResult.Properties.CodeName);
4041
}
4142
else
4243
{
4344
// Sheet assigned to variable
4445

4546
var sheetVariableName = setStatement.lExpression().GetText();
46-
var sheetDeclaration = _state.DeclarationFinder.MatchName(sheetVariableName)
47+
var sheetDeclaration = _declarationFinderProvider.DeclarationFinder.MatchName(sheetVariableName)
4748
.First(declaration =>
4849
{
4950
var moduleBodyElement = declaration.Context.GetAncestor<VBAParser.ModuleBodyElementContext>();
@@ -71,7 +72,7 @@ public override void Fix(IInspectionResult result)
7172

7273
foreach (var reference in sheetDeclaration.References)
7374
{
74-
rewriter.Replace(reference.Context, referenceResult.Properties.CodeName);
75+
rewriter.Replace(reference.Context, (string)referenceResult.Properties.CodeName);
7576
}
7677

7778
rewriter.Remove(setStatement);

Rubberduck.CodeAnalysis/QuickFixes/AddIdentifierToWhiteListQuickFix.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Rubberduck.Inspections.Abstract;
33
using Rubberduck.Inspections.Concrete;
44
using Rubberduck.Parsing.Inspections.Abstract;
5+
using Rubberduck.Parsing.Rewriter;
56
using Rubberduck.Settings;
67
using Rubberduck.SettingsProvider;
78

@@ -17,7 +18,8 @@ public AddIdentifierToWhiteListQuickFix(IPersistanceService<CodeInspectionSettin
1718
_settings = settings;
1819
}
1920

20-
public override void Fix(IInspectionResult result)
21+
//The rewriteSession is optional since it is not used in this particular quickfix.
22+
public override void Fix(IInspectionResult result, IRewriteSession rewriteSession = null)
2123
{
2224
var inspectionSettings = _settings.Load(new CodeInspectionSettings()) ?? new CodeInspectionSettings();
2325
var whitelist = inspectionSettings.WhitelistedIdentifiers;

0 commit comments

Comments
 (0)