From 364fc1a55d6fa47dca795491189be4c6a354974c Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Wed, 9 Aug 2017 15:17:21 -0700 Subject: [PATCH] Allow nullable types with generic arguments in directives --- .../Legacy/CSharpCodeParser.cs | 4 ++- .../Legacy/CSharpDirectivesTest.cs | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs b/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs index 8cd340785e..1580f0f774 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Legacy/CSharpCodeParser.cs @@ -959,7 +959,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy { if (Optional(CSharpSymbolType.Identifier) || Optional(CSharpSymbolType.Keyword)) { - Optional(CSharpSymbolType.QuestionMark); // Nullable if (Optional(CSharpSymbolType.DoubleColon)) { if (!Optional(CSharpSymbolType.Identifier)) @@ -975,6 +974,9 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy { NamespaceOrTypeName(); } + + Optional(CSharpSymbolType.QuestionMark); // Nullable + while (At(CSharpSymbolType.LeftBracket)) { Balance(BalancingModes.None); diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpDirectivesTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpDirectivesTest.cs index 7d554e4c98..9fb722ddfb 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpDirectivesTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/Legacy/CSharpDirectivesTest.cs @@ -784,6 +784,33 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy Factory.MetaCode(" ; ").Accepts(AcceptedCharactersInternal.WhiteSpace))); } + [Theory] + [InlineData("string?")] + [InlineData("string?[]")] + [InlineData("global::System.Int32?")] + [InlineData("KeyValuePair?")] + [InlineData("KeyValuePair?[]")] + [InlineData("global::System.Collections.Generic.KeyValuePair?[]")] + public void DirectiveDescriptor_AllowsNullableTypes(string expectedType) + { + // Arrange + var descriptor = DirectiveDescriptor.CreateDirective( + "custom", + DirectiveKind.SingleLine, + b => b.AddTypeToken()); + + // Act & Assert + ParseCodeBlockTest( + $"@custom {expectedType}", + new[] { descriptor }, + new DirectiveBlock( + new DirectiveChunkGenerator(descriptor), + Factory.CodeTransition(), + Factory.MetaCode("custom").Accepts(AcceptedCharactersInternal.None), + Factory.Span(SpanKindInternal.Code, " ", markup: false).Accepts(AcceptedCharactersInternal.WhiteSpace), + Factory.Span(SpanKindInternal.Code, expectedType, markup: false).AsDirectiveToken(descriptor.Tokens[0]))); + } + [Fact] public void DirectiveDescriptor_ErrorsExtraContentAfterDirective() {