Skip to content

Commit 6f1bc8f

Browse files
committed
Fix broken tests
1 parent 3e5bc55 commit 6f1bc8f

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

RetailCoder.VBE/Common/DeclarationExtensions.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ public static Selection GetVariableStmtContextSelection(this Declaration target)
4848
statement.Stop.Line, statement.Stop.Column);
4949
}
5050

51+
/// <summary>
52+
/// Returns the Selection of a ConstStmtContext.
53+
/// </summary>
54+
/// <exception cref="ArgumentException">Throws when target's DeclarationType is not Constant.</exception>
55+
/// <param name="target"></param>
56+
/// <returns></returns>
57+
public static Selection GetConstStmtContextSelection(this Declaration target)
58+
{
59+
if (target.DeclarationType != DeclarationType.Constant)
60+
{
61+
throw new ArgumentException("Target DeclarationType is not Constant.", "target");
62+
}
63+
64+
var statement = GetConstStmtContext(target);
65+
66+
return new Selection(statement.Start.Line, statement.Start.Column,
67+
statement.Stop.Line, statement.Stop.Column);
68+
}
69+
5170
/// <summary>
5271
/// Returns a VariableStmtContext.
5372
/// </summary>
@@ -70,6 +89,28 @@ public static VBAParser.VariableStmtContext GetVariableStmtContext(this Declarat
7089
return statement;
7190
}
7291

92+
/// <summary>
93+
/// Returns a ConstStmtContext.
94+
/// </summary>
95+
/// <exception cref="ArgumentException">Throws when target's DeclarationType is not Constant.</exception>
96+
/// <param name="target"></param>
97+
/// <returns></returns>
98+
public static VBAParser.ConstStmtContext GetConstStmtContext(this Declaration target)
99+
{
100+
if (target.DeclarationType != DeclarationType.Constant)
101+
{
102+
throw new ArgumentException("Target DeclarationType is not Constant.", "target");
103+
}
104+
105+
var statement = target.Context.Parent as VBAParser.ConstStmtContext;
106+
if (statement == null)
107+
{
108+
throw new MissingMemberException("Statement not found");
109+
}
110+
111+
return statement;
112+
}
113+
73114
/// <summary>
74115
/// Returns whether a variable declaration statement contains multiple declarations in a single statement.
75116
/// </summary>

RetailCoder.VBE/Inspections/IdentifierNotUsedInspectionResult.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public RemoveUnusedDeclarationQuickFix(ParserRuleContext context, QualifiedSelec
5656

5757
public override void Fix()
5858
{
59-
if (_target.DeclarationType == DeclarationType.Variable)
59+
if (_target.DeclarationType == DeclarationType.Variable || _target.DeclarationType == DeclarationType.Constant)
6060
{
6161
RemoveVariable(_target);
6262
}
@@ -65,9 +65,7 @@ public override void Fix()
6565
var module = Selection.QualifiedName.Component.CodeModule;
6666
var selection = Selection.Selection;
6767

68-
var originalCodeLines = module.Lines[selection.StartLine, selection.LineCount]
69-
.Replace("\r\n", " ")
70-
.Replace("_", string.Empty);
68+
var originalCodeLines = module.Lines[selection.StartLine, selection.LineCount];
7169

7270
var originalInstruction = Context.GetText();
7371
module.DeleteLines(selection.StartLine, selection.LineCount);
@@ -84,14 +82,12 @@ private void RemoveVariable(Declaration target)
8482
{
8583
Selection selection;
8684
var declarationText = target.Context.GetText().Replace(" _" + Environment.NewLine, string.Empty);
87-
var multipleDeclarations = target.HasMultipleDeclarationsInStatement();
88-
89-
var variableStmtContext = target.GetVariableStmtContext();
85+
var multipleDeclarations = target.DeclarationType == DeclarationType.Variable && target.HasMultipleDeclarationsInStatement();
9086

9187
if (!multipleDeclarations)
9288
{
93-
declarationText = variableStmtContext.GetText().Replace(" _" + Environment.NewLine, string.Empty);
94-
selection = target.GetVariableStmtContextSelection();
89+
declarationText = GetStmtContext(target).GetText().Replace(" _" + Environment.NewLine, string.Empty);
90+
selection = GetStmtContextSelection(target);
9591
}
9692
else
9793
{
@@ -107,7 +103,7 @@ private void RemoveVariable(Declaration target)
107103

108104
if (multipleDeclarations)
109105
{
110-
selection = target.GetVariableStmtContextSelection();
106+
selection = GetStmtContextSelection(target);
111107
newLines = RemoveExtraComma(codeModule.GetLines(selection).Replace(oldLines, newLines),
112108
target.CountOfDeclarationsInStatement(), target.IndexOfVariableDeclarationInStatement());
113109
}
@@ -144,6 +140,20 @@ private void RemoveVariable(Declaration target)
144140
}
145141
}
146142

143+
private Selection GetStmtContextSelection(Declaration target)
144+
{
145+
return target.DeclarationType == DeclarationType.Variable
146+
? target.GetVariableStmtContextSelection()
147+
: target.GetConstStmtContextSelection();
148+
}
149+
150+
private ParserRuleContext GetStmtContext(Declaration target)
151+
{
152+
return target.DeclarationType == DeclarationType.Variable
153+
? (ParserRuleContext)target.GetVariableStmtContext()
154+
: (ParserRuleContext)target.GetConstStmtContext();
155+
}
156+
147157
private string RemoveExtraComma(string str, int numParams, int indexRemoved)
148158
{
149159
// Example use cases for this method (fields and variables):

0 commit comments

Comments
 (0)