Use SuppressUniqueIds exclusively from RazorCodeGenerationOptions

This change stops pushing the SuppressUniqueIds value into RazorCodeDocument.Items.
This commit is contained in:
Dustin Campbell 2024-08-14 12:41:39 -07:00
Родитель 96adc428f7
Коммит 28bfd61d38
6 изменённых файлов: 49 добавлений и 23 удалений

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

@ -17,11 +17,8 @@ public sealed class CodeRenderingContext : IDisposable
internal static readonly object NewLineStringKey = "NewLineString";
internal static readonly object SuppressUniqueIdsKey = "SuppressUniqueIds";
public RazorCodeGenerationOptions Options { get; }
public CodeWriter CodeWriter { get; }
public string SuppressUniqueIds { get; }
private readonly RazorCodeDocument _codeDocument;
private readonly DocumentIntermediateNode _documentNode;
@ -68,8 +65,6 @@ public sealed class CodeRenderingContext : IDisposable
// Set new line character to a specific string regardless of platform, for testing purposes.
var newLineString = codeDocument.Items[NewLineStringKey] as string ?? Environment.NewLine;
CodeWriter = new CodeWriter(newLineString, options);
SuppressUniqueIds = codeDocument.Items[SuppressUniqueIdsKey] as string ?? options.SuppressUniqueIds;
}
public void Dispose()

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

@ -101,7 +101,7 @@ internal sealed class DefaultTagHelperTargetExtension : IDefaultTagHelperTargetE
// Assign a unique ID for this instance of the source HTML tag. This must be unique
// per call site, e.g. if the tag is on the view twice, there should be two IDs.
var uniqueId = context.SuppressUniqueIds;
var uniqueId = context.Options.SuppressUniqueIds;
if (uniqueId == null)
{
uniqueId = GetDeterministicId(context);

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

@ -11,8 +11,10 @@ public sealed class RazorCodeGenerationOptions(
string? rootNamespace,
string? suppressUniqueIds)
{
public bool DesignTime => flags.HasFlag(RazorCodeGenerationOptionsFlags.DesignTime);
public bool IndentWithTabs => flags.HasFlag(RazorCodeGenerationOptionsFlags.IndentWithTabs);
private readonly RazorCodeGenerationOptionsFlags _flags = flags;
public bool DesignTime => _flags.HasFlag(RazorCodeGenerationOptionsFlags.DesignTime);
public bool IndentWithTabs => _flags.HasFlag(RazorCodeGenerationOptionsFlags.IndentWithTabs);
public int IndentSize { get; } = indentSize;
/// <summary>
@ -30,7 +32,7 @@ public sealed class RazorCodeGenerationOptions(
/// purposes.
/// </remarks>
public bool SuppressChecksum
=> flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressChecksum);
=> _flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressChecksum);
/// <summary>
/// Gets a value that indicates whether to suppress the default metadata attributes in the generated
@ -49,7 +51,7 @@ public sealed class RazorCodeGenerationOptions(
/// </para>
/// </remarks>
public bool SuppressMetadataAttributes
=> flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressMetadataAttributes);
=> _flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressMetadataAttributes);
/// <summary>
/// Gets a value that indicates whether to suppress the <c>RazorSourceChecksumAttribute</c>.
@ -59,37 +61,37 @@ public sealed class RazorCodeGenerationOptions(
/// </para>
/// </summary>
public bool SuppressMetadataSourceChecksumAttributes
=> flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressMetadataSourceChecksumAttributes);
=> _flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressMetadataSourceChecksumAttributes);
/// <summary>
/// Gets or sets a value that determines if an empty body is generated for the primary method.
/// </summary>
public bool SuppressPrimaryMethodBody
=> flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressPrimaryMethodBody);
=> _flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressPrimaryMethodBody);
/// <summary>
/// Gets a value that determines if nullability type enforcement should be suppressed for user code.
/// </summary>
public bool SuppressNullabilityEnforcement
=> flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressNullabilityEnforcement);
=> _flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressNullabilityEnforcement);
/// <summary>
/// Gets a value that determines if the components code writer may omit values for minimized attributes.
/// </summary>
public bool OmitMinimizedComponentAttributeValues
=> flags.HasFlag(RazorCodeGenerationOptionsFlags.OmitMinimizedComponentAttributeValues);
=> _flags.HasFlag(RazorCodeGenerationOptionsFlags.OmitMinimizedComponentAttributeValues);
/// <summary>
/// Gets a value that determines if localized component names are to be supported.
/// </summary>
public bool SupportLocalizedComponentNames
=> flags.HasFlag(RazorCodeGenerationOptionsFlags.SupportLocalizedComponentNames);
=> _flags.HasFlag(RazorCodeGenerationOptionsFlags.SupportLocalizedComponentNames);
/// <summary>
/// Gets a value that determines if enhanced line pragmas are to be utilized.
/// </summary>
public bool UseEnhancedLinePragma
=> flags.HasFlag(RazorCodeGenerationOptionsFlags.UseEnhancedLinePragma);
=> _flags.HasFlag(RazorCodeGenerationOptionsFlags.UseEnhancedLinePragma);
/// <summary>
/// Gets a value used for unique ids for testing purposes. Null for unique ids.
@ -100,13 +102,13 @@ public sealed class RazorCodeGenerationOptions(
/// Determines whether RenderTreeBuilder.AddComponentParameter should not be used.
/// </summary>
public bool SuppressAddComponentParameter
=> flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressAddComponentParameter);
=> _flags.HasFlag(RazorCodeGenerationOptionsFlags.SuppressAddComponentParameter);
/// <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
=> flags.HasFlag(RazorCodeGenerationOptionsFlags.RemapLinePragmaPathsOnWindows);
=> _flags.HasFlag(RazorCodeGenerationOptionsFlags.RemapLinePragmaPathsOnWindows);
public static RazorCodeGenerationOptions Default { get; } = new RazorCodeGenerationOptions(
flags: RazorCodeGenerationOptionsFlags.DefaultFlags,
@ -145,4 +147,14 @@ public sealed class RazorCodeGenerationOptions(
return options;
}
public RazorCodeGenerationOptionsBuilder ToBuilder()
{
var builder = new RazorCodeGenerationOptionsBuilder(_flags);
builder.IndentSize = IndentSize;
builder.RootNamespace = RootNamespace;
builder.SuppressUniqueIds = SuppressUniqueIds;
return builder;
}
}

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

@ -149,6 +149,11 @@ public sealed class RazorCodeGenerationOptionsBuilder
Configuration = configuration;
}
public RazorCodeGenerationOptionsBuilder(RazorCodeGenerationOptionsFlags flags)
{
_flags = flags;
}
public RazorCodeGenerationOptionsBuilder(bool designTime)
{
if (designTime)

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

@ -16,7 +16,7 @@ public static class TestCodeRenderingContext
IntermediateNodeWriter nodeWriter = null)
{
var documentNode = new DocumentIntermediateNode();
var options = RazorCodeGenerationOptions.DesignTimeDefault;
var optionsBuilder = RazorCodeGenerationOptions.DesignTimeDefault.ToBuilder();
if (source == null)
{
@ -31,7 +31,7 @@ public static class TestCodeRenderingContext
if (suppressUniqueIds != null)
{
codeDocument.Items[CodeRenderingContext.SuppressUniqueIdsKey] = suppressUniqueIds;
optionsBuilder.SuppressUniqueIds = suppressUniqueIds;
}
if (nodeWriter == null)
@ -39,6 +39,8 @@ public static class TestCodeRenderingContext
nodeWriter = new DesignTimeNodeWriter();
}
var options = optionsBuilder.Build();
var context = new CodeRenderingContext(nodeWriter, codeDocument, documentNode, options);
context.Visitor = new RenderChildrenVisitor(context);
@ -52,7 +54,7 @@ public static class TestCodeRenderingContext
IntermediateNodeWriter nodeWriter = null)
{
var documentNode = new DocumentIntermediateNode();
var options = RazorCodeGenerationOptions.Default;
var optionsBuilder = RazorCodeGenerationOptions.Default.ToBuilder();
if (source == null)
{
@ -67,7 +69,7 @@ public static class TestCodeRenderingContext
if (suppressUniqueIds != null)
{
codeDocument.Items[CodeRenderingContext.SuppressUniqueIdsKey] = suppressUniqueIds;
optionsBuilder.SuppressUniqueIds = suppressUniqueIds;
}
if (nodeWriter == null)
@ -75,6 +77,8 @@ public static class TestCodeRenderingContext
nodeWriter = new RuntimeNodeWriter();
}
var options = optionsBuilder.Build();
var context = new CodeRenderingContext(nodeWriter, codeDocument, documentNode, options);
context.Visitor = new RenderChildrenVisitor(context);

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

@ -299,6 +299,7 @@ public abstract class IntegrationTestBase
{
return RazorProjectEngine.Create(configuration, FileSystem, b =>
{
b.Features.Add(new ConfigureCodeGenerationOptionsFeature());
b.Phases.Insert(0, new ConfigureCodeRenderingPhase(LineEnding));
b.RegisterExtensions();
@ -782,6 +783,16 @@ public abstract class IntegrationTestBase
return Regex.Replace(content, "(?<!\r)\n", lineEnding, RegexOptions.None, TimeSpan.FromSeconds(10));
}
private sealed class ConfigureCodeGenerationOptionsFeature : RazorEngineFeatureBase, IConfigureRazorCodeGenerationOptionsFeature
{
public int Order { get; }
public void Configure(RazorCodeGenerationOptionsBuilder options)
{
options.SuppressUniqueIds = "test";
}
}
private class ConfigureCodeRenderingPhase : RazorEnginePhaseBase
{
public ConfigureCodeRenderingPhase(string lineEnding)
@ -793,7 +804,6 @@ public abstract class IntegrationTestBase
protected override void ExecuteCore(RazorCodeDocument codeDocument)
{
codeDocument.Items[CodeRenderingContext.SuppressUniqueIdsKey] = "test";
codeDocument.Items[CodeRenderingContext.NewLineStringKey] = LineEnding;
}
}