Skip to content

Commit

Permalink
Merge branch 'next' of https://github.com/rubberduck-vba/Rubberduck i…
Browse files Browse the repository at this point in the history
…nto CleanComSafeLeakRound2

# Conflicts:
#	Rubberduck.CodeAnalysis/QuickFixes/IgnoreOnceQuickFix.cs
#	Rubberduck.Resources/Rubberduck.Resources.csproj
  • Loading branch information
bclothier committed Nov 17, 2018
2 parents 869f783 + fb0f9b7 commit 599e852
Show file tree
Hide file tree
Showing 223 changed files with 6,236 additions and 7,550 deletions.
2 changes: 1 addition & 1 deletion Rubberduck.API/Rubberduck.API.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Sunburst.NET.Sdk.WPF/1.0.47">
<PropertyGroup>
<Product>Rubberduck.API</Product>
<Description>Rubberduck Reflection API</Description>
Expand Down
11 changes: 3 additions & 8 deletions Rubberduck.API/VBA/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ internal Parser(object vbe) : this()
var projectRepository = new ProjectsRepository(_vbe);
_state = new RubberduckParserState(_vbe, projectRepository, declarationFinderFactory, _vbeEvents);
_state.StateChanged += _state_StateChanged;

var sourceFileHandler = _vbe.TempSourceFileHandler;
var vbeVersion = double.Parse(_vbe.Version, CultureInfo.InvariantCulture);
var predefinedCompilationConstants = new VBAPredefinedCompilationConstants(vbeVersion);
var typeLibProvider = new TypeLibWrapperProvider(projectRepository);
Expand All @@ -110,7 +108,6 @@ internal Parser(object vbe) : this()
var mainTokenStreamParser = new VBATokenStreamParser(mainParseErrorListenerFactory, mainParseErrorListenerFactory);
var tokenStreamProvider = new SimpleVBAModuleTokenStreamProvider();
var stringParser = new TokenStreamParserStringParserAdapterWithPreprocessing(tokenStreamProvider, mainTokenStreamParser, preprocessor);
var attributesSourceCodeHandler = new SourceFileHandlerSourceCodeHandlerAdapter(sourceFileHandler, projectRepository);
var projectManager = new RepositoryProjectManager(projectRepository);
var moduleToModuleReferenceManager = new ModuleToModuleReferenceManager();
var parserStateManager = new ParserStateManager(_state);
Expand All @@ -131,14 +128,12 @@ internal Parser(object vbe) : this()
}
);
var codePaneSourceCodeHandler = new CodePaneSourceCodeHandler(projectRepository);
var moduleRewriterFactory = new ModuleRewriterFactory(
codePaneSourceCodeHandler,
attributesSourceCodeHandler);
var sourceFileHandler = _vbe.TempSourceFileHandler;
var attributesSourceCodeHandler = new SourceFileHandlerSourceCodeHandlerAdapter(sourceFileHandler, projectRepository);
var moduleParser = new ModuleParser(
codePaneSourceCodeHandler,
attributesSourceCodeHandler,
stringParser,
moduleRewriterFactory);
stringParser);
var parseRunner = new ParseRunner(
_state,
parserStateManager,
Expand Down
7 changes: 5 additions & 2 deletions Rubberduck.CodeAnalysis/Inspections/Abstract/QuickFixBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using System.Linq;
using NLog;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.Rewriter;
using Rubberduck.Parsing.VBA.Extensions;
using Rubberduck.Parsing.VBA.Parsing;

namespace Rubberduck.Inspections.Abstract
{
Expand Down Expand Up @@ -39,7 +40,9 @@ public void RemoveInspections(params Type[] inspections)
_supportedInspections = _supportedInspections.Except(inspections).ToHashSet();
}

public abstract void Fix(IInspectionResult result);
public virtual CodeKind TargetCodeKind => CodeKind.CodePaneCode;

public abstract void Fix(IInspectionResult result, IRewriteSession rewriteSession);
public abstract string Description(IInspectionResult result);

public abstract bool CanFixInProcedure { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Rubberduck.Inspections.CodePathAnalysis.Extensions;
using System.Linq;
using Rubberduck.Inspections.Results;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Grammar;

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

protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
{
var variables = State.DeclarationFinder.UserDeclarations(DeclarationType.Variable);
var variables = State.DeclarationFinder
.UserDeclarations(DeclarationType.Variable)
.Where(d => !d.IsArray);

var nodes = new List<IdentifierReference>();
foreach (var variable in variables)
Expand Down
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
var interfaceImplementationMembers = State.DeclarationFinder.FindAllInterfaceImplementingMembers();
var functions = State.DeclarationFinder
.UserDeclarations(DeclarationType.Function)
.Where(item => !IsIgnoringInspectionResultFor(item, AnnotationName))
.Where(item => !IsIgnoringInspectionResultFor(item, AnnotationName) &&
item.References.Any(r => !IsReturnStatement(item, r) && !r.IsAssignment))
.ToList();
var interfaceMemberIssues = GetInterfaceMemberIssues(interfaceMembers);
var nonInterfaceFunctions = functions.Except(interfaceMembers.Union(interfaceImplementationMembers));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Rubberduck.Inspections.Abstract;
using Rubberduck.Inspections.Results;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Resources.Inspections;
using Rubberduck.Parsing.Symbols;
Expand All @@ -30,7 +31,7 @@ public UnassignedVariableUsageInspection(RubberduckParserState state)
protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
{
var declarations = State.DeclarationFinder.UserDeclarations(DeclarationType.Variable)
.Where(declaration =>
.Where(declaration => !declaration.IsArray &&
State.DeclarationFinder.MatchName(declaration.AsTypeName)
.All(d => d.DeclarationType != DeclarationType.UserDefinedType)
&& !declaration.IsSelfAssigned
Expand All @@ -43,12 +44,22 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
.SelectMany(d => d.References)
.Distinct()
.Where(r => !r.IsIgnoringInspectionResultFor(AnnotationName))
.Where(r => !r.Context.TryGetAncestor<VBAParser.RedimStmtContext>(out _) && !IsArraySubscriptAssignment(r))
.Select(r => new IdentifierReferenceInspectionResult(this,
string.Format(InspectionResults.UnassignedVariableUsageInspection, r.IdentifierName),
State,
r)).ToList();
}

private static bool IsArraySubscriptAssignment(IdentifierReference reference)
{
var isLetAssignment = reference.Context.TryGetAncestor<VBAParser.LetStmtContext>(out var letStmt);
var isSetAssignment = reference.Context.TryGetAncestor<VBAParser.SetStmtContext>(out var setStmt);

return isLetAssignment && letStmt.lExpression() is VBAParser.IndexExprContext ||
isSetAssignment && setStmt.lExpression() is VBAParser.IndexExprContext;
}

private static bool DeclarationReferencesContainsReference(Declaration parentDeclaration, Declaration target)
{
foreach (var targetReference in target.References)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public static class VariableRequiresSetAssignmentEvaluator
/// Determines whether the 'Set' keyword is required (whether it's present or not) for the specified identifier reference.
/// </summary>
/// <param name="reference">The identifier reference to analyze</param>
/// <param name="state">The parser state</param>
public static bool RequiresSetAssignment(IdentifierReference reference, RubberduckParserState state)
/// <param name="declarationFinderProvider">The parser state</param>
public static bool RequiresSetAssignment(IdentifierReference reference, IDeclarationFinderProvider declarationFinderProvider)
{
if (!reference.IsAssignment)
{
Expand Down Expand Up @@ -89,7 +89,7 @@ public static bool RequiresSetAssignment(IdentifierReference reference, Rubberdu

// todo resolve expression return type

var memberRefs = state.DeclarationFinder.IdentifierReferences(reference.ParentScoping.QualifiedName);
var memberRefs = declarationFinderProvider.DeclarationFinder.IdentifierReferences(reference.ParentScoping.QualifiedName);
var lastRef = memberRefs.LastOrDefault(r => !Equals(r, reference) && r.Context.GetAncestor<VBAParser.LetStmtContext>() == letStmtContext);
if (lastRef?.Declaration.AsTypeDeclaration?.DeclarationType.HasFlag(DeclarationType.ClassModule) ?? false)
{
Expand All @@ -104,7 +104,7 @@ public static bool RequiresSetAssignment(IdentifierReference reference, Rubberdu
// is the reference referring to something else in scope that's a object?
var project = Declaration.GetProjectParent(reference.ParentScoping);
var module = Declaration.GetModuleParent(reference.ParentScoping);
return state.DeclarationFinder.MatchName(expression.GetText().ToLowerInvariant())
return declarationFinderProvider.DeclarationFinder.MatchName(expression.GetText().ToLowerInvariant())
.Any(decl => (decl.DeclarationType.HasFlag(DeclarationType.ClassModule) || Tokens.Object.Equals(decl.AsTypeName))
&& AccessibilityCheck.IsAccessible(project, module, reference.ParentScoping, decl));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@
using Rubberduck.Parsing;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Parsing.Rewriter;
using Rubberduck.Parsing.VBA;

namespace Rubberduck.Inspections.QuickFixes
{
public class AccessSheetUsingCodeNameQuickFix : QuickFixBase
public sealed class AccessSheetUsingCodeNameQuickFix : QuickFixBase
{
private readonly RubberduckParserState _state;
private readonly IDeclarationFinderProvider _declarationFinderProvider;

public AccessSheetUsingCodeNameQuickFix(RubberduckParserState state)
public AccessSheetUsingCodeNameQuickFix(IDeclarationFinderProvider declarationFinderProvider)
: base(typeof(SheetAccessedUsingStringInspection))
{
_state = state;
_declarationFinderProvider = declarationFinderProvider;
}

public override void Fix(IInspectionResult result)
public override void Fix(IInspectionResult result, IRewriteSession rewriteSession)
{
var referenceResult = (IdentifierReferenceInspectionResult)result;

var rewriter = _state.GetRewriter(referenceResult.QualifiedName);
var rewriter = rewriteSession.CheckOutModuleRewriter(referenceResult.QualifiedName);

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

rewriter.Replace(indexExprContext, referenceResult.Properties.CodeName);
rewriter.Replace(indexExprContext, (string)referenceResult.Properties.CodeName);
}
else
{
// Sheet assigned to variable

var sheetVariableName = setStatement.lExpression().GetText();
var sheetDeclaration = _state.DeclarationFinder.MatchName(sheetVariableName)
var sheetDeclaration = _declarationFinderProvider.DeclarationFinder.MatchName(sheetVariableName)
.First(declaration =>
{
var moduleBodyElement = declaration.Context.GetAncestor<VBAParser.ModuleBodyElementContext>();
Expand Down Expand Up @@ -71,7 +72,7 @@ public override void Fix(IInspectionResult result)

foreach (var reference in sheetDeclaration.References)
{
rewriter.Replace(reference.Context, referenceResult.Properties.CodeName);
rewriter.Replace(reference.Context, (string)referenceResult.Properties.CodeName);
}

rewriter.Remove(setStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Rubberduck.Inspections.Abstract;
using Rubberduck.Inspections.Concrete;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Parsing.Rewriter;
using Rubberduck.Settings;
using Rubberduck.SettingsProvider;

Expand All @@ -17,7 +18,8 @@ public AddIdentifierToWhiteListQuickFix(IPersistanceService<CodeInspectionSettin
_settings = settings;
}

public override void Fix(IInspectionResult result)
//The rewriteSession is optional since it is not used in this particular quickfix.
public override void Fix(IInspectionResult result, IRewriteSession rewriteSession = null)
{
var inspectionSettings = _settings.Load(new CodeInspectionSettings()) ?? new CodeInspectionSettings();
var whitelist = inspectionSettings.WhitelistedIdentifiers;
Expand Down
25 changes: 10 additions & 15 deletions Rubberduck.CodeAnalysis/QuickFixes/AddStepOneQuickFix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@
using Rubberduck.Inspections.Concrete;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Parsing.Rewriter;
using Rubberduck.Parsing.VBA;
using System;
using static Rubberduck.Parsing.Grammar.VBAParser;

namespace Rubberduck.Inspections.QuickFixes
{
public class AddStepOneQuickFix : QuickFixBase
public sealed class AddStepOneQuickFix : QuickFixBase
{
private readonly RubberduckParserState _state;

public AddStepOneQuickFix(RubberduckParserState state)
public AddStepOneQuickFix()
: base(typeof(StepIsNotSpecifiedInspection))
{
_state = state;
}
{}

public override bool CanFixInProcedure => true;

Expand All @@ -29,20 +24,20 @@ public override string Description(IInspectionResult result)
return Resources.Inspections.QuickFixes.AddStepOneQuickFix;
}

public override void Fix(IInspectionResult result)
public override void Fix(IInspectionResult result, IRewriteSession rewriteSession)
{
IModuleRewriter rewriter = _state.GetRewriter(result.QualifiedSelection.QualifiedName);
ForNextStmtContext context = result.Context as ForNextStmtContext;
var rewriter = rewriteSession.CheckOutModuleRewriter(result.QualifiedSelection.QualifiedName);
var context = result.Context as ForNextStmtContext;

int toExpressionEnd = this.GetToExpressionEnd(context);
var toExpressionEnd = GetToExpressionEnd(context);
rewriter.InsertAfter(toExpressionEnd, " Step 1");
}

private int GetToExpressionEnd(ForNextStmtContext context)
private static int GetToExpressionEnd(ForNextStmtContext context)
{
int toNodeIndex = context.TO().Symbol.TokenIndex;
var toNodeIndex = context.TO().Symbol.TokenIndex;

foreach(ExpressionContext expressionChild in context.expression())
foreach(var expressionChild in context.expression())
{
if (expressionChild.Stop.TokenIndex > toNodeIndex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
using Rubberduck.Inspections.Abstract;
using Rubberduck.Inspections.Concrete;
using Rubberduck.Parsing.Inspections.Abstract;
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.Rewriter;

namespace Rubberduck.Inspections.QuickFixes
{
public sealed class ApplicationWorksheetFunctionQuickFix : QuickFixBase
{
private readonly RubberduckParserState _state;

public ApplicationWorksheetFunctionQuickFix(RubberduckParserState state)
public ApplicationWorksheetFunctionQuickFix()
: base(typeof(ApplicationWorksheetFunctionInspection))
{
_state = state;
}
{}

public override void Fix(IInspectionResult result)
public override void Fix(IInspectionResult result, IRewriteSession rewriteSession)
{
var rewriter = _state.GetRewriter(result.QualifiedSelection.QualifiedName);
var rewriter = rewriteSession.CheckOutModuleRewriter(result.QualifiedSelection.QualifiedName);
rewriter.InsertBefore(result.Context.Start.TokenIndex, "WorksheetFunction.");
}

Expand Down
Loading

0 comments on commit 599e852

Please sign in to comment.