Remove explicit scope managment

This commit is contained in:
Ryan Nowak 2017-07-05 08:25:42 -07:00
parent 7408bcd025
commit 647ce7515d
3 changed files with 56 additions and 68 deletions

View File

@ -10,8 +10,11 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public abstract class CodeRenderingContext
{
internal static readonly object NewLineString = "NewLineString";
internal static readonly object SuppressUniqueIds = "SuppressUniqueIds";
public abstract IEnumerable<IntermediateNode> Ancestors { get; }
public abstract CodeWriter CodeWriter { get; }
public abstract RazorDiagnosticCollection Diagnostics { get; }
@ -19,8 +22,6 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public abstract string DocumentKind { get; }
public abstract ItemCollection Items { get; }
public abstract IEnumerable<IntermediateNode> Ancestors { get; }
public abstract IntermediateNodeWriter NodeWriter { get; }
@ -30,36 +31,14 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
public abstract RazorSourceDocument SourceDocument { get; }
public abstract Scope CreateScope();
public abstract Scope CreateScope(IntermediateNodeWriter writer);
public abstract void EndScope();
public abstract void AddLineMappingFor(IntermediateNode node);
public abstract void RenderNode(IntermediateNode node);
public abstract void RenderNode(IntermediateNode node, IntermediateNodeWriter writer);
public abstract void RenderChildren(IntermediateNode node);
public abstract void AddLineMappingFor(IntermediateNode node);
public struct Scope : IDisposable
{
private readonly CodeRenderingContext _context;
public Scope(CodeRenderingContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
_context = context;
}
public void Dispose()
{
_context.EndScope();
}
}
public abstract void RenderChildren(IntermediateNode node, IntermediateNodeWriter writer);
}
}

View File

@ -130,28 +130,6 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
LineMappings.Add(lineMapping);
}
public override Scope CreateScope()
{
CreateScope(Current.Writer);
return new Scope(this);
}
public override Scope CreateScope(IntermediateNodeWriter writer)
{
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
_scopes.Add(new ScopeInternal(writer));
return new Scope(this);
}
public override void EndScope()
{
_scopes.RemoveAt(_scopes.Count - 1);
}
public override void RenderChildren(IntermediateNode node)
{
if (node == null)
@ -169,6 +147,30 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
_ancestors.Pop();
}
public override void RenderChildren(IntermediateNode node, IntermediateNodeWriter writer)
{
if (node == null)
{
throw new ArgumentNullException(nameof(node));
}
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
_scopes.Add(new ScopeInternal(writer));
_ancestors.Push(node);
for (var i = 0; i < node.Children.Count; i++)
{
Visitor.Visit(node.Children[i]);
}
_ancestors.Pop();
_scopes.RemoveAt(_scopes.Count - 1);
}
public override void RenderNode(IntermediateNode node)
{
if (node == null)
@ -179,6 +181,25 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
Visitor.Visit(node);
}
public override void RenderNode(IntermediateNode node, IntermediateNodeWriter writer)
{
if (node == null)
{
throw new ArgumentNullException(nameof(node));
}
if (writer == null)
{
throw new ArgumentNullException(nameof(writer));
}
_scopes.Add(new ScopeInternal(writer));
Visitor.Visit(node);
_scopes.RemoveAt(_scopes.Count - 1);
}
private struct ScopeInternal
{
public ScopeInternal(IntermediateNodeWriter writer)

View File

@ -113,13 +113,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
.WriteStringLiteral(uniqueId)
.WriteParameterSeparator();
// We remove and redirect writers so TagHelper authors can retrieve content.
using (context.CreateScope(new RuntimeNodeWriter()))
using (context.CodeWriter.BuildAsyncLambda())
{
using (context.CodeWriter.BuildAsyncLambda())
{
context.RenderChildren(node);
}
// We remove and redirect writers so TagHelper authors can retrieve content.
context.RenderChildren(node, new RuntimeNodeWriter());
}
context.CodeWriter.WriteEndMethodInvocation();
@ -239,10 +236,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
.Write(attributeValueStyleParameter)
.WriteEndMethodInvocation();
using (context.CreateScope(new TagHelperHtmlAttributeRuntimeNodeWriter()))
{
context.RenderChildren(node);
}
context.RenderChildren(node, new TagHelperHtmlAttributeRuntimeNodeWriter());
context.CodeWriter
.WriteMethodInvocation(
@ -261,10 +255,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
// We're building a writing scope around the provided chunks which captures everything written from the
// page. Therefore, we do not want to write to any other buffer since we're using the pages buffer to
// ensure we capture all content that's written, directly or indirectly.
using (context.CreateScope(new RuntimeNodeWriter()))
{
context.RenderChildren(node);
}
context.RenderChildren(node, new RuntimeNodeWriter());
context.CodeWriter
.WriteStartAssignment(StringValueBufferVariableName)
@ -367,10 +358,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
{
context.CodeWriter.WriteMethodInvocation(BeginWriteTagHelperAttributeMethodName);
using (context.CreateScope(new LiteralRuntimeNodeWriter()))
{
context.RenderChildren(node);
}
context.RenderChildren(node, new LiteralRuntimeNodeWriter());
context.CodeWriter
.WriteStartAssignment(StringValueBufferVariableName)