This commit is contained in:
Ajay Bhargav Baaskaran 2017-02-17 11:27:51 -08:00
parent 927e75870d
commit 0f5d5ff3b5
4 changed files with 32 additions and 27 deletions

View File

@ -139,13 +139,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
// Internal for unit testing
public CompilationResult GetCompilationFailedResult(
string relativePath,
IEnumerable<Microsoft.AspNetCore.Razor.Evolution.Legacy.RazorError> errors)
IEnumerable<RazorDiagnostic> 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<CompilationFailure>();
@ -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)

View File

@ -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<TagHelperDescriptor> Resolve(ErrorSink errorSink)
public IEnumerable<TagHelperDescriptor> Resolve(IList<RazorDiagnostic> errors)
{
return TagHelpers;
}

View File

@ -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<TagHelperDescriptor> Resolve(ErrorSink errorSink)
public IEnumerable<TagHelperDescriptor> Resolve(IList<RazorDiagnostic> errors)
{
return TagHelpers;
}

View File

@ -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<RazorError>()
Diagnostics = new List<RazorDiagnostic>()
});
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<RazorError>()
Diagnostics = new List<RazorDiagnostic>()
{
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<RazorError>()
Diagnostics = new List<RazorDiagnostic>()
});
});
@ -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);
}
}
}