diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompilationService.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompilationService.cs index a24a77b903..85f271668a 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompilationService.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorCompilationService.cs @@ -139,13 +139,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal // Internal for unit testing public CompilationResult GetCompilationFailedResult( string relativePath, - IEnumerable errors) + IEnumerable errors) { // If a SourceLocation does not specify a file path, assume it is produced // from parsing the current file. var messageGroups = errors .GroupBy(razorError => - razorError.Location.FilePath ?? relativePath, + razorError.Span.FilePath ?? relativePath, StringComparer.Ordinal); var failures = new List(); @@ -165,18 +165,18 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal } private DiagnosticMessage CreateDiagnosticMessage( - Microsoft.AspNetCore.Razor.Evolution.Legacy.RazorError error, + RazorDiagnostic error, string filePath) { - var location = error.Location; + var sourceSpan = error.Span; return new DiagnosticMessage( - message: error.Message, - formattedMessage: $"{error} ({location.LineIndex},{location.CharacterIndex}) {error.Message}", + message: error.GetMessage(), + formattedMessage: $"{error} ({sourceSpan.LineIndex},{sourceSpan.CharacterIndex}) {error.GetMessage()}", filePath: filePath, - startLine: error.Location.LineIndex + 1, - startColumn: error.Location.CharacterIndex, - endLine: error.Location.LineIndex + 1, - endColumn: error.Location.CharacterIndex + error.Length); + startLine: sourceSpan.LineIndex + 1, + startColumn: sourceSpan.CharacterIndex, + endLine: sourceSpan.LineIndex + 1, + endColumn: sourceSpan.CharacterIndex + sourceSpan.Length); } private string ReadFileContentsSafely(string relativePath) diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelExpressionPassTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelExpressionPassTest.cs index 81190f6512..22ef38a1f7 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelExpressionPassTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ModelExpressionPassTest.cs @@ -10,7 +10,6 @@ using Microsoft.AspNetCore.Razor.Evolution; using Microsoft.AspNetCore.Razor.Evolution.Intermediate; using Microsoft.AspNetCore.Razor.Evolution.Legacy; using Xunit; -using ErrorSink = Microsoft.AspNetCore.Razor.Evolution.Legacy.ErrorSink; namespace Microsoft.AspNetCore.Mvc.Razor.Host { @@ -251,7 +250,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host public TagHelperDescriptor[] TagHelpers { get; } - public IEnumerable Resolve(ErrorSink errorSink) + public IEnumerable Resolve(IList errors) { return TagHelpers; } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ViewComponentTagHelperPassTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ViewComponentTagHelperPassTest.cs index 6f3e875c69..ffd43049a7 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ViewComponentTagHelperPassTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Host.Test/ViewComponentTagHelperPassTest.cs @@ -8,7 +8,6 @@ using Microsoft.AspNetCore.Razor.Evolution; using Microsoft.AspNetCore.Razor.Evolution.Intermediate; using Microsoft.AspNetCore.Razor.Evolution.Legacy; using Xunit; -using ErrorSink = Microsoft.AspNetCore.Razor.Evolution.Legacy.ErrorSink; namespace Microsoft.AspNetCore.Mvc.Razor.Host { @@ -415,7 +414,7 @@ public class __Generated__TagCloudViewComponentTagHelper : Microsoft.AspNetCore. public TagHelperDescriptor[] TagHelpers { get; } - public IEnumerable Resolve(ErrorSink errorSink) + public IEnumerable Resolve(IList errors) { return TagHelpers; } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorCompilationServiceTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorCompilationServiceTest.cs index f44701967d..f5e4fa9b7d 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorCompilationServiceTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorCompilationServiceTest.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.IO; using Microsoft.AspNetCore.Mvc.Razor.Compilation; using Microsoft.AspNetCore.Razor.Evolution; -using Microsoft.AspNetCore.Razor.Evolution.Legacy; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Logging.Testing; using Moq; @@ -39,7 +38,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal { document.SetCSharpDocument(new RazorCSharpDocument() { - Diagnostics = new List() + Diagnostics = new List() }); Assert.Equal(viewPath, document.Source.Filename); // Assert if source file name is the root relative path @@ -78,9 +77,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal { document.SetCSharpDocument(new RazorCSharpDocument() { - Diagnostics = new List() + Diagnostics = new List() { - new RazorError("some message", 1, 1, 1, 1) + GetRazorDiagnostic("some message", new SourceLocation(1, 1, 1), length: 1) } }); }).Verifiable(); @@ -128,7 +127,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal { document.SetCSharpDocument(new RazorCSharpDocument() { - Diagnostics = new List() + Diagnostics = new List() }); }); @@ -165,10 +164,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal NullLoggerFactory.Instance); var errors = new[] { - new RazorError("message-1", new SourceLocation(1, 2, 17), length: 1), - new RazorError("message-2", new SourceLocation(viewPath, 1, 4, 6), 7), - new RazorError { Message = "message-3" }, - new RazorError("message-4", new SourceLocation(viewImportsPath, 1, 3, 8), 4), + GetRazorDiagnostic("message-1", new SourceLocation(1, 2, 17), length: 1), + GetRazorDiagnostic("message-2", new SourceLocation(viewPath, 1, 4, 6), length: 7), + GetRazorDiagnostic("message-3", SourceLocation.Undefined, length: -1), + GetRazorDiagnostic("message-4", new SourceLocation(viewImportsPath, 1, 3, 8), length: 4), }; // Act @@ -184,7 +183,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal Assert.Collection(failure.Messages, message => { - Assert.Equal(errors[0].Message, message.Message); + Assert.Equal(errors[0].GetMessage(), message.Message); Assert.Equal(viewPath, message.SourceFilePath); Assert.Equal(3, message.StartLine); Assert.Equal(17, message.StartColumn); @@ -193,7 +192,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal }, message => { - Assert.Equal(errors[1].Message, message.Message); + Assert.Equal(errors[1].GetMessage(), message.Message); Assert.Equal(viewPath, message.SourceFilePath); Assert.Equal(5, message.StartLine); Assert.Equal(6, message.StartColumn); @@ -202,7 +201,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal }, message => { - Assert.Equal(errors[2].Message, message.Message); + Assert.Equal(errors[2].GetMessage(), message.Message); Assert.Equal(viewPath, message.SourceFilePath); Assert.Equal(0, message.StartLine); Assert.Equal(-1, message.StartColumn); @@ -217,7 +216,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal Assert.Collection(failure.Messages, message => { - Assert.Equal(errors[3].Message, message.Message); + Assert.Equal(errors[3].GetMessage(), message.Message); Assert.Equal(viewImportsPath, message.SourceFilePath); Assert.Equal(4, message.StartLine); Assert.Equal(8, message.StartColumn); @@ -235,5 +234,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal return options.Object; } + + private static RazorDiagnostic GetRazorDiagnostic(string message, SourceLocation sourceLocation, int length) + { + var diagnosticDescriptor = new RazorDiagnosticDescriptor("test-id", () => message, RazorDiagnosticSeverity.Error); + var sourceSpan = new SourceSpan(sourceLocation, length); + + return RazorDiagnostic.Create(diagnosticDescriptor, sourceSpan); + } } }