Fix #1309 make CSharpDocument abstract

This commit is contained in:
Ryan Nowak 2017-05-12 13:48:30 -07:00
parent b17e506ce8
commit b9dabd24be
4 changed files with 87 additions and 13 deletions

View File

@ -0,0 +1,36 @@
// 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 System;
using System.Collections.Generic;
namespace Microsoft.AspNetCore.Razor.Language
{
internal class DefaultRazorCSharpDocument : RazorCSharpDocument
{
private readonly string _generatedCode;
private readonly RazorDiagnostic[] _diagnostics;
private readonly LineMapping[] _lineMappings;
public DefaultRazorCSharpDocument(
string generatedCode,
RazorDiagnostic[] diagnostics,
LineMapping[] lineMappings)
{
if (generatedCode == null)
{
throw new ArgumentNullException(nameof(generatedCode));
}
_generatedCode = generatedCode;
_diagnostics = diagnostics ?? Array.Empty<RazorDiagnostic>();
_lineMappings = lineMappings ?? Array.Empty<LineMapping>();
}
public override IReadOnlyList<RazorDiagnostic> Diagnostics => _diagnostics;
public override string GeneratedCode => _generatedCode;
public override IReadOnlyList<LineMapping> LineMappings => _lineMappings;
}
}

View File

@ -68,13 +68,10 @@ namespace Microsoft.AspNetCore.Razor.Language
}
diagnostics.AddRange(renderingContext.Diagnostics);
var csharpDocument = new RazorCSharpDocument()
{
GeneratedCode = renderingContext.Writer.GenerateCode(),
LineMappings = renderingContext.LineMappings,
Diagnostics = diagnostics
};
var csharpDocument = RazorCSharpDocument.Create(
renderingContext.Writer.GenerateCode(),
diagnostics,
renderingContext.LineMappings);
codeDocument.SetCSharpDocument(csharpDocument);
}
}

View File

@ -1,16 +1,56 @@
// 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 System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.AspNetCore.Razor.Language
{
public class RazorCSharpDocument
public abstract class RazorCSharpDocument
{
public string GeneratedCode { get; set; }
public abstract string GeneratedCode { get; }
public IReadOnlyList<LineMapping> LineMappings { get; set; }
public abstract IReadOnlyList<LineMapping> LineMappings { get; }
public IReadOnlyList<RazorDiagnostic> Diagnostics { get; set; }
public abstract IReadOnlyList<RazorDiagnostic> Diagnostics { get; }
public static RazorCSharpDocument Create(string generatedCode, IEnumerable<RazorDiagnostic> diagnostics)
{
if (generatedCode == null)
{
throw new ArgumentNullException(nameof(generatedCode));
}
if (diagnostics == null)
{
throw new ArgumentNullException(nameof(diagnostics));
}
return new DefaultRazorCSharpDocument(generatedCode, diagnostics.ToArray(), lineMappings: null);
}
public static RazorCSharpDocument Create(
string generatedCode,
IEnumerable<RazorDiagnostic> diagnostics,
IEnumerable<LineMapping> lineMappings)
{
if (generatedCode == null)
{
throw new ArgumentNullException(nameof(generatedCode));
}
if (diagnostics == null)
{
throw new ArgumentNullException(nameof(diagnostics));
}
if (lineMappings == null)
{
throw new ArgumentNullException(nameof(lineMappings));
}
return new DefaultRazorCSharpDocument(generatedCode, diagnostics.ToArray(), lineMappings.ToArray());
}
}
}

View File

@ -1,6 +1,7 @@
// 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 System;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Xunit;
@ -92,7 +93,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var expected = new RazorCSharpDocument();
var expected = RazorCSharpDocument.Create("", Array.Empty<RazorDiagnostic>());
codeDocument.Items[typeof(RazorCSharpDocument)] = expected;
// Act
@ -108,7 +109,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Arrange
var codeDocument = TestRazorCodeDocument.CreateEmpty();
var expected = new RazorCSharpDocument();
var expected = RazorCSharpDocument.Create("", Array.Empty<RazorDiagnostic>());
// Act
codeDocument.SetCSharpDocument(expected);