Fix #1309 make CSharpDocument abstract
This commit is contained in:
parent
b17e506ce8
commit
b9dabd24be
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -68,13 +68,10 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
}
|
}
|
||||||
diagnostics.AddRange(renderingContext.Diagnostics);
|
diagnostics.AddRange(renderingContext.Diagnostics);
|
||||||
|
|
||||||
var csharpDocument = new RazorCSharpDocument()
|
var csharpDocument = RazorCSharpDocument.Create(
|
||||||
{
|
renderingContext.Writer.GenerateCode(),
|
||||||
GeneratedCode = renderingContext.Writer.GenerateCode(),
|
diagnostics,
|
||||||
LineMappings = renderingContext.LineMappings,
|
renderingContext.LineMappings);
|
||||||
Diagnostics = diagnostics
|
|
||||||
};
|
|
||||||
|
|
||||||
codeDocument.SetCSharpDocument(csharpDocument);
|
codeDocument.SetCSharpDocument(csharpDocument);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,56 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// 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.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Razor.Language
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// 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.
|
// 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 Microsoft.AspNetCore.Razor.Language.Intermediate;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
|
|
@ -92,7 +93,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
// Arrange
|
// Arrange
|
||||||
var codeDocument = TestRazorCodeDocument.CreateEmpty();
|
var codeDocument = TestRazorCodeDocument.CreateEmpty();
|
||||||
|
|
||||||
var expected = new RazorCSharpDocument();
|
var expected = RazorCSharpDocument.Create("", Array.Empty<RazorDiagnostic>());
|
||||||
codeDocument.Items[typeof(RazorCSharpDocument)] = expected;
|
codeDocument.Items[typeof(RazorCSharpDocument)] = expected;
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
|
|
@ -108,7 +109,7 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
// Arrange
|
// Arrange
|
||||||
var codeDocument = TestRazorCodeDocument.CreateEmpty();
|
var codeDocument = TestRazorCodeDocument.CreateEmpty();
|
||||||
|
|
||||||
var expected = new RazorCSharpDocument();
|
var expected = RazorCSharpDocument.Create("", Array.Empty<RazorDiagnostic>());
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
codeDocument.SetCSharpDocument(expected);
|
codeDocument.SetCSharpDocument(expected);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue