Replace RazorError with RazorDiagnostics in public API

This commit is contained in:
Ajay Bhargav Baaskaran 2017-02-16 14:25:29 -08:00
parent 7a2f89b5de
commit ed9068cef4
20 changed files with 102 additions and 83 deletions

View File

@ -20,7 +20,10 @@ namespace Microsoft.AspNetCore.Razor.Evolution
if (errorSink.Errors.Count > 0)
{
var combinedErrors = syntaxTree.Diagnostics.Concat(errorSink.Errors).ToList();
// Temporary code while we're still using legacy diagnostics in the SyntaxTree.
var errors = errorSink.Errors.Select(error => RazorDiagnostic.Create(error));
var combinedErrors = syntaxTree.Diagnostics.Concat(errors);
syntaxTree = RazorSyntaxTree.Create(syntaxTree.Root, syntaxTree.Source, combinedErrors, syntaxTree.Options);
}

View File

@ -2,10 +2,11 @@
// 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.Evolution.CodeGeneration;
using Microsoft.AspNetCore.Razor.Evolution.Legacy;
using Microsoft.AspNetCore.Razor.Evolution.Intermediate;
using Microsoft.AspNetCore.Razor.Evolution.Legacy;
namespace Microsoft.AspNetCore.Razor.Evolution
{
@ -60,12 +61,17 @@ namespace Microsoft.AspNetCore.Razor.Evolution
renderer.VisitDocument(irDocument);
var combinedErrors = syntaxTree.Diagnostics.Concat(renderingContext.ErrorSink.Errors).ToList();
var diagnostics = new List<RazorDiagnostic>();
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)));
var csharpDocument = new RazorCSharpDocument()
{
GeneratedCode = renderingContext.Writer.GenerateCode(),
LineMappings = renderingContext.LineMappings,
Diagnostics = combinedErrors
Diagnostics = diagnostics
};
codeDocument.SetCSharpDocument(csharpDocument);

View File

@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public DefaultRazorSyntaxTree(
Block root,
RazorSourceDocument source,
IReadOnlyList<RazorError> diagnostics,
IReadOnlyList<RazorDiagnostic> diagnostics,
RazorParserOptions options)
{
Root = root;
@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
Options = options;
}
internal override IReadOnlyList<RazorError> Diagnostics { get; }
public override IReadOnlyList<RazorDiagnostic> Diagnostics { get; }
public override RazorParserOptions Options { get; }

View File

@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
/// <summary>
/// Used to manage <see cref="RazorError"/>s encountered during the Razor parsing phase.
/// </summary>
public class ErrorSink
internal class ErrorSink
{
private readonly List<RazorError> _errors;

View File

@ -10,6 +10,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
/// </summary>
public interface ITagHelperDescriptorResolver
{
IEnumerable<TagHelperDescriptor> Resolve(ErrorSink errorSink);
IEnumerable<TagHelperDescriptor> Resolve(IList<RazorDiagnostic> errors);
}
}

View File

@ -7,7 +7,7 @@ using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
{
public class RazorError : IEquatable<RazorError>
internal class RazorError : IEquatable<RazorError>
{
internal static readonly RazorError[] EmptyArray = new RazorError[0];

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq;
namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
{
@ -47,7 +48,10 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
markupParser.ParseDocument();
var root = context.Builder.Build();
var diagnostics = context.ErrorSink.Errors;
// Temporary code while we're still using legacy diagnostics in the SyntaxTree.
var diagnostics = context.ErrorSink.Errors.Select(error => RazorDiagnostic.Create(error));
return RazorSyntaxTree.Create(root, source, diagnostics, Options);
}
}

View File

@ -12,6 +12,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public IReadOnlyList<LineMapping> LineMappings { get; set; }
public IReadOnlyList<RazorError> Diagnostics { get; set; }
public IReadOnlyList<RazorDiagnostic> Diagnostics { get; set; }
}
}

View File

@ -68,7 +68,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
string IFormattable.ToString(string ignore, IFormatProvider formatProvider)
{
// Our indices are 0-based, but we we want to print them as 1-based.
return string.Format($"{Span.FilePath}({Span.LineIndex + 1},{Span.CharacterIndex + 1}): {Severity} {Id}: {GetMessage(formatProvider)}");
return $"{Span.FilePath}({Span.LineIndex + 1},{Span.CharacterIndex + 1}): {Severity} {Id}: {GetMessage(formatProvider)}";
}
}
}

View File

@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
internal static RazorSyntaxTree Create(
Block root,
RazorSourceDocument source,
IEnumerable<RazorError> diagnostics,
IEnumerable<RazorDiagnostic> diagnostics,
RazorParserOptions options)
{
if (root == null)
@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
throw new ArgumentNullException(nameof(options));
}
return new DefaultRazorSyntaxTree(root, source, new List<RazorError>(diagnostics), options);
return new DefaultRazorSyntaxTree(root, source, new List<RazorDiagnostic>(diagnostics), options);
}
public static RazorSyntaxTree Parse(RazorSourceDocument source)
@ -59,7 +59,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
return parser.Parse(source);
}
internal abstract IReadOnlyList<RazorError> Diagnostics { get; }
public abstract IReadOnlyList<RazorDiagnostic> Diagnostics { get; }
public abstract RazorParserOptions Options { get; }

View File

@ -45,35 +45,36 @@ namespace Microsoft.AspNetCore.Razor.Evolution
visitor.VisitBlock(syntaxTree.Root);
var errorList = new List<RazorDiagnostic>();
var descriptors = (IReadOnlyList<TagHelperDescriptor>)resolver.Resolve(errorList).ToList();
var errorSink = new ErrorSink();
var directives = visitor.Directives;
var descriptors = (IReadOnlyList<TagHelperDescriptor>)resolver.Resolve(errorSink).ToList();
descriptors = ProcessDirectives(directives, descriptors, errorSink);
var root = syntaxTree.Root;
if (descriptors.Count == 0)
{
// No TagHelpers, add any errors if we have them.
if (errorSink.Errors.Count > 0)
if (errorSink.Errors.Count == 0 && errorList.Count == 0)
{
var errors = CombineErrors(syntaxTree.Diagnostics, errorSink.Errors);
return RazorSyntaxTree.Create(syntaxTree.Root, syntaxTree.Source, errors, syntaxTree.Options);
// No TagHelpers and errors, no op.
return syntaxTree;
}
return syntaxTree;
}
else
{
var descriptorProvider = new TagHelperDescriptorProvider(descriptors);
var rewriter = new TagHelperParseTreeRewriter(descriptorProvider);
root = rewriter.Rewrite(root, errorSink);
}
var descriptorProvider = new TagHelperDescriptorProvider(descriptors);
var rewriter = new TagHelperParseTreeRewriter(descriptorProvider);
var rewrittenRoot = rewriter.Rewrite(syntaxTree.Root, errorSink);
var diagnostics = syntaxTree.Diagnostics;
// Temporary code while we're still using legacy diagnostics in the SyntaxTree.
errorList.AddRange(errorSink.Errors.Select(error => RazorDiagnostic.Create(error)));
if (errorSink.Errors.Count > 0)
{
diagnostics = CombineErrors(diagnostics, errorSink.Errors);
}
var diagnostics = CombineErrors(syntaxTree.Diagnostics, errorList);
var newSyntaxTree = RazorSyntaxTree.Create(rewrittenRoot, syntaxTree.Source, diagnostics, syntaxTree.Options);
var newSyntaxTree = RazorSyntaxTree.Create(root, syntaxTree.Source, diagnostics, syntaxTree.Options);
return newSyntaxTree;
}
@ -299,9 +300,9 @@ namespace Microsoft.AspNetCore.Razor.Evolution
return normalizeEmptyStringLength;
}
private IReadOnlyList<RazorError> CombineErrors(IReadOnlyList<RazorError> errors1, IReadOnlyList<RazorError> errors2)
private IReadOnlyList<RazorDiagnostic> CombineErrors(IReadOnlyList<RazorDiagnostic> errors1, IReadOnlyList<RazorDiagnostic> errors2)
{
var combinedErrors = new List<RazorError>(errors1.Count + errors2.Count);
var combinedErrors = new List<RazorDiagnostic>(errors1.Count + errors2.Count);
combinedErrors.AddRange(errors1);
combinedErrors.AddRange(errors2);

View File

@ -43,7 +43,7 @@ namespace Microsoft.CodeAnalysis.Razor
{
_referenceFeature = referenceFeature;
}
public IEnumerable<TagHelperDescriptor> Resolve(ErrorSink errorSink)
public IEnumerable<TagHelperDescriptor> Resolve(IList<RazorDiagnostic> errors)
{
var compilation = CSharpCompilation.Create("__TagHelpers", references: _referenceFeature.References);
return TagHelpers.GetTagHelpers(compilation);

View File

@ -1,8 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Razor.Evolution.Legacy;
using System;
using System.Linq;
using Microsoft.AspNetCore.Razor.Evolution.Legacy;
using Xunit;
namespace Microsoft.AspNetCore.Razor.Evolution
@ -49,6 +50,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
new SourceLocation(41 + Environment.NewLine.Length * 4, 4, 4),
length: 8),
};
var expectedDiagnostics = expectedErrors.Select(error => RazorDiagnostic.Create(error));
var engine = RazorEngine.Create();
var pass = new DefaultDirectiveSyntaxTreePass()
{
@ -72,7 +74,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Assert
Assert.Empty(originalTree.Diagnostics);
Assert.NotSame(originalTree, outputTree);
Assert.Equal(expectedErrors, outputTree.Diagnostics);
Assert.Equal(expectedDiagnostics, outputTree.Diagnostics);
}
[Fact]
@ -90,6 +92,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
new SourceLocation(41 + Environment.NewLine.Length * 4, 4, 4),
length: 8),
};
var expectedDiagnostics = expectedErrors.Select(error => RazorDiagnostic.Create(error)).ToList();
var engine = RazorEngine.Create();
var pass = new DefaultDirectiveSyntaxTreePass()
{
@ -109,7 +112,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var erroredOriginalTree = RazorSyntaxTree.Create(
originalTree.Root,
originalTree.Source,
new[] { expectedErrors[0] },
new[] { expectedDiagnostics[0] },
originalTree.Options);
// Act
@ -118,7 +121,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
// Assert
Assert.Empty(originalTree.Diagnostics);
Assert.NotSame(originalTree, outputTree);
Assert.Equal(expectedErrors, outputTree.Diagnostics);
Assert.Equal(expectedDiagnostics, outputTree.Diagnostics);
}
}
}

View File

@ -80,18 +80,18 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests
var document = RazorCodeDocument.Create(TestRazorSourceDocument.Create("@!!!"));
var expected = RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Unexpected_Character_At_Start_Of_CodeBlock_CS("!"),
new SourceLocation(1, 0, 1),
length: 1));
// Act
engine.Process(document);
// Assert
var csharpDocument = document.GetCSharpDocument();
var error = Assert.Single(csharpDocument.Diagnostics);
Assert.Equal(
new RazorError(
LegacyResources.FormatParseError_Unexpected_Character_At_Start_Of_CodeBlock_CS("!"),
new SourceLocation(1, 0, 1),
length: 1),
error);
Assert.Equal(expected, error);
}
[Fact]
@ -102,18 +102,18 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests
var document = RazorCodeDocument.Create(TestRazorSourceDocument.Create("@{"));
var expected = RazorDiagnostic.Create(new RazorError(
LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(LegacyResources.BlockName_Code, "}", "{"),
new SourceLocation(1, 0, 1),
length: 1));
// Act
engine.Process(document);
// Assert
var csharpDocument = document.GetCSharpDocument();
var error = Assert.Single(csharpDocument.Diagnostics);
Assert.Equal(
new RazorError(
LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(LegacyResources.BlockName_Code, "}", "{"),
new SourceLocation(1, 0, 1),
length: 1),
error);
Assert.Equal(expected, error);
}
}
}

View File

@ -883,7 +883,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
{
var result = ParseCodeBlock(document, descriptors, designTime: false);
EvaluateResults(result, expected, expectedErrors);
EvaluateResults(result, expected, expectedErrors.Select(error => RazorDiagnostic.Create(error)).ToList());
}
}
}

View File

@ -66,7 +66,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
parser.ParseBlock();
var root = context.Builder.Build();
var diagnostics = context.ErrorSink.Errors;
var diagnostics = context.ErrorSink.Errors?.Select(error => RazorDiagnostic.Create(error));
var options = RazorParserOptions.CreateDefaultOptions();
options.DesignTimeMode = designTime;
@ -99,7 +99,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
parser.ParseBlock();
var root = context.Builder.Build();
var diagnostics = context.ErrorSink.Errors;
var diagnostics = context.ErrorSink.Errors?.Select(error => RazorDiagnostic.Create(error));
var options = RazorParserOptions.CreateDefaultOptions();
options.DesignTimeMode = designTime;
@ -173,7 +173,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
if (!ReferenceEquals(expected, IgnoreOutput))
{
EvaluateResults(result, expected, expectedErrors);
EvaluateResults(result, expected, expectedErrors?.Select(error => RazorDiagnostic.Create(error)).ToList());
}
}
@ -223,7 +223,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
if (!ReferenceEquals(expected, IgnoreOutput))
{
EvaluateResults(result, expected, expectedErrors);
EvaluateResults(result, expected, expectedErrors?.Select(error => RazorDiagnostic.Create(error)).ToList());
}
}
@ -269,7 +269,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
if (!ReferenceEquals(expected, IgnoreOutput))
{
EvaluateResults(result, expected, expectedErrors);
EvaluateResults(result, expected, expectedErrors?.Select(error => RazorDiagnostic.Create(error)).ToList());
}
}
@ -300,7 +300,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
EvaluateResults(result, expectedRoot, null);
}
internal static void EvaluateResults(RazorSyntaxTree result, Block expectedRoot, IList<RazorError> expectedErrors)
internal static void EvaluateResults(RazorSyntaxTree result, Block expectedRoot, IList<RazorDiagnostic> expectedErrors)
{
EvaluateParseTree(result.Root, expectedRoot);
EvaluateRazorErrors(result.Diagnostics, expectedErrors);
@ -494,7 +494,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
collector.AddError("{0} - FAILED :: Actual: << Null >>", expected);
}
internal static void EvaluateRazorErrors(IEnumerable<RazorError> actualErrors, IList<RazorError> expectedErrors)
internal static void EvaluateRazorErrors(IEnumerable<RazorDiagnostic> actualErrors, IList<RazorDiagnostic> expectedErrors)
{
var realCount = actualErrors.Count();
@ -517,7 +517,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
WriteTraceLine("Expected Errors were raised:" + Environment.NewLine + FormatErrors(expectedErrors));
}
internal static string FormatErrors(IEnumerable<RazorError> errors)
internal static string FormatErrors(IEnumerable<RazorDiagnostic> errors)
{
if (errors == null)
{
@ -525,9 +525,9 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
}
var builder = new StringBuilder();
foreach (RazorError err in errors)
foreach (var error in errors)
{
builder.AppendFormat("\t{0}", err);
builder.AppendFormat("\t{0}", error);
builder.AppendLine();
}
return builder.ToString();

View File

@ -64,12 +64,12 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Legacy
var parseTreeRewriter = new TagHelperParseTreeRewriter(provider);
var actualTree = parseTreeRewriter.Rewrite(syntaxTree.Root, errorSink);
var allErrors = syntaxTree.Diagnostics.Concat(errorSink.Errors);
var allErrors = syntaxTree.Diagnostics.Concat(errorSink.Errors.Select(error => RazorDiagnostic.Create(error)));
var actualErrors = allErrors
.OrderBy(error => error.Location.AbsoluteIndex)
.OrderBy(error => error.Span.AbsoluteIndex)
.ToList();
EvaluateRazorErrors(actualErrors, expectedErrors.ToList());
EvaluateRazorErrors(actualErrors, expectedErrors.Select(error => RazorDiagnostic.Create(error)).ToList());
EvaluateParseTree(actualTree, expectedOutput);
}
}

View File

@ -275,10 +275,11 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var codeDocument = RazorCodeDocument.Create(sourceDocument);
var originalTree = RazorSyntaxTree.Parse(sourceDocument);
var expectedError = new RazorError(
Resources.FormatTagHelperAssemblyCouldNotBeResolved("TestAssembly"),
new SourceLocation(Environment.NewLine.Length + 17, 1, 1),
length: 12);
var expectedError = RazorDiagnostic.Create(
new RazorError(
Resources.FormatTagHelperAssemblyCouldNotBeResolved("TestAssembly"),
new SourceLocation(Environment.NewLine.Length + 17, 1, 1),
length: 12));
// Act
var outputTree = pass.Execute(codeDocument, originalTree);
@ -294,7 +295,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public void Execute_RecreatesSyntaxTreeOnResolverErrors()
{
// Arrange
var resolverError = new RazorError("Test error", new SourceLocation(19, 1, 17), length: 12);
var resolverError = RazorDiagnostic.Create(new RazorError("Test error", new SourceLocation(19, 1, 17), length: 12));
var engine = RazorEngine.Create(builder =>
{
var resolver = new ErrorLoggingTagHelperDescriptorResolver(resolverError);
@ -310,7 +311,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var codeDocument = RazorCodeDocument.Create(sourceDocument);
var originalTree = RazorSyntaxTree.Parse(sourceDocument);
var initialError = new RazorError("Initial test error", SourceLocation.Zero, length: 1);
var initialError = RazorDiagnostic.Create(new RazorError("Initial test error", SourceLocation.Zero, length: 1));
var erroredOriginalTree = RazorSyntaxTree.Create(
originalTree.Root,
originalTree.Source,
@ -362,11 +363,12 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var originalTree = RazorSyntaxTree.Parse(sourceDocument);
var initialError = new RazorError("Initial test error", SourceLocation.Zero, length: 1);
var expectedRewritingError = new RazorError(
LegacyResources.FormatTagHelpersParseTreeRewriter_FoundMalformedTagHelper("form"),
new SourceLocation(Environment.NewLine.Length * 2 + 30, 2, 1),
length: 4);
var initialError = RazorDiagnostic.Create(new RazorError("Initial test error", SourceLocation.Zero, length: 1));
var expectedRewritingError = RazorDiagnostic.Create(
new RazorError(
LegacyResources.FormatTagHelpersParseTreeRewriter_FoundMalformedTagHelper("form"),
new SourceLocation(Environment.NewLine.Length * 2 + 30, 2, 1),
length: 4));
var erroredOriginalTree = RazorSyntaxTree.Create(originalTree.Root, originalTree.Source, new[] { initialError }, originalTree.Options);
@ -1357,16 +1359,16 @@ namespace Microsoft.AspNetCore.Razor.Evolution
private class ErrorLoggingTagHelperDescriptorResolver : ITagHelperDescriptorResolver
{
private readonly RazorError _error;
private readonly RazorDiagnostic _error;
public ErrorLoggingTagHelperDescriptorResolver(RazorError error)
public ErrorLoggingTagHelperDescriptorResolver(RazorDiagnostic error)
{
_error = error;
}
public IEnumerable<TagHelperDescriptor> Resolve(ErrorSink errorSink)
public IEnumerable<TagHelperDescriptor> Resolve(IList<RazorDiagnostic> errors)
{
errorSink.OnError(_error);
errors.Add(_error);
return new[] { new TagHelperDescriptor() { AssemblyName = "TestAssembly" } };
}

View File

@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public List<TagHelperDescriptor> TagHelpers { get; } = new List<TagHelperDescriptor>();
public IEnumerable<TagHelperDescriptor> Resolve(ErrorSink errorSink)
public IEnumerable<TagHelperDescriptor> Resolve(IList<RazorDiagnostic> errors)
{
return TagHelpers;
}

View File

@ -492,9 +492,9 @@ namespace Microsoft.AspNetCore.Razor.Test.Framework
}
var builder = new StringBuilder();
foreach (RazorError err in errors)
foreach (var error in errors)
{
builder.AppendFormat("\t{0}", err);
builder.AppendFormat("\t{0}", error);
builder.AppendLine();
}
return builder.ToString();