Add razor parser errors to show up
This commit is contained in:
parent
6279229a7c
commit
6a886d39ab
|
|
@ -0,0 +1,29 @@
|
|||
using Microsoft.AspNet.Razor.Parser.SyntaxTree;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor
|
||||
{
|
||||
public static class RazorErrorExtensions
|
||||
{
|
||||
public static Diagnostic ToDiagnostics([NotNull] this RazorError error, [NotNull] string filePath)
|
||||
{
|
||||
var descriptor = new DiagnosticDescriptor(id: "Razor",
|
||||
title: "Razor parsing error",
|
||||
messageFormat: error.Message.Replace("{", "{{").Replace("}", "}}"),
|
||||
category: "Razor.Parser",
|
||||
defaultSeverity: DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true);
|
||||
|
||||
var textSpan = new TextSpan(error.Location.AbsoluteIndex, error.Length);
|
||||
var linePositionStart = new LinePosition(error.Location.LineIndex, error.Location.CharacterIndex);
|
||||
var linePositionEnd = new LinePosition(error.Location.LineIndex,
|
||||
error.Location.CharacterIndex + error.Length);
|
||||
var linePositionSpan = new LinePositionSpan(linePositionStart, linePositionEnd);
|
||||
|
||||
var location = Location.Create(filePath, textSpan, linePositionSpan);
|
||||
|
||||
return Diagnostic.Create(descriptor, location);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -44,12 +44,16 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
public virtual void CompileViews([NotNull] IBeforeCompileContext context)
|
||||
{
|
||||
var descriptors = CreateCompilationDescriptors(context);
|
||||
var collectionGenerator = new RazorFileInfoCollectionGenerator(
|
||||
descriptors,
|
||||
SyntaxTreeGenerator.GetParseOptions(context.CSharpCompilation));
|
||||
|
||||
var tree = collectionGenerator.GenerateCollection();
|
||||
context.CSharpCompilation = context.CSharpCompilation.AddSyntaxTrees(tree);
|
||||
if (descriptors.Count > 0)
|
||||
{
|
||||
var collectionGenerator = new RazorFileInfoCollectionGenerator(
|
||||
descriptors,
|
||||
SyntaxTreeGenerator.GetParseOptions(context.CSharpCompilation));
|
||||
|
||||
var tree = collectionGenerator.GenerateCollection();
|
||||
context.CSharpCompilation = context.CSharpCompilation.AddSyntaxTrees(tree);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual IReadOnlyList<RazorFileInfo> CreateCompilationDescriptors(
|
||||
|
|
@ -115,9 +119,12 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
using (var stream = fileInfo.FileInfo.CreateReadStream())
|
||||
{
|
||||
var results = _host.GenerateCode(fileInfo.RelativePath, stream);
|
||||
if (results.Success)
|
||||
|
||||
var generatedCode = results.GeneratedCode;
|
||||
|
||||
if (generatedCode != null)
|
||||
{
|
||||
var syntaxTree = SyntaxTreeGenerator.Generate(results.GeneratedCode, fileInfo.FileInfo.PhysicalPath, options);
|
||||
var syntaxTree = SyntaxTreeGenerator.Generate(generatedCode, fileInfo.FileInfo.PhysicalPath, options);
|
||||
var fullTypeName = results.GetMainClassName(_host, syntaxTree);
|
||||
|
||||
if (fullTypeName != null)
|
||||
|
|
@ -136,6 +143,11 @@ namespace Microsoft.AspNet.Mvc.Razor
|
|||
};
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var parserError in results.ParserErrors)
|
||||
{
|
||||
context.Diagnostics.Add(parserError.ToDiagnostics(fileInfo.FileInfo.PhysicalPath));
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Add diagnostics when view parsing/code generation failed.
|
||||
|
|
|
|||
Loading…
Reference in New Issue