Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,20 @@ protected override void WriteReferenceCaptureInnards(CodeRenderingContext contex
private void WriteAttribute(CodeRenderingContext context, string key, IList<IntermediateToken> value)
{
BeginWriteAttribute(context, key);

if (value.Count > 0)
{
context.CodeWriter.WriteParameterSeparator();
WriteAttributeValue(context, value);
}
else if (!context.Options.OmitMinimizedComponentAttributeValues)
{
// In version 5+, there's no need to supply a value for a minimized attribute.
// But for older language versions, minimized attributes were represented as "true".
context.CodeWriter.WriteParameterSeparator();
context.CodeWriter.WriteBooleanLiteral(true);
}

context.CodeWriter.WriteEndMethodInvocation();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.AspNetCore.Razor.Language
Expand All @@ -13,7 +13,8 @@ public DefaultRazorCodeGenerationOptions(
bool suppressChecksum,
bool suppressMetadataAttributes,
bool suppressPrimaryMethodBody,
bool suppressNullabilityEnforcement)
bool suppressNullabilityEnforcement,
bool omitMinimizedComponentAttributeValues)
{
IndentWithTabs = indentWithTabs;
IndentSize = indentSize;
Expand All @@ -23,6 +24,7 @@ public DefaultRazorCodeGenerationOptions(
SuppressMetadataAttributes = suppressMetadataAttributes;
SuppressPrimaryMethodBody = suppressPrimaryMethodBody;
SuppressNullabilityEnforcement = suppressNullabilityEnforcement;
OmitMinimizedComponentAttributeValues = omitMinimizedComponentAttributeValues;
}

public override bool DesignTime { get; }
Expand All @@ -36,5 +38,7 @@ public DefaultRazorCodeGenerationOptions(
public override bool SuppressChecksum { get; }

public override bool SuppressNullabilityEnforcement { get; }

public override bool OmitMinimizedComponentAttributeValues { get; }
Copy link
Member Author

@SteveSandersonMS SteveSandersonMS Aug 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've defined this new option, instead of using the RazorLanguageVersion directly inside the code writer, because:

  1. It seems like the existing philosophy is that the language version should be used to trigger the values for options, rather than code doing things directly based on the language version, so this is consistent.
  2. The code writer has no existing way of knowing the RazorLanguageVersion (perhaps because of the philosophy mentioned above), and threading it through many layers would be quite disruptive, including to some public APIs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have to address in this PR but might want to add a new CodeGenerationFeatureFlags property to contain these similar to this. That makes it cleaner if we introduce more code generation features in the future.

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -39,6 +39,8 @@ public DefaultRazorCodeGenerationOptionsBuilder(bool designTime)

public override bool SuppressNullabilityEnforcement { get; set; }

public override bool OmitMinimizedComponentAttributeValues { get; set; }

public override RazorCodeGenerationOptions Build()
{
return new DefaultRazorCodeGenerationOptions(
Expand All @@ -49,7 +51,8 @@ public override RazorCodeGenerationOptions Build()
SuppressChecksum,
SuppressMetadataAttributes,
SuppressPrimaryMethodBody,
SuppressNullabilityEnforcement);
SuppressNullabilityEnforcement,
OmitMinimizedComponentAttributeValues);
}

public override void SetDesignTime(bool designTime)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand All @@ -17,7 +17,8 @@ public static RazorCodeGenerationOptions CreateDefault()
rootNamespace: null,
suppressMetadataAttributes: false,
suppressPrimaryMethodBody: false,
suppressNullabilityEnforcement: false);
suppressNullabilityEnforcement: false,
omitMinimizedComponentAttributeValues: false);
}

public static RazorCodeGenerationOptions CreateDesignTimeDefault()
Expand All @@ -30,7 +31,8 @@ public static RazorCodeGenerationOptions CreateDesignTimeDefault()
suppressChecksum: false,
suppressMetadataAttributes: true,
suppressPrimaryMethodBody: false,
suppressNullabilityEnforcement: false);
suppressNullabilityEnforcement: false,
omitMinimizedComponentAttributeValues: false);
}

public static RazorCodeGenerationOptions Create(Action<RazorCodeGenerationOptionsBuilder> configure)
Expand Down Expand Up @@ -114,5 +116,10 @@ public static RazorCodeGenerationOptions CreateDesignTime(Action<RazorCodeGenera
/// Gets a value that determines if nullability type enforcement should be suppressed for user code.
/// </summary>
public virtual bool SuppressNullabilityEnforcement { get; }

/// <summary>
/// Gets a value that determines if the components code writer may omit values for minimized attributes.
/// </summary>
public virtual bool OmitMinimizedComponentAttributeValues { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.AspNetCore.Razor.Language
Expand Down Expand Up @@ -59,6 +59,11 @@ public abstract class RazorCodeGenerationOptionsBuilder
/// </summary>
public virtual bool SuppressNullabilityEnforcement { get; set; }

/// <summary>
/// Gets or sets a value that determines if the components code writer may omit values for minimized attributes.
/// </summary>
public virtual bool OmitMinimizedComponentAttributeValues { get; set; }

public abstract RazorCodeGenerationOptions Build();

public virtual void SetDesignTime(bool designTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
{
public abstract class ComponentCodeGenerationTestBase : RazorBaselineIntegrationTestBase
{
private RazorConfiguration _configuration;

internal override string FileKind => FileKinds.Component;

internal override bool UseTwoPhaseCompilation => true;

internal override RazorConfiguration Configuration => _configuration ?? base.Configuration;

protected ComponentCodeGenerationTestBase()
: base(generateBaselines: null)
{
Expand Down Expand Up @@ -364,6 +368,38 @@ public void MarkupComment_IsNotIncluded()
CompileToAssembly(generated);
}

[Fact]
public void OmitsMinimizedAttributeValueParameter()
{
// Act
var generated = CompileToCSharp(@"
<elem normal-attr=""@(""val"")"" minimized-attr empty-string-atttr=""""></elem>");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

[Fact]
public void IncludesMinimizedAttributeValueParameterBeforeLanguageVersion5()
{
// Arrange
_configuration = RazorConfiguration.Create(
RazorLanguageVersion.Version_3_0,
base.Configuration.ConfigurationName,
base.Configuration.Extensions);

// Act
var generated = CompileToCSharp(@"
<elem normal-attr=""@(""val"")"" minimized-attr empty-string-atttr=""""></elem>");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

[Fact]
public void Component_WithFullyQualifiedTagNames()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static System.Object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__o =
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
"val"

#line default
#line hidden
#nullable disable
;
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [12] ) - System
UsingDirective - (18:2,1 [32] ) - System.Collections.Generic
UsingDirective - (53:3,1 [17] ) - System.Linq
UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static System.Object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupElement - (0:0,0 [73] x:\dir\subdir\Test\TestComponent.cshtml) - elem
HtmlAttribute - (5:0,5 [23] x:\dir\subdir\Test\TestComponent.cshtml) - normal-attr=" - "
CSharpExpressionAttributeValue - (19:0,19 [8] x:\dir\subdir\Test\TestComponent.cshtml) -
LazyIntermediateToken - (21:0,21 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - "val"
HtmlAttribute - (28:0,28 [15] x:\dir\subdir\Test\TestComponent.cshtml) - minimized-attr -
HtmlAttribute - (43:0,43 [22] x:\dir\subdir\Test\TestComponent.cshtml) - empty-string-atttr=" - "
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Source Location: (21:0,21 [5] x:\dir\subdir\Test\TestComponent.cshtml)
|"val"|
Generated Location: (893:25,21 [5] )
|"val"|

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static System.Object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__o =
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
"val"

#line default
#line hidden
#nullable disable
;
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [12] ) - System
UsingDirective - (18:2,1 [32] ) - System.Collections.Generic
UsingDirective - (53:3,1 [17] ) - System.Linq
UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static System.Object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupElement - (0:0,0 [73] x:\dir\subdir\Test\TestComponent.cshtml) - elem
HtmlAttribute - (5:0,5 [23] x:\dir\subdir\Test\TestComponent.cshtml) - normal-attr=" - "
CSharpExpressionAttributeValue - (19:0,19 [8] x:\dir\subdir\Test\TestComponent.cshtml) -
LazyIntermediateToken - (21:0,21 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - "val"
HtmlAttribute - (28:0,28 [15] x:\dir\subdir\Test\TestComponent.cshtml) - minimized-attr -
HtmlAttribute - (43:0,43 [22] x:\dir\subdir\Test\TestComponent.cshtml) - empty-string-atttr=" - "
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Source Location: (21:0,21 [5] x:\dir\subdir\Test\TestComponent.cshtml)
|"val"|
Generated Location: (893:25,21 [5] )
|"val"|

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.OpenElement(0, "elem");
__builder.AddAttribute(1, "normal-attr",
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
"val"

#line default
#line hidden
#nullable disable
);
__builder.AddAttribute(2, "minimized-attr", true);
__builder.AddAttribute(3, "empty-string-atttr", true);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above two lines show the compiler output for 3.x language version.

__builder.CloseElement();
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [14] ) - System
UsingDirective - (18:2,1 [34] ) - System.Collections.Generic
UsingDirective - (53:3,1 [19] ) - System.Linq
UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupElement - (0:0,0 [73] x:\dir\subdir\Test\TestComponent.cshtml) - elem
HtmlAttribute - (5:0,5 [23] x:\dir\subdir\Test\TestComponent.cshtml) - normal-attr=" - "
CSharpExpressionAttributeValue - (19:0,19 [8] x:\dir\subdir\Test\TestComponent.cshtml) -
LazyIntermediateToken - (21:0,21 [5] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - "val"
HtmlAttribute - (28:0,28 [15] x:\dir\subdir\Test\TestComponent.cshtml) - minimized-attr -
HtmlAttribute - (43:0,43 [22] x:\dir\subdir\Test\TestComponent.cshtml) - empty-string-atttr=" - "
Loading