Design update.

\n\nCommit migrated from c9e0d2a60f
This commit is contained in:
N. Taylor Mullen 2019-03-22 10:42:28 -07:00
parent f33e1fca53
commit 40340e9b91
12 changed files with 97 additions and 38 deletions

View File

@ -56,12 +56,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
target.WriteInjectProperty(context, node);
// Assert
Assert.Equal(
Assert.Equal(Environment.NewLine +
"#nullable restore" + Environment.NewLine +
"#line 2 \"test-path\"" + Environment.NewLine +
"[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]" + Environment.NewLine +
"public PropertyType<ModelType> PropertyName { get; private set; }" + Environment.NewLine + Environment.NewLine +
"#line default" + Environment.NewLine +
"#line hidden" + Environment.NewLine,
"#line hidden" + Environment.NewLine +
"#nullable disable" + Environment.NewLine,
context.CodeWriter.GenerateCode());
}
}

View File

@ -616,6 +616,11 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
if (!_codeGenerationOptions.SuppressNullabilityEnforcement)
{
var endsWithNewline = _writer.Length > 0 && _writer[_writer.Length - 1] == '\n';
if (!endsWithNewline)
{
_writer.WriteLine();
}
_writer.WriteLine("#nullable restore");
}

View File

@ -462,7 +462,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
// the "usings directive is unnecessary" message.
// Looks like:
// __o = typeof(SomeNamespace.SomeComponent);
using (context.CodeWriter.BuildLinePragma(node.Source.Value))
using (context.CodeWriter.BuildLinePragma(node.Source.Value, context))
{
context.CodeWriter.Write(DesignTimeVariable);
context.CodeWriter.Write(" = ");

View File

@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Razor.Language
bool designTime,
string rootNamespace,
bool suppressChecksum,
bool supressMetadataAttributes,
bool suppressMetadataAttributes,
bool suppressPrimaryMethodBody,
bool suppressNullabilityEnforcement)
{
@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Razor.Language
DesignTime = designTime;
RootNamespace = rootNamespace;
SuppressChecksum = suppressChecksum;
SuppressMetadataAttributes = supressMetadataAttributes;
SuppressMetadataAttributes = suppressMetadataAttributes;
SuppressPrimaryMethodBody = suppressPrimaryMethodBody;
SuppressNullabilityEnforcement = suppressNullabilityEnforcement;
}

View File

@ -15,7 +15,7 @@ namespace Microsoft.AspNetCore.Razor.Language
designTime: false,
suppressChecksum: false,
rootNamespace: null,
supressMetadataAttributes: false,
suppressMetadataAttributes: false,
suppressPrimaryMethodBody: false,
suppressNullabilityEnforcement: false);
}
@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Razor.Language
designTime: true,
rootNamespace: null,
suppressChecksum: false,
supressMetadataAttributes: true,
suppressMetadataAttributes: true,
suppressPrimaryMethodBody: false,
suppressNullabilityEnforcement: false);
}
@ -111,7 +111,7 @@ namespace Microsoft.AspNetCore.Razor.Language
public virtual bool SuppressPrimaryMethodBody { get; protected set; }
/// <summary>
/// Gets or sets a value that determines if nullability type enforcement is restored to project settings for user code.
/// Gets a value that determines if nullability type enforcement should be suppressed for user code.
/// </summary>
public virtual bool SuppressNullabilityEnforcement { get; }
}

View File

@ -55,7 +55,7 @@ namespace Microsoft.AspNetCore.Razor.Language
public virtual bool SuppressPrimaryMethodBody { get; set; }
/// <summary>
/// Gets or sets a value that determines if nullability type enforcement is restored to project settings for user code.
/// Gets or sets a value that determines if nullability type enforcement should be suppressed for user code.
/// </summary>
public virtual bool SuppressNullabilityEnforcement { get; set; }

View File

@ -14,7 +14,7 @@ namespace Microsoft.CodeAnalysis.Razor
public static class RazorProjectEngineBuilderExtensions
{
/// <summary>
/// Sets the C# language version to respect when generating code.
/// Sets the C# language version to target when generating code.
/// </summary>
/// <param name="builder">The <see cref="RazorProjectEngineBuilder"/>.</param>
/// <param name="csharpLanguageVersion">The C# <see cref="LanguageVersion"/>.</param>
@ -26,12 +26,6 @@ namespace Microsoft.CodeAnalysis.Razor
throw new ArgumentNullException(nameof(builder));
}
if (builder.Configuration.LanguageVersion.Major < 3)
{
// Prior to 3.0 there were no C# version specific controlled features so there's no value in setting a CSharp language version, noop.
return builder;
}
var existingFeature = builder.Features.OfType<ConfigureParserForCSharpVersionFeature>().FirstOrDefault();
if (existingFeature != null)
{
@ -63,8 +57,16 @@ namespace Microsoft.CodeAnalysis.Razor
throw new ArgumentNullException(nameof(options));
}
if (options.Configuration.LanguageVersion.Major < 3)
{
// Prior to 3.0 there were no C# version specific controlled features. Suppress nullability enforcement.
options.SuppressNullabilityEnforcement = true;
return;
}
if (CSharpLanguageVersion < LanguageVersion.CSharp8)
{
// Having nullable flags < C# 8.0 would cause compile errors.
options.SuppressNullabilityEnforcement = true;
}
else
@ -73,10 +75,11 @@ namespace Microsoft.CodeAnalysis.Razor
// cases in tooling when the project isn't fully configured yet at which point the CSharpLanguageVersion
// may be Default (value 0). In those cases that C# version is equivalently "unspecified" and is up to the consumer
// to act in a safe manner to not cause unneeded errors for older compilers. Therefore if the version isn't
// >= 8.0 (or Latest) then nullability enforcement is suppressed.
// >= 8.0 (Latest has a higher value) then nullability enforcement is suppressed.
//
// Once the project finishes configuration the C# language version will be updated to reflect the effective
// language version for the project.
// language version for the project by our workspace change detectors. That mechanism extracts the correlated
// Roslyn project and acquires the effective C# version at that point.
options.SuppressNullabilityEnforcement = false;
}
}

View File

@ -13,7 +13,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<Project ToolsVersion="14.0">
<!--
What follows in this file is based on:
https://github.com/dotnet/roslyn/blob/4d92b18aee99ba8b1b4770ce65133e9ca65a94fe/src/Compilers/Core/MSBuildTask/Microsoft.CSharp.Core.targets
https://github.com/dotnet/roslyn/blob/db4f836b97db1e7ad261e51c67cc80c6f3c28791/src/Compilers/Core/MSBuildTask/Microsoft.CSharp.Core.targets
We need to keep this basically up to date, as well as track the set of modifications we've made. Try to keep the formatting
similar to the original to reduce noise. In general try to only deviate from the CoreCompile target when we need to for
@ -25,32 +25,45 @@ Copyright (c) .NET Foundation. All rights reserved.
Changes:
Name="RazorCoreCompile"
Replace Name="CoreCompile" with Name="RazorCoreCompile"
Replace @(Compile) with @(RazorCompile)
Replace @(_DebugSymbolsIntermediatePath) with @(_RazorDebugSymbolsIntermediatePath)
Replace @(IntermediateAssembly) with @(RazorIntermediateAssembly)
Replace @(ReferencePathWithRefAssemblies) with @(RazorReferencePath)
Remove @(_CoreCompileResourceInputs) with @(_RazorCoreCompileResourceInputs)
Replace @(_CoreCompileResourceInputs) with @(_RazorCoreCompileResourceInputs)
Replace <Csc Condition="'%(_CoreCompileResourceInputs.WithCulture)' != 'true'" with <Csc
Remove comment above ^
Set TargetType="$(OutputType)" to TargetType="Library" - Razor is always a .dll
Remove Returns="@(CscCommandLineArgs)"
Remove <Import Project="Microsoft.Managed.Core.targets"/>
Remove Returns="@(CscCommandLineArgs)"
Remove @(EmbeddedFiles)
Remove $(ApplicationIcon) $(Win32Resource) $(Win32Manifest)
Remove @(EmbeddedDocumentation) and @(EmbeddedFiles)
Remove @(CustomAdditionalCompileInputs) and @(CustomAdditionalCompileOutputs)
Remove @(DocFileItem)
Remove PdbFile="$(PdbFile)"
Remove $(IntermediateRefAssembly)
Remove OutputRefAssembly="@(IntermediateRefAssembly)"
Remove MainEntryPoint="$(StartupObject)"
Remove winmdobj <PdbFile ....> and comment
Remove RefOnly="$(ProduceOnlyReferenceAssembly)"
Remove Win32Icon="$(ApplicationIcon)"
Remove Win32Manifest="$(Win32Manifest)"
Remove Win32Resource="$(Win32Resource)"
Remove DocumentationFile="@(DocFileItem)"
Remove EmbedAllSources="$(EmbedAllSources)" - not supported by our supported version of MSBuild
Remove additional steps after calling CSC
Add our FileWrites after the call to CSC
Add Condition="'@(RazorCompile)'!=''" as a condition to the RazorCoreCompile target.
-->
<Target Name="RazorCoreCompile"
<Target Name="RazorCoreCompile"
Inputs="$(MSBuildAllProjects);
@(RazorCompile);
$(AssemblyOriginatorKeyFile);
@ -76,12 +89,6 @@ Copyright (c) .NET Foundation. All rights reserved.
<NoWarn Condition="'$(BuildingInsideVisualStudio)' == 'true' AND '$(VisualStudioVersion)' != '' AND '$(VisualStudioVersion)' &gt; '10.0'">$(NoWarn);2008</NoWarn>
</PropertyGroup>
<ItemGroup Condition="'$(TargetingClr2Framework)' == 'true'">
<ReferencePathWithRefAssemblies>
<EmbedInteropTypes />
</ReferencePathWithRefAssemblies>
</ItemGroup>
<PropertyGroup>
<!-- If the user has specified AppConfigForCompiler, we'll use it. If they have not, but they set UseAppConfigForCompiler,
then we'll use AppConfig -->
@ -104,6 +111,12 @@ Copyright (c) .NET Foundation. All rights reserved.
<UseSharedCompilation>true</UseSharedCompilation>
</PropertyGroup>
<PropertyGroup>
<LangVersion Condition="'$(LangVersion)' == '' AND
(('$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(TargetFrameworkVersion)' == 'v3.0') OR
('$(TargetFrameworkIdentifier)' == '.NETStandard' AND '$(TargetFrameworkVersion)' == 'v2.1'))">8.0</LangVersion>
</PropertyGroup>
<Csc
AdditionalLibPaths="$(AdditionalLibPaths)"
AddModules="@(AddModules)"
@ -120,6 +133,7 @@ Copyright (c) .NET Foundation. All rights reserved.
DefineConstants="$(DefineConstants)"
DelaySign="$(DelaySign)"
DisabledWarnings="$(NoWarn)"
DisableSdkPath="$(DisableSdkPath)"
EmitDebugInformation="$(DebugSymbols)"
EnvironmentVariables="$(CscEnvironment)"
ErrorEndLocation="$(ErrorEndLocation)"
@ -139,6 +153,7 @@ Copyright (c) .NET Foundation. All rights reserved.
NoLogo="$(NoLogo)"
NoStandardLib="$(NoCompilerStandardLib)"
NoWin32Manifest="$(NoWin32Manifest)"
NullableContextOptions="$(NullableContextOptions)"
Optimize="$(Optimize)"
Deterministic="$(Deterministic)"
PublicSign="$(PublicSign)"

View File

@ -153,12 +153,6 @@ Copyright (c) .NET Foundation. All rights reserved.
<NoWarn Condition="'$(BuildingInsideVisualStudio)' == 'true' AND '$(VisualStudioVersion)' != '' AND '$(VisualStudioVersion)' &gt; '10.0'">$(NoWarn);2008</NoWarn>
</PropertyGroup>
<ItemGroup Condition="'$(TargetingClr2Framework)' == 'true'">
<ReferencePathWithRefAssemblies>
<EmbedInteropTypes />
</ReferencePathWithRefAssemblies>
</ItemGroup>
<PropertyGroup>
<!-- If the user has specified AppConfigForCompiler, we'll use it. If they have not, but they set UseAppConfigForCompiler,
then we'll use AppConfig -->
@ -181,6 +175,12 @@ Copyright (c) .NET Foundation. All rights reserved.
<UseSharedCompilation>true</UseSharedCompilation>
</PropertyGroup>
<PropertyGroup>
<LangVersion Condition="'$(LangVersion)' == '' AND
(('$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(TargetFrameworkVersion)' == 'v3.0') OR
('$(TargetFrameworkIdentifier)' == '.NETStandard' AND '$(TargetFrameworkVersion)' == 'v2.1'))">8.0</LangVersion>
</PropertyGroup>
<Csc
AdditionalLibPaths="$(AdditionalLibPaths)"
AddModules="@(AddModules)"
@ -211,12 +211,12 @@ Copyright (c) .NET Foundation. All rights reserved.
KeyFile="$(KeyOriginatorFile)"
LangVersion="$(LangVersion)"
LinkResources="@(LinkResource)"
MainEntryPoint="$(StartupObject)"
ModuleAssemblyName="$(ModuleAssemblyName)"
NoConfig="true"
NoLogo="$(NoLogo)"
NoStandardLib="$(NoCompilerStandardLib)"
NoWin32Manifest="$(NoWin32Manifest)"
NullableContextOptions="$(NullableContextOptions)"
Optimize="$(Optimize)"
Deterministic="$(Deterministic)"
PublicSign="$(PublicSign)"

View File

@ -647,6 +647,37 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.FileDoesNotExist(result, IntermediateOutputPath, "SimpleMvc.Views.dll");
}
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_CSharp8_NullableEnforcement_WarningsDuringBuild_BuildServer()
{
var result = await DotnetMSBuild(
"Build",
"/p:LangVersion=8.0 /p:NullableContextOptions=enable");
var indexFilePath = Path.Combine(RazorIntermediateOutputPath, "Views", "Home", "Index.cshtml.g.cs");
Assert.BuildPassed(result, allowWarnings: true);
Assert.BuildWarning(result, "CS8618", "Models\\ErrorViewModel.cs(5,18)");
Assert.FileContainsLine(result, indexFilePath, "#nullable restore");
Assert.FileContainsLine(result, indexFilePath, "#nullable disable");
}
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_CSharp8_NullableEnforcement_WarningsDuringBuild_NoBuildServer()
{
var result = await DotnetMSBuild(
"Build",
"/p:LangVersion=8.0 /p:NullableContextOptions=enable",
suppressBuildServer: true);
var indexFilePath = Path.Combine(RazorIntermediateOutputPath, "Views", "Home", "Index.cshtml.g.cs");
Assert.BuildPassed(result, allowWarnings: true);
Assert.BuildWarning(result, "CS8618", "Models\\ErrorViewModel.cs(5,18)");
Assert.FileContainsLine(result, indexFilePath, "#nullable restore");
Assert.FileContainsLine(result, indexFilePath, "#nullable disable");
}
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_Mvc_WithoutAddRazorSupportForMvc()

View File

@ -65,7 +65,7 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
/// <summary>
/// Gets the parse options applied when using <see cref="AddCSharpSyntaxTree(string, string)"/>.
/// </summary>
protected virtual CSharpParseOptions CSharpParseOptions { get; } = new CSharpParseOptions(LanguageVersion.Latest);
protected virtual CSharpParseOptions CSharpParseOptions { get; } = new CSharpParseOptions(LanguageVersion.Preview);
/// <summary>
/// Gets the compilation options applied when compiling assemblies.
@ -323,6 +323,7 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
}
b.Features.Add(new DefaultTypeNameFeature());
b.SetCSharpLanguageVersion(CSharpParseOptions.LanguageVersion);
// Decorate each import feature so we can normalize line endings.
foreach (var feature in b.Features.OfType<IImportProjectFeature>().ToArray())

View File

@ -52,7 +52,7 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
referenceAssemblies,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
CSharpParseOptions = new CSharpParseOptions(LanguageVersion.CSharp7_3);
CSharpParseOptions = new CSharpParseOptions(LanguageVersion.Preview);
}
public RazorIntegrationTestBase()
@ -129,6 +129,8 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
References = references,
});
b.SetCSharpLanguageVersion(CSharpParseOptions.LanguageVersion);
CompilerFeatures.Register(b);
});
}