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

Fixes #1245 - Make TemplateCodeExtension public #1357

Merged
merged 1 commit into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;

namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
{
Expand All @@ -15,6 +16,10 @@ public static void Register(IRazorEngineBuilder builder)
PageDirective.Register(builder);

builder.AddTargetExtension(new InjectDirectiveTargetExtension());
builder.AddTargetExtension(new TemplateTargetExtension()
{
TemplateTypeName = "global::Microsoft.AspNetCore.Mvc.Razor.HelperResult",
});

builder.Features.Add(new ModelExpressionPass());
builder.Features.Add(new PagesPropertyInjectionPass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public TagHelperWriterScope Push(TagHelperWriter writer)
return scope;
}

public void ReportMissingExtension<TExtension>() where TExtension : ICodeTargetExtension
{
var documentKind = CodeDocument.GetIRDocument()?.DocumentKind ?? string.Empty;
Diagnostics.Add(RazorDiagnosticFactory.CreateCodeTarget_UnsupportedExtension(documentKind, typeof(TExtension)));
}

internal TagHelperRenderingContextScope Push(TagHelperRenderingContext context)
{
if (context == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.AspNetCore.Razor.Language.Legacy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Globalization;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.AspNetCore.Razor.Language.Legacy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Microsoft.AspNetCore.Razor.Language.Legacy;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// 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.Language.Intermediate;
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;

namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
namespace Microsoft.AspNetCore.Razor.Language.Extensions
{
internal interface ITemplateTargetExtension : ICodeTargetExtension
public interface ITemplateTargetExtension : ICodeTargetExtension
{
void WriteTemplate(CSharpRenderingContext context, TemplateIRNode node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
using Microsoft.AspNetCore.Razor.Language.Intermediate;

namespace Microsoft.AspNetCore.Razor.Language.Intermediate
namespace Microsoft.AspNetCore.Razor.Language.Extensions
{
public sealed class TemplateIRNode : ExtensionIRNode
{
Expand All @@ -28,6 +29,12 @@ public override void Accept(RazorIRNodeVisitor visitor)
public override void WriteNode(CodeTarget target, CSharpRenderingContext context)
{
var extension = target.GetExtension<ITemplateTargetExtension>();
if (extension == null)
{
context.ReportMissingExtension<ITemplateTargetExtension>();
return;
}

extension.WriteTemplate(context, this);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// 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.Language.Intermediate;
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;

namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
namespace Microsoft.AspNetCore.Razor.Language.Extensions
{
internal class TemplateTargetExtension : ITemplateTargetExtension
public class TemplateTargetExtension : ITemplateTargetExtension
{
public static readonly string DefaultTemplateTypeName = "Microsoft.AspNetCore.Mvc.Razor.HelperResult";
public static readonly string DefaultTemplateTypeName = "Template";
public static readonly string DefaultPushWriterMethod = "PushWriter";
public static readonly string DefaultPopWriterMethod = "PopWriter";

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 16 additions & 12 deletions src/Microsoft.AspNetCore.Razor.Language/RazorDiagnosticFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// 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;

namespace Microsoft.AspNetCore.Razor.Language
{
internal static class RazorDiagnosticFactory
Expand All @@ -9,33 +11,35 @@ internal static class RazorDiagnosticFactory

#region General Errors

/*
* General Errors ID Offset = 0
*/
// General Errors ID Offset = 0

#endregion

#region Language Errors

/*
* Language Errors ID Offset = 1000
*/
// Language Errors ID Offset = 1000

#endregion

#region Semantic Errors

/*
* Semantic Errors ID Offset = 2000
*/
// Semantic Errors ID Offset = 2000

public static readonly RazorDiagnosticDescriptor CodeTarget_UnsupportedExtension =
new RazorDiagnosticDescriptor(
$"{DiagnosticPrefix}2000",
() => Resources.Diagnostic_CodeTarget_UnsupportedExtension,
RazorDiagnosticSeverity.Error);
public static RazorDiagnostic CreateCodeTarget_UnsupportedExtension(string documentKind, Type extensionType)
{
return RazorDiagnostic.Create(CodeTarget_UnsupportedExtension, SourceSpan.Undefined, documentKind, extensionType.Name);
}

#endregion

#region TagHelper Errors

/*
* TagHelper Errors ID Offset = 3000
*/
// TagHelper Errors ID Offset = 3000

private static readonly RazorDiagnosticDescriptor TagHelper_InvalidRestrictedChildNullOrWhitespace =
new RazorDiagnosticDescriptor(
Expand Down
1 change: 0 additions & 1 deletion src/Microsoft.AspNetCore.Razor.Language/RazorEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ internal static void AddDefaults(IRazorEngineBuilder builder)
builder.Features.Add(new DirectiveRemovalIROptimizationPass());

// Default Runtime Targets
builder.AddTargetExtension(new TemplateTargetExtension());
builder.AddTargetExtension(new PreallocatedAttributeTargetExtension());

// Default configuration
Expand Down
3 changes: 3 additions & 0 deletions src/Microsoft.AspNetCore.Razor.Language/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,7 @@
<data name="FeatureMustBeInitialized" xml:space="preserve">
<value>The feature must be initialized by setting the '{0}' property.</value>
</data>
<data name="Diagnostic_CodeTarget_UnsupportedExtension" xml:space="preserve">
<value>The document type '{0}' does not support the extension '{1}'.</value>
</data>
</root>
2 changes: 2 additions & 0 deletions src/Microsoft.AspNetCore.Razor.Language/SourceSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Microsoft.AspNetCore.Razor.Language
{
public struct SourceSpan : IEquatable<SourceSpan>
{
public static readonly SourceSpan Undefined = new SourceSpan(SourceLocation.Undefined, 0);

public SourceSpan(int absoluteIndex, int length)
: this(null, absoluteIndex, -1, -1, length)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Microsoft.AspNetCore.Razor.Language.IntegrationTests;
using Xunit;

Expand Down Expand Up @@ -46,6 +47,9 @@ public void BasicTest()
{
b.AddTagHelpers(descriptors);
b.Features.Add(new InstrumentationPass());

// This test includes templates
b.AddTargetExtension(new TemplateTargetExtension());
});

var document = CreateCodeDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class Template
EndContext();
BeginContext(217, 29, false);
#line 9 "TestFiles/IntegrationTests/InstrumentationPassIntegrationTest/BasicTest.cshtml"
Write(Foo(item => new Microsoft.AspNetCore.Mvc.Razor.HelperResult(async(__razor_template_writer) => {
Write(Foo(item => new Template(async(__razor_template_writer) => {
PushWriter(__razor_template_writer);
BeginContext(222, 24, true);
WriteLiteral("<span>Hello world</span>");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// 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.Language.Intermediate;
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
using Microsoft.AspNetCore.Razor.Language.Legacy;
using Xunit;

namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration
namespace Microsoft.AspNetCore.Razor.Language.Extensions
{
public class TemplateTargetExtensionTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.Language.Extensions;
using Xunit;

namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
Expand Down Expand Up @@ -868,7 +869,14 @@ public void TagHelpersWithTemplate_DesignTime()
private void DesignTimeTest()
{
// Arrange
var engine = RazorEngine.CreateDesignTime(builder => builder.Features.Add(new ApiSetsIRTestAdapter()));
var engine = RazorEngine.CreateDesignTime(builder =>
{
builder.Features.Add(new ApiSetsIRTestAdapter());

// Some of these tests use templates
builder.AddTargetExtension(new TemplateTargetExtension());
});

var document = CreateCodeDocument();

// Act
Expand All @@ -883,7 +891,14 @@ private void DesignTimeTest()
private void RunTimeTest()
{
// Arrange
var engine = RazorEngine.Create(builder => builder.Features.Add(new ApiSetsIRTestAdapter()));
var engine = RazorEngine.Create(builder =>
{
builder.Features.Add(new ApiSetsIRTestAdapter());

// Some of these tests use templates
builder.AddTargetExtension(new TemplateTargetExtension());
});

var document = CreateCodeDocument();

// Act
Expand All @@ -901,6 +916,9 @@ private void RunRuntimeTagHelpersTest(IEnumerable<TagHelperDescriptor> descripto
{
builder.Features.Add(new ApiSetsIRTestAdapter());
builder.AddTagHelpers(descriptors);

// Some of these tests use templates
builder.AddTargetExtension(new TemplateTargetExtension());
});

var document = CreateCodeDocument();
Expand All @@ -920,6 +938,9 @@ private void RunDesignTimeTagHelpersTest(IEnumerable<TagHelperDescriptor> descri
{
builder.Features.Add(new ApiSetsIRTestAdapter());
builder.AddTagHelpers(descriptors);

// Some of these tests use templates
builder.AddTargetExtension(new TemplateTargetExtension());
});

var document = CreateCodeDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ private static void AssertDefaultTargetExtensions(RazorEngine engine)
var feature = engine.Features.OfType<IRazorTargetExtensionFeature>().FirstOrDefault();
Assert.NotNull(feature);

Assert.Collection(
feature.TargetExtensions,
f => Assert.IsType<TemplateTargetExtension>(f));
Assert.Empty(feature.TargetExtensions);
}

private static void AssertDefaultRuntimeFeatures(IEnumerable<IRazorEngineFeature> features)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public async System.Threading.Tasks.Task ExecuteAsync()
#line default
#line hidden
#line 35 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml"
__o = someMethod(item => new Microsoft.AspNetCore.Mvc.Razor.HelperResult(async(__razor_template_writer) => {
__o = someMethod(item => new Template(async(__razor_template_writer) => {
__TestNamespace_InputTagHelper = CreateTagHelper<global::TestNamespace.InputTagHelper>();
__TestNamespace_InputTagHelper2 = CreateTagHelper<global::TestNamespace.InputTagHelper2>();
#line 35 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,23 +271,23 @@ Generated Location: (8375:173,9 [11] )

Source Location: (1410:34,64 [7] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml)
|checked|
Generated Location: (8828:177,63 [7] )
Generated Location: (8793:177,63 [7] )
|checked|

Source Location: (1375:34,29 [3] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml)
|123|
Generated Location: (9083:183,33 [3] )
Generated Location: (9048:183,33 [3] )
|123|

Source Location: (1424:34,78 [1] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml)
|)|
Generated Location: (9124:188,1 [1] )
Generated Location: (9089:188,1 [1] )
|)|

Source Location: (1437:35,10 [3] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml)
|
}|
Generated Location: (9263:193,10 [3] )
Generated Location: (9228:193,10 [3] )
|
}|

Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ public async System.Threading.Tasks.Task ExecuteAsync()
__tagHelperExecutionContext = __tagHelperScopeManager.End();
WriteLiteral("\r\n ");
#line 35 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/ComplexTagHelpers.cshtml"
Write(someMethod(item => new Microsoft.AspNetCore.Mvc.Razor.HelperResult(async(__razor_template_writer) => {
Write(someMethod(item => new Template(async(__razor_template_writer) => {
PushWriter(__razor_template_writer);
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("p", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "test", async() => {
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("input", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, "test", async() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async System.Threading.Tasks.Task ExecuteAsync()
#line default
#line hidden
#line 9 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/DesignTime.cshtml"
__o = Foo(item => new Microsoft.AspNetCore.Mvc.Razor.HelperResult(async(__razor_template_writer) => {
__o = Foo(item => new Template(async(__razor_template_writer) => {
#line 9 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/DesignTime.cshtml"
__o = baz;

Expand Down
Loading