Merge RazorCodeGenerationOptions and DefaultRazorCodeGenerationOptions

This commit is contained in:
Dustin Campbell 2024-08-14 08:55:05 -07:00
Родитель 03e2d074c0
Коммит 972691bc1e
3 изменённых файлов: 127 добавлений и 160 удалений

Просмотреть файл

@ -1,57 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
namespace Microsoft.AspNetCore.Razor.Language;
internal class DefaultRazorCodeGenerationOptions : RazorCodeGenerationOptions
{
public DefaultRazorCodeGenerationOptions(
bool indentWithTabs,
int indentSize,
bool designTime,
string rootNamespace,
bool suppressChecksum,
bool suppressMetadataAttributes,
bool suppressPrimaryMethodBody,
bool suppressNullabilityEnforcement,
bool omitMinimizedComponentAttributeValues,
bool supportLocalizedComponentNames,
bool useEnhancedLinePragma,
string suppressUniqueIds,
bool suppressAddComponentParameter,
bool remapLinePragmaPathsOnWindows)
{
IndentWithTabs = indentWithTabs;
IndentSize = indentSize;
DesignTime = designTime;
RootNamespace = rootNamespace;
SuppressChecksum = suppressChecksum;
SuppressMetadataAttributes = suppressMetadataAttributes;
SuppressPrimaryMethodBody = suppressPrimaryMethodBody;
SuppressNullabilityEnforcement = suppressNullabilityEnforcement;
OmitMinimizedComponentAttributeValues = omitMinimizedComponentAttributeValues;
SupportLocalizedComponentNames = supportLocalizedComponentNames;
UseEnhancedLinePragma = useEnhancedLinePragma;
SuppressUniqueIds = suppressUniqueIds;
SuppressAddComponentParameter = suppressAddComponentParameter;
RemapLinePragmaPathsOnWindows = remapLinePragmaPathsOnWindows;
}
public override bool DesignTime { get; }
public override bool IndentWithTabs { get; }
public override int IndentSize { get; }
public override string RootNamespace { get; }
public override bool SuppressChecksum { get; }
public override bool SuppressNullabilityEnforcement { get; }
public override bool OmitMinimizedComponentAttributeValues { get; }
public override bool UseEnhancedLinePragma { get; }
}

Просмотреть файл

@ -1,17 +1,136 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using System;
namespace Microsoft.AspNetCore.Razor.Language;
public abstract class RazorCodeGenerationOptions
public sealed class RazorCodeGenerationOptions
{
public bool DesignTime { get; }
public bool IndentWithTabs { get; }
public int IndentSize { get; }
/// <summary>
/// Gets the root namespace for the generated code.
/// </summary>
public string? RootNamespace { get; }
/// <summary>
/// Gets a value that indicates whether to suppress the default <c>#pragma checksum</c> directive in the
/// generated C# code. If <c>false</c> the checksum directive will be included, otherwise it will not be
/// generated. Defaults to <c>false</c>, meaning that the checksum will be included.
/// </summary>
/// <remarks>
/// The <c>#pragma checksum</c> is required to enable debugging and should only be suppressed for testing
/// purposes.
/// </remarks>
public bool SuppressChecksum { get; }
/// <summary>
/// Gets a value that indicates whether to suppress the default metadata attributes in the generated
/// C# code. If <c>false</c> the default attributes will be included, otherwise they will not be generated.
/// Defaults to <c>false</c> at run time, meaning that the attributes will be included. Defaults to
/// <c>true</c> at design time, meaning that the attributes will not be included.
/// </summary>
/// <remarks>
/// <para>
/// The <c>Microsoft.AspNetCore.Razor.Runtime</c> package includes a default set of attributes intended
/// for runtimes to discover metadata about the compiled code.
/// </para>
/// <para>
/// The default metadata attributes should be suppressed if code generation targets a runtime without
/// a reference to <c>Microsoft.AspNetCore.Razor.Runtime</c>, or for testing purposes.
/// </para>
/// </remarks>
public bool SuppressMetadataAttributes { get; }
/// <summary>
/// Gets a value that indicates whether to suppress the <c>RazorSourceChecksumAttribute</c>.
/// <para>
/// Used by default in .NET 6 apps since including a type-level attribute that changes on every
/// edit are treated as rude edits by hot reload.
/// </para>
/// </summary>
internal bool SuppressMetadataSourceChecksumAttributes { get; set; }
/// <summary>
/// Gets or sets a value that determines if an empty body is generated for the primary method.
/// </summary>
public bool SuppressPrimaryMethodBody { get; }
/// <summary>
/// Gets a value that determines if nullability type enforcement should be suppressed for user code.
/// </summary>
public bool SuppressNullabilityEnforcement { get; }
/// <summary>
/// Gets a value that determines if the components code writer may omit values for minimized attributes.
/// </summary>
public bool OmitMinimizedComponentAttributeValues { get; }
/// <summary>
/// Gets a value that determines if localized component names are to be supported.
/// </summary>
public bool SupportLocalizedComponentNames { get; set; }
/// <summary>
/// Gets a value that determines if enhanced line pragmas are to be utilized.
/// </summary>
public bool UseEnhancedLinePragma { get; }
/// <summary>
/// Gets a value used for unique ids for testing purposes. Null for unique ids.
/// </summary>
internal string? SuppressUniqueIds { get; }
/// <summary>
/// Determines whether RenderTreeBuilder.AddComponentParameter should not be used.
/// </summary>
internal bool SuppressAddComponentParameter { get; }
/// <summary>
/// Determines if the file paths emitted as part of line pragmas should be mapped back to a valid path on windows.
/// </summary>
public bool RemapLinePragmaPathsOnWindows { get; }
public RazorCodeGenerationOptions(
bool indentWithTabs,
int indentSize,
bool designTime,
string? rootNamespace,
bool suppressChecksum,
bool suppressMetadataAttributes,
bool suppressPrimaryMethodBody,
bool suppressNullabilityEnforcement,
bool omitMinimizedComponentAttributeValues,
bool supportLocalizedComponentNames,
bool useEnhancedLinePragma,
string? suppressUniqueIds,
bool suppressAddComponentParameter,
bool remapLinePragmaPathsOnWindows)
{
IndentWithTabs = indentWithTabs;
IndentSize = indentSize;
DesignTime = designTime;
RootNamespace = rootNamespace;
SuppressChecksum = suppressChecksum;
SuppressMetadataAttributes = suppressMetadataAttributes;
SuppressPrimaryMethodBody = suppressPrimaryMethodBody;
SuppressNullabilityEnforcement = suppressNullabilityEnforcement;
OmitMinimizedComponentAttributeValues = omitMinimizedComponentAttributeValues;
SupportLocalizedComponentNames = supportLocalizedComponentNames;
UseEnhancedLinePragma = useEnhancedLinePragma;
SuppressUniqueIds = suppressUniqueIds;
SuppressAddComponentParameter = suppressAddComponentParameter;
RemapLinePragmaPathsOnWindows = remapLinePragmaPathsOnWindows;
}
public static RazorCodeGenerationOptions CreateDefault()
{
return new DefaultRazorCodeGenerationOptions(
return new RazorCodeGenerationOptions(
indentWithTabs: false,
indentSize: 4,
designTime: false,
@ -30,7 +149,7 @@ public abstract class RazorCodeGenerationOptions
public static RazorCodeGenerationOptions CreateDesignTimeDefault()
{
return new DefaultRazorCodeGenerationOptions(
return new RazorCodeGenerationOptions(
indentWithTabs: false,
indentSize: 4,
designTime: true,
@ -49,10 +168,7 @@ public abstract class RazorCodeGenerationOptions
public static RazorCodeGenerationOptions Create(Action<RazorCodeGenerationOptionsBuilder> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgHelper.ThrowIfNull(configure);
var builder = new RazorCodeGenerationOptionsBuilder(designTime: false);
configure(builder);
@ -63,10 +179,7 @@ public abstract class RazorCodeGenerationOptions
public static RazorCodeGenerationOptions CreateDesignTime(Action<RazorCodeGenerationOptionsBuilder> configure)
{
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
ArgHelper.ThrowIfNull(configure);
var builder = new RazorCodeGenerationOptionsBuilder(designTime: true)
{
@ -78,93 +191,4 @@ public abstract class RazorCodeGenerationOptions
return options;
}
public abstract bool DesignTime { get; }
public abstract bool IndentWithTabs { get; }
public abstract int IndentSize { get; }
/// <summary>
/// Gets the root namespace for the generated code.
/// </summary>
public virtual string RootNamespace { get; }
/// <summary>
/// Gets a value that indicates whether to suppress the default <c>#pragma checksum</c> directive in the
/// generated C# code. If <c>false</c> the checksum directive will be included, otherwise it will not be
/// generated. Defaults to <c>false</c>, meaning that the checksum will be included.
/// </summary>
/// <remarks>
/// The <c>#pragma checksum</c> is required to enable debugging and should only be suppressed for testing
/// purposes.
/// </remarks>
public abstract bool SuppressChecksum { get; }
/// <summary>
/// Gets a value that indicates whether to suppress the default metadata attributes in the generated
/// C# code. If <c>false</c> the default attributes will be included, otherwise they will not be generated.
/// Defaults to <c>false</c> at run time, meaning that the attributes will be included. Defaults to
/// <c>true</c> at design time, meaning that the attributes will not be included.
/// </summary>
/// <remarks>
/// <para>
/// The <c>Microsoft.AspNetCore.Razor.Runtime</c> package includes a default set of attributes intended
/// for runtimes to discover metadata about the compiled code.
/// </para>
/// <para>
/// The default metadata attributes should be suppressed if code generation targets a runtime without
/// a reference to <c>Microsoft.AspNetCore.Razor.Runtime</c>, or for testing purposes.
/// </para>
/// </remarks>
public virtual bool SuppressMetadataAttributes { get; protected set; }
/// <summary>
/// Gets a value that indicates whether to suppress the <c>RazorSourceChecksumAttribute</c>.
/// <para>
/// Used by default in .NET 6 apps since including a type-level attribute that changes on every
/// edit are treated as rude edits by hot reload.
/// </para>
/// </summary>
internal bool SuppressMetadataSourceChecksumAttributes { get; set; }
/// <summary>
/// Gets or sets a value that determines if an empty body is generated for the primary method.
/// </summary>
public virtual bool SuppressPrimaryMethodBody { get; protected set; }
/// <summary>
/// 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; }
/// <summary>
/// Gets a value that determines if localized component names are to be supported.
/// </summary>
public virtual bool SupportLocalizedComponentNames { get; set; }
/// <summary>
/// Gets a value that determines if enhanced line pragmas are to be utilized.
/// </summary>
public virtual bool UseEnhancedLinePragma { get; }
/// <summary>
/// Gets a value used for unique ids for testing purposes. Null for unique ids.
/// </summary>
internal string SuppressUniqueIds { get; private protected init; }
/// <summary>
/// Determines whether RenderTreeBuilder.AddComponentParameter should not be used.
/// </summary>
internal bool SuppressAddComponentParameter { get; private protected init; }
/// <summary>
/// Determines if the file paths emitted as part of line pragmas should be mapped back to a valid path on windows.
/// </summary>
public bool RemapLinePragmaPathsOnWindows { get; private protected init; }
}

Просмотреть файл

@ -115,7 +115,7 @@ public sealed class RazorCodeGenerationOptionsBuilder
public RazorCodeGenerationOptions Build()
{
return new DefaultRazorCodeGenerationOptions(
return new RazorCodeGenerationOptions(
IndentWithTabs,
IndentSize,
DesignTime,