Skip to content

Commit f9b81cb

Browse files
committed
Changed AttributesUpdateer methods from assert to logged guard clause
Also adds more guard clauses, with logging if it can only happen due to a implementation error.
1 parent a300d15 commit f9b81cb

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

Rubberduck.Parsing/VBA/AttributesUpdater.cs

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.Linq;
5-
using System.Text;
64
using Antlr4.Runtime;
75
using Antlr4.Runtime.Misc;
6+
using NLog;
87
using Rubberduck.Parsing.Grammar;
98
using Rubberduck.Parsing.Rewriter;
109
using Rubberduck.Parsing.Symbols;
@@ -15,6 +14,8 @@ namespace Rubberduck.Parsing.VBA
1514
public class AttributesUpdater : IAttributesUpdater
1615
{
1716
private readonly IParseTreeProvider _parseTreeProvider;
17+
private readonly Logger _logger = LogManager.GetCurrentClassLogger();
18+
1819

1920
public AttributesUpdater(IParseTreeProvider parseTreeProvider)
2021
{
@@ -24,14 +25,31 @@ public AttributesUpdater(IParseTreeProvider parseTreeProvider)
2425

2526
public void AddAttribute(IRewriteSession rewriteSession, Declaration declaration, string attribute, IReadOnlyList<string> values)
2627
{
27-
Debug.Assert(rewriteSession.TargetCodeKind == CodeKind.AttributesCode);
28+
if (string.IsNullOrEmpty(attribute))
29+
{
30+
return;
31+
}
2832

2933
//Attributes must have at least one value.
3034
if (values == null || !values.Any())
3135
{
3236
return;
3337
}
3438

39+
if (declaration == null)
40+
{
41+
_logger.Warn("Tried to add an attribute to a declaration that is null.");
42+
_logger.Trace($"Tried to add attribute {attribute} with values {AttributeValuesText(values)} to a declaration that is null.");
43+
return;
44+
}
45+
46+
if (rewriteSession.TargetCodeKind != CodeKind.AttributesCode)
47+
{
48+
_logger.Warn($"Tried to add an attribute with a rewriter not suitable for attributes. (target code kind = {rewriteSession.TargetCodeKind})");
49+
_logger.Trace($"Tried to add attribute {attribute} with values {AttributeValuesText(values)} to {declaration.QualifiedModuleName} using a rewriter not suitable for attributes.");
50+
return;
51+
}
52+
3553
//VB_Ext_Key is special in that this attribute can be declared multiple times, but only once for each key.
3654
if (attribute.ToUpperInvariant().EndsWith("VB_EXT_KEY"))
3755
{
@@ -74,7 +92,7 @@ public void AddAttribute(IRewriteSession rewriteSession, Declaration declaration
7492
}
7593
else
7694
{
77-
ParserRuleContext attributesContext = declaration.AttributesPassContext;
95+
var attributesContext = declaration.AttributesPassContext;
7896
var firstEndOfLineInMember = attributesContext.GetDescendent<VBAParser.EndOfLineContext>();
7997
if (firstEndOfLineInMember == null)
8098
{
@@ -96,7 +114,24 @@ private static string AttributeValuesText(IEnumerable<string> attributeValues)
96114

97115
public void RemoveAttribute(IRewriteSession rewriteSession, Declaration declaration, string attribute, IReadOnlyList<string> values = null)
98116
{
99-
Debug.Assert(rewriteSession.TargetCodeKind == CodeKind.AttributesCode);
117+
if (string.IsNullOrEmpty(attribute))
118+
{
119+
return;
120+
}
121+
122+
if (declaration == null)
123+
{
124+
_logger.Warn("Tried to remove an attribute from a declaration that is null.");
125+
_logger.Trace($"Tried to remove attribute {attribute} {(values != null ? $"with values {AttributeValuesText(values)} " : string.Empty)}from a declaration that is null.");
126+
return;
127+
}
128+
129+
if (rewriteSession.TargetCodeKind != CodeKind.AttributesCode)
130+
{
131+
_logger.Warn($"Tried to remove an attribute with a rewriter not suitable for attributes. (target code kind = {rewriteSession.TargetCodeKind})");
132+
_logger.Trace($"Tried to remove attribute {attribute} {(values != null ? $"with values {AttributeValuesText(values)} " : string.Empty)}from {declaration.QualifiedModuleName} using a rewriter not suitable for attributes.");
133+
return;
134+
}
100135

101136
var attributeNodes = ApplicableAttributeNodes(declaration, attribute, values);
102137

@@ -140,7 +175,30 @@ private static IList<AttributeNode> ApplicableAttributeNodes(Declaration declara
140175

141176
public void UpdateAttribute(IRewriteSession rewriteSession, Declaration declaration, string attribute, IReadOnlyList<string> newValues, IReadOnlyList<string> oldValues = null)
142177
{
143-
Debug.Assert(rewriteSession.TargetCodeKind == CodeKind.AttributesCode);
178+
if (string.IsNullOrEmpty(attribute))
179+
{
180+
return;
181+
}
182+
183+
//Attributes must have at least one value.
184+
if (newValues == null || !newValues.Any())
185+
{
186+
return;
187+
}
188+
189+
if (declaration == null)
190+
{
191+
_logger.Warn("Tried to updtae an attribute on a declaration that is null.");
192+
_logger.Trace($"Tried to update values for attribute {attribute} {(oldValues != null ? $"with values {AttributeValuesText(oldValues)} " : string.Empty)}on a declaration that is null.");
193+
return;
194+
}
195+
196+
if (rewriteSession.TargetCodeKind != CodeKind.AttributesCode)
197+
{
198+
_logger.Warn($"Tried to update an attribute with a rewriter not suitable for attributes. (target code kind = {rewriteSession.TargetCodeKind})");
199+
_logger.Trace($"Tried to update values for attribute {attribute} {(oldValues != null ? $"with values {AttributeValuesText(oldValues)} " : string.Empty)}on {declaration.QualifiedModuleName} to {AttributeValuesText(oldValues)} using a rewriter not suitable for attributes.");
200+
return;
201+
}
144202

145203
var attributeNodes = ApplicableAttributeNodes(declaration, attribute, oldValues);
146204

0 commit comments

Comments
 (0)