diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/IInjectTargetExtension.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/IInjectTargetExtension.cs index 52352987dc..89f74d5aaa 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/IInjectTargetExtension.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/IInjectTargetExtension.cs @@ -7,6 +7,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions { public interface IInjectTargetExtension : ICodeTargetExtension { - void WriteInjectProperty(CSharpRenderingContext context, InjectIntermediateNode node); + void WriteInjectProperty(CodeRenderingContext context, InjectIntermediateNode node); } } diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectIntermediateNode.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectIntermediateNode.cs index e1b3178e60..fb6fc261f3 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectIntermediateNode.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectIntermediateNode.cs @@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions AcceptExtensionNode(this, visitor); } - public override void WriteNode(CodeTarget target, CSharpRenderingContext context) + public override void WriteNode(CodeTarget target, CodeRenderingContext context) { if (target == null) { @@ -41,7 +41,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions var extension = target.GetExtension(); if (extension == null) { - context.ReportMissingExtension(); + ReportMissingCodeTargetExtension(context); return; } diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectTargetExtension.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectTargetExtension.cs index 22167a3c7e..9569cca41f 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectTargetExtension.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/InjectTargetExtension.cs @@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions { private const string RazorInjectAttribute = "[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]"; - public void WriteInjectProperty(CSharpRenderingContext context, InjectIntermediateNode node) + public void WriteInjectProperty(CodeRenderingContext context, InjectIntermediateNode node) { if (context == null) { @@ -26,16 +26,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions if (node.Source.HasValue) { - using (context.Writer.BuildLinePragma(node.Source.Value)) + using (context.CodeWriter.BuildLinePragma(node.Source.Value)) { - context.Writer + context.CodeWriter .WriteLine(RazorInjectAttribute) .WriteLine(property); } } else { - context.Writer + context.CodeWriter .WriteLine(RazorInjectAttribute) .WriteLine(property); } diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperPass.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperPass.cs index 956413b66e..aca2e80833 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperPass.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperPass.cs @@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions private void GenerateVCTHClass(ClassDeclarationIntermediateNode @class, TagHelperDescriptor tagHelper) { - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); WriteClass(writer, tagHelper); var statement = new CSharpCodeIntermediateNode(); @@ -93,7 +93,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions return $"__Generated__{vcName}ViewComponentTagHelper"; } - private void WriteClass(CSharpCodeWriter writer, TagHelperDescriptor descriptor) + private void WriteClass(CodeWriter writer, TagHelperDescriptor descriptor) { // Add target element. BuildTargetElementString(writer, descriptor); @@ -121,7 +121,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions } } - private void BuildConstructorString(CSharpCodeWriter writer, string className) + private void BuildConstructorString(CodeWriter writer, string className) { writer.Write("public ") .Write(className) @@ -136,7 +136,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions } } - private void BuildAttributeDeclarations(CSharpCodeWriter writer, TagHelperDescriptor descriptor) + private void BuildAttributeDeclarations(CodeWriter writer, TagHelperDescriptor descriptor) { writer.Write("[") .Write("Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeNotBoundAttribute") @@ -165,7 +165,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions } } - private void BuildProcessMethodString(CSharpCodeWriter writer, TagHelperDescriptor descriptor) + private void BuildProcessMethodString(CodeWriter writer, TagHelperDescriptor descriptor) { var contextVariable = "context"; var outputVariable = "output"; @@ -210,7 +210,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions return methodParameters; } - private void BuildTargetElementString(CSharpCodeWriter writer, TagHelperDescriptor descriptor) + private void BuildTargetElementString(CodeWriter writer, TagHelperDescriptor descriptor) { Debug.Assert(descriptor.TagMatchingRules.Count() == 1); diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/BasicWriter.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/BasicWriter.cs deleted file mode 100644 index 47074193f3..0000000000 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/BasicWriter.cs +++ /dev/null @@ -1,30 +0,0 @@ -// 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; - -namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration -{ - public abstract class BasicWriter - { - public abstract void WriteUsingDirective(CSharpRenderingContext context, UsingDirectiveIntermediateNode node); - - public abstract void WriteCSharpExpression(CSharpRenderingContext context, CSharpExpressionIntermediateNode node); - - public abstract void WriteCSharpCode(CSharpRenderingContext context, CSharpCodeIntermediateNode node); - - public abstract void WriteHtmlContent(CSharpRenderingContext context, HtmlContentIntermediateNode node); - - public abstract void WriteHtmlAttribute(CSharpRenderingContext context, HtmlAttributeIntermediateNode node); - - public abstract void WriteHtmlAttributeValue(CSharpRenderingContext context, HtmlAttributeValueIntermediateNode node); - - public abstract void WriteCSharpExpressionAttributeValue(CSharpRenderingContext context, CSharpExpressionAttributeValueIntermediateNode node); - - public abstract void WriteCSharpCodeAttributeValue(CSharpRenderingContext context, CSharpCodeAttributeValueIntermediateNode node); - - public abstract void BeginWriterScope(CSharpRenderingContext context, string writer); - - public abstract void EndWriterScope(CSharpRenderingContext context); - } -} diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CSharpRenderingContext.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CSharpRenderingContext.cs deleted file mode 100644 index f1e0357262..0000000000 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CSharpRenderingContext.cs +++ /dev/null @@ -1,178 +0,0 @@ -// 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 System.Collections.Generic; -using Microsoft.AspNetCore.Razor.Language.Intermediate; - -namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration -{ - public class CSharpRenderingContext - { - internal ICollection Directives { get; set; } - - internal Func IdGenerator { get; set; } = () => Guid.NewGuid().ToString("N"); - - internal List LineMappings { get; } = new List(); - - public CSharpCodeWriter Writer { get; set; } - - internal IList Diagnostics { get; } = new List(); - - internal RazorCodeDocument CodeDocument { get; set; } - - internal RazorSourceDocument SourceDocument => CodeDocument?.Source; - - internal RazorCodeGenerationOptions Options { get; set; } - - internal TagHelperRenderingContext TagHelperRenderingContext { get; set; } - - internal Action RenderChildren { get; set; } - - internal Action RenderNode { get; set; } - - public BasicWriter BasicWriter { get; set; } - - public TagHelperWriter TagHelperWriter { get; set; } - - public void AddLineMappingFor(IntermediateNode node) - { - if (node.Source == null) - { - return; - } - - if (SourceDocument.FilePath != null && - !string.Equals(SourceDocument.FilePath, node.Source.Value.FilePath, StringComparison.OrdinalIgnoreCase)) - { - // We don't want to generate line mappings for imports. - return; - } - - var source = node.Source.Value; - - var generatedLocation = new SourceSpan(Writer.Location, source.Length); - var lineMapping = new LineMapping(source, generatedLocation); - - LineMappings.Add(lineMapping); - } - - public BasicWriterScope Push(BasicWriter writer) - { - if (writer == null) - { - throw new ArgumentNullException(nameof(writer)); - } - - var scope = new BasicWriterScope(this, BasicWriter); - BasicWriter = writer; - return scope; - } - - public TagHelperWriterScope Push(TagHelperWriter writer) - { - if (writer == null) - { - throw new ArgumentNullException(nameof(writer)); - } - - var scope = new TagHelperWriterScope(this, TagHelperWriter); - TagHelperWriter = writer; - return scope; - } - - public void ReportMissingExtension() where TExtension : ICodeTargetExtension - { - var documentKind = CodeDocument.GetDocumentIntermediateNode()?.DocumentKind ?? string.Empty; - Diagnostics.Add(RazorDiagnosticFactory.CreateCodeTarget_UnsupportedExtension(documentKind, typeof(TExtension))); - } - - internal TagHelperRenderingContextScope Push(TagHelperRenderingContext context) - { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - var scope = new TagHelperRenderingContextScope(this, TagHelperRenderingContext); - TagHelperRenderingContext = context; - return scope; - } - - public struct BasicWriterScope : IDisposable - { - private readonly CSharpRenderingContext _context; - private readonly BasicWriter _writer; - - public BasicWriterScope(CSharpRenderingContext context, BasicWriter writer) - { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - if (writer == null) - { - throw new ArgumentNullException(nameof(writer)); - } - - _context = context; - _writer = writer; - } - - public void Dispose() - { - _context.BasicWriter = _writer; - } - } - - public struct TagHelperWriterScope : IDisposable - { - private readonly CSharpRenderingContext _context; - private readonly TagHelperWriter _writer; - - public TagHelperWriterScope(CSharpRenderingContext context, TagHelperWriter writer) - { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - if (writer == null) - { - throw new ArgumentNullException(nameof(writer)); - } - - _context = context; - _writer = writer; - } - - public void Dispose() - { - _context.TagHelperWriter = _writer; - } - } - - internal struct TagHelperRenderingContextScope : IDisposable - { - private readonly CSharpRenderingContext _context; - private readonly TagHelperRenderingContext _renderingContext; - - public TagHelperRenderingContextScope(CSharpRenderingContext context, TagHelperRenderingContext renderingContext) - { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - _context = context; - _renderingContext = renderingContext; - } - - public void Dispose() - { - _context.TagHelperRenderingContext = _renderingContext; - } - } - } -} diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeRenderingContext.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeRenderingContext.cs new file mode 100644 index 0000000000..a6019e3a10 --- /dev/null +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeRenderingContext.cs @@ -0,0 +1,184 @@ +// 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.CodeGeneration +{ + public abstract class CodeRenderingContext + { + internal static readonly object NewLineString = "NewLineString"; + internal static readonly object SuppressUniqueIds = "SuppressUniqueIds"; + + public static CodeRenderingContext Create(RazorCodeDocument codeDocument, RazorCodeGenerationOptions options) + { + if (codeDocument == null) + { + throw new ArgumentNullException(nameof(codeDocument)); + } + + if (options == null) + { + throw new ArgumentNullException(nameof(options)); + } + + IntermediateNodeWriter nodeWriter; + TagHelperWriter tagHelperWriter; + + if (options.DesignTime) + { + nodeWriter = new DesignTimeNodeWriter(); + tagHelperWriter = new DesignTimeTagHelperWriter(); + } + else + { + nodeWriter = new RuntimeNodeWriter(); + tagHelperWriter = new RuntimeTagHelperWriter(); + } + + var documentKind = codeDocument.GetDocumentIntermediateNode()?.DocumentKind; + var codeWriter = new CodeWriter(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, documentKind, codeDocument.Source, options) + { + TagHelperWriter = tagHelperWriter + }; + + var newLineString = codeDocument.Items[NewLineString]; + if (newLineString != null) + { + // Set new line character to a specific string regardless of platform, for testing purposes. + codeWriter.NewLine = (string)newLineString; + } + + context.Items[SuppressUniqueIds] = codeDocument.Items[SuppressUniqueIds]; + + return context; + } + + public abstract CodeWriter CodeWriter { get; } + + public abstract IntermediateNodeWriter NodeWriter { get; protected set; } + + public abstract RazorSourceDocument SourceDocument { get; } + + public abstract RazorCodeGenerationOptions Options { get; } + + public abstract RazorDiagnosticCollection Diagnostics { get; } + + public abstract ItemCollection Items { get; } + + public abstract string DocumentKind { get; } + + public abstract IntermediateNodeWriterScope Push(IntermediateNodeWriter writer); + + public struct IntermediateNodeWriterScope : IDisposable + { + private readonly CodeRenderingContext _context; + private readonly IntermediateNodeWriter _writer; + + public IntermediateNodeWriterScope(CodeRenderingContext context, IntermediateNodeWriter writer) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (writer == null) + { + throw new ArgumentNullException(nameof(writer)); + } + + _context = context; + _writer = writer; + } + + public void Dispose() + { + _context.NodeWriter = _writer; + } + } + + + // All bits below here are temporary + + #region Temporary TagHelper bits + internal TagHelperWriter TagHelperWriter { get; set; } + + internal TagHelperRenderingContext TagHelperRenderingContext { get; set; } + + internal TagHelperWriterScope Push(TagHelperWriter writer) + { + if (writer == null) + { + throw new ArgumentNullException(nameof(writer)); + } + + var scope = new TagHelperWriterScope(this, TagHelperWriter); + TagHelperWriter = writer; + return scope; + } + + internal TagHelperRenderingContextScope Push(TagHelperRenderingContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + var scope = new TagHelperRenderingContextScope(this, TagHelperRenderingContext); + TagHelperRenderingContext = context; + return scope; + } + + internal struct TagHelperWriterScope : IDisposable + { + private readonly CodeRenderingContext _context; + private readonly TagHelperWriter _writer; + + public TagHelperWriterScope(CodeRenderingContext context, TagHelperWriter writer) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (writer == null) + { + throw new ArgumentNullException(nameof(writer)); + } + + _context = context; + _writer = writer; + } + + public void Dispose() + { + _context.TagHelperWriter = _writer; + } + } + + internal struct TagHelperRenderingContextScope : IDisposable + { + private readonly CodeRenderingContext _context; + private readonly TagHelperRenderingContext _renderingContext; + + public TagHelperRenderingContextScope(CodeRenderingContext context, TagHelperRenderingContext renderingContext) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + _context = context; + _renderingContext = renderingContext; + } + + public void Dispose() + { + _context.TagHelperRenderingContext = _renderingContext; + } + } + + #endregion + } +} diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeRenderingContextExtensions.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeRenderingContextExtensions.cs new file mode 100644 index 0000000000..adc9aa42be --- /dev/null +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeRenderingContextExtensions.cs @@ -0,0 +1,126 @@ +// 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 System.Collections.Generic; +using Microsoft.AspNetCore.Razor.Language.Intermediate; + +namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration +{ + public static class CodeRenderingContextExtensions + { + private static readonly object RenderNodeKey = new object(); + private static readonly object RenderChildrenKey = new object(); + private static readonly object LineMappingsKey = new object(); + + public static void SetRenderNode(this CodeRenderingContext context, Action renderNode) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + context.Items[RenderNodeKey] = renderNode; + } + + public static void RenderNode(this CodeRenderingContext context, IntermediateNode node) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (node == null) + { + throw new ArgumentNullException(nameof(node)); + } + + var renderNode = (Action)context.Items[RenderNodeKey]; + + if (renderNode == null) + { + throw new InvalidOperationException( + Resources.FormatRenderingContextRequiresDelegate(nameof(CodeRenderingContext), nameof(RenderNode))); + } + + renderNode(node); + } + + public static void SetRenderChildren(this CodeRenderingContext context, Action renderChildren) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + context.Items[RenderChildrenKey] = renderChildren; + } + + public static void RenderChildren(this CodeRenderingContext context, IntermediateNode node) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (node == null) + { + throw new ArgumentNullException(nameof(node)); + } + + var renderChildren = (Action)context.Items[RenderChildrenKey]; + if (renderChildren == null) + { + throw new InvalidOperationException( + Resources.FormatRenderingContextRequiresDelegate(nameof(CodeRenderingContext), nameof(RenderChildren))); + } + + renderChildren(node); + } + + public static void AddLineMappingFor(this CodeRenderingContext context, IntermediateNode node) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if (node == null) + { + throw new ArgumentNullException(nameof(node)); + } + + var lineMappings = (List)context.Items[LineMappingsKey]; + if (lineMappings == null) + { + lineMappings = new List(); + context.Items[LineMappingsKey] = lineMappings; + } + + if (node.Source == null) + { + return; + } + + if (context.SourceDocument.FilePath != null && + !string.Equals(context.SourceDocument.FilePath, node.Source.Value.FilePath, StringComparison.OrdinalIgnoreCase)) + { + // We don't want to generate line mappings for imports. + return; + } + + var source = node.Source.Value; + var generatedLocation = new SourceSpan(context.CodeWriter.Location, source.Length); + var lineMapping = new LineMapping(source, generatedLocation); + + lineMappings.Add(lineMapping); + } + + public static IReadOnlyList GetLineMappings(this CodeRenderingContext context) + { + var lineMappings = (IReadOnlyList)context.Items[LineMappingsKey] ?? Array.Empty(); + + return lineMappings; + } + } +} diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeTarget.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeTarget.cs index e52dedb3d6..954844bd86 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeTarget.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeTarget.cs @@ -86,7 +86,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } - public abstract DocumentWriter CreateWriter(CSharpRenderingContext context); + public abstract DocumentWriter CreateWriter(CodeRenderingContext context); public abstract TExtension GetExtension() where TExtension : class, ICodeTargetExtension; diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CSharpCodeWriter.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeWriter.cs similarity index 90% rename from src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CSharpCodeWriter.cs rename to src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeWriter.cs index 00df8e1ba1..9dde08cecb 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CSharpCodeWriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/CodeWriter.cs @@ -10,7 +10,7 @@ using System.Text; namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - public sealed class CSharpCodeWriter + public sealed class CodeWriter { private const string InstanceMethodFormat = "{0}.{1}"; @@ -46,7 +46,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public SourceLocation Location => new SourceLocation(_absoluteIndex, _currentLineIndex, _currentLineCharacterIndex); // Internal for testing. - internal CSharpCodeWriter Indent(int size) + internal CodeWriter Indent(int size) { if (IsAfterNewLine) { @@ -62,7 +62,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration return this; } - public CSharpCodeWriter Write(string data) + public CodeWriter Write(string data) { if (data == null) { @@ -72,7 +72,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration return Write(data, 0, data.Length); } - public CSharpCodeWriter Write(string data, int index, int count) + public CodeWriter Write(string data, int index, int count) { if (data == null || count == 0) { @@ -147,7 +147,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration return this; } - public CSharpCodeWriter WriteLine() + public CodeWriter WriteLine() { Builder.Append(NewLine); @@ -161,7 +161,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration return this; } - public CSharpCodeWriter WriteLine(string data) + public CodeWriter WriteLine(string data) { return Write(data).WriteLine(); } @@ -177,7 +177,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration return _cache; } - public CSharpCodeWriter WritePadding(int offset, SourceSpan? span, CSharpRenderingContext context) + public CodeWriter WritePadding(int offset, SourceSpan? span, CodeRenderingContext context) { if (span == null) { @@ -237,7 +237,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - public CSharpCodeWriter WriteVariableDeclaration(string type, string name, string value) + public CodeWriter WriteVariableDeclaration(string type, string name, string value) { Write(type).Write(" ").Write(name); if (!string.IsNullOrEmpty(value)) @@ -254,27 +254,27 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration return this; } - public CSharpCodeWriter WriteBooleanLiteral(bool value) + public CodeWriter WriteBooleanLiteral(bool value) { return Write(value.ToString().ToLowerInvariant()); } - public CSharpCodeWriter WriteStartAssignment(string name) + public CodeWriter WriteStartAssignment(string name) { return Write(name).Write(" = "); } - public CSharpCodeWriter WriteParameterSeparator() + public CodeWriter WriteParameterSeparator() { return Write(", "); } - public CSharpCodeWriter WriteStartNewObject(string typeName) + public CodeWriter WriteStartNewObject(string typeName) { return Write("new ").Write(typeName).Write("("); } - public CSharpCodeWriter WriteStringLiteral(string literal) + public CodeWriter WriteStringLiteral(string literal) { if (literal.Length >= 256 && literal.Length <= 1500 && literal.IndexOf('\0') == -1) { @@ -288,12 +288,12 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration return this; } - public CSharpCodeWriter WriteUsing(string name) + public CodeWriter WriteUsing(string name) { return WriteUsing(name, endLine: true); } - public CSharpCodeWriter WriteUsing(string name, bool endLine) + public CodeWriter WriteUsing(string name, bool endLine) { Write("using "); Write(name); @@ -311,8 +311,8 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration /// /// The location to generate the line pragma for. /// The file to generate the line pragma for. - /// The current instance of . - public CSharpCodeWriter WriteLineNumberDirective(SourceSpan location, string file) + /// The current instance of . + public CodeWriter WriteLineNumberDirective(SourceSpan location, string file) { if (location.FilePath != null) { @@ -328,19 +328,19 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration return Write("#line ").Write(lineNumberAsString).Write(" \"").Write(file).WriteLine("\""); } - public CSharpCodeWriter WriteStartMethodInvocation(string methodName) + public CodeWriter WriteStartMethodInvocation(string methodName) { Write(methodName); return Write("("); } - public CSharpCodeWriter WriteEndMethodInvocation() + public CodeWriter WriteEndMethodInvocation() { return WriteEndMethodInvocation(endLine: true); } - public CSharpCodeWriter WriteEndMethodInvocation(bool endLine) + public CodeWriter WriteEndMethodInvocation(bool endLine) { Write(")"); if (endLine) @@ -352,7 +352,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } // Writes a method invocation for the given instance name. - public CSharpCodeWriter WriteInstanceMethodInvocation( + public CodeWriter WriteInstanceMethodInvocation( string instanceName, string methodName, params string[] parameters) @@ -371,7 +371,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } // Writes a method invocation for the given instance name. - public CSharpCodeWriter WriteInstanceMethodInvocation( + public CodeWriter WriteInstanceMethodInvocation( string instanceName, string methodName, bool endLine, @@ -393,7 +393,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration parameters); } - public CSharpCodeWriter WriteStartInstanceMethodInvocation(string instanceName, string methodName) + public CodeWriter WriteStartInstanceMethodInvocation(string instanceName, string methodName) { if (instanceName == null) { @@ -409,7 +409,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration string.Format(CultureInfo.InvariantCulture, InstanceMethodFormat, instanceName, methodName)); } - public CSharpCodeWriter WriteField(IList modifiers, string typeName, string fieldName) + public CodeWriter WriteField(IList modifiers, string typeName, string fieldName) { if (modifiers == null) { @@ -441,19 +441,19 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration return this; } - public CSharpCodeWriter WriteMethodInvocation(string methodName, params string[] parameters) + public CodeWriter WriteMethodInvocation(string methodName, params string[] parameters) { return WriteMethodInvocation(methodName, endLine: true, parameters: parameters); } - public CSharpCodeWriter WriteMethodInvocation(string methodName, bool endLine, params string[] parameters) + public CodeWriter WriteMethodInvocation(string methodName, bool endLine, params string[] parameters) { return WriteStartMethodInvocation(methodName) .Write(string.Join(", ", parameters)) .WriteEndMethodInvocation(endLine); } - public CSharpCodeWriter WriteAutoPropertyDeclaration(IList modifiers, string typeName, string propertyName) + public CodeWriter WriteAutoPropertyDeclaration(IList modifiers, string typeName, string propertyName) { if (modifiers == null) { @@ -675,12 +675,12 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public struct CSharpCodeWritingScope : IDisposable { - private CSharpCodeWriter _writer; + private CodeWriter _writer; private bool _autoSpace; private int _tabSize; private int _startIndent; - public CSharpCodeWritingScope(CSharpCodeWriter writer, int tabSize = 4, bool autoSpace = true) + public CSharpCodeWritingScope(CodeWriter writer, int tabSize = 4, bool autoSpace = true) { _writer = writer; _autoSpace = true; @@ -730,10 +730,10 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration private class LinePragmaWriter : IDisposable { - private readonly CSharpCodeWriter _writer; + private readonly CodeWriter _writer; private readonly int _startIndent; - public LinePragmaWriter(CSharpCodeWriter writer, SourceSpan documentLocation) + public LinePragmaWriter(CodeWriter writer, SourceSpan documentLocation) { if (writer == null) { diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DefaultCodeRenderingContext.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DefaultCodeRenderingContext.cs new file mode 100644 index 0000000000..1f916dd1df --- /dev/null +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DefaultCodeRenderingContext.cs @@ -0,0 +1,63 @@ + +// 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.CodeGeneration +{ + internal class DefaultCodeRenderingContext : CodeRenderingContext + { + // Internal for unit testing + internal DefaultCodeRenderingContext( + CodeWriter codeWriter, + IntermediateNodeWriter nodeWriter, + RazorSourceDocument sourceDocument, + RazorCodeGenerationOptions options) + : this (codeWriter, nodeWriter, null, sourceDocument, options) + { + } + + public DefaultCodeRenderingContext( + CodeWriter codeWriter, + IntermediateNodeWriter nodeWriter, + string documentKind, + RazorSourceDocument sourceDocument, + RazorCodeGenerationOptions options) + { + CodeWriter = codeWriter; + NodeWriter = nodeWriter; + DocumentKind = documentKind; + SourceDocument = sourceDocument; + Options = options; + Diagnostics = new DefaultRazorDiagnosticCollection(); + Items = new DefaultItemCollection(); + } + + public override CodeWriter CodeWriter { get; } + + public override IntermediateNodeWriter NodeWriter { get; protected set; } + + public override RazorSourceDocument SourceDocument { get; } + + public override RazorCodeGenerationOptions Options { get; } + + public override RazorDiagnosticCollection Diagnostics { get; } + + public override ItemCollection Items { get; } + + public override string DocumentKind { get; } + + public override IntermediateNodeWriterScope Push(IntermediateNodeWriter writer) + { + if (writer == null) + { + throw new ArgumentNullException(nameof(writer)); + } + + var scope = new IntermediateNodeWriterScope(this, NodeWriter); + NodeWriter = writer; + return scope; + } + } +} diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DefaultCodeTarget.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DefaultCodeTarget.cs index 65452f5fdc..6bfb26d29a 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DefaultCodeTarget.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DefaultCodeTarget.cs @@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public ICodeTargetExtension[] Extensions { get; } - public override DocumentWriter CreateWriter(CSharpRenderingContext context) + public override DocumentWriter CreateWriter(CodeRenderingContext context) { return new DefaultDocumentWriter(this, context); } diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DefaultDocumentWriter.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DefaultDocumentWriter.cs index d692a87f53..10a6c97d42 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DefaultDocumentWriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DefaultDocumentWriter.cs @@ -9,10 +9,10 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { internal class DefaultDocumentWriter : DocumentWriter { - private readonly CSharpRenderingContext _context; + private readonly CodeRenderingContext _context; private readonly CodeTarget _target; - public DefaultDocumentWriter(CodeTarget target, CSharpRenderingContext context) + public DefaultDocumentWriter(CodeTarget target, CodeRenderingContext context) { if (target == null) { @@ -36,28 +36,27 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } var visitor = new Visitor(_target, _context); - _context.RenderChildren = visitor.RenderChildren; - _context.RenderNode = visitor.Visit; - - _context.BasicWriter = _context.Options.DesignTime ? (BasicWriter)new DesignTimeBasicWriter() : new RuntimeBasicWriter(); - _context.TagHelperWriter = _context.Options.DesignTime ? (TagHelperWriter)new DesignTimeTagHelperWriter() : new RuntimeTagHelperWriter(); + _context.SetRenderNode(visitor.Visit); + _context.SetRenderChildren(visitor.RenderChildren); visitor.VisitDocument(node); - _context.RenderChildren = null; + + _context.SetRenderChildren(null); + _context.SetRenderNode(null); } private class Visitor : IntermediateNodeVisitor { - private readonly CSharpRenderingContext _context; + private readonly CodeRenderingContext _context; private readonly CodeTarget _target; - public Visitor(CodeTarget target, CSharpRenderingContext context) + public Visitor(CodeTarget target, CodeRenderingContext context) { _target = target; _context = context; } - private CSharpRenderingContext Context => _context; + private CodeRenderingContext Context => _context; public void RenderChildren(IntermediateNode node) { @@ -88,7 +87,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration if (!string.IsNullOrEmpty(bytes)) { - Context.Writer + Context.CodeWriter .Write("#pragma checksum \"") .Write(sourceDocument.FilePath) .Write("\" \"") @@ -99,32 +98,32 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - Context.Writer + Context.CodeWriter .WriteLine("// ") .WriteLine("#pragma warning disable 1591"); RenderChildren(node); - Context.Writer.WriteLine("#pragma warning restore 1591"); + Context.CodeWriter.WriteLine("#pragma warning restore 1591"); } public override void VisitUsingDirective(UsingDirectiveIntermediateNode node) { - Context.BasicWriter.WriteUsingDirective(Context, node); + Context.NodeWriter.WriteUsingDirective(Context, node); } public override void VisitNamespaceDeclaration(NamespaceDeclarationIntermediateNode node) { - using (Context.Writer.BuildNamespace(node.Content)) + using (Context.CodeWriter.BuildNamespace(node.Content)) { - Context.Writer.WriteLine("#line hidden"); + Context.CodeWriter.WriteLine("#line hidden"); RenderChildren(node); } } public override void VisitClassDeclaration(ClassDeclarationIntermediateNode node) { - using (Context.Writer.BuildClassDeclaration(node.Modifiers, node.Name, node.BaseType, node.Interfaces)) + using (Context.CodeWriter.BuildClassDeclaration(node.Modifiers, node.Name, node.BaseType, node.Interfaces)) { RenderChildren(node); } @@ -132,36 +131,36 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public override void VisitMethodDeclaration(MethodDeclarationIntermediateNode node) { - Context.Writer.WriteLine("#pragma warning disable 1998"); + Context.CodeWriter.WriteLine("#pragma warning disable 1998"); for (var i = 0; i < node.Modifiers.Count; i++) { - Context.Writer.Write(node.Modifiers[i]); - Context.Writer.Write(" "); + Context.CodeWriter.Write(node.Modifiers[i]); + Context.CodeWriter.Write(" "); } - Context.Writer + Context.CodeWriter .Write(node.ReturnType) .Write(" ") .Write(node.Name) .WriteLine("()"); - using (Context.Writer.BuildScope()) + using (Context.CodeWriter.BuildScope()) { RenderChildren(node); } - Context.Writer.WriteLine("#pragma warning restore 1998"); + Context.CodeWriter.WriteLine("#pragma warning restore 1998"); } public override void VisitFieldDeclaration(FieldDeclarationIntermediateNode node) { - Context.Writer.WriteField(node.Modifiers, node.Type, node.Name); + Context.CodeWriter.WriteField(node.Modifiers, node.Type, node.Name); } public override void VisitPropertyDeclaration(PropertyDeclarationIntermediateNode node) { - Context.Writer.WriteAutoPropertyDeclaration(node.Modifiers, node.Type, node.Name); + Context.CodeWriter.WriteAutoPropertyDeclaration(node.Modifiers, node.Type, node.Name); } public override void VisitExtension(ExtensionIntermediateNode node) @@ -171,37 +170,37 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public override void VisitCSharpExpression(CSharpExpressionIntermediateNode node) { - Context.BasicWriter.WriteCSharpExpression(Context, node); + Context.NodeWriter.WriteCSharpExpression(Context, node); } public override void VisitCSharpCode(CSharpCodeIntermediateNode node) { - Context.BasicWriter.WriteCSharpCode(Context, node); + Context.NodeWriter.WriteCSharpCode(Context, node); } public override void VisitHtmlAttribute(HtmlAttributeIntermediateNode node) { - Context.BasicWriter.WriteHtmlAttribute(Context, node); + Context.NodeWriter.WriteHtmlAttribute(Context, node); } public override void VisitHtmlAttributeValue(HtmlAttributeValueIntermediateNode node) { - Context.BasicWriter.WriteHtmlAttributeValue(Context, node); + Context.NodeWriter.WriteHtmlAttributeValue(Context, node); } public override void VisitCSharpExpressionAttributeValue(CSharpExpressionAttributeValueIntermediateNode node) { - Context.BasicWriter.WriteCSharpExpressionAttributeValue(Context, node); + Context.NodeWriter.WriteCSharpExpressionAttributeValue(Context, node); } public override void VisitCSharpCodeAttributeValue(CSharpCodeAttributeValueIntermediateNode node) { - Context.BasicWriter.WriteCSharpCodeAttributeValue(Context, node); + Context.NodeWriter.WriteCSharpCodeAttributeValue(Context, node); } public override void VisitHtml(HtmlContentIntermediateNode node) { - Context.BasicWriter.WriteHtmlContent(Context, node); + Context.NodeWriter.WriteHtmlContent(Context, node); } public override void VisitDeclareTagHelperFields(DeclareTagHelperFieldsIntermediateNode node) diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DesignTimeBasicWriter.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DesignTimeNodeWriter.cs similarity index 70% rename from src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DesignTimeBasicWriter.cs rename to src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DesignTimeNodeWriter.cs index 097bb47105..3bd2e504f2 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DesignTimeBasicWriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DesignTimeNodeWriter.cs @@ -7,25 +7,25 @@ using Microsoft.AspNetCore.Razor.Language.Intermediate; namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - public class DesignTimeBasicWriter : BasicWriter + public class DesignTimeNodeWriter : IntermediateNodeWriter { - public override void WriteUsingDirective(CSharpRenderingContext context, UsingDirectiveIntermediateNode node) + public override void WriteUsingDirective(CodeRenderingContext context, UsingDirectiveIntermediateNode node) { if (node.Source.HasValue) { - using (context.Writer.BuildLinePragma(node.Source.Value)) + using (context.CodeWriter.BuildLinePragma(node.Source.Value)) { context.AddLineMappingFor(node); - context.Writer.WriteUsing(node.Content); + context.CodeWriter.WriteUsing(node.Content); } } else { - context.Writer.WriteUsing(node.Content); + context.CodeWriter.WriteUsing(node.Content); } } - public override void WriteCSharpExpression(CSharpRenderingContext context, CSharpExpressionIntermediateNode node) + public override void WriteCSharpExpression(CodeRenderingContext context, CSharpExpressionIntermediateNode node) { if (context == null) { @@ -44,18 +44,18 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration if (node.Source != null) { - using (context.Writer.BuildLinePragma(node.Source.Value)) + using (context.CodeWriter.BuildLinePragma(node.Source.Value)) { var offset = DesignTimeDirectivePass.DesignTimeVariable.Length + " = ".Length; - context.Writer.WritePadding(offset, node.Source, context); - context.Writer.WriteStartAssignment(DesignTimeDirectivePass.DesignTimeVariable); + context.CodeWriter.WritePadding(offset, node.Source, context); + context.CodeWriter.WriteStartAssignment(DesignTimeDirectivePass.DesignTimeVariable); for (var i = 0; i < node.Children.Count; i++) { if (node.Children[i] is IntermediateToken token && token.IsCSharp) { context.AddLineMappingFor(token); - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); } else { @@ -64,17 +64,17 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - context.Writer.WriteLine(";"); + context.CodeWriter.WriteLine(";"); } } else { - context.Writer.WriteStartAssignment(DesignTimeDirectivePass.DesignTimeVariable); + context.CodeWriter.WriteStartAssignment(DesignTimeDirectivePass.DesignTimeVariable); for (var i = 0; i < node.Children.Count; i++) { if (node.Children[i] is IntermediateToken token && token.IsCSharp) { - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); } else { @@ -82,11 +82,11 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration context.RenderNode(node.Children[i]); } } - context.Writer.WriteLine(";"); + context.CodeWriter.WriteLine(";"); } } - public override void WriteCSharpCode(CSharpRenderingContext context, CSharpCodeIntermediateNode node) + public override void WriteCSharpCode(CodeRenderingContext context, CSharpCodeIntermediateNode node) { var isWhitespaceStatement = true; for (var i = 0; i < node.Children.Count; i++) @@ -104,10 +104,10 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { if (!isWhitespaceStatement) { - linePragmaScope = context.Writer.BuildLinePragma(node.Source.Value); + linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value); } - context.Writer.WritePadding(0, node.Source.Value, context); + context.CodeWriter.WritePadding(0, node.Source.Value, context); } else if (isWhitespaceStatement) { @@ -120,7 +120,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration if (node.Children[i] is IntermediateToken token && token.IsCSharp) { context.AddLineMappingFor(token); - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); } else { @@ -135,21 +135,21 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } else { - context.Writer.WriteLine(); + context.CodeWriter.WriteLine(); } } - public override void WriteHtmlAttribute(CSharpRenderingContext context, HtmlAttributeIntermediateNode node) + public override void WriteHtmlAttribute(CodeRenderingContext context, HtmlAttributeIntermediateNode node) { context.RenderChildren(node); } - public override void WriteHtmlAttributeValue(CSharpRenderingContext context, HtmlAttributeValueIntermediateNode node) + public override void WriteHtmlAttributeValue(CodeRenderingContext context, HtmlAttributeValueIntermediateNode node) { context.RenderChildren(node); } - public override void WriteCSharpExpressionAttributeValue(CSharpRenderingContext context, CSharpExpressionAttributeValueIntermediateNode node) + public override void WriteCSharpExpressionAttributeValue(CodeRenderingContext context, CSharpExpressionAttributeValueIntermediateNode node) { if (context == null) { @@ -169,18 +169,18 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration var firstChild = node.Children[0]; if (firstChild.Source != null) { - using (context.Writer.BuildLinePragma(firstChild.Source.Value)) + using (context.CodeWriter.BuildLinePragma(firstChild.Source.Value)) { var offset = DesignTimeDirectivePass.DesignTimeVariable.Length + " = ".Length; - context.Writer.WritePadding(offset, firstChild.Source, context); - context.Writer.WriteStartAssignment(DesignTimeDirectivePass.DesignTimeVariable); + context.CodeWriter.WritePadding(offset, firstChild.Source, context); + context.CodeWriter.WriteStartAssignment(DesignTimeDirectivePass.DesignTimeVariable); for (var i = 0; i < node.Children.Count; i++) { if (node.Children[i] is IntermediateToken token && token.IsCSharp) { context.AddLineMappingFor(token); - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); } else { @@ -189,12 +189,12 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - context.Writer.WriteLine(";"); + context.CodeWriter.WriteLine(";"); } } else { - context.Writer.WriteStartAssignment(DesignTimeDirectivePass.DesignTimeVariable); + context.CodeWriter.WriteStartAssignment(DesignTimeDirectivePass.DesignTimeVariable); for (var i = 0; i < node.Children.Count; i++) { if (node.Children[i] is IntermediateToken token && token.IsCSharp) @@ -204,7 +204,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration context.AddLineMappingFor(token); } - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); } else { @@ -212,11 +212,11 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration context.RenderNode(node.Children[i]); } } - context.Writer.WriteLine(";"); + context.CodeWriter.WriteLine(";"); } } - public override void WriteCSharpCodeAttributeValue(CSharpRenderingContext context, CSharpCodeAttributeValueIntermediateNode node) + public override void WriteCSharpCodeAttributeValue(CodeRenderingContext context, CSharpCodeAttributeValueIntermediateNode node) { for (var i = 0; i < node.Children.Count; i++) { @@ -229,10 +229,10 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { if (!isWhitespaceStatement) { - linePragmaScope = context.Writer.BuildLinePragma(token.Source.Value); + linePragmaScope = context.CodeWriter.BuildLinePragma(token.Source.Value); } - context.Writer.WritePadding(0, token.Source.Value, context); + context.CodeWriter.WritePadding(0, token.Source.Value, context); } else if (isWhitespaceStatement) { @@ -241,7 +241,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } context.AddLineMappingFor(token); - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); if (linePragmaScope != null) { @@ -249,7 +249,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } else { - context.Writer.WriteLine(); + context.CodeWriter.WriteLine(); } } else @@ -260,17 +260,17 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - public override void WriteHtmlContent(CSharpRenderingContext context, HtmlContentIntermediateNode node) + public override void WriteHtmlContent(CodeRenderingContext context, HtmlContentIntermediateNode node) { // Do nothing } - public override void BeginWriterScope(CSharpRenderingContext context, string writer) + public override void BeginWriterScope(CodeRenderingContext context, string writer) { // Do nothing } - public override void EndWriterScope(CSharpRenderingContext context) + public override void EndWriterScope(CodeRenderingContext context) { // Do nothing } diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DesignTimeTagHelperWriter.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DesignTimeTagHelperWriter.cs index f9a1806156..9e63d30501 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DesignTimeTagHelperWriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/DesignTimeTagHelperWriter.cs @@ -10,16 +10,16 @@ using Microsoft.AspNetCore.Razor.Language.Legacy; namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - public class DesignTimeTagHelperWriter : TagHelperWriter + internal class DesignTimeTagHelperWriter : TagHelperWriter { public string CreateTagHelperMethodName { get; set; } = "CreateTagHelper"; - public override void WriteDeclareTagHelperFields(CSharpRenderingContext context, DeclareTagHelperFieldsIntermediateNode node) + public override void WriteDeclareTagHelperFields(CodeRenderingContext context, DeclareTagHelperFieldsIntermediateNode node) { foreach (var tagHelperTypeName in node.UsedTagHelperTypeNames) { var tagHelperVariableName = GetTagHelperVariableName(tagHelperTypeName); - context.Writer + context.CodeWriter .Write("private global::") .WriteVariableDeclaration( tagHelperTypeName, @@ -28,32 +28,32 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - public override void WriteTagHelper(CSharpRenderingContext context, TagHelperIntermediateNode node) + public override void WriteTagHelper(CodeRenderingContext context, TagHelperIntermediateNode node) { context.RenderChildren(node); } - public override void WriteTagHelperBody(CSharpRenderingContext context, TagHelperBodyIntermediateNode node) + public override void WriteTagHelperBody(CodeRenderingContext context, TagHelperBodyIntermediateNode node) { context.RenderChildren(node); } - public override void WriteCreateTagHelper(CSharpRenderingContext context, CreateTagHelperIntermediateNode node) + public override void WriteCreateTagHelper(CodeRenderingContext context, CreateTagHelperIntermediateNode node) { var tagHelperVariableName = GetTagHelperVariableName(node.TagHelperTypeName); - context.Writer + context.CodeWriter .WriteStartAssignment(tagHelperVariableName) .Write(CreateTagHelperMethodName) .WriteLine($"();"); } - public override void WriteAddTagHelperHtmlAttribute(CSharpRenderingContext context, AddTagHelperHtmlAttributeIntermediateNode node) + public override void WriteAddTagHelperHtmlAttribute(CodeRenderingContext context, AddTagHelperHtmlAttributeIntermediateNode node) { context.RenderChildren(node); } - public override void WriteSetTagHelperProperty(CSharpRenderingContext context, SetTagHelperPropertyIntermediateNode node) + public override void WriteSetTagHelperProperty(CodeRenderingContext context, SetTagHelperPropertyIntermediateNode node) { var tagHelperVariableName = GetTagHelperVariableName(node.TagHelperTypeName); var tagHelperRenderingContext = context.TagHelperRenderingContext; @@ -61,7 +61,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration if (tagHelperRenderingContext.RenderedBoundAttributes.TryGetValue(node.AttributeName, out string previousValueAccessor)) { - context.Writer + context.CodeWriter .WriteStartAssignment(propertyValueAccessor) .Write(previousValueAccessor) .WriteLine(";"); @@ -77,24 +77,24 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { context.RenderChildren(node); - context.Writer.WriteStartAssignment(propertyValueAccessor); + context.CodeWriter.WriteStartAssignment(propertyValueAccessor); if (node.Children.Count == 1 && node.Children.First() is HtmlContentIntermediateNode htmlNode) { var content = GetContent(htmlNode); - context.Writer.WriteStringLiteral(content); + context.CodeWriter.WriteStringLiteral(content); } else { - context.Writer.Write("string.Empty"); + context.CodeWriter.Write("string.Empty"); } - context.Writer.WriteLine(";"); + context.CodeWriter.WriteLine(";"); } else { var firstMappedChild = node.Children.FirstOrDefault(child => child.Source != null) as IntermediateNode; var valueStart = firstMappedChild?.Source; - using (context.Writer.BuildLinePragma(node.Source.Value)) + using (context.CodeWriter.BuildLinePragma(node.Source.Value)) { var assignmentPrefixLength = propertyValueAccessor.Length + " = ".Length; if (node.Descriptor.IsEnum && @@ -106,10 +106,10 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration if (valueStart != null) { - context.Writer.WritePadding(assignmentPrefixLength, node.Source.Value, context); + context.CodeWriter.WritePadding(assignmentPrefixLength, node.Source.Value, context); } - context.Writer + context.CodeWriter .WriteStartAssignment(propertyValueAccessor) .Write("global::") .Write(node.Descriptor.TypeName) @@ -119,21 +119,21 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { if (valueStart != null) { - context.Writer.WritePadding(assignmentPrefixLength, node.Source.Value, context); + context.CodeWriter.WritePadding(assignmentPrefixLength, node.Source.Value, context); } - context.Writer.WriteStartAssignment(propertyValueAccessor); + context.CodeWriter.WriteStartAssignment(propertyValueAccessor); } RenderTagHelperAttributeInline(context, node, node.Source.Value); - context.Writer.WriteLine(";"); + context.CodeWriter.WriteLine(";"); } } } private void RenderTagHelperAttributeInline( - CSharpRenderingContext context, + CodeRenderingContext context, SetTagHelperPropertyIntermediateNode property, SourceSpan documentLocation) { @@ -144,7 +144,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } private void RenderTagHelperAttributeInline( - CSharpRenderingContext context, + CodeRenderingContext context, SetTagHelperPropertyIntermediateNode property, IntermediateNode node, SourceSpan documentLocation) @@ -163,7 +163,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration context.AddLineMappingFor(node); } - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); } else if (node is CSharpCodeIntermediateNode) { diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/IntermediateNodeWriter.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/IntermediateNodeWriter.cs new file mode 100644 index 0000000000..d8c636f064 --- /dev/null +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/IntermediateNodeWriter.cs @@ -0,0 +1,30 @@ +// 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; + +namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration +{ + public abstract class IntermediateNodeWriter + { + public abstract void WriteUsingDirective(CodeRenderingContext context, UsingDirectiveIntermediateNode node); + + public abstract void WriteCSharpExpression(CodeRenderingContext context, CSharpExpressionIntermediateNode node); + + public abstract void WriteCSharpCode(CodeRenderingContext context, CSharpCodeIntermediateNode node); + + public abstract void WriteHtmlContent(CodeRenderingContext context, HtmlContentIntermediateNode node); + + public abstract void WriteHtmlAttribute(CodeRenderingContext context, HtmlAttributeIntermediateNode node); + + public abstract void WriteHtmlAttributeValue(CodeRenderingContext context, HtmlAttributeValueIntermediateNode node); + + public abstract void WriteCSharpExpressionAttributeValue(CodeRenderingContext context, CSharpExpressionAttributeValueIntermediateNode node); + + public abstract void WriteCSharpCodeAttributeValue(CodeRenderingContext context, CSharpCodeAttributeValueIntermediateNode node); + + public abstract void BeginWriterScope(CodeRenderingContext context, string writer); + + public abstract void EndWriterScope(CodeRenderingContext context); + } +} diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/LiteralRuntimeBasicWriter.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/LiteralRuntimeNodeWriter.cs similarity index 83% rename from src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/LiteralRuntimeBasicWriter.cs rename to src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/LiteralRuntimeNodeWriter.cs index 484c3a7cd7..21d542f663 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/LiteralRuntimeBasicWriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/LiteralRuntimeNodeWriter.cs @@ -3,7 +3,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - internal class LiteralRuntimeBasicWriter : RuntimeBasicWriter + internal class LiteralRuntimeNodeWriter : RuntimeNodeWriter { public override string WriteCSharpExpressionMethod { get; set; } = "WriteLiteral"; } diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/RuntimeBasicWriter.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/RuntimeNodeWriter.cs similarity index 78% rename from src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/RuntimeBasicWriter.cs rename to src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/RuntimeNodeWriter.cs index d4928a1bd9..289561229f 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/RuntimeBasicWriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/RuntimeNodeWriter.cs @@ -10,7 +10,7 @@ using Microsoft.AspNetCore.Razor.Language.Intermediate; namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - public class RuntimeBasicWriter : BasicWriter + public class RuntimeNodeWriter : IntermediateNodeWriter { public virtual string WriteCSharpExpressionMethod { get; set; } = "Write"; @@ -28,22 +28,22 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public string TemplateTypeName { get; set; } = "Microsoft.AspNetCore.Mvc.Razor.HelperResult"; - public override void WriteUsingDirective(CSharpRenderingContext context, UsingDirectiveIntermediateNode node) + public override void WriteUsingDirective(CodeRenderingContext context, UsingDirectiveIntermediateNode node) { if (node.Source.HasValue) { - using (context.Writer.BuildLinePragma(node.Source.Value)) + using (context.CodeWriter.BuildLinePragma(node.Source.Value)) { - context.Writer.WriteUsing(node.Content); + context.CodeWriter.WriteUsing(node.Content); } } else { - context.Writer.WriteUsing(node.Content); + context.CodeWriter.WriteUsing(node.Content); } } - public override void WriteCSharpExpression(CSharpRenderingContext context, CSharpExpressionIntermediateNode node) + public override void WriteCSharpExpression(CodeRenderingContext context, CSharpExpressionIntermediateNode node) { if (context == null) { @@ -58,17 +58,17 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration IDisposable linePragmaScope = null; if (node.Source != null) { - linePragmaScope = context.Writer.BuildLinePragma(node.Source.Value); - context.Writer.WritePadding(WriteCSharpExpressionMethod.Length + 1, node.Source, context); + linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value); + context.CodeWriter.WritePadding(WriteCSharpExpressionMethod.Length + 1, node.Source, context); } - context.Writer.WriteStartMethodInvocation(WriteCSharpExpressionMethod); + context.CodeWriter.WriteStartMethodInvocation(WriteCSharpExpressionMethod); for (var i = 0; i < node.Children.Count; i++) { if (node.Children[i] is IntermediateToken token && token.IsCSharp) { - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); } else { @@ -77,12 +77,12 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - context.Writer.WriteEndMethodInvocation(); + context.CodeWriter.WriteEndMethodInvocation(); linePragmaScope?.Dispose(); } - public override void WriteCSharpCode(CSharpRenderingContext context, CSharpCodeIntermediateNode node) + public override void WriteCSharpCode(CodeRenderingContext context, CSharpCodeIntermediateNode node) { var isWhitespaceStatement = true; for (var i = 0; i < node.Children.Count; i++) @@ -103,15 +103,15 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration IDisposable linePragmaScope = null; if (node.Source != null) { - linePragmaScope = context.Writer.BuildLinePragma(node.Source.Value); - context.Writer.WritePadding(0, node.Source.Value, context); + linePragmaScope = context.CodeWriter.BuildLinePragma(node.Source.Value); + context.CodeWriter.WritePadding(0, node.Source.Value, context); } for (var i = 0; i < node.Children.Count; i++) { if (node.Children[i] is IntermediateToken token && token.IsCSharp) { - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); } else { @@ -122,13 +122,13 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration if (linePragmaScope == null) { - context.Writer.WriteLine(); + context.CodeWriter.WriteLine(); } linePragmaScope?.Dispose(); } - public override void WriteHtmlAttribute(CSharpRenderingContext context, HtmlAttributeIntermediateNode node) + public override void WriteHtmlAttribute(CodeRenderingContext context, HtmlAttributeIntermediateNode node) { var valuePieceCount = node .Children @@ -140,7 +140,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration var prefixLocation = node.Source.Value.AbsoluteIndex; var suffixLocation = node.Source.Value.AbsoluteIndex + node.Source.Value.Length - node.Suffix.Length; - context.Writer + context.CodeWriter .WriteStartMethodInvocation(BeginWriteAttributeMethod) .WriteStringLiteral(node.Name) .WriteParameterSeparator() @@ -157,17 +157,17 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration context.RenderChildren(node); - context.Writer + context.CodeWriter .WriteStartMethodInvocation(EndWriteAttributeMethod) .WriteEndMethodInvocation(); } - public override void WriteHtmlAttributeValue(CSharpRenderingContext context, HtmlAttributeValueIntermediateNode node) + public override void WriteHtmlAttributeValue(CodeRenderingContext context, HtmlAttributeValueIntermediateNode node) { var prefixLocation = node.Source.Value.AbsoluteIndex; var valueLocation = node.Source.Value.AbsoluteIndex + node.Prefix.Length; var valueLength = node.Source.Value.Length; - context.Writer + context.CodeWriter .WriteStartMethodInvocation(WriteAttributeValueMethod) .WriteStringLiteral(node.Prefix) .WriteParameterSeparator() @@ -179,7 +179,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { if (node.Children[i] is IntermediateToken token && token.IsHtml) { - context.Writer.WriteStringLiteral(token.Content); + context.CodeWriter.WriteStringLiteral(token.Content); } else { @@ -188,7 +188,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - context.Writer + context.CodeWriter .WriteParameterSeparator() .Write(valueLocation.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() @@ -198,12 +198,12 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration .WriteEndMethodInvocation(); } - public override void WriteCSharpExpressionAttributeValue(CSharpRenderingContext context, CSharpExpressionAttributeValueIntermediateNode node) + public override void WriteCSharpExpressionAttributeValue(CodeRenderingContext context, CSharpExpressionAttributeValueIntermediateNode node) { - using (context.Writer.BuildLinePragma(node.Source.Value)) + using (context.CodeWriter.BuildLinePragma(node.Source.Value)) { var prefixLocation = node.Source.Value.AbsoluteIndex; - context.Writer + context.CodeWriter .WriteStartMethodInvocation(WriteAttributeValueMethod) .WriteStringLiteral(node.Prefix) .WriteParameterSeparator() @@ -214,7 +214,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { if (node.Children[i] is IntermediateToken token && token.IsCSharp) { - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); } else { @@ -225,7 +225,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration var valueLocation = node.Source.Value.AbsoluteIndex + node.Prefix.Length; var valueLength = node.Source.Value.Length - node.Prefix.Length; - context.Writer + context.CodeWriter .WriteParameterSeparator() .Write(valueLocation.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() @@ -236,23 +236,23 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - public override void WriteCSharpCodeAttributeValue(CSharpRenderingContext context, CSharpCodeAttributeValueIntermediateNode node) + public override void WriteCSharpCodeAttributeValue(CodeRenderingContext context, CSharpCodeAttributeValueIntermediateNode node) { const string ValueWriterName = "__razor_attribute_value_writer"; var prefixLocation = node.Source.Value.AbsoluteIndex; var valueLocation = node.Source.Value.AbsoluteIndex + node.Prefix.Length; var valueLength = node.Source.Value.Length - node.Prefix.Length; - context.Writer + context.CodeWriter .WriteStartMethodInvocation(WriteAttributeValueMethod) .WriteStringLiteral(node.Prefix) .WriteParameterSeparator() .Write(prefixLocation.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator(); - context.Writer.WriteStartNewObject(TemplateTypeName); + context.CodeWriter.WriteStartNewObject(TemplateTypeName); - using (context.Writer.BuildAsyncLambda(ValueWriterName)) + using (context.CodeWriter.BuildAsyncLambda(ValueWriterName)) { BeginWriterScope(context, ValueWriterName); @@ -266,10 +266,10 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { if (!isWhitespaceStatement) { - linePragmaScope = context.Writer.BuildLinePragma(token.Source.Value); + linePragmaScope = context.CodeWriter.BuildLinePragma(token.Source.Value); } - context.Writer.WritePadding(0, token.Source.Value, context); + context.CodeWriter.WritePadding(0, token.Source.Value, context); } else if (isWhitespaceStatement) { @@ -277,7 +277,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration continue; } - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); if (linePragmaScope != null) { @@ -285,7 +285,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } else { - context.Writer.WriteLine(); + context.CodeWriter.WriteLine(); } } else @@ -298,9 +298,9 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration EndWriterScope(context); } - context.Writer.WriteEndMethodInvocation(false); + context.CodeWriter.WriteEndMethodInvocation(false); - context.Writer + context.CodeWriter .WriteParameterSeparator() .Write(valueLocation.ToString(CultureInfo.InvariantCulture)) .WriteParameterSeparator() @@ -310,7 +310,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration .WriteEndMethodInvocation(); } - public override void WriteHtmlContent(CSharpRenderingContext context, HtmlContentIntermediateNode node) + public override void WriteHtmlContent(CodeRenderingContext context, HtmlContentIntermediateNode node) { const int MaxStringLiteralLength = 1024; @@ -341,7 +341,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration textToRender = content.Substring(charactersConsumed, charactersToSubstring); } - context.Writer + context.CodeWriter .WriteStartMethodInvocation(WriteHtmlContentMethod) .WriteStringLiteral(textToRender) .WriteEndMethodInvocation(); @@ -350,14 +350,14 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - public override void BeginWriterScope(CSharpRenderingContext context, string writer) + public override void BeginWriterScope(CodeRenderingContext context, string writer) { - context.Writer.WriteMethodInvocation(PushWriterMethod, writer); + context.CodeWriter.WriteMethodInvocation(PushWriterMethod, writer); } - public override void EndWriterScope(CSharpRenderingContext context) + public override void EndWriterScope(CodeRenderingContext context) { - context.Writer.WriteMethodInvocation(PopWriterMethod); + context.CodeWriter.WriteMethodInvocation(PopWriterMethod); } } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/RuntimeTagHelperWriter.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/RuntimeTagHelperWriter.cs index 41d426d561..820d702ae8 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/RuntimeTagHelperWriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/RuntimeTagHelperWriter.cs @@ -10,7 +10,7 @@ using Microsoft.AspNetCore.Razor.Language.Legacy; namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - public class RuntimeTagHelperWriter : TagHelperWriter + internal class RuntimeTagHelperWriter : TagHelperWriter { public virtual string WriteTagHelperOutputMethod { get; set; } = "Write"; @@ -68,26 +68,26 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public string FormatInvalidIndexerAssignmentMethodName { get; set; } = "InvalidTagHelperIndexerAssignment"; - public override void WriteDeclareTagHelperFields(CSharpRenderingContext context, DeclareTagHelperFieldsIntermediateNode node) + public override void WriteDeclareTagHelperFields(CodeRenderingContext context, DeclareTagHelperFieldsIntermediateNode node) { - context.Writer.WriteLine("#line hidden"); + context.CodeWriter.WriteLine("#line hidden"); // 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 + context.CodeWriter .WriteLine("#pragma warning disable 0414") .Write("private ") .WriteVariableDeclaration("string", StringValueBufferVariableName, value: null) .WriteLine("#pragma warning restore 0414"); - context.Writer + context.CodeWriter .Write("private ") .WriteVariableDeclaration( ExecutionContextTypeName, ExecutionContextVariableName, value: null); - context.Writer + context.CodeWriter .Write("private ") .Write(RunnerTypeName) .Write(" ") @@ -97,32 +97,32 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration .WriteLine("();"); var backedScopeManageVariableName = "__backed" + ScopeManagerVariableName; - context.Writer + context.CodeWriter .Write("private ") .WriteVariableDeclaration( ScopeManagerTypeName, backedScopeManageVariableName, value: null); - context.Writer + context.CodeWriter .Write("private ") .Write(ScopeManagerTypeName) .Write(" ") .WriteLine(ScopeManagerVariableName); - using (context.Writer.BuildScope()) + using (context.CodeWriter.BuildScope()) { - context.Writer.WriteLine("get"); - using (context.Writer.BuildScope()) + context.CodeWriter.WriteLine("get"); + using (context.CodeWriter.BuildScope()) { - context.Writer + context.CodeWriter .Write("if (") .Write(backedScopeManageVariableName) .WriteLine(" == null)"); - using (context.Writer.BuildScope()) + using (context.CodeWriter.BuildScope()) { - context.Writer + context.CodeWriter .WriteStartAssignment(backedScopeManageVariableName) .WriteStartNewObject(ScopeManagerTypeName) .Write(StartTagHelperWritingScopeMethodName) @@ -131,7 +131,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration .WriteEndMethodInvocation(); } - context.Writer + context.CodeWriter .Write("return ") .Write(backedScopeManageVariableName) .WriteLine(";"); @@ -141,7 +141,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration foreach (var tagHelperTypeName in node.UsedTagHelperTypeNames) { var tagHelperVariableName = GetTagHelperVariableName(tagHelperTypeName); - context.Writer + context.CodeWriter .Write("private global::") .WriteVariableDeclaration( tagHelperTypeName, @@ -150,12 +150,12 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - public override void WriteTagHelper(CSharpRenderingContext context, TagHelperIntermediateNode node) + public override void WriteTagHelper(CodeRenderingContext context, TagHelperIntermediateNode node) { context.RenderChildren(node); // Execute tag helpers - context.Writer + context.CodeWriter .Write("await ") .WriteStartInstanceMethodInvocation( RunnerVariableName, @@ -165,23 +165,23 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration var tagHelperOutputAccessor = $"{ExecutionContextVariableName}.{ExecutionContextOutputPropertyName}"; - context.Writer + context.CodeWriter .Write("if (!") .Write(tagHelperOutputAccessor) .Write(".") .Write(TagHelperOutputIsContentModifiedPropertyName) .WriteLine(")"); - using (context.Writer.BuildScope()) + using (context.CodeWriter.BuildScope()) { - context.Writer + context.CodeWriter .Write("await ") .WriteInstanceMethodInvocation( ExecutionContextVariableName, ExecutionContextSetOutputContentAsyncMethodName); } - context.Writer + context.CodeWriter .WriteStartMethodInvocation(WriteTagHelperOutputMethod) .Write(tagHelperOutputAccessor) .WriteEndMethodInvocation() @@ -191,56 +191,62 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration ScopeManagerEndMethodName); } - public override void WriteTagHelperBody(CSharpRenderingContext context, TagHelperBodyIntermediateNode node) + public override void WriteTagHelperBody(CodeRenderingContext context, TagHelperBodyIntermediateNode node) { // Call into the tag helper scope manager to start a new tag helper scope. // Also capture the value as the current execution context. - context.Writer + context.CodeWriter .WriteStartAssignment(ExecutionContextVariableName) .WriteStartInstanceMethodInvocation( ScopeManagerVariableName, ScopeManagerBeginMethodName); + var uniqueId = context.Items[CodeRenderingContext.SuppressUniqueIds]?.ToString(); + if (uniqueId == null) + { + uniqueId = Guid.NewGuid().ToString("N"); + } + // Assign a unique ID for this instance of the source HTML tag. This must be unique // per call site, e.g. if the tag is on the view twice, there should be two IDs. - context.Writer.WriteStringLiteral(context.TagHelperRenderingContext.TagName) + context.CodeWriter.WriteStringLiteral(context.TagHelperRenderingContext.TagName) .WriteParameterSeparator() .Write(TagModeTypeName) .Write(".") .Write(context.TagHelperRenderingContext.TagMode.ToString()) .WriteParameterSeparator() - .WriteStringLiteral(context.IdGenerator()) + .WriteStringLiteral(uniqueId) .WriteParameterSeparator(); // We remove and redirect writers so TagHelper authors can retrieve content. - using (context.Push(new RuntimeBasicWriter())) + using (context.Push(new RuntimeNodeWriter())) using (context.Push(new RuntimeTagHelperWriter())) { - using (context.Writer.BuildAsyncLambda()) + using (context.CodeWriter.BuildAsyncLambda()) { context.RenderChildren(node); } } - context.Writer.WriteEndMethodInvocation(); + context.CodeWriter.WriteEndMethodInvocation(); } - public override void WriteCreateTagHelper(CSharpRenderingContext context, CreateTagHelperIntermediateNode node) + public override void WriteCreateTagHelper(CodeRenderingContext context, CreateTagHelperIntermediateNode node) { var tagHelperVariableName = GetTagHelperVariableName(node.TagHelperTypeName); - context.Writer + context.CodeWriter .WriteStartAssignment(tagHelperVariableName) .Write(CreateTagHelperMethodName) .WriteLine($"();"); - context.Writer.WriteInstanceMethodInvocation( + context.CodeWriter.WriteInstanceMethodInvocation( ExecutionContextVariableName, ExecutionContextAddMethodName, tagHelperVariableName); } - public override void WriteAddTagHelperHtmlAttribute(CSharpRenderingContext context, AddTagHelperHtmlAttributeIntermediateNode node) + public override void WriteAddTagHelperHtmlAttribute(CodeRenderingContext context, AddTagHelperHtmlAttributeIntermediateNode node) { var attributeValueStyleParameter = $"{HtmlAttributeValueStyleTypeName}.{node.AttributeStructure}"; var isConditionalAttributeValue = node.Children.Any( @@ -261,7 +267,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration child is CSharpCodeAttributeValueIntermediateNode || child is ExtensionIntermediateNode); - context.Writer + context.CodeWriter .WriteStartMethodInvocation(BeginAddHtmlAttributeValuesMethodName) .Write(ExecutionContextVariableName) .WriteParameterSeparator() @@ -272,12 +278,12 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration .Write(attributeValueStyleParameter) .WriteEndMethodInvocation(); - using (context.Push(new TagHelperHtmlAttributeRuntimeBasicWriter())) + using (context.Push(new TagHelperHtmlAttributeRuntimeNodeWriter())) { context.RenderChildren(node); } - context.Writer + context.CodeWriter .WriteMethodInvocation( EndAddHtmlAttributeValuesMethodName, ExecutionContextVariableName); @@ -289,18 +295,18 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration // determine its final value. // Attribute value is not plain text, must be buffered to determine its final value. - context.Writer.WriteMethodInvocation(BeginWriteTagHelperAttributeMethodName); + context.CodeWriter.WriteMethodInvocation(BeginWriteTagHelperAttributeMethodName); // 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.Push(new RuntimeBasicWriter())) + using (context.Push(new RuntimeNodeWriter())) using (context.Push(new RuntimeTagHelperWriter())) { context.RenderChildren(node); } - context.Writer + context.CodeWriter .WriteStartAssignment(StringValueBufferVariableName) .WriteMethodInvocation(EndWriteTagHelperAttributeMethodName) .WriteStartInstanceMethodInvocation( @@ -317,7 +323,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } } - public override void WriteSetTagHelperProperty(CSharpRenderingContext context, SetTagHelperPropertyIntermediateNode node) + public override void WriteSetTagHelperProperty(CodeRenderingContext context, SetTagHelperPropertyIntermediateNode node) { var tagHelperVariableName = GetTagHelperVariableName(node.TagHelperTypeName); var tagHelperRenderingContext = context.TagHelperRenderingContext; @@ -328,17 +334,17 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration tagHelperRenderingContext.VerifiedPropertyDictionaries.Add($"{node.TagHelperTypeName}.{propertyName}")) { // Throw a reasonable Exception at runtime if the dictionary property is null. - context.Writer + context.CodeWriter .Write("if (") .Write(tagHelperVariableName) .Write(".") .Write(propertyName) .WriteLine(" == null)"); - using (context.Writer.BuildScope()) + using (context.CodeWriter.BuildScope()) { // System is in Host.NamespaceImports for all MVC scenarios. No need to generate FullName // of InvalidOperationException type. - context.Writer + context.CodeWriter .Write("throw ") .WriteStartNewObject(nameof(InvalidOperationException)) .WriteStartMethodInvocation(FormatInvalidIndexerAssignmentMethodName) @@ -356,7 +362,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration if (tagHelperRenderingContext.RenderedBoundAttributes.TryGetValue(node.AttributeName, out var previousValueAccessor)) { - context.Writer + context.CodeWriter .WriteStartAssignment(propertyValueAccessor) .Write(previousValueAccessor) .WriteLine(";"); @@ -370,14 +376,14 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration if (node.Descriptor.IsStringProperty || (node.IsIndexerNameMatch && node.Descriptor.IsIndexerStringProperty)) { - context.Writer.WriteMethodInvocation(BeginWriteTagHelperAttributeMethodName); + context.CodeWriter.WriteMethodInvocation(BeginWriteTagHelperAttributeMethodName); - using (context.Push(new LiteralRuntimeBasicWriter())) + using (context.Push(new LiteralRuntimeNodeWriter())) { context.RenderChildren(node); } - context.Writer + context.CodeWriter .WriteStartAssignment(StringValueBufferVariableName) .WriteMethodInvocation(EndWriteTagHelperAttributeMethodName) .WriteStartAssignment(propertyValueAccessor) @@ -386,16 +392,16 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } else { - using (context.Writer.BuildLinePragma(node.Source.Value)) + using (context.CodeWriter.BuildLinePragma(node.Source.Value)) { - context.Writer.WriteStartAssignment(propertyValueAccessor); + context.CodeWriter.WriteStartAssignment(propertyValueAccessor); if (node.Descriptor.IsEnum && node.Children.Count == 1 && node.Children.First() is IntermediateToken token && token.IsCSharp) { - context.Writer + context.CodeWriter .Write("global::") .Write(node.Descriptor.TypeName) .Write("."); @@ -403,12 +409,12 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration RenderTagHelperAttributeInline(context, node, node.Source.Value); - context.Writer.WriteLine(";"); + context.CodeWriter.WriteLine(";"); } } // We need to inform the context of the attribute value. - context.Writer + context.CodeWriter .WriteStartInstanceMethodInvocation( ExecutionContextVariableName, ExecutionContextAddTagHelperAttributeMethodName) @@ -421,7 +427,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } private void RenderTagHelperAttributeInline( - CSharpRenderingContext context, + CodeRenderingContext context, SetTagHelperPropertyIntermediateNode property, SourceSpan documentLocation) { @@ -432,7 +438,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } private void RenderTagHelperAttributeInline( - CSharpRenderingContext context, + CodeRenderingContext context, SetTagHelperPropertyIntermediateNode property, IntermediateNode node, SourceSpan documentLocation) @@ -446,7 +452,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration } else if (node is IntermediateToken token) { - context.Writer.Write(token.Content); + context.CodeWriter.Write(token.Content); } else if (node is CSharpCodeIntermediateNode) { diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/TagHelperHtmlAttributeRuntimeBasicWriter.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/TagHelperHtmlAttributeRuntimeNodeWriter.cs similarity index 81% rename from src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/TagHelperHtmlAttributeRuntimeBasicWriter.cs rename to src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/TagHelperHtmlAttributeRuntimeNodeWriter.cs index f70bed7623..eec5a76575 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/TagHelperHtmlAttributeRuntimeBasicWriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/TagHelperHtmlAttributeRuntimeNodeWriter.cs @@ -3,7 +3,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - internal class TagHelperHtmlAttributeRuntimeBasicWriter : RuntimeBasicWriter + internal class TagHelperHtmlAttributeRuntimeNodeWriter : RuntimeNodeWriter { public override string WriteAttributeValueMethod { get; set; } = "AddHtmlAttributeValue"; } diff --git a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/TagHelperWriter.cs b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/TagHelperWriter.cs index cf1422f1ee..47ca6393db 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/TagHelperWriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/CodeGeneration/TagHelperWriter.cs @@ -5,18 +5,18 @@ using Microsoft.AspNetCore.Razor.Language.Intermediate; namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - public abstract class TagHelperWriter + internal abstract class TagHelperWriter { - public abstract void WriteDeclareTagHelperFields(CSharpRenderingContext context, DeclareTagHelperFieldsIntermediateNode node); + public abstract void WriteDeclareTagHelperFields(CodeRenderingContext context, DeclareTagHelperFieldsIntermediateNode node); - public abstract void WriteTagHelper(CSharpRenderingContext context, TagHelperIntermediateNode node); + public abstract void WriteTagHelper(CodeRenderingContext context, TagHelperIntermediateNode node); - public abstract void WriteTagHelperBody(CSharpRenderingContext context, TagHelperBodyIntermediateNode node); + public abstract void WriteTagHelperBody(CodeRenderingContext context, TagHelperBodyIntermediateNode node); - public abstract void WriteCreateTagHelper(CSharpRenderingContext context, CreateTagHelperIntermediateNode node); + public abstract void WriteCreateTagHelper(CodeRenderingContext context, CreateTagHelperIntermediateNode node); - public abstract void WriteAddTagHelperHtmlAttribute(CSharpRenderingContext context, AddTagHelperHtmlAttributeIntermediateNode node); + public abstract void WriteAddTagHelperHtmlAttribute(CodeRenderingContext context, AddTagHelperHtmlAttributeIntermediateNode node); - public abstract void WriteSetTagHelperProperty(CSharpRenderingContext context, SetTagHelperPropertyIntermediateNode node); + public abstract void WriteSetTagHelperProperty(CodeRenderingContext context, SetTagHelperPropertyIntermediateNode node); } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorCSharpLoweringPhase.cs b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorCSharpLoweringPhase.cs index 460f6a3a13..ef10953c25 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorCSharpLoweringPhase.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/DefaultRazorCSharpLoweringPhase.cs @@ -10,10 +10,6 @@ namespace Microsoft.AspNetCore.Razor.Language { internal class DefaultRazorCSharpLoweringPhase : RazorEnginePhaseBase, IRazorCSharpLoweringPhase { - internal static readonly object NewLineString = "NewLineString"; - - internal static readonly object SuppressUniqueIds = "SuppressUniqueIds"; - protected override void ExecuteCore(RazorCodeDocument codeDocument) { var irDocument = codeDocument.GetDocumentIntermediateNode(); @@ -29,40 +25,20 @@ namespace Microsoft.AspNetCore.Razor.Language throw new InvalidOperationException(message); } - var codeWriter = new CSharpCodeWriter(); - var newLineString = codeDocument.Items[NewLineString]; - if (newLineString != null) - { - // Set new line character to a specific string regardless of platform, for testing purposes. - codeWriter.NewLine = (string)newLineString; - } - - var renderingContext = new CSharpRenderingContext() - { - Writer = codeWriter, - CodeDocument = codeDocument, - Options = irDocument.Options, - }; - - var idValue = codeDocument.Items[SuppressUniqueIds]; - if (idValue != null) - { - // Generate a static value for unique ids instead of a guid, for testing purposes. - renderingContext.IdGenerator = () => idValue.ToString(); - } - - var documentWriter = target.CreateWriter(renderingContext); + var context = CodeRenderingContext.Create(codeDocument, irDocument.Options); + var documentWriter = target.CreateWriter(context); documentWriter.WriteDocument(irDocument); var diagnostics = new List(); diagnostics.AddRange(irDocument.GetAllDiagnostics()); - diagnostics.AddRange(renderingContext.Diagnostics); + diagnostics.AddRange(context.Diagnostics); + var lineMappings = context.GetLineMappings(); var csharpDocument = RazorCSharpDocument.Create( - renderingContext.Writer.GenerateCode(), + context.CodeWriter.GenerateCode(), irDocument.Options, diagnostics, - renderingContext.LineMappings); + lineMappings); codeDocument.SetCSharpDocument(csharpDocument); } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/AddPreallocatedTagHelperHtmlAttributeIntermediateNode.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/AddPreallocatedTagHelperHtmlAttributeIntermediateNode.cs index fc207b827b..d302b5b30d 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/AddPreallocatedTagHelperHtmlAttributeIntermediateNode.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/AddPreallocatedTagHelperHtmlAttributeIntermediateNode.cs @@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions AcceptExtensionNode(this, visitor); } - public override void WriteNode(CodeTarget target, CSharpRenderingContext context) + public override void WriteNode(CodeTarget target, CodeRenderingContext context) { if (target == null) { @@ -42,7 +42,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions var extension = target.GetExtension(); if (extension == null) { - context.ReportMissingExtension(); + ReportMissingCodeTargetExtension(context); return; } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/DeclarePreallocatedTagHelperAttributeIntermediateNode.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/DeclarePreallocatedTagHelperAttributeIntermediateNode.cs index 6423f47b25..4671dd600b 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/DeclarePreallocatedTagHelperAttributeIntermediateNode.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/DeclarePreallocatedTagHelperAttributeIntermediateNode.cs @@ -33,7 +33,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions AcceptExtensionNode(this, visitor); } - public override void WriteNode(CodeTarget target, CSharpRenderingContext context) + public override void WriteNode(CodeTarget target, CodeRenderingContext context) { if (target == null) { @@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions var extension = target.GetExtension(); if (extension == null) { - context.ReportMissingExtension(); + ReportMissingCodeTargetExtension(context); return; } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/DeclarePreallocatedTagHelperHtmlAttributeIntermediateNode.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/DeclarePreallocatedTagHelperHtmlAttributeIntermediateNode.cs index 54e30cb8c7..54ad60cb88 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/DeclarePreallocatedTagHelperHtmlAttributeIntermediateNode.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/DeclarePreallocatedTagHelperHtmlAttributeIntermediateNode.cs @@ -33,7 +33,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions AcceptExtensionNode(this, visitor); } - public override void WriteNode(CodeTarget target, CSharpRenderingContext context) + public override void WriteNode(CodeTarget target, CodeRenderingContext context) { if (target == null) { @@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions var extension = target.GetExtension(); if (extension == null) { - context.ReportMissingExtension(); + ReportMissingCodeTargetExtension(context); return; } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectiveIntermediateNode.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectiveIntermediateNode.cs index bc038d5ad3..1898b8fb00 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectiveIntermediateNode.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectiveIntermediateNode.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions AcceptExtensionNode(this, visitor); } - public override void WriteNode(CodeTarget target, CSharpRenderingContext context) + public override void WriteNode(CodeTarget target, CodeRenderingContext context) { if (target == null) { @@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions var extension = target.GetExtension(); if (extension == null) { - context.ReportMissingExtension(); + ReportMissingCodeTargetExtension(context); return; } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectiveTargetExtension.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectiveTargetExtension.cs index 0536246d97..cedc37fe2c 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectiveTargetExtension.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/DesignTimeDirectiveTargetExtension.cs @@ -12,9 +12,9 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions private const string DirectiveTokenHelperMethodName = "__RazorDirectiveTokenHelpers__"; private const string TypeHelper = "__typeHelper"; - public void WriteDesignTimeDirective(CSharpRenderingContext context, DesignTimeDirectiveIntermediateNode node) + public void WriteDesignTimeDirective(CodeRenderingContext context, DesignTimeDirectiveIntermediateNode node) { - context.Writer + context.CodeWriter .WriteLine("#pragma warning disable 219") .WriteLine($"private void {DirectiveTokenHelperMethodName}() {{"); @@ -26,12 +26,12 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions } } - context.Writer + context.CodeWriter .WriteLine("}") .WriteLine("#pragma warning restore 219"); } - private void WriteDesignTimeDirectiveToken(CSharpRenderingContext context, DirectiveTokenIntermediateNode node) + private void WriteDesignTimeDirectiveToken(CodeRenderingContext context, DirectiveTokenIntermediateNode node) { var tokenKind = node.Descriptor.Kind; if (!node.Source.HasValue || @@ -45,14 +45,14 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions } // Wrap the directive token in a lambda to isolate variable names. - context.Writer + context.CodeWriter .Write("((") .Write(typeof(Action).FullName) .Write(")("); - using (context.Writer.BuildLambda()) + using (context.CodeWriter.BuildLambda()) { - var originalIndent = context.Writer.CurrentIndent; - context.Writer.CurrentIndent = 0; + var originalIndent = context.CodeWriter.CurrentIndent; + context.CodeWriter.CurrentIndent = 0; switch (tokenKind) { case DirectiveTokenKind.Type: @@ -60,7 +60,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions // {node.Content} __typeHelper = default({node.Content}); context.AddLineMappingFor(node); - context.Writer + context.CodeWriter .Write(node.Content) .Write(" ") .WriteStartAssignment(TypeHelper) @@ -73,13 +73,13 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions // global::System.Object {node.content} = null; - context.Writer + context.CodeWriter .Write("global::") .Write(typeof(object).FullName) .Write(" "); context.AddLineMappingFor(node); - context.Writer + context.CodeWriter .Write(node.Content) .WriteLine(" = null;"); break; @@ -88,16 +88,16 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions // global::System.Object __typeHelper = nameof({node.Content}); - context.Writer + context.CodeWriter .Write("global::") .Write(typeof(object).FullName) .Write(" ") .WriteStartAssignment(TypeHelper); - context.Writer.Write("nameof("); + context.CodeWriter.Write("nameof("); context.AddLineMappingFor(node); - context.Writer + context.CodeWriter .Write(node.Content) .WriteLine(");"); break; @@ -106,7 +106,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions // global::System.Object __typeHelper = "{node.Content}"; - context.Writer + context.CodeWriter .Write("global::") .Write(typeof(object).FullName) .Write(" ") @@ -115,23 +115,23 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions if (node.Content.StartsWith("\"", StringComparison.Ordinal)) { context.AddLineMappingFor(node); - context.Writer.Write(node.Content); + context.CodeWriter.Write(node.Content); } else { - context.Writer.Write("\""); + context.CodeWriter.Write("\""); context.AddLineMappingFor(node); - context.Writer + context.CodeWriter .Write(node.Content) .Write("\""); } - context.Writer.WriteLine(";"); + context.CodeWriter.WriteLine(";"); break; } - context.Writer.CurrentIndent = originalIndent; + context.CodeWriter.CurrentIndent = originalIndent; } - context.Writer.WriteLine("))();"); + context.CodeWriter.WriteLine("))();"); } } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/IDesignTimeDirectiveTargetExtension.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/IDesignTimeDirectiveTargetExtension.cs index 67aa5ea482..8ffde8178d 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/IDesignTimeDirectiveTargetExtension.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/IDesignTimeDirectiveTargetExtension.cs @@ -8,6 +8,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { internal interface IDesignTimeDirectiveTargetExtension : ICodeTargetExtension { - void WriteDesignTimeDirective(CSharpRenderingContext context, DesignTimeDirectiveIntermediateNode node); + void WriteDesignTimeDirective(CodeRenderingContext context, DesignTimeDirectiveIntermediateNode node); } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/IPreallocatedAttributeTargetExtension.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/IPreallocatedAttributeTargetExtension.cs index 85cd26fce0..322e1fc400 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/IPreallocatedAttributeTargetExtension.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/IPreallocatedAttributeTargetExtension.cs @@ -8,12 +8,12 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { internal interface IPreallocatedAttributeTargetExtension : ICodeTargetExtension { - void WriteDeclarePreallocatedTagHelperHtmlAttribute(CSharpRenderingContext context, DeclarePreallocatedTagHelperHtmlAttributeIntermediateNode node); + void WriteDeclarePreallocatedTagHelperHtmlAttribute(CodeRenderingContext context, DeclarePreallocatedTagHelperHtmlAttributeIntermediateNode node); - void WriteAddPreallocatedTagHelperHtmlAttribute(CSharpRenderingContext context, AddPreallocatedTagHelperHtmlAttributeIntermediateNode node); + void WriteAddPreallocatedTagHelperHtmlAttribute(CodeRenderingContext context, AddPreallocatedTagHelperHtmlAttributeIntermediateNode node); - void WriteDeclarePreallocatedTagHelperAttribute(CSharpRenderingContext context, DeclarePreallocatedTagHelperAttributeIntermediateNode node); + void WriteDeclarePreallocatedTagHelperAttribute(CodeRenderingContext context, DeclarePreallocatedTagHelperAttributeIntermediateNode node); - void WriteSetPreallocatedTagHelperProperty(CSharpRenderingContext context, SetPreallocatedTagHelperPropertyIntermediateNode node); + void WriteSetPreallocatedTagHelperProperty(CodeRenderingContext context, SetPreallocatedTagHelperPropertyIntermediateNode node); } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/ISectionTargetExtension.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/ISectionTargetExtension.cs index 3cfe15f0a0..1398eeb9da 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/ISectionTargetExtension.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/ISectionTargetExtension.cs @@ -7,6 +7,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { public interface ISectionTargetExtension : ICodeTargetExtension { - void WriteSection(CSharpRenderingContext context, SectionIntermediateNode node); + void WriteSection(CodeRenderingContext context, SectionIntermediateNode node); } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/ITemplateTargetExtension.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/ITemplateTargetExtension.cs index ace89af164..8d1568e624 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/ITemplateTargetExtension.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/ITemplateTargetExtension.cs @@ -7,6 +7,6 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { public interface ITemplateTargetExtension : ICodeTargetExtension { - void WriteTemplate(CSharpRenderingContext context, TemplateIntermediateNode node); + void WriteTemplate(CodeRenderingContext context, TemplateIntermediateNode node); } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/PreallocatedAttributeTargetExtension.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/PreallocatedAttributeTargetExtension.cs index 85725b1f06..0566a0881f 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/PreallocatedAttributeTargetExtension.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/PreallocatedAttributeTargetExtension.cs @@ -20,9 +20,9 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions public string FormatInvalidIndexerAssignmentMethodName { get; set; } = "InvalidTagHelperIndexerAssignment"; - public void WriteDeclarePreallocatedTagHelperHtmlAttribute(CSharpRenderingContext context, DeclarePreallocatedTagHelperHtmlAttributeIntermediateNode node) + public void WriteDeclarePreallocatedTagHelperHtmlAttribute(CodeRenderingContext context, DeclarePreallocatedTagHelperHtmlAttributeIntermediateNode node) { - context.Writer + context.CodeWriter .Write("private static readonly global::") .Write(TagHelperAttributeTypeName) .Write(" ") @@ -33,11 +33,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions if (node.AttributeStructure == AttributeStructure.Minimized) { - context.Writer.WriteEndMethodInvocation(); + context.CodeWriter.WriteEndMethodInvocation(); } else { - context.Writer + context.CodeWriter .WriteParameterSeparator() .WriteStartNewObject("global::" + EncodedHtmlStringTypeName) .WriteStringLiteral(node.Value) @@ -48,17 +48,17 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions } } - public void WriteAddPreallocatedTagHelperHtmlAttribute(CSharpRenderingContext context, AddPreallocatedTagHelperHtmlAttributeIntermediateNode node) + public void WriteAddPreallocatedTagHelperHtmlAttribute(CodeRenderingContext context, AddPreallocatedTagHelperHtmlAttributeIntermediateNode node) { - context.Writer + context.CodeWriter .WriteStartInstanceMethodInvocation(ExecutionContextVariableName, ExecutionContextAddHtmlAttributeMethodName) .Write(node.VariableName) .WriteEndMethodInvocation(); } - public void WriteDeclarePreallocatedTagHelperAttribute(CSharpRenderingContext context, DeclarePreallocatedTagHelperAttributeIntermediateNode node) + public void WriteDeclarePreallocatedTagHelperAttribute(CodeRenderingContext context, DeclarePreallocatedTagHelperAttributeIntermediateNode node) { - context.Writer + context.CodeWriter .Write("private static readonly global::") .Write(TagHelperAttributeTypeName) .Write(" ") @@ -73,7 +73,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions .WriteEndMethodInvocation(); } - public void WriteSetPreallocatedTagHelperProperty(CSharpRenderingContext context, SetPreallocatedTagHelperPropertyIntermediateNode node) + public void WriteSetPreallocatedTagHelperProperty(CodeRenderingContext context, SetPreallocatedTagHelperPropertyIntermediateNode node) { var tagHelperVariableName = GetTagHelperVariableName(node.TagHelperTypeName); var propertyName = node.Descriptor.GetPropertyName(); @@ -85,17 +85,17 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions context.TagHelperRenderingContext.VerifiedPropertyDictionaries.Add($"{node.TagHelperTypeName}.{propertyName}")) { // Throw a reasonable Exception at runtime if the dictionary property is null. - context.Writer + context.CodeWriter .Write("if (") .Write(tagHelperVariableName) .Write(".") .Write(propertyName) .WriteLine(" == null)"); - using (context.Writer.BuildScope()) + using (context.CodeWriter.BuildScope()) { // System is in Host.NamespaceImports for all MVC scenarios. No need to generate FullName // of InvalidOperationException type. - context.Writer + context.CodeWriter .Write("throw ") .WriteStartNewObject(nameof(InvalidOperationException)) .WriteStartMethodInvocation(FormatInvalidIndexerAssignmentMethodName) @@ -109,7 +109,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions } } - context.Writer + context.CodeWriter .WriteStartAssignment(propertyValueAccessor) .Write("(string)") .Write(attributeValueAccessor) diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionIntermediateNode.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionIntermediateNode.cs index 86f48f9cc8..bd4039460c 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionIntermediateNode.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionIntermediateNode.cs @@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions AcceptExtensionNode(this, visitor); } - public override void WriteNode(CodeTarget target, CSharpRenderingContext context) + public override void WriteNode(CodeTarget target, CodeRenderingContext context) { if (target == null) { @@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions var extension = target.GetExtension(); if (extension == null) { - context.ReportMissingExtension(); + ReportMissingCodeTargetExtension(context); return; } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionTargetExtension.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionTargetExtension.cs index 74383e319a..274dda0590 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionTargetExtension.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionTargetExtension.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions public string SectionMethodName { get; set; } = DefaultSectionMethodName; - public void WriteSection(CSharpRenderingContext context, SectionIntermediateNode node) + public void WriteSection(CodeRenderingContext context, SectionIntermediateNode node) { // Quirk Alert! // @@ -26,18 +26,18 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions // design time, at least until we have multi-targeting. var writerName = context.Options.DesignTime ? DefaultWriterName : string.Empty; - context.Writer + context.CodeWriter .WriteStartMethodInvocation(SectionMethodName) .Write("\"") .Write(node.Name) .Write("\", "); - using (context.Writer.BuildAsyncLambda(writerName)) + using (context.CodeWriter.BuildAsyncLambda(writerName)) { context.RenderChildren(node); } - context.Writer.WriteEndMethodInvocation(endLine: true); + context.CodeWriter.WriteEndMethodInvocation(endLine: true); } } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/SetPreallocatedTagHelperPropertyIntermediateNode.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/SetPreallocatedTagHelperPropertyIntermediateNode.cs index 7660d948ba..6635a6fce7 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/SetPreallocatedTagHelperPropertyIntermediateNode.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/SetPreallocatedTagHelperPropertyIntermediateNode.cs @@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions AcceptExtensionNode(this, visitor); } - public override void WriteNode(CodeTarget target, CSharpRenderingContext context) + public override void WriteNode(CodeTarget target, CodeRenderingContext context) { if (target == null) { @@ -54,7 +54,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions var extension = target.GetExtension(); if (extension == null) { - context.ReportMissingExtension(); + ReportMissingCodeTargetExtension(context); return; } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/TemplateIntermediateNode.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/TemplateIntermediateNode.cs index aa9c8e5679..3a342a02eb 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/TemplateIntermediateNode.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/TemplateIntermediateNode.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions AcceptExtensionNode(this, visitor); } - public override void WriteNode(CodeTarget target, CSharpRenderingContext context) + public override void WriteNode(CodeTarget target, CodeRenderingContext context) { if (target == null) { @@ -36,7 +36,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions var extension = target.GetExtension(); if (extension == null) { - context.ReportMissingExtension(); + ReportMissingCodeTargetExtension(context); return; } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Extensions/TemplateTargetExtension.cs b/src/Microsoft.AspNetCore.Razor.Language/Extensions/TemplateTargetExtension.cs index 24b9d511a3..8ec532e21a 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Extensions/TemplateTargetExtension.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Extensions/TemplateTargetExtension.cs @@ -11,25 +11,25 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions public string TemplateTypeName { get; set; } = DefaultTemplateTypeName; - public void WriteTemplate(CSharpRenderingContext context, TemplateIntermediateNode node) + public void WriteTemplate(CodeRenderingContext context, TemplateIntermediateNode node) { const string ItemParameterName = "item"; const string TemplateWriterName = "__razor_template_writer"; - context.Writer + context.CodeWriter .Write(ItemParameterName).Write(" => ") .WriteStartNewObject(TemplateTypeName); - using (context.Writer.BuildAsyncLambda(TemplateWriterName)) + using (context.CodeWriter.BuildAsyncLambda(TemplateWriterName)) { - context.BasicWriter.BeginWriterScope(context, TemplateWriterName); + context.NodeWriter.BeginWriterScope(context, TemplateWriterName); context.RenderChildren(node); - context.BasicWriter.EndWriterScope(context); + context.NodeWriter.EndWriterScope(context); } - context.Writer.WriteEndMethodInvocation(endLine: false); + context.CodeWriter.WriteEndMethodInvocation(endLine: false); } } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Intermediate/ExtensionIntermediateNode.cs b/src/Microsoft.AspNetCore.Razor.Language/Intermediate/ExtensionIntermediateNode.cs index b49bc19d0a..f744337486 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Intermediate/ExtensionIntermediateNode.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Intermediate/ExtensionIntermediateNode.cs @@ -1,6 +1,7 @@ // 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.CodeGeneration; namespace Microsoft.AspNetCore.Razor.Language.Intermediate @@ -40,7 +41,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate public override bool HasDiagnostics => _diagnostics != null && _diagnostics.Count > 0; - public abstract void WriteNode(CodeTarget target, CSharpRenderingContext context); + public abstract void WriteNode(CodeTarget target, CodeRenderingContext context); protected static void AcceptExtensionNode(TNode node, IntermediateNodeVisitor visitor) where TNode : ExtensionIntermediateNode @@ -55,5 +56,19 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate typedVisitor.VisitExtension(node); } } + + protected void ReportMissingCodeTargetExtension(CodeRenderingContext context) + { + if (context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + var documentKind = context.DocumentKind ?? string.Empty; + context.Diagnostics.Add( + RazorDiagnosticFactory.CreateCodeTarget_UnsupportedExtension( + documentKind, + typeof(TDependency))); + } } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/Properties/Resources.Designer.cs b/src/Microsoft.AspNetCore.Razor.Language/Properties/Resources.Designer.cs index a11ff244ba..3850448e4e 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Properties/Resources.Designer.cs @@ -570,6 +570,20 @@ namespace Microsoft.AspNetCore.Razor.Language internal static string FormatUnexpectedDirectiveKind(object p0) => string.Format(CultureInfo.CurrentCulture, GetString("UnexpectedDirectiveKind"), p0); + /// + /// The '{0}' requires a '{1}' delegate to be set. + /// + internal static string RenderingContextRequiresDelegate + { + get => GetString("RenderingContextRequiresDelegate"); + } + + /// + /// The '{0}' requires a '{1}' delegate to be set. + /// + internal static string FormatRenderingContextRequiresDelegate(object p0, object p1) + => string.Format(CultureInfo.CurrentCulture, GetString("RenderingContextRequiresDelegate"), p0, p1); + private static string GetString(string name, params string[] formatterNames) { var value = _resourceManager.GetString(name); diff --git a/src/Microsoft.AspNetCore.Razor.Language/Resources.resx b/src/Microsoft.AspNetCore.Razor.Language/Resources.resx index 6e07cf8e3d..fc64c848ea 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Resources.resx +++ b/src/Microsoft.AspNetCore.Razor.Language/Resources.resx @@ -237,4 +237,7 @@ Unreachable code. This can happen when a new {0} is introduced. + + The '{0}' requires a '{1}' delegate to be set. + \ No newline at end of file diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/InjectTargetExtensionTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/InjectTargetExtensionTest.cs index 99e27e068c..b853c76466 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/InjectTargetExtensionTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/InjectTargetExtensionTest.cs @@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions Assert.Equal( "[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]" + Environment.NewLine + "public PropertyType PropertyName { get; private set; }" + Environment.NewLine, - context.Writer.Builder.ToString()); + context.CodeWriter.Builder.ToString()); } [Fact] @@ -61,15 +61,17 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions "public PropertyType PropertyName { get; private set; }" + Environment.NewLine + Environment.NewLine + "#line default" + Environment.NewLine + "#line hidden" + Environment.NewLine, - context.Writer.Builder.ToString()); + context.CodeWriter.Builder.ToString()); } - private CSharpRenderingContext GetRenderingContext() + private CodeRenderingContext GetRenderingContext() { - return new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter() - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument: null, options: options); + + return context; } } } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/CSharpCodeWriterTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/CSharpCodeWriterTest.cs index d42c681534..fdd149a742 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/CSharpCodeWriterTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/CSharpCodeWriterTest.cs @@ -29,7 +29,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithWrite() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.Write("1234"); @@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithIndent() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.WriteLine(); @@ -62,7 +62,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithWriteLine() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.WriteLine("1234"); @@ -80,7 +80,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithWriteLine_WithNewLineInContent(string newLine) { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.WriteLine("1234" + newLine + "12"); @@ -101,7 +101,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithWrite_WithNewlineInContent(string newLine) { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.Write("1234" + newLine + "123" + newLine + "12"); @@ -121,7 +121,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithWrite_WithNewlineInContent_RepeatedN() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.Write("1234\n\n123"); @@ -141,7 +141,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithWrite_WithMixedNewlineInContent() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.Write("1234\r123\r\n12\n1"); @@ -161,7 +161,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithNewline_SplitAcrossWrites() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.Write("1234\r"); @@ -182,7 +182,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithTwoNewline_SplitAcrossWrites_R() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.Write("1234\r"); @@ -203,7 +203,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithTwoNewline_SplitAcrossWrites_N() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.Write("1234\n"); @@ -224,7 +224,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithTwoNewline_SplitAcrossWrites_Reversed() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.Write("1234\n"); @@ -245,7 +245,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CSharpCodeWriter_TracksPosition_WithNewline_SplitAcrossWrites_AtBeginning() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.Write("\r"); @@ -267,7 +267,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { // Arrange var filePath = "some-path"; - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); var expected = $"#line 5 \"{filePath}\"" + writer.NewLine; var sourceLocation = new SourceLocation(10, 4, 3); var mappingLocation = new SourceSpan(sourceLocation, 9); @@ -288,7 +288,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { // Arrange var filePath = "some-path"; - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); var expected = $"#line 5 \"{sourceLocationFilePath}\"" + writer.NewLine; var sourceLocation = new SourceLocation(sourceLocationFilePath, 10, 4, 3); var mappingLocation = new SourceSpan(sourceLocation, 9); @@ -305,7 +305,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void WriteField_WritesFieldDeclaration() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.WriteField(new[] { "private" }, "global::System.String", "_myString"); @@ -319,7 +319,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void WriteField_WithModifiers_WritesFieldDeclaration() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.WriteField(new[] { "private", "readonly", "static" }, "global::System.String", "_myString"); @@ -333,7 +333,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void WriteAutoPropertyDeclaration_WritesPropertyDeclaration() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.WriteAutoPropertyDeclaration(new[] { "public" }, "global::System.String", "MyString"); @@ -347,7 +347,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void WriteAutoPropertyDeclaration_WithModifiers_WritesPropertyDeclaration() { // Arrange - var writer = new CSharpCodeWriter(); + var writer = new CodeWriter(); // Act writer.WriteAutoPropertyDeclaration(new[] { "public", "static" }, "global::System.String", "MyString"); diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DefaultCodeTargetTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DefaultCodeTargetTest.cs index d661e5f487..a01dad9bbf 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DefaultCodeTargetTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DefaultCodeTargetTest.cs @@ -31,12 +31,14 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void CreateWriter_CreatesDefaultDocumentWriter() { // Arrange + var codeWriter = new CodeWriter(); + var codeDocument = TestRazorCodeDocument.Create(string.Empty); var options = RazorCodeGenerationOptions.CreateDefault(); - + var context = CodeRenderingContext.Create(codeDocument, options); var target = new DefaultCodeTarget(options, Enumerable.Empty()); // Act - var writer = target.CreateWriter(new CSharpRenderingContext()); + var writer = target.CreateWriter(context); // Assert Assert.IsType(writer); diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DefaultDocumentWriterTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DefaultDocumentWriterTest.cs index 2d0cd0314b..a7aa1140c8 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DefaultDocumentWriterTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DefaultDocumentWriterTest.cs @@ -17,12 +17,9 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration var options = RazorCodeGenerationOptions.CreateDefault(); var target = CodeTarget.CreateDefault(codeDocument, options); - var context = new CSharpRenderingContext() - { - Options = options, - Writer = new CSharpCodeWriter(), - CodeDocument = codeDocument - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, codeDocument.Source, options); var writer = new DefaultDocumentWriter(target, context); @@ -33,7 +30,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.WriteDocument(document); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma checksum ""test.cshtml"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""da39a3ee5e6b4b0d3255bfef95601890afd80709"" // @@ -56,12 +53,9 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration var options = optionsBuilder.Build(); var target = CodeTarget.CreateDefault(codeDocument, options); - var context = new CSharpRenderingContext() - { - Options = options, - Writer = new CSharpCodeWriter(), - CodeDocument = codeDocument - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, codeDocument.Source, options); var writer = new DefaultDocumentWriter(target, context); @@ -72,7 +66,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.WriteDocument(document); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"// #pragma warning disable 1591 @@ -90,12 +84,9 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration var options = RazorCodeGenerationOptions.CreateDefault(); var target = CodeTarget.CreateDefault(codeDocument, options); - var context = new CSharpRenderingContext() - { - Options = options, - Writer = new CSharpCodeWriter(), - CodeDocument = codeDocument - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, codeDocument.Source, options); var writer = new DefaultDocumentWriter(target, context); @@ -110,7 +101,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.WriteDocument(document); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma checksum ""test.cshtml"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""da39a3ee5e6b4b0d3255bfef95601890afd80709"" // @@ -133,12 +124,9 @@ namespace TestNamespace var options = RazorCodeGenerationOptions.CreateDefault(); var target = CodeTarget.CreateDefault(codeDocument, options); - var context = new CSharpRenderingContext() - { - Options = options, - Writer = new CSharpCodeWriter(), - CodeDocument = codeDocument - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, codeDocument.Source, options); var writer = new DefaultDocumentWriter(target, context); var document = new DocumentIntermediateNode(); @@ -158,7 +146,7 @@ namespace TestNamespace writer.WriteDocument(document); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma checksum ""test.cshtml"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""da39a3ee5e6b4b0d3255bfef95601890afd80709"" // @@ -180,12 +168,9 @@ internal class TestClass : TestBase, IFoo, IBar var options = RazorCodeGenerationOptions.CreateDefault(); var target = CodeTarget.CreateDefault(codeDocument, options); - var context = new CSharpRenderingContext() - { - Options = options, - Writer = new CSharpCodeWriter(), - CodeDocument = codeDocument - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, codeDocument.Source, options); var writer = new DefaultDocumentWriter(target, context); var document = new DocumentIntermediateNode(); @@ -206,7 +191,7 @@ internal class TestClass : TestBase, IFoo, IBar writer.WriteDocument(document); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma checksum ""test.cshtml"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""da39a3ee5e6b4b0d3255bfef95601890afd80709"" // @@ -230,12 +215,9 @@ internal virtual async string TestMethod() var options = RazorCodeGenerationOptions.CreateDefault(); var target = CodeTarget.CreateDefault(codeDocument, options); - var context = new CSharpRenderingContext() - { - Options = options, - Writer = new CSharpCodeWriter(), - CodeDocument = codeDocument - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, codeDocument.Source, options); var writer = new DefaultDocumentWriter(target, context); var document = new DocumentIntermediateNode(); @@ -255,7 +237,7 @@ internal virtual async string TestMethod() writer.WriteDocument(document); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma checksum ""test.cshtml"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""da39a3ee5e6b4b0d3255bfef95601890afd80709"" // @@ -275,12 +257,9 @@ internal readonly string _foo; var options = RazorCodeGenerationOptions.CreateDefault(); var target = CodeTarget.CreateDefault(codeDocument, options); - var context = new CSharpRenderingContext() - { - Options = options, - Writer = new CSharpCodeWriter(), - CodeDocument = codeDocument - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, codeDocument.Source, options); var writer = new DefaultDocumentWriter(target, context); var document = new DocumentIntermediateNode(); @@ -300,7 +279,7 @@ internal readonly string _foo; writer.WriteDocument(document); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma checksum ""test.cshtml"" ""{ff1816ec-aa5e-4d10-87f7-6f4963833460}"" ""da39a3ee5e6b4b0d3255bfef95601890afd80709"" // diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DesignTimeBasicWriterTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DesignTimeNodeWriterTest.cs similarity index 70% rename from test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DesignTimeBasicWriterTest.cs rename to test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DesignTimeNodeWriterTest.cs index 4d800c236d..a40e2741da 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DesignTimeBasicWriterTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DesignTimeNodeWriterTest.cs @@ -2,24 +2,22 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Razor.Language.Intermediate; using Xunit; namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - public class DesignTimeBasicWriterTest + public class DesignTimeNodeWriterTest { [Fact] public void WriteUsingDirective_NoSource_WritesContent() { // Arrange - var writer = new DesignTimeBasicWriter(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter() - }; + var writer = new DesignTimeNodeWriter(); + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new UsingDirectiveIntermediateNode() { @@ -30,7 +28,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.WriteUsingDirective(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"using System; ", @@ -42,13 +40,11 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void WriteUsingDirective_WithSource_WritesContentWithLinePragmaAndMapping() { // Arrange - var writer = new DesignTimeBasicWriter(); + var writer = new DesignTimeNodeWriter(); var sourceDocument = TestRazorSourceDocument.Create("@using System;"); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - CodeDocument = RazorCodeDocument.Create(sourceDocument) - }; + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument, options); var originalSpan = new SourceSpan("test.cshtml", 0, 0, 0, 6); var generatedSpan = new SourceSpan(null, 21 + Environment.NewLine.Length, 1, 0, 6); @@ -63,9 +59,9 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.WriteUsingDirective(context, node); // Assert - var mapping = Assert.Single(context.LineMappings); + var mapping = Assert.Single(context.GetLineMappings()); Assert.Equal(expectedLineMapping, mapping); - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" using System; @@ -81,12 +77,10 @@ using System; public void WriteCSharpExpression_SkipsLinePragma_WithoutSource() { // Arrange - var writer = new DesignTimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - }; + var writer = new DesignTimeNodeWriter(); + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpExpressionIntermediateNode(); var builder = IntermediateNodeBuilder.Create(node); @@ -100,7 +94,7 @@ using System; writer.WriteCSharpExpression(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"__o = i++; ", @@ -112,13 +106,10 @@ using System; public void WriteCSharpExpression_WritesLinePragma_WithSource() { // Arrange - var writer = new DesignTimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Options = RazorCodeGenerationOptions.CreateDefault(), - Writer = new CSharpCodeWriter(), - }; + var writer = new DesignTimeNodeWriter(); + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpExpressionIntermediateNode() { @@ -135,7 +126,7 @@ using System; writer.WriteCSharpExpression(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" __o = i++; @@ -151,12 +142,10 @@ __o = i++; public void WriteCSharpExpression_WithExtensionNode_WritesPadding() { // Arrange - var writer = new DesignTimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - }; + var writer = new DesignTimeNodeWriter(); + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpExpressionIntermediateNode(); var builder = IntermediateNodeBuilder.Create(node); @@ -172,13 +161,13 @@ __o = i++; Kind = IntermediateToken.TokenKind.CSharp, }); - context.RenderNode = (n) => Assert.IsType(n); + context.SetRenderNode((n) => Assert.IsType(n)); // Act writer.WriteCSharpExpression(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"__o = i++; ", @@ -190,15 +179,11 @@ __o = i++; public void WriteCSharpExpression_WithSource_WritesPadding() { // Arrange - var writer = new DesignTimeBasicWriter(); + var writer = new DesignTimeNodeWriter(); var sourceDocument = TestRazorSourceDocument.Create(" @i++"); - - var context = new CSharpRenderingContext() - { - Options = RazorCodeGenerationOptions.CreateDefault(), - CodeDocument = RazorCodeDocument.Create(sourceDocument), - Writer = new CSharpCodeWriter(), - }; + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument, options); var node = new CSharpExpressionIntermediateNode() { @@ -217,13 +202,13 @@ __o = i++; Kind = IntermediateToken.TokenKind.CSharp, }); - context.RenderNode = (n) => Assert.IsType(n); + context.SetRenderNode((n) => Assert.IsType(n)); // Act writer.WriteCSharpExpression(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" __o = i++; @@ -239,12 +224,10 @@ __o = i++; public void WriteCSharpCode_WhitespaceContent_DoesNothing() { // Arrange - var writer = new DesignTimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - }; + var writer = new DesignTimeNodeWriter(); + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpCodeIntermediateNode(); IntermediateNodeBuilder.Create(node) @@ -258,7 +241,7 @@ __o = i++; writer.WriteCSharpCode(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Empty(csharp); } @@ -266,13 +249,10 @@ __o = i++; public void WriteCSharpCode_WhitespaceContentWithSource_WritesContent() { // Arrange - var writer = new DesignTimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - Options = RazorCodeGenerationOptions.CreateDefault(), - }; + var writer = new DesignTimeNodeWriter(); + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpCodeIntermediateNode() { @@ -289,7 +269,7 @@ __o = i++; writer.WriteCSharpCode(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @" ", @@ -301,12 +281,10 @@ __o = i++; public void WriteCSharpCode_SkipsLinePragma_WithoutSource() { // Arrange - var writer = new DesignTimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - }; + var writer = new DesignTimeNodeWriter(); + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpCodeIntermediateNode(); IntermediateNodeBuilder.Create(node) @@ -320,7 +298,7 @@ __o = i++; writer.WriteCSharpCode(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"if (true) { } ", @@ -332,13 +310,10 @@ __o = i++; public void WriteCSharpCode_WritesLinePragma_WithSource() { // Arrange - var writer = new DesignTimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - Options = RazorCodeGenerationOptions.CreateDefault(), - }; + var writer = new DesignTimeNodeWriter(); + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpCodeIntermediateNode() { @@ -355,7 +330,7 @@ __o = i++; writer.WriteCSharpCode(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" if (true) { } @@ -371,13 +346,10 @@ if (true) { } public void WriteCSharpCode_WritesPadding_WithSource() { // Arrange - var writer = new DesignTimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - Options = RazorCodeGenerationOptions.CreateDefault(), - }; + var writer = new DesignTimeNodeWriter(); + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpCodeIntermediateNode() { @@ -394,7 +366,7 @@ if (true) { } writer.WriteCSharpCode(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" if (true) { } @@ -409,13 +381,12 @@ if (true) { } [Fact] public void WriteCSharpExpressionAttributeValue_RendersCorrectly() { - var writer = new DesignTimeBasicWriter(); - var context = GetCSharpRenderingContext(writer); + var writer = new DesignTimeNodeWriter(); var content = ""; var sourceDocument = TestRazorSourceDocument.Create(content); var codeDocument = RazorCodeDocument.Create(sourceDocument); - context.CodeDocument = codeDocument; + var context = GetCodeRenderingContext(writer, sourceDocument); var irDocument = Lower(codeDocument); var node = irDocument.Children.OfType().Single().Children[1] as CSharpExpressionAttributeValueIntermediateNode; @@ -423,7 +394,7 @@ if (true) { } writer.WriteCSharpExpressionAttributeValue(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" __o = false; @@ -438,13 +409,11 @@ if (true) { } [Fact] public void WriteCSharpCodeAttributeValue_RendersCorrectly() { - var writer = new DesignTimeBasicWriter(); - var context = GetCSharpRenderingContext(writer); - + var writer = new DesignTimeNodeWriter(); var content = ""; var sourceDocument = TestRazorSourceDocument.Create(content); var codeDocument = RazorCodeDocument.Create(sourceDocument); - context.CodeDocument = codeDocument; + var context = GetCodeRenderingContext(writer, sourceDocument); var irDocument = Lower(codeDocument); var node = irDocument.Children.OfType().Single().Children[1] as CSharpCodeAttributeValueIntermediateNode; @@ -452,7 +421,7 @@ if (true) { } writer.WriteCSharpCodeAttributeValue(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" if(@true){ } @@ -467,13 +436,11 @@ if (true) { } [Fact] public void WriteCSharpCodeAttributeValue_WithExpression_RendersCorrectly() { - var writer = new DesignTimeBasicWriter(); - var context = GetCSharpRenderingContext(writer); - + var writer = new DesignTimeNodeWriter(); var content = ""; var sourceDocument = TestRazorSourceDocument.Create(content); var codeDocument = RazorCodeDocument.Create(sourceDocument); - context.CodeDocument = codeDocument; + var context = GetCodeRenderingContext(writer, sourceDocument); var irDocument = Lower(codeDocument); var node = irDocument.Children.OfType().Single().Children[1] as CSharpCodeAttributeValueIntermediateNode; @@ -481,7 +448,7 @@ if (true) { } writer.WriteCSharpCodeAttributeValue(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" if(@true){ @@ -499,24 +466,15 @@ Render Node - CSharpExpressionIntermediateNode ignoreLineEndingDifferences: true); } - private static CSharpRenderingContext GetCSharpRenderingContext(BasicWriter writer) + private static CodeRenderingContext GetCodeRenderingContext(IntermediateNodeWriter writer, RazorSourceDocument sourceDocument) { var options = RazorCodeGenerationOptions.CreateDefault(); - var codeWriter = new CSharpCodeWriter(); - var context = new CSharpRenderingContext() + var codeWriter = new CodeWriter(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument, options); + context.SetRenderNode(n => { - Writer = codeWriter, - Options = options, - BasicWriter = writer, - RenderChildren = n => - { - codeWriter.WriteLine("Render Children"); - }, - RenderNode = n => - { - codeWriter.WriteLine($"Render Node - {n.GetType().Name}"); - } - }; + codeWriter.WriteLine($"Render Node - {n.GetType().Name}"); + }); return context; } @@ -556,7 +514,7 @@ Render Node - CSharpExpressionIntermediateNode throw new NotImplementedException(); } - public override void WriteNode(CodeTarget target, CSharpRenderingContext context) + public override void WriteNode(CodeTarget target, CodeRenderingContext context) { throw new NotImplementedException(); } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DesignTimeTagHelperWriterTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DesignTimeTagHelperWriterTest.cs index 35e9d0a1f5..5a5270278a 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DesignTimeTagHelperWriterTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/DesignTimeTagHelperWriterTest.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { // Arrange var writer = new DesignTimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer); + var context = GetCodeRenderingContext(writer); var node = new DeclareTagHelperFieldsIntermediateNode(); node.UsedTagHelperTypeNames.Add("PTagHelper"); node.UsedTagHelperTypeNames.Add("MyTagHelper"); @@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.WriteDeclareTagHelperFields(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"private global::PTagHelper __PTagHelper = null; private global::MyTagHelper __MyTagHelper = null; @@ -39,7 +39,7 @@ private global::MyTagHelper __MyTagHelper = null; { // Arrange var writer = new DesignTimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer); + var context = GetCodeRenderingContext(writer); var node = new CreateTagHelperIntermediateNode() { TagHelperTypeName = "TestNamespace.MyTagHelper" @@ -49,7 +49,7 @@ private global::MyTagHelper __MyTagHelper = null; writer.WriteCreateTagHelper(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"__TestNamespace_MyTagHelper = CreateTagHelper(); ", @@ -85,13 +85,13 @@ private global::MyTagHelper __MyTagHelper = null; var node = irDocument.Children.Last().Children[2] as SetTagHelperPropertyIntermediateNode; var writer = new DesignTimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer, codeDocument); + var context = GetCodeRenderingContext(writer, codeDocument); // Act writer.WriteSetTagHelperProperty(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"Render Children __InputTagHelper.FooProp = ""value""; @@ -128,13 +128,13 @@ __InputTagHelper.FooProp = ""value""; var node = irDocument.Children.Last().Children[2] as SetTagHelperPropertyIntermediateNode; var writer = new DesignTimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer, codeDocument); + var context = GetCodeRenderingContext(writer, codeDocument); // Act writer.WriteSetTagHelperProperty(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 3 ""test.cshtml"" __InputTagHelper.FooProp = 42; @@ -175,13 +175,13 @@ __InputTagHelper.FooProp = 42; var node = irDocument.Children.Last().Children[2] as SetTagHelperPropertyIntermediateNode; var writer = new DesignTimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer, codeDocument); + var context = GetCodeRenderingContext(writer, codeDocument); // Act writer.WriteSetTagHelperProperty(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 3 ""test.cshtml"" __InputTagHelper.FooProp[""bound""] = 42; @@ -193,23 +193,20 @@ __InputTagHelper.FooProp[""bound""] = 42; ignoreLineEndingDifferences: true); } - private static CSharpRenderingContext GetCSharpRenderingContext(TagHelperWriter writer, RazorCodeDocument codeDocument = null) + private static CodeRenderingContext GetCodeRenderingContext(TagHelperWriter writer, RazorCodeDocument codeDocument = null) { var options = RazorCodeGenerationOptions.CreateDefault(); - var codeWriter = new CSharpCodeWriter(); - var context = new CSharpRenderingContext() + var codeWriter = new CodeWriter(); + var nodeWriter = new DesignTimeNodeWriter(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, codeDocument?.Source, options) { - Writer = codeWriter, - Options = options, - BasicWriter = new DesignTimeBasicWriter(), TagHelperWriter = writer, - TagHelperRenderingContext = new TagHelperRenderingContext(), - CodeDocument = codeDocument, - RenderChildren = n => - { - codeWriter.WriteLine("Render Children"); - } + TagHelperRenderingContext = new TagHelperRenderingContext() }; + context.SetRenderChildren(n => + { + codeWriter.WriteLine("Render Children"); + }); return context; } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/LiteralRuntimeBasicWriterTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/LiteralRuntimeNodeWriterTest.cs similarity index 73% rename from test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/LiteralRuntimeBasicWriterTest.cs rename to test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/LiteralRuntimeNodeWriterTest.cs index 86c0f983a8..6042da482e 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/LiteralRuntimeBasicWriterTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/LiteralRuntimeNodeWriterTest.cs @@ -6,19 +6,16 @@ using Xunit; namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - public class LiteralRuntimeBasicWriterTest + public class LiteralRuntimeNodeWriterTest { [Fact] public void WriteCSharpExpression_UsesWriteLiteral_WritesLinePragma_WithSource() { // Arrange - var writer = new LiteralRuntimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Options = RazorCodeGenerationOptions.CreateDefault(), - Writer = new CSharpCodeWriter(), - }; + var writer = new LiteralRuntimeNodeWriter(); + var codeWriter = new CodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpExpressionIntermediateNode() { @@ -35,7 +32,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.WriteCSharpExpression(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" WriteLiteral(i++); diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/RuntimeBasicWriterTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/RuntimeNodeWriterTest.cs similarity index 71% rename from test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/RuntimeBasicWriterTest.cs rename to test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/RuntimeNodeWriterTest.cs index 261056f9bd..eaccb542a4 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/RuntimeBasicWriterTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/RuntimeNodeWriterTest.cs @@ -8,18 +8,17 @@ using Xunit; namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - public class RuntimeBasicWriterTest + public class RuntimeNodeWriterTest { [Fact] public void WriteUsingDirective_NoSource_WritesContent() { // Arrange - var writer = new RuntimeBasicWriter(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter() - }; - + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); + var node = new UsingDirectiveIntermediateNode() { Content = "System", @@ -29,7 +28,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.WriteUsingDirective(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"using System; ", @@ -41,11 +40,10 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration public void WriteUsingDirective_WithSource_WritesContentWithLinePragma() { // Arrange - var writer = new RuntimeBasicWriter(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter() - }; + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new UsingDirectiveIntermediateNode() { @@ -57,7 +55,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.WriteUsingDirective(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" using System; @@ -73,15 +71,13 @@ using System; public void WriteCSharpExpression_SkipsLinePragma_WithoutSource() { // Arrange - var writer = new RuntimeBasicWriter() + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter() { WriteCSharpExpressionMethod = "Test", }; - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - }; + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpExpressionIntermediateNode(); var builder = IntermediateNodeBuilder.Create(node); @@ -95,7 +91,7 @@ using System; writer.WriteCSharpExpression(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"Test(i++); ", @@ -107,16 +103,13 @@ using System; public void WriteCSharpExpression_WritesLinePragma_WithSource() { // Arrange - var writer = new RuntimeBasicWriter() + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter() { WriteCSharpExpressionMethod = "Test", }; - - var context = new CSharpRenderingContext() - { - Options = RazorCodeGenerationOptions.CreateDefault(), - Writer = new CSharpCodeWriter(), - }; + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpExpressionIntermediateNode() { @@ -133,7 +126,7 @@ using System; writer.WriteCSharpExpression(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" Test(i++); @@ -149,15 +142,13 @@ Test(i++); public void WriteCSharpExpression_WithExtensionNode_WritesPadding() { // Arrange - var writer = new RuntimeBasicWriter() + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter() { WriteCSharpExpressionMethod = "Test", }; - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - }; + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpExpressionIntermediateNode(); var builder = IntermediateNodeBuilder.Create(node); @@ -173,13 +164,13 @@ Test(i++); Kind = IntermediateToken.TokenKind.CSharp, }); - context.RenderNode = (n) => Assert.IsType(n); + context.SetRenderNode((n) => Assert.IsType(n)); // Act writer.WriteCSharpExpression(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"Test(i++); ", @@ -191,18 +182,14 @@ Test(i++); public void WriteCSharpExpression_WithSource_WritesPadding() { // Arrange - var writer = new RuntimeBasicWriter() + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter() { WriteCSharpExpressionMethod = "Test", }; + var options = RazorCodeGenerationOptions.CreateDefault(); var sourceDocument = TestRazorSourceDocument.Create(" @i++"); - - var context = new CSharpRenderingContext() - { - Options = RazorCodeGenerationOptions.CreateDefault(), - CodeDocument = RazorCodeDocument.Create(sourceDocument), - Writer = new CSharpCodeWriter(), - }; + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument, options); var node = new CSharpExpressionIntermediateNode() { @@ -221,13 +208,13 @@ Test(i++); Kind = IntermediateToken.TokenKind.CSharp, }); - context.RenderNode = (n) => Assert.IsType(n); + context.SetRenderNode((n) => Assert.IsType(n)); // Act writer.WriteCSharpExpression(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" Test(i++); @@ -243,12 +230,10 @@ Test(i++); public void WriteCSharpCode_WhitespaceContent_DoesNothing() { // Arrange - var writer = new RuntimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - }; + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpCodeIntermediateNode(); IntermediateNodeBuilder.Create(node) @@ -262,7 +247,7 @@ Test(i++); writer.WriteCSharpCode(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Empty(csharp); } @@ -270,12 +255,10 @@ Test(i++); public void WriteCSharpCode_SkipsLinePragma_WithoutSource() { // Arrange - var writer = new RuntimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - }; + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpCodeIntermediateNode(); IntermediateNodeBuilder.Create(node) @@ -289,7 +272,7 @@ Test(i++); writer.WriteCSharpCode(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"if (true) { } ", @@ -301,13 +284,10 @@ Test(i++); public void WriteCSharpCode_WritesLinePragma_WithSource() { // Arrange - var writer = new RuntimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - Options = RazorCodeGenerationOptions.CreateDefault(), - }; + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpCodeIntermediateNode() { @@ -324,7 +304,7 @@ Test(i++); writer.WriteCSharpCode(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" if (true) { } @@ -340,13 +320,10 @@ if (true) { } public void WriteCSharpCode_WritesPadding_WithSource() { // Arrange - var writer = new RuntimeBasicWriter(); - - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - Options = RazorCodeGenerationOptions.CreateDefault(), - }; + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new CSharpCodeIntermediateNode() { @@ -363,7 +340,7 @@ if (true) { } writer.WriteCSharpCode(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" if (true) { } @@ -379,12 +356,10 @@ if (true) { } public void WriteHtmlContent_RendersContentCorrectly() { // Arrange - var writer = new RuntimeBasicWriter(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - Options = RazorCodeGenerationOptions.CreateDefault(), - }; + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new HtmlContentIntermediateNode(); node.Children.Add(new IntermediateToken() @@ -397,7 +372,7 @@ if (true) { } writer.WriteHtmlContent(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"WriteLiteral(""SomeContent""); ", @@ -409,12 +384,10 @@ if (true) { } public void WriteHtmlContent_LargeStringLiteral_UsesMultipleWrites() { // Arrange - var writer = new RuntimeBasicWriter(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - Options = RazorCodeGenerationOptions.CreateDefault(), - }; + var codeWriter = new CodeWriter(); + var writer = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument: null, options: options); var node = new HtmlContentIntermediateNode(); @@ -428,7 +401,7 @@ if (true) { } writer.WriteHtmlContent(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal(string.Format( @"WriteLiteral(@""{0}""); WriteLiteral(@""{1}""); @@ -441,12 +414,11 @@ WriteLiteral(@""{1}""); public void WriteHtmlAttribute_RendersCorrectly() { // Arrange - var writer = new RuntimeBasicWriter(); - var context = GetCSharpRenderingContext(writer); - + var writer = new RuntimeNodeWriter(); var content = ""; var sourceDocument = TestRazorSourceDocument.Create(content); var codeDocument = RazorCodeDocument.Create(sourceDocument); + var context = GetCodeRenderingContext(writer, sourceDocument); var irDocument = Lower(codeDocument); var node = irDocument.Children.OfType().Single(); @@ -454,7 +426,7 @@ WriteLiteral(@""{1}""); writer.WriteHtmlAttribute(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"BeginWriteAttribute(""checked"", "" checked=\"""", 6, ""\"""", 34, 2); Render Children @@ -468,12 +440,11 @@ EndWriteAttribute(); public void WriteHtmlAttributeValue_RendersCorrectly() { // Arrange - var writer = new RuntimeBasicWriter(); - var context = GetCSharpRenderingContext(writer); - + var writer = new RuntimeNodeWriter(); var content = ""; var sourceDocument = TestRazorSourceDocument.Create(content); var codeDocument = RazorCodeDocument.Create(sourceDocument); + var context = GetCodeRenderingContext(writer, sourceDocument); var irDocument = Lower(codeDocument); var node = irDocument.Children.OfType().Single().Children[0] as HtmlAttributeValueIntermediateNode; @@ -481,7 +452,7 @@ EndWriteAttribute(); writer.WriteHtmlAttributeValue(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"WriteAttributeValue("""", 16, ""hello-world"", 16, 11, true); ", @@ -493,12 +464,11 @@ EndWriteAttribute(); public void WriteCSharpExpressionAttributeValue_RendersCorrectly() { // Arrange - var writer = new RuntimeBasicWriter(); - var context = GetCSharpRenderingContext(writer); - + var writer = new RuntimeNodeWriter(); var content = ""; var sourceDocument = TestRazorSourceDocument.Create(content); var codeDocument = RazorCodeDocument.Create(sourceDocument); + var context = GetCodeRenderingContext(writer, sourceDocument); var irDocument = Lower(codeDocument); var node = irDocument.Children.OfType().Single().Children[1] as CSharpExpressionAttributeValueIntermediateNode; @@ -506,7 +476,7 @@ EndWriteAttribute(); writer.WriteCSharpExpressionAttributeValue(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" WriteAttributeValue("" "", 27, false, 28, 6, false); @@ -522,13 +492,12 @@ WriteAttributeValue("" "", 27, false, 28, 6, false); public void WriteCSharpCodeAttributeValue_BuffersResult() { // Arrange - var writer = new RuntimeBasicWriter(); - var context = GetCSharpRenderingContext(writer); + var writer = new RuntimeNodeWriter(); var content = ""; var sourceDocument = TestRazorSourceDocument.Create(content); var codeDocument = RazorCodeDocument.Create(sourceDocument); - context.CodeDocument = codeDocument; + var context = GetCodeRenderingContext(writer, sourceDocument); var irDocument = Lower(codeDocument); var node = irDocument.Children.OfType().Single().Children[1] as CSharpCodeAttributeValueIntermediateNode; @@ -536,7 +505,7 @@ WriteAttributeValue("" "", 27, false, 28, 6, false); writer.WriteCSharpCodeAttributeValue(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"WriteAttributeValue("" "", 27, new Microsoft.AspNetCore.Mvc.Razor.HelperResult(async(__razor_attribute_value_writer) => { PushWriter(__razor_attribute_value_writer); @@ -557,17 +526,17 @@ WriteAttributeValue("" "", 27, false, 28, 6, false); public void BeginWriterScope_UsesSpecifiedWriter_RendersCorrectly() { // Arrange - var writer = new RuntimeBasicWriter() + var writer = new RuntimeNodeWriter() { PushWriterMethod = "TestPushWriter" }; - var context = GetCSharpRenderingContext(writer); + var context = GetCodeRenderingContext(writer, null); // Act writer.BeginWriterScope(context, "MyWriter"); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"TestPushWriter(MyWriter); ", @@ -579,17 +548,17 @@ WriteAttributeValue("" "", 27, false, 28, 6, false); public void EndWriterScope_RendersCorrectly() { // Arrange - var writer = new RuntimeBasicWriter() + var writer = new RuntimeNodeWriter() { PopWriterMethod = "TestPopWriter" }; - var context = GetCSharpRenderingContext(writer); + var context = GetCodeRenderingContext(writer, null); // Act writer.EndWriterScope(context); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"TestPopWriter(); ", @@ -597,20 +566,15 @@ WriteAttributeValue("" "", 27, false, 28, 6, false); ignoreLineEndingDifferences: true); } - private static CSharpRenderingContext GetCSharpRenderingContext(BasicWriter writer) + private static CodeRenderingContext GetCodeRenderingContext(IntermediateNodeWriter writer, RazorSourceDocument sourceDocument) { + var codeWriter = new CodeWriter(); var options = RazorCodeGenerationOptions.CreateDefault(); - var codeWriter = new CSharpCodeWriter(); - var context = new CSharpRenderingContext() + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument, options); + context.SetRenderChildren(n => { - Writer = codeWriter, - Options = options, - BasicWriter = writer, - RenderChildren = n => - { - codeWriter.WriteLine("Render Children"); - } - }; + codeWriter.WriteLine("Render Children"); + }); return context; } @@ -652,7 +616,7 @@ WriteAttributeValue("" "", 27, false, 28, 6, false); throw new NotImplementedException(); } - public override void WriteNode(CodeTarget target, CSharpRenderingContext context) + public override void WriteNode(CodeTarget target, CodeRenderingContext context) { throw new NotImplementedException(); } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/RuntimeTagHelperWriterTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/RuntimeTagHelperWriterTest.cs index e1cc2a1b3b..16b6a50a41 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/RuntimeTagHelperWriterTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/RuntimeTagHelperWriterTest.cs @@ -17,13 +17,13 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration // Arrange var node = new DeclareTagHelperFieldsIntermediateNode(); var writer = new RuntimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer); + var context = GetCodeRenderingContext(writer); // Act writer.WriteDeclareTagHelperFields(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line hidden #pragma warning disable 0414 @@ -57,13 +57,13 @@ private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeMana node.UsedTagHelperTypeNames.Add("MyTagHelper"); var writer = new RuntimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer); + var context = GetCodeRenderingContext(writer); // Act writer.WriteDeclareTagHelperFields(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line hidden #pragma warning disable 0414 @@ -96,8 +96,8 @@ private global::MyTagHelper __MyTagHelper = null; // Arrange var node = new TagHelperBodyIntermediateNode(); var writer = new RuntimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer); - context.IdGenerator = () => "test"; + var context = GetCodeRenderingContext(writer); + context.Items[CodeRenderingContext.SuppressUniqueIds] = "test"; context.TagHelperRenderingContext = new TagHelperRenderingContext() { TagName = "p", @@ -108,7 +108,7 @@ private global::MyTagHelper __MyTagHelper = null; writer.WriteTagHelperBody(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"__tagHelperExecutionContext = __tagHelperScopeManager.Begin(""p"", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, ""test"", async() => { Render Children @@ -128,13 +128,13 @@ private global::MyTagHelper __MyTagHelper = null; TagHelperTypeName = "TestNamespace.MyTagHelper" }; var writer = new RuntimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer); + var context = GetCodeRenderingContext(writer); // Act writer.WriteCreateTagHelper(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"__TestNamespace_MyTagHelper = CreateTagHelper(); __tagHelperExecutionContext.Add(__TestNamespace_MyTagHelper); @@ -149,13 +149,13 @@ __tagHelperExecutionContext.Add(__TestNamespace_MyTagHelper); // Arrange var node = new TagHelperIntermediateNode(); var writer = new RuntimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer); + var context = GetCodeRenderingContext(writer); // Act writer.WriteTagHelper(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"Render Children await __tagHelperRunner.RunAsync(__tagHelperExecutionContext); @@ -191,13 +191,13 @@ __tagHelperExecutionContext = __tagHelperScopeManager.End(); var node = irDocument.Children.Last().Children[2] as AddTagHelperHtmlAttributeIntermediateNode; var writer = new RuntimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer, codeDocument); + var context = GetCodeRenderingContext(writer, codeDocument); // Act writer.WriteAddTagHelperHtmlAttribute(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"BeginWriteTagHelperAttribute(); Render Children @@ -229,13 +229,13 @@ __tagHelperExecutionContext.AddHtmlAttribute(""name"", Html.Raw(__tagHelperStrin var node = irDocument.Children.Last().Children[2] as AddTagHelperHtmlAttributeIntermediateNode; var writer = new RuntimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer, codeDocument); + var context = GetCodeRenderingContext(writer, codeDocument); // Act writer.WriteAddTagHelperHtmlAttribute(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"BeginWriteTagHelperAttribute(); Render Children @@ -267,13 +267,13 @@ __tagHelperExecutionContext.AddHtmlAttribute(""data-test"", Html.Raw(__tagHelper var node = irDocument.Children.Last().Children[2] as AddTagHelperHtmlAttributeIntermediateNode; var writer = new RuntimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer, codeDocument); + var context = GetCodeRenderingContext(writer, codeDocument); // Act writer.WriteAddTagHelperHtmlAttribute(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"BeginAddHtmlAttributeValues(__tagHelperExecutionContext, ""test"", 2, global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); Render Children @@ -311,13 +311,13 @@ EndAddHtmlAttributeValues(__tagHelperExecutionContext); var node = irDocument.Children.Last().Children[2] as SetTagHelperPropertyIntermediateNode; var writer = new RuntimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer, codeDocument); + var context = GetCodeRenderingContext(writer, codeDocument); // Act writer.WriteSetTagHelperProperty(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); // The attribute value is not rendered inline because we are not using the preallocated writer. Assert.Equal( @@ -359,13 +359,13 @@ __tagHelperExecutionContext.AddTagHelperAttribute(""bound"", __InputTagHelper.Fo var node = irDocument.Children.Last().Children[2] as SetTagHelperPropertyIntermediateNode; var writer = new RuntimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer, codeDocument); + var context = GetCodeRenderingContext(writer, codeDocument); // Act writer.WriteSetTagHelperProperty(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 3 ""test.cshtml"" __InputTagHelper.FooProp = 42; @@ -407,13 +407,13 @@ __tagHelperExecutionContext.AddTagHelperAttribute(""bound"", __InputTagHelper.Fo var node = irDocument.Children.Last().Children[2] as SetTagHelperPropertyIntermediateNode; var writer = new RuntimeTagHelperWriter(); - var context = GetCSharpRenderingContext(writer, codeDocument); + var context = GetCodeRenderingContext(writer, codeDocument); // Act writer.WriteSetTagHelperProperty(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"if (__InputTagHelper.FooProp == null) { @@ -430,22 +430,20 @@ __tagHelperExecutionContext.AddTagHelperAttribute(""foo-bound"", __InputTagHelpe ignoreLineEndingDifferences: true); } - private static CSharpRenderingContext GetCSharpRenderingContext(TagHelperWriter writer, RazorCodeDocument codeDocument = null) + private static CodeRenderingContext GetCodeRenderingContext(TagHelperWriter writer, RazorCodeDocument codeDocument = null) { + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); var options = RazorCodeGenerationOptions.CreateDefault(); - var codeWriter = new CSharpCodeWriter(); - var context = new CSharpRenderingContext() + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, codeDocument?.Source, options) { - Writer = codeWriter, - Options = options, - BasicWriter = new RuntimeBasicWriter(), TagHelperWriter = writer, - TagHelperRenderingContext = new TagHelperRenderingContext(), - RenderChildren = n => - { - codeWriter.WriteLine("Render Children"); - } + TagHelperRenderingContext = new TagHelperRenderingContext() }; + context.SetRenderChildren(n => + { + codeWriter.WriteLine("Render Children"); + }); return context; } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/TagHelperHtmlAttributeRuntimeBasicWriterTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/TagHelperHtmlAttributeRuntimeNodeWriterTest.cs similarity index 77% rename from test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/TagHelperHtmlAttributeRuntimeBasicWriterTest.cs rename to test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/TagHelperHtmlAttributeRuntimeNodeWriterTest.cs index 056eb71ae0..f249d9e251 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/TagHelperHtmlAttributeRuntimeBasicWriterTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/CodeGeneration/TagHelperHtmlAttributeRuntimeNodeWriterTest.cs @@ -7,17 +7,17 @@ using Xunit; namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration { - public class TagHelperHtmlAttributeRuntimeBasicWriterTest + public class TagHelperHtmlAttributeRuntimeNodeWriterTest { [Fact] public void WriteHtmlAttributeValue_RendersCorrectly() { - var writer = new TagHelperHtmlAttributeRuntimeBasicWriter(); - var context = GetCSharpRenderingContext(writer); + var writer = new TagHelperHtmlAttributeRuntimeNodeWriter(); var content = ""; var sourceDocument = TestRazorSourceDocument.Create(content); var codeDocument = RazorCodeDocument.Create(sourceDocument); + var context = GetCodeRenderingContext(writer, sourceDocument); var irDocument = Lower(codeDocument); var node = irDocument.Children.OfType().Single().Children[0] as HtmlAttributeValueIntermediateNode; @@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.WriteHtmlAttributeValue(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"AddHtmlAttributeValue("""", 16, ""hello-world"", 16, 11, true); ", @@ -36,12 +36,11 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration [Fact] public void WriteCSharpExpressionAttributeValue_RendersCorrectly() { - var writer = new TagHelperHtmlAttributeRuntimeBasicWriter(); - var context = GetCSharpRenderingContext(writer); - + var writer = new TagHelperHtmlAttributeRuntimeNodeWriter(); var content = ""; var sourceDocument = TestRazorSourceDocument.Create(content); var codeDocument = RazorCodeDocument.Create(sourceDocument); + var context = GetCodeRenderingContext(writer, sourceDocument); var irDocument = Lower(codeDocument); var node = irDocument.Children.OfType().Single().Children[1] as CSharpExpressionAttributeValueIntermediateNode; @@ -49,7 +48,7 @@ namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration writer.WriteCSharpExpressionAttributeValue(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#line 1 ""test.cshtml"" AddHtmlAttributeValue("" "", 27, false, 28, 6, false); @@ -64,13 +63,12 @@ AddHtmlAttributeValue("" "", 27, false, 28, 6, false); [Fact] public void WriteCSharpCodeAttributeValue_BuffersResult() { - var writer = new TagHelperHtmlAttributeRuntimeBasicWriter(); - var context = GetCSharpRenderingContext(writer); + var writer = new TagHelperHtmlAttributeRuntimeNodeWriter(); var content = ""; var sourceDocument = TestRazorSourceDocument.Create(content); var codeDocument = RazorCodeDocument.Create(sourceDocument); - context.CodeDocument = codeDocument; + var context = GetCodeRenderingContext(writer, sourceDocument); var irDocument = Lower(codeDocument); var node = irDocument.Children.OfType().Single().Children[1] as CSharpCodeAttributeValueIntermediateNode; @@ -78,7 +76,7 @@ AddHtmlAttributeValue("" "", 27, false, 28, 6, false); writer.WriteCSharpCodeAttributeValue(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"AddHtmlAttributeValue("" "", 27, new Microsoft.AspNetCore.Mvc.Razor.HelperResult(async(__razor_attribute_value_writer) => { PushWriter(__razor_attribute_value_writer); @@ -95,20 +93,15 @@ AddHtmlAttributeValue("" "", 27, false, 28, 6, false); ignoreLineEndingDifferences: true); } - private static CSharpRenderingContext GetCSharpRenderingContext(BasicWriter writer) + private static CodeRenderingContext GetCodeRenderingContext(IntermediateNodeWriter writer, RazorSourceDocument sourceDocument) { + var codeWriter = new CodeWriter(); var options = RazorCodeGenerationOptions.CreateDefault(); - var codeWriter = new CSharpCodeWriter(); - var context = new CSharpRenderingContext() + var context = new DefaultCodeRenderingContext(codeWriter, writer, sourceDocument, options); + context.SetRenderChildren(_ => { - Writer = codeWriter, - Options = options, - BasicWriter = writer, - RenderChildren = n => - { - codeWriter.WriteLine("Render Children"); - } - }; + codeWriter.WriteLine("Render Children"); + }); return context; } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/DesignTimeDirectiveTargetExtensionTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/DesignTimeDirectiveTargetExtensionTest.cs index 3cf963a604..103f0357d4 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/DesignTimeDirectiveTargetExtensionTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/DesignTimeDirectiveTargetExtensionTest.cs @@ -14,10 +14,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { // Arrange var extension = new DesignTimeDirectiveTargetExtension(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter() - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new DesignTimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument: null, options: options); var node = new DesignTimeDirectiveIntermediateNode(); @@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions extension.WriteDesignTimeDirective(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { @@ -41,11 +41,11 @@ private void __RazorDirectiveTokenHelpers__() { { // Arrange var extension = new DesignTimeDirectiveTargetExtension(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - CodeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("test content", "test.cshtml")) - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new DesignTimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var sourceDocument = RazorSourceDocument.Create("test content", "test.cshtml"); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument, options); var node = new DesignTimeDirectiveIntermediateNode(); var token = new DirectiveTokenIntermediateNode() @@ -60,7 +60,7 @@ private void __RazorDirectiveTokenHelpers__() { extension.WriteDesignTimeDirective(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { @@ -80,11 +80,11 @@ System.String __typeHelper = default(System.String); { // Arrange var extension = new DesignTimeDirectiveTargetExtension(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - CodeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("test content", "test.cshtml")) - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new DesignTimeNodeWriter(); + var sourceDocument = RazorSourceDocument.Create("test content", "test.cshtml"); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument, options); var node = new DesignTimeDirectiveIntermediateNode(); var token = new DirectiveTokenIntermediateNode() @@ -99,7 +99,7 @@ System.String __typeHelper = default(System.String); extension.WriteDesignTimeDirective(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { @@ -119,11 +119,11 @@ global::System.Object __typeHelper = nameof(System.Collections.Generic); { // Arrange var extension = new DesignTimeDirectiveTargetExtension(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - CodeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("test content", "test.cshtml")) - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new DesignTimeNodeWriter(); + var sourceDocument = RazorSourceDocument.Create("test content", "test.cshtml"); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument, options); var node = new DesignTimeDirectiveIntermediateNode(); var token = new DirectiveTokenIntermediateNode() @@ -138,7 +138,7 @@ global::System.Object __typeHelper = nameof(System.Collections.Generic); extension.WriteDesignTimeDirective(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { @@ -158,11 +158,11 @@ global::System.Object Foo = null; { // Arrange var extension = new DesignTimeDirectiveTargetExtension(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - CodeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("test content", "test.cshtml")) - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new DesignTimeNodeWriter(); + var sourceDocument = RazorSourceDocument.Create("test content", "test.cshtml"); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument, options); var node = new DesignTimeDirectiveIntermediateNode(); var token = new DirectiveTokenIntermediateNode() @@ -184,7 +184,7 @@ global::System.Object Foo = null; extension.WriteDesignTimeDirective(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { @@ -208,11 +208,11 @@ global::System.Object __typeHelper = ""Value""; { // Arrange var extension = new DesignTimeDirectiveTargetExtension(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter(), - CodeDocument = RazorCodeDocument.Create(RazorSourceDocument.Create("test content", "test.cshtml")) - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new DesignTimeNodeWriter(); + var sourceDocument = RazorSourceDocument.Create("test content", "test.cshtml"); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument, options); var node = new DesignTimeDirectiveIntermediateNode(); var token = new DirectiveTokenIntermediateNode() @@ -226,7 +226,7 @@ global::System.Object __typeHelper = ""Value""; extension.WriteDesignTimeDirective(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"#pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/PreallocatedAttributeTargetExtensionTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/PreallocatedAttributeTargetExtensionTest.cs index ffe636535c..bc49a7de1e 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/PreallocatedAttributeTargetExtensionTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/PreallocatedAttributeTargetExtensionTest.cs @@ -13,10 +13,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { // Arrange var extension = new PreallocatedAttributeTargetExtension(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter() - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument: null, options: options); var node = new DeclarePreallocatedTagHelperHtmlAttributeIntermediateNode() { @@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions extension.WriteDeclarePreallocatedTagHelperHtmlAttribute(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute MyProp = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute(""Foo"", new global::Microsoft.AspNetCore.Html.HtmlString(""Bar""), global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); ", @@ -43,10 +43,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { // Arrange var extension = new PreallocatedAttributeTargetExtension(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter() - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument: null, options: options); var node = new DeclarePreallocatedTagHelperHtmlAttributeIntermediateNode() { @@ -60,7 +60,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions extension.WriteDeclarePreallocatedTagHelperHtmlAttribute(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute _tagHelper1 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute(""Foo""); ", @@ -73,10 +73,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { // Arrange var extension = new PreallocatedAttributeTargetExtension(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter() - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument: null, options: options); var node = new AddPreallocatedTagHelperHtmlAttributeIntermediateNode() { @@ -87,7 +87,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions extension.WriteAddPreallocatedTagHelperHtmlAttribute(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"__tagHelperExecutionContext.AddHtmlAttribute(_tagHelper1); ", @@ -100,10 +100,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { // Arrange var extension = new PreallocatedAttributeTargetExtension(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter() - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument: null, options: options); var node = new DeclarePreallocatedTagHelperAttributeIntermediateNode() { @@ -117,7 +117,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions extension.WriteDeclarePreallocatedTagHelperAttribute(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute _tagHelper1 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute(""Foo"", ""Bar"", global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes); ", @@ -130,10 +130,10 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { // Arrange var extension = new PreallocatedAttributeTargetExtension(); - var context = new CSharpRenderingContext() - { - Writer = new CSharpCodeWriter() - }; + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument: null, options: options); var tagHelperBuilder = new DefaultTagHelperDescriptorBuilder(TagHelperConventions.DefaultKind, "FooTagHelper", "Test"); tagHelperBuilder.TypeName("FooTagHelper"); @@ -144,7 +144,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions .Name("Foo") .TypeName("System.String") .PropertyName("FooProp"); - + var descriptor = builder.Build(); var node = new SetPreallocatedTagHelperPropertyIntermediateNode() @@ -159,7 +159,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions extension.WriteSetPreallocatedTagHelperProperty(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"__FooTagHelper.FooProp = (string)_tagHelper1.Value; __tagHelperExecutionContext.AddTagHelperAttribute(_tagHelper1); @@ -173,9 +173,11 @@ __tagHelperExecutionContext.AddTagHelperAttribute(_tagHelper1); { // Arrange var extension = new PreallocatedAttributeTargetExtension(); - var context = new CSharpRenderingContext() + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument: null, options: options) { - Writer = new CSharpCodeWriter(), TagHelperRenderingContext = new TagHelperRenderingContext() }; @@ -205,7 +207,7 @@ __tagHelperExecutionContext.AddTagHelperAttribute(_tagHelper1); extension.WriteSetPreallocatedTagHelperProperty(context, node); // Assert - var csharp = context.Writer.Builder.ToString(); + var csharp = context.CodeWriter.Builder.ToString(); Assert.Equal( @"if (__FooTagHelper.FooProp == null) { diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/SectionTargetExtensionTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/SectionTargetExtensionTest.cs index 9ec5ffda13..4ae7083b2a 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/SectionTargetExtensionTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/SectionTargetExtensionTest.cs @@ -24,19 +24,19 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions SectionMethodName = "CreateSection" }; - var context = new CSharpRenderingContext() + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument: null, options: options) { - BasicWriter = new RuntimeBasicWriter(), TagHelperWriter = new RuntimeTagHelperWriter(), - Writer = new CSharpCodeWriter(), - Options = RazorCodeGenerationOptions.CreateDefault(), }; - context.RenderChildren = (n) => + context.SetRenderChildren((n) => { Assert.Same(node, n); - context.Writer.WriteLine(" var s = \"Inside\""); - }; + context.CodeWriter.WriteLine(" var s = \"Inside\""); + }); // Act extension.WriteSection(context, node); @@ -48,7 +48,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions ); "; - var output = context.Writer.Builder.ToString(); + var output = context.CodeWriter.Builder.ToString(); Assert.Equal(expected, output); } @@ -66,19 +66,19 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions SectionMethodName = "CreateSection" }; - var context = new CSharpRenderingContext() + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter(); + var options = RazorCodeGenerationOptions.Create(false, 4, true, false); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument: null, options: options) { - BasicWriter = new RuntimeBasicWriter(), TagHelperWriter = new RuntimeTagHelperWriter(), - Writer = new CSharpCodeWriter(), - Options = RazorCodeGenerationOptions.Create(false, 4, true, false), }; - context.RenderChildren = (n) => + context.SetRenderChildren((n) => { Assert.Same(node, n); - context.Writer.WriteLine(" var s = \"Inside\""); - }; + context.CodeWriter.WriteLine(" var s = \"Inside\""); + }); // Act extension.WriteSection(context, node); @@ -90,7 +90,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions ); "; - var output = context.Writer.Builder.ToString(); + var output = context.CodeWriter.Builder.ToString(); Assert.Equal(expected, output); } } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/TemplateTargetExtensionTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/TemplateTargetExtensionTest.cs index 40c176b029..06c424342e 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/TemplateTargetExtensionTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/Extensions/TemplateTargetExtensionTest.cs @@ -2,9 +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.CodeGeneration; -using Microsoft.AspNetCore.Razor.Language.Legacy; using Xunit; -using static Microsoft.AspNetCore.Razor.Language.Intermediate.IntermediateNodeAssert; namespace Microsoft.AspNetCore.Razor.Language.Extensions { @@ -15,29 +13,28 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions { // Arrange var node = new TemplateIntermediateNode(); - var extension = new TemplateTargetExtension() { TemplateTypeName = "global::TestTemplate" }; - var context = new CSharpRenderingContext() - { - BasicWriter = new RuntimeBasicWriter() - { - PushWriterMethod = "TestPushWriter", - PopWriterMethod = "TestPopWriter" - }, + var codeWriter = new CodeWriter(); + var nodeWriter = new RuntimeNodeWriter() + { + PushWriterMethod = "TestPushWriter", + PopWriterMethod = "TestPopWriter" + }; + var options = RazorCodeGenerationOptions.CreateDefault(); + var context = new DefaultCodeRenderingContext(codeWriter, nodeWriter, sourceDocument: null, options: options) + { TagHelperWriter = new RuntimeTagHelperWriter(), - Writer = new CSharpCodeWriter(), - Options = RazorCodeGenerationOptions.CreateDefault(), }; - context.RenderChildren = (n) => + context.SetRenderChildren((n) => { Assert.Same(node, n); - context.Writer.WriteLine(" var s = \"Inside\""); - }; + context.CodeWriter.WriteLine(" var s = \"Inside\""); + }); // Act extension.WriteTemplate(context, node); @@ -50,7 +47,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions } )"; - var output = context.Writer.Builder.ToString(); + var output = context.CodeWriter.Builder.ToString(); Assert.Equal(expected, output); } } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/Intermediate/ExtensionIntermediateNodeTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/Intermediate/ExtensionIntermediateNodeTest.cs index 71aac3b8a0..f299cdfb08 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/Intermediate/ExtensionIntermediateNodeTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/Intermediate/ExtensionIntermediateNodeTest.cs @@ -51,7 +51,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Intermediate AcceptExtensionNode(this, visitor); } - public override void WriteNode(CodeTarget target, CSharpRenderingContext context) + public override void WriteNode(CodeTarget target, CodeRenderingContext context) { throw new NotImplementedException(); } diff --git a/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs b/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs index 1372940a71..2c76d9d289 100644 --- a/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs +++ b/test/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/IntegrationTestBase.cs @@ -13,6 +13,7 @@ using System.Runtime.Remoting.Messaging; #else using System.Threading; #endif +using Microsoft.AspNetCore.Razor.Language.CodeGeneration; using Microsoft.AspNetCore.Razor.Language.Intermediate; using Xunit; using Xunit.Sdk; @@ -94,10 +95,10 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests var codeDocument = RazorCodeDocument.Create(source, imports); // This will ensure that we're not putting any randomly generated data in a baseline. - codeDocument.Items[DefaultRazorCSharpLoweringPhase.SuppressUniqueIds] = "test"; + codeDocument.Items[CodeRenderingContext.SuppressUniqueIds] = "test"; // This is to make tests work cross platform. - codeDocument.Items[DefaultRazorCSharpLoweringPhase.NewLineString] = "\r\n"; + codeDocument.Items[CodeRenderingContext.NewLineString] = "\r\n"; OnCreatedCodeDocument(ref codeDocument);