Pass RazorSourceDocument into CodeRenderingContext rather than RazorCodeDocument

This commit is contained in:
Dustin Campbell 2024-08-14 13:04:06 -07:00
Родитель 1eb601c417
Коммит 7a9f32ea37
3 изменённых файлов: 38 добавлений и 64 удалений

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

@ -15,10 +15,10 @@ public sealed class CodeRenderingContext : IDisposable
{
private readonly record struct ScopeInternal(IntermediateNodeWriter Writer);
public RazorSourceDocument SourceDocument { get; }
public RazorCodeGenerationOptions Options { get; }
public CodeWriter CodeWriter { get; }
private readonly RazorCodeDocument _codeDocument;
private readonly DocumentIntermediateNode _documentNode;
private readonly Stack<IntermediateNode> _ancestorStack;
@ -29,20 +29,19 @@ public sealed class CodeRenderingContext : IDisposable
private readonly ImmutableArray<LinePragma>.Builder _linePragmas;
public string DocumentKind => _documentNode.DocumentKind;
public RazorSourceDocument SourceDocument => _codeDocument.Source;
public CodeRenderingContext(
IntermediateNodeWriter nodeWriter,
RazorCodeDocument codeDocument,
RazorSourceDocument sourceDocument,
DocumentIntermediateNode documentNode,
RazorCodeGenerationOptions options)
{
ArgHelper.ThrowIfNull(nodeWriter);
ArgHelper.ThrowIfNull(codeDocument);
ArgHelper.ThrowIfNull(sourceDocument);
ArgHelper.ThrowIfNull(documentNode);
ArgHelper.ThrowIfNull(options);
_codeDocument = codeDocument;
SourceDocument = sourceDocument;
_documentNode = documentNode;
Options = options;

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

@ -4,7 +4,6 @@
#nullable disable
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Security.Cryptography;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
@ -24,21 +23,15 @@ internal class DefaultDocumentWriter : DocumentWriter
public override RazorCSharpDocument WriteDocument(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
{
if (codeDocument == null)
{
throw new ArgumentNullException(nameof(codeDocument));
}
if (documentNode == null)
{
throw new ArgumentNullException(nameof(documentNode));
}
ArgHelper.ThrowIfNull(codeDocument);
ArgHelper.ThrowIfNull(documentNode);
using var context = new CodeRenderingContext(
_codeTarget.CreateNodeWriter(),
codeDocument,
codeDocument.Source,
documentNode,
_options);
context.Visitor = new Visitor(_codeTarget, context);
context.Visitor.VisitDocument(documentNode);

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

@ -15,33 +15,13 @@ public static class TestCodeRenderingContext
RazorSourceDocument source = null,
IntermediateNodeWriter nodeWriter = null)
{
nodeWriter ??= new RuntimeNodeWriter();
source ??= TestRazorSourceDocument.Create();
var documentNode = new DocumentIntermediateNode();
var optionsBuilder = RazorCodeGenerationOptions.DesignTimeDefault.ToBuilder();
if (source == null)
{
source = TestRazorSourceDocument.Create();
}
var options = ConfigureOptions(RazorCodeGenerationOptions.DesignTimeDefault, newLineString, suppressUniqueIds);
var codeDocument = RazorCodeDocument.Create(source);
if (newLineString != null)
{
optionsBuilder.NewLine = newLineString;
}
if (suppressUniqueIds != null)
{
optionsBuilder.SuppressUniqueIds = suppressUniqueIds;
}
if (nodeWriter == null)
{
nodeWriter = new DesignTimeNodeWriter();
}
var options = optionsBuilder.Build();
var context = new CodeRenderingContext(nodeWriter, codeDocument, documentNode, options);
var context = new CodeRenderingContext(nodeWriter, source, documentNode, options);
context.Visitor = new RenderChildrenVisitor(context);
return context;
@ -53,38 +33,40 @@ public static class TestCodeRenderingContext
RazorSourceDocument source = null,
IntermediateNodeWriter nodeWriter = null)
{
nodeWriter ??= new RuntimeNodeWriter();
source ??= TestRazorSourceDocument.Create();
var documentNode = new DocumentIntermediateNode();
var optionsBuilder = RazorCodeGenerationOptions.Default.ToBuilder();
if (source == null)
{
source = TestRazorSourceDocument.Create();
}
var options = ConfigureOptions(RazorCodeGenerationOptions.Default, newLineString, suppressUniqueIds);
var codeDocument = RazorCodeDocument.Create(source);
if (newLineString != null)
{
optionsBuilder.NewLine = newLineString;
}
if (suppressUniqueIds != null)
{
optionsBuilder.SuppressUniqueIds = suppressUniqueIds;
}
if (nodeWriter == null)
{
nodeWriter = new RuntimeNodeWriter();
}
var options = optionsBuilder.Build();
var context = new CodeRenderingContext(nodeWriter, codeDocument, documentNode, options);
var context = new CodeRenderingContext(nodeWriter, source, documentNode, options);
context.Visitor = new RenderChildrenVisitor(context);
return context;
}
private static RazorCodeGenerationOptions ConfigureOptions(RazorCodeGenerationOptions options, string newLine, string suppressUniqueIds)
{
if (newLine is null && suppressUniqueIds is null)
{
return options;
}
var builder = options.ToBuilder();
if (newLine is not null)
{
builder.NewLine = newLine;
}
if (suppressUniqueIds is not null)
{
builder.SuppressUniqueIds = suppressUniqueIds;
}
return builder.Build();
}
private class RenderChildrenVisitor : IntermediateNodeVisitor
{
private readonly CodeRenderingContext _context;