Add support for C# 8 using variable declarations and add more C# 8 coverage.

- When adding additional C# 8 tests found that we didn't fully support this.
- Updated the C# 8.0 test to fully encompass everything C# 8.0.
- Added a feature flag to control using variable declaration errors when not top level.
- Added using variable declaration specific tests.

aspnet/AspNetCoredotnet/aspnetcore-tooling#5092
\n\nCommit migrated from ac08ad3659
This commit is contained in:
N. Taylor Mullen 2019-03-25 23:43:53 -07:00
parent 6eb9044660
commit 784596aba6
2 changed files with 20 additions and 3 deletions

View File

@ -2001,9 +2001,17 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
// using Identifier ==> Using Declaration // using Identifier ==> Using Declaration
if (!topLevel) if (!topLevel)
{ {
Context.ErrorSink.OnError( // using Variable Declaration
RazorDiagnosticFactory.CreateParsing_NamespaceImportAndTypeAliasCannotExistWithinCodeBlock(
new SourceSpan(block.Start, block.Name.Length))); if (!Context.FeatureFlags.AllowUsingVariableDeclarations)
{
Context.ErrorSink.OnError(
RazorDiagnosticFactory.CreateParsing_NamespaceImportAndTypeAliasCannotExistWithinCodeBlock(
new SourceSpan(block.Start, block.Name.Length)));
}
// There are cases when a user will do @using var x = 123; At which point we let C# notify the user
// of their error like we do any other invalid expression.
if (transition != null) if (transition != null)
{ {
builder.Add(transition); builder.Add(transition);

View File

@ -11,6 +11,7 @@ namespace Microsoft.AspNetCore.Razor.Language
var allowHtmlCommentsInTagHelpers = false; var allowHtmlCommentsInTagHelpers = false;
var allowComponentFileKind = false; var allowComponentFileKind = false;
var allowRazorInAllCodeBlocks = false; var allowRazorInAllCodeBlocks = false;
var allowUsingVariableDeclarations = false;
var experimental_AllowConditionalDataDashAttributes = false; var experimental_AllowConditionalDataDashAttributes = false;
if (version.CompareTo(RazorLanguageVersion.Version_2_1) >= 0) if (version.CompareTo(RazorLanguageVersion.Version_2_1) >= 0)
@ -25,6 +26,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Added in 3.0 // Added in 3.0
allowComponentFileKind = true; allowComponentFileKind = true;
allowRazorInAllCodeBlocks = true; allowRazorInAllCodeBlocks = true;
allowUsingVariableDeclarations = true;
} }
if (version.CompareTo(RazorLanguageVersion.Experimental) >= 0) if (version.CompareTo(RazorLanguageVersion.Experimental) >= 0)
@ -37,6 +39,7 @@ namespace Microsoft.AspNetCore.Razor.Language
allowHtmlCommentsInTagHelpers, allowHtmlCommentsInTagHelpers,
allowComponentFileKind, allowComponentFileKind,
allowRazorInAllCodeBlocks, allowRazorInAllCodeBlocks,
allowUsingVariableDeclarations,
experimental_AllowConditionalDataDashAttributes); experimental_AllowConditionalDataDashAttributes);
} }
@ -48,6 +51,8 @@ namespace Microsoft.AspNetCore.Razor.Language
public abstract bool AllowRazorInAllCodeBlocks { get; } public abstract bool AllowRazorInAllCodeBlocks { get; }
public abstract bool AllowUsingVariableDeclarations { get; }
public abstract bool EXPERIMENTAL_AllowConditionalDataDashAttributes { get; } public abstract bool EXPERIMENTAL_AllowConditionalDataDashAttributes { get; }
private class DefaultRazorParserFeatureFlags : RazorParserFeatureFlags private class DefaultRazorParserFeatureFlags : RazorParserFeatureFlags
@ -57,12 +62,14 @@ namespace Microsoft.AspNetCore.Razor.Language
bool allowHtmlCommentsInTagHelpers, bool allowHtmlCommentsInTagHelpers,
bool allowComponentFileKind, bool allowComponentFileKind,
bool allowRazorInAllCodeBlocks, bool allowRazorInAllCodeBlocks,
bool allowUsingVariableDeclarations,
bool experimental_AllowConditionalDataDashAttributes) bool experimental_AllowConditionalDataDashAttributes)
{ {
AllowMinimizedBooleanTagHelperAttributes = allowMinimizedBooleanTagHelperAttributes; AllowMinimizedBooleanTagHelperAttributes = allowMinimizedBooleanTagHelperAttributes;
AllowHtmlCommentsInTagHelpers = allowHtmlCommentsInTagHelpers; AllowHtmlCommentsInTagHelpers = allowHtmlCommentsInTagHelpers;
AllowComponentFileKind = allowComponentFileKind; AllowComponentFileKind = allowComponentFileKind;
AllowRazorInAllCodeBlocks = allowRazorInAllCodeBlocks; AllowRazorInAllCodeBlocks = allowRazorInAllCodeBlocks;
AllowUsingVariableDeclarations = allowUsingVariableDeclarations;
EXPERIMENTAL_AllowConditionalDataDashAttributes = experimental_AllowConditionalDataDashAttributes; EXPERIMENTAL_AllowConditionalDataDashAttributes = experimental_AllowConditionalDataDashAttributes;
} }
@ -74,6 +81,8 @@ namespace Microsoft.AspNetCore.Razor.Language
public override bool AllowRazorInAllCodeBlocks { get; } public override bool AllowRazorInAllCodeBlocks { get; }
public override bool AllowUsingVariableDeclarations { get; }
public override bool EXPERIMENTAL_AllowConditionalDataDashAttributes { get; } public override bool EXPERIMENTAL_AllowConditionalDataDashAttributes { get; }
} }
} }