Skip to content

Commit afdfd1a

Browse files
authored
Merge branch 'rubberduck-vba:next' into dev-5459-Move_Closer_to_Usage_WithoutEditParsing
2 parents 51b7b7a + f9baa8c commit afdfd1a

File tree

13 files changed

+161
-15
lines changed

13 files changed

+161
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![banner](https://user-images.githubusercontent.com/5751684/113501222-8edfe880-94f1-11eb-99a9-64583e413ef3.png)
1+
<!-- ![banner](https://user-images.githubusercontent.com/5751684/113501222-8edfe880-94f1-11eb-99a9-64583e413ef3.png) -->
22

33
[**Installing**](https://github.com/rubberduck-vba/Rubberduck/wiki/Installing)[Contributing](https://github.com/rubberduck-vba/Rubberduck/blob/next/CONTRIBUTING.md)[Attributions](https://github.com/rubberduck-vba/Rubberduck/blob/next/docs/Attributions.md)[Blog](https://rubberduckvba.blog)[Wiki](https://github.com/rubberduck-vba/Rubberduck/wiki)[rubberduckvba.com](https://rubberduckvba.com)
44

Rubberduck.CodeAnalysis/Inspections/Concrete/EmptyModuleInspection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public override bool VisitModuleDeclarations(VBAParser.ModuleDeclarationsContext
8484
public override bool VisitModuleDeclarationsElement(VBAParser.ModuleDeclarationsElementContext context)
8585
{
8686
return context.moduleVariableStmt() == null
87-
&& context.constStmt() == null
87+
&& context.moduleConstStmt() == null
8888
&& context.enumerationStmt() == null
8989
&& context.udtDeclaration() == null
9090
&& context.eventStmt() == null

Rubberduck.CodeAnalysis/Inspections/Concrete/ProcedureCanBeWrittenAsFunctionInspection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ namespace Rubberduck.CodeAnalysis.Inspections.Concrete
1313
/// Warns about 'Sub' procedures that could be refactored into a 'Function'.
1414
/// </summary>
1515
/// <why>
16-
/// Idiomatic VB code uses 'Function' procedures to return a single value. If the procedure isn't side-effecting, consider writing is as a
17-
/// 'Function' rather than a 'Sub' the returns a result through a 'ByRef' parameter.
16+
/// Idiomatic VB code uses 'Function' procedures to return a single value. If the procedure isn't side-effecting, consider writing it as a
17+
/// 'Function' rather than a 'Sub' that returns a result through a 'ByRef' parameter.
1818
/// </why>
1919
/// <example hasResult="true">
2020
/// <module name="MyModule" type="Standard Module">

Rubberduck.Parsing/Grammar/VBAParser.g4

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ moduleDeclarationsElement :
102102
| defDirective
103103
| enumerationStmt
104104
| eventStmt
105-
| constStmt
105+
| moduleConstStmt
106106
| implementsStmt
107107
| moduleVariableStmt
108108
| moduleOption
@@ -114,6 +114,11 @@ moduleVariableStmt :
114114
(endOfLine attributeStmt)*
115115
;
116116

117+
moduleConstStmt :
118+
constStmt
119+
(endOfLine attributeStmt)*
120+
;
121+
117122
moduleBody :
118123
whiteSpace?
119124
((moduleBodyElement | attributeStmt) endOfStatement)*;

Rubberduck.Parsing/Rewriter/RewriterInfo/ConstantRewriterInfoFinder.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private static RewriterInfo GetRewriterInfo(VBAParser.ConstSubStmtContext target
2424
var itemIndex = items.ToList().IndexOf(target);
2525
var count = items.Length;
2626

27-
var element = context.Parent as VBAParser.ModuleDeclarationsElementContext;
27+
var element = context.Parent?.Parent as VBAParser.ModuleDeclarationsElementContext;
2828
if (element != null)
2929
{
3030
return GetModuleConstantRemovalInfo(target, element, count, itemIndex, items);
@@ -34,8 +34,11 @@ private static RewriterInfo GetRewriterInfo(VBAParser.ConstSubStmtContext target
3434
}
3535

3636
private static RewriterInfo GetModuleConstantRemovalInfo(
37-
VBAParser.ConstSubStmtContext target, VBAParser.ModuleDeclarationsElementContext element,
38-
int count, int itemIndex, IReadOnlyList<VBAParser.ConstSubStmtContext> items)
37+
VBAParser.ConstSubStmtContext target,
38+
VBAParser.ModuleDeclarationsElementContext element,
39+
int count,
40+
int itemIndex,
41+
IReadOnlyList<VBAParser.ConstSubStmtContext> items)
3942
{
4043
if (count == 1)
4144
{

Rubberduck.Parsing/Symbols/Identifier.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public static string GetName(VBAParser.VariableSubStmtContext context)
4747
return GetName(nameContext);
4848
}
4949

50+
public static string GetName(VBAParser.ConstSubStmtContext context)
51+
{
52+
var nameContext = context.identifier();
53+
return GetName(nameContext);
54+
}
55+
5056
public static string GetName(VBAParser.ConstSubStmtContext context, out Interval tokenInterval)
5157
{
5258
var nameContext = context.identifier();

Rubberduck.Parsing/Symbols/ValuedDeclaration.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ public ValuedDeclaration(
2323
string value,
2424
ParserRuleContext context,
2525
Selection selection,
26-
bool isUserDefined = true)
26+
bool isUserDefined = true,
27+
ParserRuleContext attributesPassContext = null,
28+
Attributes attributes = null
29+
)
2730
:base(
2831
qualifiedName,
2932
parentDeclaration,
@@ -35,12 +38,13 @@ public ValuedDeclaration(
3538
accessibility,
3639
declarationType,
3740
context,
38-
null,
41+
attributesPassContext,
3942
selection,
4043
false,
4144
asTypeContext,
4245
isUserDefined,
43-
annotations)
46+
annotations,
47+
attributes: attributes)
4448
{
4549
Expression = value;
4650
}

Rubberduck.Parsing/VBA/DeclarationResolving/DeclarationSymbolsListener.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,10 @@ public override void EnterConstSubStmt(VBAParser.ConstSubStmtContext context)
721721
var value = context.expression().GetText();
722722
var constStmt = (VBAParser.ConstStmtContext)context.Parent;
723723

724+
var key = (name, DeclarationType.Constant);
725+
_attributes.TryGetValue(key, out var attributes);
726+
_membersAllowingAttributes.TryGetValue(key, out var attributesPassContext);
727+
724728
var declaration = new ValuedDeclaration(
725729
new QualifiedMemberName(_qualifiedModuleName, name),
726730
_parentDeclaration,
@@ -733,7 +737,9 @@ public override void EnterConstSubStmt(VBAParser.ConstSubStmtContext context)
733737
DeclarationType.Constant,
734738
value,
735739
context,
736-
identifier.GetSelection());
740+
identifier.GetSelection(),
741+
attributesPassContext: attributesPassContext,
742+
attributes: attributes);
737743

738744
AddDeclaration(declaration);
739745
}

Rubberduck.Parsing/VBA/Parsing/AttributeListener.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public override void EnterModuleVariableStmt(VBAParser.ModuleVariableStmtContext
4242
}
4343
}
4444

45+
public override void EnterModuleConstStmt(VBAParser.ModuleConstStmtContext context)
46+
{
47+
var constantDeclarationStatementList = context.constStmt().constSubStmt();
48+
foreach (var constContext in constantDeclarationStatementList)
49+
{
50+
var constantName = Identifier.GetName(constContext);
51+
_membersAllowingAttributes[(constantName, DeclarationType.Constant)] = context;
52+
}
53+
}
54+
4555
public override void EnterDeclareStmt(VBAParser.DeclareStmtContext context)
4656
{
4757
var name = Identifier.GetName(context.identifier());

RubberduckTests/Inspections/IllegalAnnotationsInspectionTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,25 @@ Option Private Module
477477
'@Obsolete
478478
Public foo As Long
479479
480+
Public Sub Test2()
481+
End Sub
482+
";
483+
484+
var inspectionResults = InspectionResultsForStandardModule(inputCode);
485+
Assert.IsFalse(inspectionResults.Any());
486+
}
487+
488+
[Test]
489+
[Category("Inspections")]
490+
public void VariableAnnotationOnConstant_NoResult()
491+
{
492+
const string inputCode = @"
493+
Option Explicit
494+
Option Private Module
495+
496+
'@VariableDescription ""Test""
497+
Private Const Test As String = ""TestTestTest""
498+
480499
Public Sub Test2()
481500
End Sub
482501
";

0 commit comments

Comments
 (0)