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

Commit 2b5f2ee

Browse files
author
N. Taylor Mullen
committed
Addressed code review comments.
1 parent f0a70dc commit 2b5f2ee

File tree

8 files changed

+117
-89
lines changed

8 files changed

+117
-89
lines changed

src/Microsoft.AspNet.Razor/Generator/Compiler/CodeBuilder/CSharp/CSharpCodeBuilder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public override CodeBuilderResult Build()
3939
writer.WriteLine("private static object @__o;");
4040
}
4141

42+
new CSharpTagHelperFieldDeclarationVisitor(writer, Context).Accept(Tree.Chunks);
4243
new CSharpHelperVisitor(writer, Context).Accept(Tree.Chunks);
4344
new CSharpTypeMemberVisitor(writer, Context).Accept(Tree.Chunks);
4445
new CSharpDesignTimeHelpersVisitor(writer, Context).AcceptTree(Tree);
@@ -52,8 +53,6 @@ public override CodeBuilderResult Build()
5253
{
5354
using (writer.BuildMethodDeclaration("public override async", "Task", Host.GeneratedClassContext.ExecuteMethodName))
5455
{
55-
new CSharpTagHelperDeclarationVisitor(writer, Context).Accept(Tree.Chunks);
56-
5756
new CSharpCodeVisitor(writer, Context).Accept(Tree.Chunks);
5857
}
5958
}

src/Microsoft.AspNet.Razor/Generator/Compiler/CodeBuilder/CSharp/CSharpTagHelperCodeRenderer.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
1616
/// </summary>
1717
public class CSharpTagHelperCodeRenderer
1818
{
19-
internal static readonly string ExecutionContextVariableName = "__tagHelperExecutionContext";
19+
internal static readonly string ExecutionContextVariableName = "__tagHelpersExecutionContext";
2020
internal static readonly string StringValueBufferVariableName = "__tagHelperStringValueBuffer";
2121
internal static readonly string ScopeManagerVariableName = "__tagHelperScopeManager";
2222
internal static readonly string RunnerVariableName = "__tagHelperRunner";
@@ -89,12 +89,12 @@ public void RenderTagHelper(TagHelperChunk chunk)
8989
{
9090
RenderRunTagHelpers(bufferedBody: false);
9191

92-
RenderTagOutput(_tagHelperContext.TagHelperOutputGenerateStartTagMethodName);
92+
RenderTagOutput(_tagHelperContext.OutputGenerateStartTagMethodName);
9393
}
9494

9595
if (contentBehavior == ContentBehavior.Prepend)
9696
{
97-
RenderTagOutput(_tagHelperContext.TagHelperOutputGenerateContentMethodName);
97+
RenderTagOutput(_tagHelperContext.OutputGenerateContentMethodName);
9898
}
9999

100100
// No need to render children if the body is being replaced anyways
@@ -108,15 +108,15 @@ public void RenderTagHelper(TagHelperChunk chunk)
108108
{
109109
RenderRunTagHelpers(bufferedBody: true);
110110

111-
RenderTagOutput(_tagHelperContext.TagHelperOutputGenerateStartTagMethodName);
111+
RenderTagOutput(_tagHelperContext.OutputGenerateStartTagMethodName);
112112
}
113113

114114
if (contentBehavior != ContentBehavior.None && contentBehavior != ContentBehavior.Prepend)
115115
{
116-
RenderTagOutput(_tagHelperContext.TagHelperOutputGenerateContentMethodName);
116+
RenderTagOutput(_tagHelperContext.OutputGenerateContentMethodName);
117117
}
118118

119-
RenderTagOutput(_tagHelperContext.TagHelperOutputGenerateEndTagMethodName);
119+
RenderTagOutput(_tagHelperContext.OutputGenerateEndTagMethodName);
120120

121121
RenderEndTagHelpersScope();
122122
}
@@ -233,6 +233,15 @@ private void RenderBoundHTMLAttributes(IDictionary<string, Chunk> chunkAttribute
233233
}
234234
else
235235
{
236+
// TODO: Make complex types in non-bufferable attributes work in
237+
// https://github.com/aspnet/Razor/issues/129
238+
if (!isPlainTextValue)
239+
{
240+
throw new InvalidOperationException(
241+
RazorResources.FormatTagHelpers_AttributesThatAreNotStringsMustNotContainAtSymbols(
242+
attributeDescriptor.AttributePropertyName));
243+
}
244+
236245
// We aren't a bufferable attribute which means we have no Razor code in our value.
237246
// Therefore we can just use the "textValue" as the attribute value.
238247
RenderRawAttributeValue(textValue, attributeDescriptor);
@@ -338,7 +347,7 @@ private void RenderRunTagHelpers(bool bufferedBody)
338347
_writer.Write(ExecutionContextVariableName)
339348
.Write(".")
340349
.Write(_tagHelperContext.ExecutionContextOutputPropertyName)
341-
.Write(" = await ")
350+
.Write(" = ")
342351
.WriteStartInstanceMethodInvocation(RunnerVariableName,
343352
_tagHelperContext.RunnerRunAsyncMethodName);
344353
_writer.Write(ExecutionContextVariableName);
@@ -349,7 +358,8 @@ private void RenderRunTagHelpers(bool bufferedBody)
349358
.Write(StringValueBufferVariableName);
350359
}
351360

352-
_writer.WriteEndMethodInvocation();
361+
_writer.WriteEndMethodInvocation(endLine: false)
362+
.WriteLine(".Result;");
353363
}
354364

355365
private void RenderBufferedAttributeValueAccessor()

src/Microsoft.AspNet.Razor/Generator/Compiler/CodeBuilder/CSharp/Visitors/CSharpTagHelperDeclarationVisitor.cs

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using Microsoft.AspNet.Razor.TagHelpers;
8+
9+
namespace Microsoft.AspNet.Razor.Generator.Compiler.CSharp
10+
{
11+
public class CSharpTagHelperFieldDeclarationVisitor : CodeVisitor<CSharpCodeWriter>
12+
{
13+
private readonly HashSet<string> _declaredTagHelpers;
14+
private readonly GeneratedTagHelperContext _tagHelperContext;
15+
private bool _foundTagHelpers;
16+
17+
public CSharpTagHelperFieldDeclarationVisitor([NotNull] CSharpCodeWriter writer,
18+
[NotNull] CodeBuilderContext context)
19+
: base(writer, context)
20+
{
21+
_declaredTagHelpers = new HashSet<string>(StringComparer.Ordinal);
22+
_tagHelperContext = Context.Host.GeneratedClassContext.GeneratedTagHelperContext;
23+
}
24+
25+
protected override void Visit(TagHelperChunk chunk)
26+
{
27+
// We only want to setup tag helper manager fields if there are tag helpers, and only once
28+
if (!_foundTagHelpers)
29+
{
30+
_foundTagHelpers = true;
31+
32+
WritePrivateField(typeof(TextWriter).FullName,
33+
CSharpTagHelperCodeRenderer.StringValueBufferVariableName,
34+
value: null);
35+
36+
WritePrivateField(_tagHelperContext.ExecutionContextTypeName,
37+
CSharpTagHelperCodeRenderer.ExecutionContextVariableName,
38+
value: null);
39+
40+
WritePrivateField(_tagHelperContext.RunnerTypeName,
41+
CSharpTagHelperCodeRenderer.RunnerVariableName,
42+
"new " + _tagHelperContext.RunnerTypeName + "()");
43+
44+
WritePrivateField(_tagHelperContext.ScopeManagerTypeName,
45+
CSharpTagHelperCodeRenderer.ScopeManagerVariableName,
46+
"new " + _tagHelperContext.ScopeManagerTypeName + "()");
47+
}
48+
49+
foreach (var descriptor in chunk.Descriptors)
50+
{
51+
if (!_declaredTagHelpers.Contains(descriptor.TagHelperName))
52+
{
53+
_declaredTagHelpers.Add(descriptor.TagHelperName);
54+
55+
WritePrivateField(descriptor.TagHelperName,
56+
CSharpTagHelperCodeRenderer.GetVariableName(descriptor),
57+
value: null);
58+
}
59+
}
60+
61+
// We need to dive deeper to ensure we pick up any nested tag helpers.
62+
Accept(chunk.Children);
63+
}
64+
65+
private void WritePrivateField(string type, string name, string value)
66+
{
67+
Writer.Write("private ")
68+
.WriteVariableDeclaration(type, name, value);
69+
}
70+
}
71+
}

src/Microsoft.AspNet.Razor/Generator/GeneratedClassContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct GeneratedClassContext
1616
public static readonly string DefaultWriteAttributeToMethodName = "WriteAttributeTo";
1717

1818
public static readonly GeneratedClassContext Default =
19-
new GeneratedClassContext(GeneratedTagHelperContext.Default,
19+
new GeneratedClassContext(new GeneratedTagHelperContext(),
2020
DefaultExecuteMethodName,
2121
DefaultWriteMethodName,
2222
DefaultWriteLiteralMethodName);

src/Microsoft.AspNet.Razor/Generator/GeneratedTagHelperContext.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,18 @@ namespace Microsoft.AspNet.Razor.Generator
88
/// </summary>
99
public class GeneratedTagHelperContext
1010
{
11-
/// <summary>
12-
/// The default <see cref="GeneratedTagHelperContext"/>.
13-
/// </summary>
14-
public static readonly GeneratedTagHelperContext Default =
15-
new GeneratedTagHelperContext();
16-
1711
/// <summary>
1812
/// Instantiates a new instance of the <see cref="GeneratedTagHelperContext"/> with default values.
1913
/// </summary>
20-
private GeneratedTagHelperContext()
14+
public GeneratedTagHelperContext()
2115
{
2216
CreateTagHelperMethodName = "CreateTagHelper";
2317
RunnerRunAsyncMethodName = "RunAsync";
2418
ScopeManagerBeginMethodName = "Begin";
2519
ScopeManagerEndMethodName = "End";
26-
TagHelperOutputGenerateStartTagMethodName = "GenerateStartTag";
27-
TagHelperOutputGenerateContentMethodName = "GenerateContent";
28-
TagHelperOutputGenerateEndTagMethodName = "GenerateEndTag";
20+
OutputGenerateStartTagMethodName = "GenerateStartTag";
21+
OutputGenerateContentMethodName = "GenerateContent";
22+
OutputGenerateEndTagMethodName = "GenerateEndTag";
2923
ExecutionContextAddMethodName = "Add";
3024
ExecutionContextAddTagHelperAttributeMethodName = "AddTagHelperAttribute";
3125
ExecutionContextAddHtmlAttributeMethodName = "AddHtmlAttribute";
@@ -62,17 +56,17 @@ private GeneratedTagHelperContext()
6256
/// <summary>
6357
/// The name of the method used to generate a tag helper output's start tag.
6458
/// </summary>
65-
public string TagHelperOutputGenerateStartTagMethodName { get; set; }
59+
public string OutputGenerateStartTagMethodName { get; set; }
6660

6761
/// <summary>
6862
/// The name of the method used to generate a tag helper outputs content.
6963
/// </summary>
70-
public string TagHelperOutputGenerateContentMethodName { get; set; }
64+
public string OutputGenerateContentMethodName { get; set; }
7165

7266
/// <summary>
7367
/// The name of the method used to generate a tag helper outputs end tag.
7468
/// </summary>
75-
public string TagHelperOutputGenerateEndTagMethodName { get; set; }
69+
public string OutputGenerateEndTagMethodName { get; set; }
7670

7771
/// <summary>
7872
/// The method in <see cref="ExecutionContextTypeName"/> used to add tag helper attributes.

src/Microsoft.AspNet.Razor/Properties/RazorResources.Designer.cs

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.AspNet.Razor/RazorResources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,7 @@ Instead, wrap the contents of the block in "{{}}":
412412
<data name="TagHelpers_TagHelperCodeGeneartorMustBeAssociatedWithATagHelperBlock" xml:space="preserve">
413413
<value>A TagHelperCodeGenerator must only be used with TagHelperBlocks.</value>
414414
</data>
415+
<data name="TagHelpers_AttributesThatAreNotStringsMustNotContainAtSymbols" xml:space="preserve">
416+
<value>TagHelper attributes that do not expect strings must not have @ symbols within them. Found attribute '{0}' with an invalid value.</value>
417+
</data>
415418
</root>

0 commit comments

Comments
 (0)