This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved DeclareTagHelperFields from renderer to writer
- Loading branch information
1 parent
ac176f8
commit 7d8bd29
Showing
10 changed files
with
292 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/RuntimeTagHelperWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.AspNetCore.Razor.Evolution.Intermediate; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration | ||
{ | ||
public class RuntimeTagHelperWriter : TagHelperWriter | ||
{ | ||
public string StringValueBufferVariableName { get; set; } = "__tagHelperStringValueBuffer"; | ||
|
||
public string ExecutionContextTypeName { get; set; } = "Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext"; | ||
|
||
public string ExecutionContextVariableName { get; set; } = "__tagHelperExecutionContext"; | ||
|
||
public string RunnerTypeName { get; set; } = "Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner"; | ||
|
||
public string RunnerVariableName { get; set; } = "__tagHelperRunner"; | ||
|
||
public string ScopeManagerTypeName { get; set; } = "Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager"; | ||
|
||
public string ScopeManagerVariableName { get; set; } = "__tagHelperScopeManager"; | ||
|
||
public string StartTagHelperWritingScopeMethodName { get; set; } = "StartTagHelperWritingScope"; | ||
|
||
public string EndTagHelperWritingScopeMethodName { get; set; } = "EndTagHelperWritingScope"; | ||
|
||
public override void WriteDeclareTagHelperFields(CSharpRenderingContext context, DeclareTagHelperFieldsIRNode node) | ||
{ | ||
context.Writer.WriteLineHiddenDirective(); | ||
|
||
// Need to disable the warning "X is assigned to but never used." for the value buffer since | ||
// whether it's used depends on how a TagHelper is used. | ||
context.Writer | ||
.WritePragma("warning disable 0414") | ||
.Write("private ") | ||
.WriteVariableDeclaration("string", StringValueBufferVariableName, value: null) | ||
.WritePragma("warning restore 0414"); | ||
|
||
context.Writer | ||
.Write("private global::") | ||
.WriteVariableDeclaration( | ||
ExecutionContextTypeName, | ||
ExecutionContextVariableName, | ||
value: null); | ||
|
||
context.Writer | ||
.Write("private global::") | ||
.Write(RunnerTypeName) | ||
.Write(" ") | ||
.Write(RunnerVariableName) | ||
.Write(" = new global::") | ||
.Write(RunnerTypeName) | ||
.WriteLine("();"); | ||
|
||
var backedScopeManageVariableName = "__backed" + ScopeManagerVariableName; | ||
context.Writer | ||
.Write("private global::") | ||
.WriteVariableDeclaration( | ||
ScopeManagerTypeName, | ||
backedScopeManageVariableName, | ||
value: null); | ||
|
||
context.Writer | ||
.Write("private global::") | ||
.Write(ScopeManagerTypeName) | ||
.Write(" ") | ||
.WriteLine(ScopeManagerVariableName); | ||
|
||
using (context.Writer.BuildScope()) | ||
{ | ||
context.Writer.WriteLine("get"); | ||
using (context.Writer.BuildScope()) | ||
{ | ||
context.Writer | ||
.Write("if (") | ||
.Write(backedScopeManageVariableName) | ||
.WriteLine(" == null)"); | ||
|
||
using (context.Writer.BuildScope()) | ||
{ | ||
context.Writer | ||
.WriteStartAssignment(backedScopeManageVariableName) | ||
.WriteStartNewObject(ScopeManagerTypeName) | ||
.Write(StartTagHelperWritingScopeMethodName) | ||
.WriteParameterSeparator() | ||
.Write(EndTagHelperWritingScopeMethodName) | ||
.WriteEndMethodInvocation(); | ||
} | ||
|
||
context.Writer.WriteReturn(backedScopeManageVariableName); | ||
} | ||
} | ||
|
||
foreach (var tagHelperTypeName in node.UsedTagHelperTypeNames) | ||
{ | ||
var tagHelperVariableName = GetTagHelperVariableName(tagHelperTypeName); | ||
context.Writer | ||
.Write("private global::") | ||
.WriteVariableDeclaration( | ||
tagHelperTypeName, | ||
tagHelperVariableName, | ||
value: null); | ||
} | ||
} | ||
|
||
public override void WriteAddTagHelperHtmlAttribute(CSharpRenderingContext context, AddTagHelperHtmlAttributeIRNode node) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void WriteCreateTagHelper(CSharpRenderingContext context, CreateTagHelperIRNode node) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void WriteExecuteTagHelpers(CSharpRenderingContext context, ExecuteTagHelpersIRNode node) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void WriteInitializeTagHelperStructure(CSharpRenderingContext context, InitializeTagHelperStructureIRNode node) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public override void WriteSetTagHelperProperty(CSharpRenderingContext context, SetTagHelperPropertyIRNode node) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private static string GetTagHelperVariableName(string tagHelperTypeName) => "__" + tagHelperTypeName.Replace('.', '_'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...Microsoft.AspNetCore.Razor.Evolution.Test/CodeGeneration/DesignTimeTagHelperWriterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Razor.Evolution.Intermediate; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration | ||
{ | ||
public class DesignTimeTagHelperWriterTest | ||
{ | ||
[Fact] | ||
public void WriteDeclareTagHelperFields_DeclaresUsedTagHelperTypes() | ||
{ | ||
// Arrange | ||
var writer = new DesignTimeTagHelperWriter(); | ||
var context = new CSharpRenderingContext() | ||
{ | ||
Writer = new Legacy.CSharpCodeWriter(), | ||
}; | ||
var node = new DeclareTagHelperFieldsIRNode(); | ||
node.UsedTagHelperTypeNames.Add("PTagHelper"); | ||
node.UsedTagHelperTypeNames.Add("MyTagHelper"); | ||
|
||
// Act | ||
writer.WriteDeclareTagHelperFields(context, node); | ||
|
||
// Assert | ||
var csharp = context.Writer.Builder.ToString(); | ||
Assert.Equal( | ||
@"private global::PTagHelper __PTagHelper = null; | ||
private global::MyTagHelper __MyTagHelper = null; | ||
", | ||
csharp, | ||
ignoreLineEndingDifferences: true); | ||
} | ||
} | ||
} |
Oops, something went wrong.