From d422e61c3e15049f4a6df831963d181965e6f954 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Tue, 28 Feb 2017 10:37:26 -0800 Subject: [PATCH] Make CSharpRenderingContext and friends public --- .../CodeGeneration/CSharpRenderingContext.cs | 20 ++++---- .../DesignTimeCSharpRenderer.cs | 19 ++++--- .../CodeGeneration/LinePragmaWriter.cs | 49 ------------------- .../CodeGeneration/RuntimeCSharpRenderer.cs | 20 ++++---- .../TagHelperRenderingContext.cs | 2 +- .../DefaultRazorCSharpLoweringPhase.cs | 4 +- .../Intermediate/ExtensionIRNode.cs | 2 +- .../Intermediate/TemplateIRNode.cs | 2 +- .../Legacy/CSharpCodeWriter.cs | 47 ++++++++++++++++++ .../Intermediate/ExtensionIRNodeTest.cs | 2 +- 10 files changed, 84 insertions(+), 83 deletions(-) delete mode 100644 src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/LinePragmaWriter.cs diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/CSharpRenderingContext.cs b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/CSharpRenderingContext.cs index ea0ef88004..f6f66ed3af 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/CSharpRenderingContext.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/CSharpRenderingContext.cs @@ -8,19 +8,19 @@ using Microsoft.AspNetCore.Razor.Evolution.Intermediate; namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration { - internal class CSharpRenderingContext + public class CSharpRenderingContext { private CSharpRenderingConventions _renderingConventions; - public ICollection Directives { get; set; } + internal ICollection Directives { get; set; } - public Func IdGenerator { get; set; } = () => Guid.NewGuid().ToString("N"); + internal Func IdGenerator { get; set; } = () => Guid.NewGuid().ToString("N"); - public List LineMappings { get; } = new List(); + internal List LineMappings { get; } = new List(); public CSharpCodeWriter Writer { get; set; } - public CSharpRenderingConventions RenderingConventions + internal CSharpRenderingConventions RenderingConventions { get { @@ -37,14 +37,14 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration } } - public ErrorSink ErrorSink { get; } = new ErrorSink(); + internal IList Diagnostics { get; } = new List(); - public RazorSourceDocument SourceDocument { get; set; } + internal RazorSourceDocument SourceDocument { get; set; } - public RazorParserOptions Options { get; set; } + internal RazorParserOptions Options { get; set; } - public TagHelperRenderingContext TagHelperRenderingContext { get; set; } + internal TagHelperRenderingContext TagHelperRenderingContext { get; set; } - public Action RenderChildren { get; set; } + internal Action RenderChildren { get; set; } } } diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DesignTimeCSharpRenderer.cs b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DesignTimeCSharpRenderer.cs index 95174cbee3..ac12c435bd 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DesignTimeCSharpRenderer.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/DesignTimeCSharpRenderer.cs @@ -4,6 +4,7 @@ using System; using System.Linq; using Microsoft.AspNetCore.Razor.Evolution.Intermediate; +using Microsoft.AspNetCore.Razor.Evolution.Legacy; namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration { @@ -23,7 +24,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration if (node.Source != null) { - using (new LinePragmaWriter(Context.Writer, node.Source.Value)) + using (Context.Writer.BuildLinePragma(node.Source.Value)) { var padding = BuildOffsetPadding(RazorDesignTimeIRPass.DesignTimeVariable.Length, node.Source.Value, Context); @@ -61,7 +62,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration { if (node.Source.HasValue) { - using (new LinePragmaWriter(Context.Writer, node.Source.Value)) + using (Context.Writer.BuildLinePragma(node.Source.Value)) { Context.Writer.WriteUsing(node.Content); } @@ -76,7 +77,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration { if (node.Source != null) { - using (new LinePragmaWriter(Context.Writer, node.Source.Value)) + using (Context.Writer.BuildLinePragma(node.Source.Value)) { var padding = BuildOffsetPadding(0, node.Source.Value, Context); Context.Writer.Write(padding); @@ -229,7 +230,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration var firstMappedChild = node.Children.FirstOrDefault(child => child.Source != null) as RazorIRNode; var valueStart = firstMappedChild?.Source; - using (new LinePragmaWriter(Context.Writer, node.Source.Value)) + using (Context.Writer.BuildLinePragma(node.Source.Value)) { var assignmentPrefixLength = propertyValueAccessor.Length + " = ".Length; if (node.Descriptor.IsEnum && @@ -330,18 +331,20 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration } else if (node is CSharpStatementIRNode) { - Context.ErrorSink.OnError( - new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length), + var error = new RazorError( LegacyResources.TagHelpers_CodeBlocks_NotSupported_InAttributes, + new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length), documentLocation.Length); + Context.Diagnostics.Add(RazorDiagnostic.Create(error)); } else if (node is TemplateIRNode) { var attributeValueNode = (SetTagHelperPropertyIRNode)node.Parent; - Context.ErrorSink.OnError( - new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length), + var error = new RazorError( LegacyResources.FormatTagHelpers_InlineMarkupBlocks_NotSupported_InAttributes(attributeValueNode.Descriptor.TypeName), + new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length), documentLocation.Length); + Context.Diagnostics.Add(RazorDiagnostic.Create(error)); } } } diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/LinePragmaWriter.cs b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/LinePragmaWriter.cs deleted file mode 100644 index 4302b80674..0000000000 --- a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/LinePragmaWriter.cs +++ /dev/null @@ -1,49 +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 Microsoft.AspNetCore.Razor.Evolution.Legacy; - -namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration -{ - internal class LinePragmaWriter : IDisposable - { - private readonly CSharpCodeWriter _writer; - private readonly int _startIndent; - - public LinePragmaWriter(CSharpCodeWriter writer, SourceSpan documentLocation) - { - if (writer == null) - { - throw new ArgumentNullException(nameof(writer)); - } - - _writer = writer; - _startIndent = _writer.CurrentIndent; - _writer.ResetIndent(); - _writer.WriteLineNumberDirective(documentLocation, documentLocation.FilePath); - } - - public void Dispose() - { - // Need to add an additional line at the end IF there wasn't one already written. - // This is needed to work with the C# editor's handling of #line ... - var builder = _writer.Builder; - var endsWithNewline = builder.Length > 0 && builder[builder.Length - 1] == '\n'; - - // Always write at least 1 empty line to potentially separate code from pragmas. - _writer.WriteLine(); - - // Check if the previous empty line wasn't enough to separate code from pragmas. - if (!endsWithNewline) - { - _writer.WriteLine(); - } - - _writer - .WriteLineDefaultDirective() - .WriteLineHiddenDirective() - .SetIndent(_startIndent); - } - } -} diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/RuntimeCSharpRenderer.cs b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/RuntimeCSharpRenderer.cs index 4b71736aba..fdec104939 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/RuntimeCSharpRenderer.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/RuntimeCSharpRenderer.cs @@ -66,7 +66,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration IDisposable linePragmaScope = null; if (node.Source != null) { - linePragmaScope = new LinePragmaWriter(Context.Writer, node.Source.Value); + linePragmaScope = Context.Writer.BuildLinePragma(node.Source.Value); var padding = BuildOffsetPadding(Context.RenderingConventions.StartWriteMethod.Length, node.Source.Value, Context); Context.Writer.Write(padding); } @@ -95,7 +95,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration { if (node.Source.HasValue) { - using (new LinePragmaWriter(Context.Writer, node.Source.Value)) + using (Context.Writer.BuildLinePragma(node.Source.Value)) { Context.Writer.WriteUsing(node.Content); } @@ -161,7 +161,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration const string ValueWriterName = "__razor_attribute_value_writer"; var expressionValue = node.Children.FirstOrDefault() as CSharpExpressionIRNode; - var linePragma = expressionValue != null ? new LinePragmaWriter(Context.Writer, node.Source.Value) : null; + var linePragma = expressionValue != null ? Context.Writer.BuildLinePragma(node.Source.Value) : null; var prefixLocation = node.Source.Value.AbsoluteIndex; var valueLocation = node.Source.Value.AbsoluteIndex + node.Prefix.Length; var valueLength = node.Source.Value.Length - node.Prefix.Length; @@ -215,7 +215,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration if (node.Source != null) { - using (new LinePragmaWriter(Context.Writer, node.Source.Value)) + using (Context.Writer.BuildLinePragma(node.Source.Value)) { var padding = BuildOffsetPadding(0, node.Source.Value, Context); Context.Writer @@ -455,7 +455,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration } else { - using (new LinePragmaWriter(Context.Writer, node.Source.Value)) + using (Context.Writer.BuildLinePragma(node.Source.Value)) { Context.Writer.WriteStartAssignment(propertyValueAccessor); @@ -673,18 +673,20 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration } else if (node is CSharpStatementIRNode) { - Context.ErrorSink.OnError( - new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length), + var error = new RazorError( LegacyResources.TagHelpers_CodeBlocks_NotSupported_InAttributes, + new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length), documentLocation.Length); + Context.Diagnostics.Add(RazorDiagnostic.Create(error)); } else if (node is TemplateIRNode) { var attributeValueNode = (SetTagHelperPropertyIRNode)node.Parent; - Context.ErrorSink.OnError( - new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length), + var error = new RazorError( LegacyResources.FormatTagHelpers_InlineMarkupBlocks_NotSupported_InAttributes(attributeValueNode.Descriptor.TypeName), + new SourceLocation(documentLocation.AbsoluteIndex, documentLocation.CharacterIndex, documentLocation.Length), documentLocation.Length); + Context.Diagnostics.Add(RazorDiagnostic.Create(error)); } } } diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/TagHelperRenderingContext.cs b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/TagHelperRenderingContext.cs index 0c5c763e1f..c7e88fe41d 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/TagHelperRenderingContext.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/CodeGeneration/TagHelperRenderingContext.cs @@ -6,7 +6,7 @@ using System.Collections.Generic; namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration { - internal class TagHelperRenderingContext + public class TagHelperRenderingContext { private Dictionary _renderedBoundAttributes; private HashSet _verifiedPropertyDictionaries; diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorCSharpLoweringPhase.cs b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorCSharpLoweringPhase.cs index cdb76b39bf..9414e0b25d 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorCSharpLoweringPhase.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/DefaultRazorCSharpLoweringPhase.cs @@ -63,9 +63,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution var diagnostics = new List(); diagnostics.AddRange(syntaxTree.Diagnostics); - - // Temporary code while we're still using legacy diagnostics in the SyntaxTree. - diagnostics.AddRange(renderingContext.ErrorSink.Errors.Select(error => RazorDiagnostic.Create(error))); + diagnostics.AddRange(renderingContext.Diagnostics); var csharpDocument = new RazorCSharpDocument() { diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/Intermediate/ExtensionIRNode.cs b/src/Microsoft.AspNetCore.Razor.Evolution/Intermediate/ExtensionIRNode.cs index 11644fc968..f0a8b49854 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/Intermediate/ExtensionIRNode.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/Intermediate/ExtensionIRNode.cs @@ -7,7 +7,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate { public abstract class ExtensionIRNode : RazorIRNode { - internal abstract void WriteNode(RuntimeTarget target, CSharpRenderingContext context); + public abstract void WriteNode(RuntimeTarget target, CSharpRenderingContext context); protected static void AcceptExtensionNode(TNode node, RazorIRNodeVisitor visitor) where TNode : ExtensionIRNode diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/Intermediate/TemplateIRNode.cs b/src/Microsoft.AspNetCore.Razor.Evolution/Intermediate/TemplateIRNode.cs index baebcfe841..81b1b0bd44 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/Intermediate/TemplateIRNode.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/Intermediate/TemplateIRNode.cs @@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate return AcceptExtensionNode(this, visitor); } - internal override void WriteNode(RuntimeTarget target, CSharpRenderingContext context) + public override void WriteNode(RuntimeTarget target, CSharpRenderingContext context) { var extension = target.GetExtension(); extension.WriteTemplate(context, this); diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/Legacy/CSharpCodeWriter.cs b/src/Microsoft.AspNetCore.Razor.Evolution/Legacy/CSharpCodeWriter.cs index 357d5fe0fe..99a4273d71 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/Legacy/CSharpCodeWriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/Legacy/CSharpCodeWriter.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; +using Microsoft.AspNetCore.Razor.Evolution.CodeGeneration; namespace Microsoft.AspNetCore.Razor.Evolution.Legacy { @@ -446,6 +447,11 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy return new CSharpCodeWritingScope(this); } + public IDisposable BuildLinePragma(SourceSpan documentLocation) + { + return new LinePragmaWriter(this, documentLocation); + } + private void WriteVerbatimStringLiteral(string literal) { Write("@\""); @@ -525,5 +531,46 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy Write("\""); } + + private class LinePragmaWriter : IDisposable + { + private readonly CSharpCodeWriter _writer; + private readonly int _startIndent; + + public LinePragmaWriter(CSharpCodeWriter writer, SourceSpan documentLocation) + { + if (writer == null) + { + throw new ArgumentNullException(nameof(writer)); + } + + _writer = writer; + _startIndent = _writer.CurrentIndent; + _writer.ResetIndent(); + _writer.WriteLineNumberDirective(documentLocation, documentLocation.FilePath); + } + + public void Dispose() + { + // Need to add an additional line at the end IF there wasn't one already written. + // This is needed to work with the C# editor's handling of #line ... + var builder = _writer.Builder; + var endsWithNewline = builder.Length > 0 && builder[builder.Length - 1] == '\n'; + + // Always write at least 1 empty line to potentially separate code from pragmas. + _writer.WriteLine(); + + // Check if the previous empty line wasn't enough to separate code from pragmas. + if (!endsWithNewline) + { + _writer.WriteLine(); + } + + _writer + .WriteLineDefaultDirective() + .WriteLineHiddenDirective() + .SetIndent(_startIndent); + } + } } } diff --git a/test/Microsoft.AspNetCore.Razor.Evolution.Test/Intermediate/ExtensionIRNodeTest.cs b/test/Microsoft.AspNetCore.Razor.Evolution.Test/Intermediate/ExtensionIRNodeTest.cs index 197621214a..4a844e90a3 100644 --- a/test/Microsoft.AspNetCore.Razor.Evolution.Test/Intermediate/ExtensionIRNodeTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Evolution.Test/Intermediate/ExtensionIRNodeTest.cs @@ -91,7 +91,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate return AcceptExtensionNode(this, visitor); } - internal override void WriteNode(RuntimeTarget target, CSharpRenderingContext context) + public override void WriteNode(RuntimeTarget target, CSharpRenderingContext context) { throw new NotImplementedException(); }