Skip to content
Permalink
Browse files

Stop including enclosing brackets into the name of enum elements

The VBE ignores the outermost enclosing brackets both for references and declarations.
  • Loading branch information
MDoerner committed Oct 17, 2019
1 parent 175de28 commit 1b58fc6c479ef982ed94268fe7f65ebcea94ee65
@@ -829,7 +829,7 @@ public override void ExitEnumerationStmt(VBAParser.EnumerationStmtContext contex
public override void EnterEnumerationStmt_Constant(VBAParser.EnumerationStmt_ConstantContext context)
{
AddDeclaration(CreateDeclaration(
context.identifier().GetText(),
WithBracketsRemoved(context.identifier().GetText()),
"Long",
Accessibility.Implicit,
DeclarationType.EnumerationMember,
@@ -840,6 +840,16 @@ public override void EnterEnumerationStmt_Constant(VBAParser.EnumerationStmt_Con
null));
}

private static string WithBracketsRemoved(string enumElementName)
{
if (enumElementName.StartsWith("[") && enumElementName.EndsWith("]"))
{
return enumElementName.Substring(1, enumElementName.Length - 2);
}

return enumElementName;
}

public override void EnterOptionPrivateModuleStmt(VBAParser.OptionPrivateModuleStmtContext context)
{
((ProceduralModuleDeclaration)_moduleDeclaration).IsPrivateModule = true;
@@ -6857,5 +6857,172 @@ End Function
Assert.AreEqual(2, debugPrintReferences.Count());
}
}

[Category("Grammar")]
[Category("Resolver")]
[Test]
public void HiddenEnumVariableHasCorrectName()
{
var moduleCode = $@"
Private Enum SomeEnum
[_hiddenElement]
End Enum
Private Function Test() As Variant
Debug.Print SomeEnum.[_hiddenElement]
End Function
";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(moduleCode, out _);

using (var state = Resolve(vbe.Object))
{
var enumMember = state.DeclarationFinder.UserDeclarations(DeclarationType.EnumerationMember).Single();
var enumMemberName = enumMember.IdentifierName;

Assert.AreEqual("_hiddenElement", enumMemberName);
}
}

[Category("Grammar")]
[Category("Resolver")]
[Test]
public void HiddenEnumVariableHasReference()
{
var moduleCode = $@"
Private Enum SomeEnum
[_hiddenElement]
End Enum
Private Function Test() As Variant
Debug.Print SomeEnum.[_hiddenElement]
End Function
";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(moduleCode, out _);

using (var state = Resolve(vbe.Object))
{
var enumMember = state.DeclarationFinder.UserDeclarations(DeclarationType.EnumerationMember).Single();
var enumMemberReferences = enumMember.References;

Assert.AreEqual(1, enumMemberReferences.Count());
}
}

[Category("Grammar")]
[Category("Resolver")]
[Test]
[TestCase("nonHiddenElement")]
[TestCase("[nonHiddenElement]")]
public void NonHiddenBracketedEnumVariableHasCorrectName(string enumElementName)
{
var moduleCode = $@"
Private Enum SomeEnum
[{enumElementName}]
End Enum
";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(moduleCode, out _);

using (var state = Resolve(vbe.Object))
{
var enumMember = state.DeclarationFinder.UserDeclarations(DeclarationType.EnumerationMember).Single();
var enumMemberName = enumMember.IdentifierName;

Assert.AreEqual(enumElementName, enumMemberName);
}
}

[Category("Grammar")]
[Category("Resolver")]
[Test]
[TestCase("nonHiddenElement", "nonHiddenElement", 1)]
[TestCase("[nonHiddenElement]", "[nonHiddenElement]", 0)]
[TestCase("[nonHiddenElement]", "[[nonHiddenElement]]", 1)]
public void NonHiddenBracketedEnumVariableHasReference(string enumElementName, string referenceText, int expectedNumberOfReferences)
{
var moduleCode = $@"
Private Enum SomeEnum
[{enumElementName}]
End Enum
Private Function Test() As Variant
Debug.Print SomeEnum.{referenceText}
End Function
";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(moduleCode, out _);

using (var state = Resolve(vbe.Object))
{
var enumMember = state.DeclarationFinder.UserDeclarations(DeclarationType.EnumerationMember).Single();
var enumMemberReferences = enumMember.References;

Assert.AreEqual(expectedNumberOfReferences, enumMemberReferences.Count());
}
}

[Category("Grammar")]
[Category("Resolver")]
[Test]
public void BracketedEnumElementsCorrectElementReferenced()
{
var moduleCode = $@"
Private Enum SomeEnum
enumElement
[[enumElement]]
End Enum
Private Function Test() As Variant
Debug.Print SomeEnum.[enumElement]
End Function
";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(moduleCode, out _);

using (var state = Resolve(vbe.Object))
{
var module = state.DeclarationFinder
.AllModules.Single(qmn => qmn.ComponentType == ComponentType.StandardModule);
var enumMemberReference = state.DeclarationFinder
.IdentifierReferences(module)
.Single(reference => reference.Declaration.DeclarationType == DeclarationType.EnumerationMember);

var referencedDeclarationName = enumMemberReference.Declaration.IdentifierName;

Assert.AreEqual("enumElement", referencedDeclarationName);
}
}

[Category("Grammar")]
[Category("Resolver")]
[Test]
public void BracketedEnumElementsCorrectElementReferencedIdentifierName()
{
var moduleCode = $@"
Private Enum SomeEnum
enumElement
[[enumElement]]
End Enum
Private Function Test() As Variant
Debug.Print SomeEnum.[enumElement]
End Function
";

var vbe = MockVbeBuilder.BuildFromSingleStandardModule(moduleCode, out _);

using (var state = Resolve(vbe.Object))
{
var module = state.DeclarationFinder
.AllModules.Single(qmn => qmn.ComponentType == ComponentType.StandardModule);
var enumMemberReference = state.DeclarationFinder
.IdentifierReferences(module)
.Single(reference => reference.Declaration.DeclarationType == DeclarationType.EnumerationMember);

Assert.AreEqual("enumElement", enumMemberReference.IdentifierName);
}
}
}
}

0 comments on commit 1b58fc6

Please sign in to comment.
You can’t perform that action at this time.