From 6a886d39ab7b4a0a5f66bc42b332087178b81b1f Mon Sep 17 00:00:00 2001 From: YishaiGalatzer Date: Mon, 29 Sep 2014 18:57:08 -0700 Subject: [PATCH] Add razor parser errors to show up --- .../PreCompileViews/RazorErrorExtensions.cs | 29 +++++++++++++++++++ .../Razor/PreCompileViews/RazorPreCompiler.cs | 26 ++++++++++++----- 2 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorErrorExtensions.cs diff --git a/src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorErrorExtensions.cs b/src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorErrorExtensions.cs new file mode 100644 index 0000000000..d25cb6989f --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorErrorExtensions.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorPreCompiler.cs b/src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorPreCompiler.cs index 1f890996c7..0d1d4a1d25 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorPreCompiler.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Razor/PreCompileViews/RazorPreCompiler.cs @@ -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 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.