Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Addressed more code review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
NTaylorMullen committed Sep 25, 2014
1 parent 3b80ec1 commit a9b9b7a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,16 @@ public CSharpCodeWriter WriteEndMethodInvocation(bool endLine)
}

// Writes a method invocation for the given instance name.
public CSharpCodeWriter WriteInstanceMethodInvocation(string instanceName,
string methodName,
public CSharpCodeWriter WriteInstanceMethodInvocation([NotNull] string instanceName,
[NotNull] string methodName,
params string[] parameters)
{
return WriteInstanceMethodInvocation(instanceName, methodName, endLine: true, parameters: parameters);
}

// Writes a method invocation for the given instance name.
public CSharpCodeWriter WriteInstanceMethodInvocation(string instanceName,
string methodName,
public CSharpCodeWriter WriteInstanceMethodInvocation([NotNull] string instanceName,
[NotNull] string methodName,
bool endLine,
params string[] parameters)
{
Expand All @@ -238,8 +238,8 @@ public CSharpCodeWriter WriteInstanceMethodInvocation(string instanceName,
parameters);
}

public CSharpCodeWriter WriteStartInstanceMethodInvocation(string instanceName,
string methodName)
public CSharpCodeWriter WriteStartInstanceMethodInvocation([NotNull] string instanceName,
[NotNull] string methodName)
{
return WriteStartMethodInvocation(
string.Format(CultureInfo.InvariantCulture, InstanceMethodFormat, instanceName, methodName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
/// </summary>
public class CSharpTagHelperCodeRenderer
{
internal static readonly string TagHelperScopeManagerVariableName = "__tagHelperScopeManager";
internal static readonly string TagHelperRunnerVariableName = "__tagHelperRunner";
internal static readonly string TagHelperExecutionContextVariableName = "__executionContext";
internal static readonly string BufferedStringValueVariableName = "__tagHelperBufferedStringValue";
internal static readonly string DefaultTagHelperScopeManagerVariableName = "__tagHelperScopeManager";
internal static readonly string DefaultTagHelperRunnerVariableName = "__tagHelperRunner";
internal static readonly string DefaultTagHelperExecutionContextVariableName = "__executionContext";
internal static readonly string DefaultBufferedStringValueVariableName = "__tagHelperBufferedStringValue";

private static readonly TypeInfo StringTypeInfo = typeof(string).GetTypeInfo();
private static readonly TagHelperAttributeDescriptorComparer AttributeDescriptorComparer =
Expand All @@ -37,8 +37,8 @@ public class CSharpTagHelperCodeRenderer
/// Instantiates a new <see cref="CSharpTagHelperCodeRenderer"/>.
/// </summary>
/// <param name="bodyVisitor">The <see cref="IChunkVisitor"/> used to render chunks found in the body.</param>
/// <param name="writer">The <see cref="CSharpCodeWriter"/> that's used to write code.</param>
/// <param name="context">A <see cref="CodeGeneratorContext"/> instance that contains information about
/// <param name="writer">The <see cref="CSharpCodeWriter"/> used to write code.</param>
/// <param name="context">A <see cref="CodeBuilderContext"/> instance that contains information about
/// the current code generation process.</param>
public CSharpTagHelperCodeRenderer([NotNull] IChunkVisitor bodyVisitor,
[NotNull] CSharpCodeWriter writer,
Expand Down Expand Up @@ -66,7 +66,7 @@ public void RenderTagHelper(TagHelperChunk chunk)

// Find the first content behavior that doesn't have a content behavior of None.
// The resolver restricts content behavior collisions so the first one that's not None will be
// the content behavior we need to abide by.S
// the content behavior we need to abide by. None can work in unison with other ContentBehaviors.
var contentBehavior = tagHelperDescriptors.Select(descriptor => descriptor.ContentBehavior)
.FirstOrDefault(
behavior => behavior != ContentBehavior.None);
Expand Down Expand Up @@ -98,7 +98,11 @@ public void RenderTagHelper(TagHelperChunk chunk)
RenderTagOutput(_tagHelperContext.TagHelperOutputGenerateTagContentMethodName);
}

RenderTagHelperBody(chunk.Children, contentBehavior);
// No need to render children if the body is being replaced anyways
if (contentBehavior != ContentBehavior.Replace)
{
RenderTagHelperBody(chunk.Children, contentBehavior);
}

// If content behavior is modify then we need to execute AFTER now (after the children)
// and then render the start tag.
Expand All @@ -121,18 +125,15 @@ public void RenderTagHelper(TagHelperChunk chunk)

internal static string GetVariableName(TagHelperDescriptor descriptor)
{
return string.Format(CultureInfo.InvariantCulture,
"__{0}_{1}",
descriptor.TagName.Replace('-', '_'),
descriptor.TagHelperName.Replace('.', '_'));
return "__" + descriptor.TagHelperName.Replace('.', '_');
}

private void RenderBeginTagHelperScope(string tagName, ContentBehavior contentBehavior)
{
// Call into the tag helper scope manager to start a new tag helper scope.
// Also capture the value as the current execution context.
_writer.WriteStartAssignment(TagHelperExecutionContextVariableName)
.WriteStartInstanceMethodInvocation(TagHelperScopeManagerVariableName,
_writer.WriteStartAssignment(DefaultTagHelperExecutionContextVariableName)
.WriteStartInstanceMethodInvocation(DefaultTagHelperScopeManagerVariableName,
_tagHelperContext.TagHelperScopeManagerBeginMethodName);
_writer.WriteStringLiteral(tagName)
.WriteEndMethodInvocation();
Expand All @@ -158,7 +159,7 @@ private void RenderTagHelpersCreation(TagHelperChunk chunk)
tagHelperDescriptor.TagHelperName)
.WriteEndMethodInvocation();

_writer.WriteInstanceMethodInvocation(TagHelperExecutionContextVariableName,
_writer.WriteInstanceMethodInvocation(DefaultTagHelperExecutionContextVariableName,
_tagHelperContext.ExecutionContextAddMethodName,
tagHelperVariableName);

Expand Down Expand Up @@ -244,7 +245,7 @@ private void RenderBoundHTMLAttributes(IDictionary<string, Chunk> chunkAttribute

// We need to inform the context of the attribute value.
_writer.WriteStartInstanceMethodInvocation(
TagHelperExecutionContextVariableName,
DefaultTagHelperExecutionContextVariableName,
_tagHelperContext.ExecutionContextAddTagHelperAttributeMethodName);

_writer.WriteStringLiteral(attributeDescriptor.AttributeName)
Expand Down Expand Up @@ -278,7 +279,7 @@ private void RenderUnboundHTMLAttributes(IEnumerable<KeyValuePair<string, Chunk>
BuildBufferedAttributeValue(attributeValue);
}

_writer.WriteStartInstanceMethodInvocation(TagHelperExecutionContextVariableName,
_writer.WriteStartInstanceMethodInvocation(DefaultTagHelperExecutionContextVariableName,
_tagHelperContext.ExecutionContextAddHtmlAttributeMethodName);
_writer.WriteStringLiteral(htmlAttribute.Key)
.WriteParameterSeparator();
Expand All @@ -299,38 +300,34 @@ private void RenderUnboundHTMLAttributes(IEnumerable<KeyValuePair<string, Chunk>

private void RenderTagHelperBody(IList<Chunk> children, ContentBehavior contentBehavior)
{
// No need to render children if the body is being replaced anyways
if (contentBehavior != ContentBehavior.Replace)
// If content behavior is modify we need to use a different writer to buffer the body.
if (contentBehavior == ContentBehavior.Modify)
{
// If content behavior is modify we need to use a different writer to buffer the body.
if (contentBehavior == ContentBehavior.Modify)
{
BuildBufferedWritingScope(() =>
{
// Render all of the tag helper children
_bodyVisitor.Accept(children);
});
}
else
BuildBufferedWritingScope(() =>
{
// Render all of the tag helper children
_bodyVisitor.Accept(children);
}
});
}
else
{
// Render all of the tag helper children
_bodyVisitor.Accept(children);
}
}

private void RenderEndTagHelpersScope()
{
_writer.WriteStartAssignment(TagHelperExecutionContextVariableName)
.WriteInstanceMethodInvocation(TagHelperScopeManagerVariableName,
_writer.WriteStartAssignment(DefaultTagHelperExecutionContextVariableName)
.WriteInstanceMethodInvocation(DefaultTagHelperScopeManagerVariableName,
_tagHelperContext.TagHelperScopeManagerEndMethodName);
}

private void RenderTagOutput(string tagOutputMethodName)
{
CSharpCodeVisitor.RenderPreWriteStart(_writer, _context);

_writer.Write(TagHelperExecutionContextVariableName)
_writer.Write(DefaultTagHelperExecutionContextVariableName)
.Write(".")
.Write(_tagHelperContext.ExecutionContextOutputPropertyName)
.Write(".")
Expand All @@ -340,26 +337,26 @@ private void RenderTagOutput(string tagOutputMethodName)

private void RenderRunTagHelpers(bool bufferedBody)
{
_writer.Write(TagHelperExecutionContextVariableName)
_writer.Write(DefaultTagHelperExecutionContextVariableName)
.Write(".")
.Write(_tagHelperContext.ExecutionContextOutputPropertyName)
.Write(" = await ")
.WriteStartInstanceMethodInvocation(TagHelperRunnerVariableName,
.WriteStartInstanceMethodInvocation(DefaultTagHelperRunnerVariableName,
_tagHelperContext.TagHelperRunnerRunAsyncMethodName);
_writer.Write(TagHelperExecutionContextVariableName);
_writer.Write(DefaultTagHelperExecutionContextVariableName);

if(bufferedBody)
if (bufferedBody)
{
_writer.WriteParameterSeparator()
.Write(BufferedStringValueVariableName);
.Write(DefaultBufferedStringValueVariableName);
}

_writer.WriteEndMethodInvocation();
}

private void RenderBufferedAttributeValueAccessor()
{
_writer.WriteInstanceMethodInvocation(BufferedStringValueVariableName,
_writer.WriteInstanceMethodInvocation(DefaultBufferedStringValueVariableName,
"ToString",
endLine: false);
}
Expand Down Expand Up @@ -415,7 +412,7 @@ private void BuildBufferedWritingScope(Action renderCode)
_writer.Write("finally");
using (_writer.BuildScope())
{
_writer.WriteStartAssignment(BufferedStringValueVariableName)
_writer.WriteStartAssignment(DefaultBufferedStringValueVariableName)
.WriteMethodInvocation(_tagHelperContext.EndWritingScopeMethodName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ protected override void Visit(TagHelperChunk chunk)
_foundTagHelpers = true;

WriteActivatedProperty(_tagHelperContext.TagHelperRunnerTypeName,
CSharpTagHelperCodeRenderer.TagHelperRunnerVariableName);
CSharpTagHelperCodeRenderer.DefaultTagHelperRunnerVariableName);
WriteActivatedProperty(_tagHelperContext.TagHelperScopeManagerTypeName,
CSharpTagHelperCodeRenderer.TagHelperScopeManagerVariableName);
CSharpTagHelperCodeRenderer.DefaultTagHelperScopeManagerVariableName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ protected override void Visit(TagHelperChunk chunk)
_foundTagHelpers = true;

Writer.WriteVariableDeclaration("var",
CSharpTagHelperCodeRenderer.BufferedStringValueVariableName,
CSharpTagHelperCodeRenderer.DefaultBufferedStringValueVariableName,
"string.Empty");
Writer.WriteVariableDeclaration(_tagHelperContext.TagHelperExecutionContextTypeName,
CSharpTagHelperCodeRenderer.TagHelperExecutionContextVariableName,
CSharpTagHelperCodeRenderer.DefaultTagHelperExecutionContextVariableName,
null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public class TagHelperCodeGenerator : BlockCodeGenerator
/// <summary>
/// Instantiates a new <see cref="TagHelperCodeGenerator"/>.
/// </summary>
/// <param name="tagHelperDescriptors"><see cref="TagHelperDescriptor"/>s associated with the current tag
/// helper</param>
/// <param name="tagHelperDescriptors">
/// <see cref="TagHelperDescriptor"/>s associated with the current HTML tag.
/// </param>
public TagHelperCodeGenerator(IEnumerable<TagHelperDescriptor> tagHelperDescriptors)
{
_tagHelperDescriptors = tagHelperDescriptors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class TagHelperAttributeValueCodeRenderer
/// </remarks>
public void RenderAttributeValue([NotNull] TagHelperAttributeDescriptor attributeDescriptor,
[NotNull] CSharpCodeWriter writer,
[NotNull] CodeGeneratorContext context,
[NotNull] CodeBuilderContext context,
[NotNull] Action<CSharpCodeWriter> renderAttributeValue)
{
renderAttributeValue(writer);
Expand Down

0 comments on commit a9b9b7a

Please sign in to comment.