Skip to content

Commit cd9d575

Browse files
authored
Merge branch 'next' into Issue2056
2 parents 596f1f0 + c2d0696 commit cd9d575

File tree

6 files changed

+164
-2
lines changed

6 files changed

+164
-2
lines changed

RetailCoder.VBE/Inspections/InspectionsUI.Designer.cs

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RetailCoder.VBE/Inspections/InspectionsUI.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,7 @@ If the parameter can be null, ignore this inspection result; passing a null valu
565565
<data name="MalformedAnnotationInspectionResultFormat" xml:space="preserve">
566566
<value>Malformed '{0}' annotation.</value>
567567
</data>
568-
</root>
568+
<data name="WriteOnlyPropertyQuickFix" xml:space="preserve">
569+
<value>Add property get</value>
570+
</data>
571+
</root>

RetailCoder.VBE/Inspections/WriteOnlyPropertyInspection.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public override IEnumerable<CodeInspectionQuickFix> QuickFixes
5454
{
5555
return new CodeInspectionQuickFix[]
5656
{
57+
new WriteOnlyPropertyQuickFix(Context, Target),
5758
new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName)
5859
};
5960
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Linq;
3+
using Antlr4.Runtime;
4+
using Rubberduck.Parsing.Grammar;
5+
using Rubberduck.Parsing.Symbols;
6+
7+
namespace Rubberduck.Inspections
8+
{
9+
public class WriteOnlyPropertyQuickFix : CodeInspectionQuickFix
10+
{
11+
private readonly Declaration _target;
12+
13+
public WriteOnlyPropertyQuickFix(ParserRuleContext context, Declaration target)
14+
: base(context, target.QualifiedSelection, InspectionsUI.WriteOnlyPropertyQuickFix)
15+
{
16+
_target = target;
17+
}
18+
19+
public override void Fix()
20+
{
21+
var parameters = ((IDeclarationWithParameter) _target).Parameters.Cast<ParameterDeclaration>().ToList();
22+
23+
var signatureParams = parameters.Except(new[] {parameters.Last()}).Select(GetParamText);
24+
var propertyGet = "Public Property Get " + _target.IdentifierName + "(" + string.Join(", ", signatureParams) +
25+
") As " + parameters.Last().AsTypeName + Environment.NewLine + "End Property";
26+
27+
var module = _target.QualifiedName.QualifiedModuleName.Component.CodeModule;
28+
module.InsertLines(_target.Selection.StartLine, propertyGet);
29+
}
30+
31+
private string GetParamText(ParameterDeclaration param)
32+
{
33+
return (((VBAParser.ArgContext)param.Context).BYVAL() == null ? "ByRef " : "ByVal ") + param.IdentifierName + " As " + param.AsTypeName;
34+
}
35+
}
36+
}

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@
369369
<Compile Include="Inspections\MalformedAnnotationInspection.cs" />
370370
<Compile Include="Inspections\ObjectVariableNotSetInspection.cs" />
371371
<Compile Include="Inspections\RemoveExplicitCallStatmentQuickFix.cs" />
372+
<Compile Include="Inspections\WriteOnlyPropertyQuickFix.cs" />
372373
<Compile Include="Navigation\CodeExplorer\ICodeExplorerDeclarationViewModel.cs" />
373374
<Compile Include="Navigation\Folders\FolderHelper.cs" />
374375
<Compile Include="Refactorings\ExtractMethod\ExtractedMethod.cs" />

RubberduckTests/Inspections/WriteOnlyPropertyInspectionTests.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,117 @@ Property Let Foo(value)
193193
Assert.IsFalse(inspectionResults.Any());
194194
}
195195

196+
[TestMethod]
197+
[TestCategory("Inspections")]
198+
public void WriteOnlyProperty_AddPropertyGetQuickFixWorks_ImplicitTypesAndAccessibility()
199+
{
200+
const string inputCode =
201+
@"Property Let Foo(value)
202+
End Property";
203+
204+
const string expectedCode =
205+
@"Public Property Get Foo() As Variant
206+
End Property
207+
Property Let Foo(value)
208+
End Property";
209+
210+
//Arrange
211+
var builder = new MockVbeBuilder();
212+
var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none)
213+
.AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
214+
.Build();
215+
var module = project.Object.VBComponents.Item(0).CodeModule;
216+
var vbe = builder.AddProject(project).Build();
217+
218+
var mockHost = new Mock<IHostApplication>();
219+
mockHost.SetupAllProperties();
220+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock<ISinks>().Object));
221+
222+
parser.Parse(new CancellationTokenSource());
223+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
224+
225+
var inspection = new WriteOnlyPropertyInspection(parser.State);
226+
var inspectionResults = inspection.GetInspectionResults();
227+
228+
inspectionResults.First().QuickFixes.Single(s => s is WriteOnlyPropertyQuickFix).Fix();
229+
230+
Assert.AreEqual(expectedCode, module.Lines());
231+
}
232+
233+
[TestMethod]
234+
[TestCategory("Inspections")]
235+
public void WriteOnlyProperty_AddPropertyGetQuickFixWorks_ExlicitTypesAndAccessibility()
236+
{
237+
const string inputCode =
238+
@"Public Property Let Foo(ByVal value As Integer)
239+
End Property";
240+
241+
const string expectedCode =
242+
@"Public Property Get Foo() As Integer
243+
End Property
244+
Public Property Let Foo(ByVal value As Integer)
245+
End Property";
246+
247+
//Arrange
248+
var builder = new MockVbeBuilder();
249+
var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none)
250+
.AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
251+
.Build();
252+
var module = project.Object.VBComponents.Item(0).CodeModule;
253+
var vbe = builder.AddProject(project).Build();
254+
255+
var mockHost = new Mock<IHostApplication>();
256+
mockHost.SetupAllProperties();
257+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock<ISinks>().Object));
258+
259+
parser.Parse(new CancellationTokenSource());
260+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
261+
262+
var inspection = new WriteOnlyPropertyInspection(parser.State);
263+
var inspectionResults = inspection.GetInspectionResults();
264+
265+
inspectionResults.First().QuickFixes.Single(s => s is WriteOnlyPropertyQuickFix).Fix();
266+
267+
Assert.AreEqual(expectedCode, module.Lines());
268+
}
269+
270+
[TestMethod]
271+
[TestCategory("Inspections")]
272+
public void WriteOnlyProperty_AddPropertyGetQuickFixWorks_MultipleParams()
273+
{
274+
const string inputCode =
275+
@"Public Property Let Foo(value1, ByVal value2 As Integer, ByRef value3 As Long, value4 As Date, ByVal value5, value6 As String)
276+
End Property";
277+
278+
const string expectedCode =
279+
@"Public Property Get Foo(ByRef value1 As Variant, ByVal value2 As Integer, ByRef value3 As Long, ByRef value4 As Date, ByVal value5 As Variant) As String
280+
End Property
281+
Public Property Let Foo(value1, ByVal value2 As Integer, ByRef value3 As Long, value4 As Date, ByVal value5, value6 As String)
282+
End Property";
283+
284+
//Arrange
285+
var builder = new MockVbeBuilder();
286+
var project = builder.ProjectBuilder("VBAProject", vbext_ProjectProtection.vbext_pp_none)
287+
.AddComponent("MyClass", vbext_ComponentType.vbext_ct_ClassModule, inputCode)
288+
.Build();
289+
var module = project.Object.VBComponents.Item(0).CodeModule;
290+
var vbe = builder.AddProject(project).Build();
291+
292+
var mockHost = new Mock<IHostApplication>();
293+
mockHost.SetupAllProperties();
294+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock<ISinks>().Object));
295+
296+
parser.Parse(new CancellationTokenSource());
297+
if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); }
298+
299+
var inspection = new WriteOnlyPropertyInspection(parser.State);
300+
var inspectionResults = inspection.GetInspectionResults();
301+
302+
inspectionResults.First().QuickFixes.Single(s => s is WriteOnlyPropertyQuickFix).Fix();
303+
304+
Assert.AreEqual(expectedCode, module.Lines());
305+
}
306+
196307
[TestMethod]
197308
[TestCategory("Inspections")]
198309
public void WriteOnlyProperty_IgnoreQuickFixWorks()

0 commit comments

Comments
 (0)