Removed LegacyRazorDiagnostic and RazorError
This commit is contained in:
parent
439c742dec
commit
34fe4099c2
|
|
@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
|||
/// <summary>
|
||||
/// Tracks the given <paramref name="error"/>.
|
||||
/// </summary>
|
||||
/// <param name="error">The <see cref="RazorError"/> to track.</param>
|
||||
/// <param name="error">The <see cref="RazorDiagnostic"/> to track.</param>
|
||||
public void OnError(RazorDiagnostic error) =>_errors.Add(error);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,81 +0,0 @@
|
|||
// 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.Globalization;
|
||||
using Microsoft.Extensions.Internal;
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||
{
|
||||
internal class RazorError : IEquatable<RazorError>
|
||||
{
|
||||
internal static readonly RazorError[] EmptyArray = new RazorError[0];
|
||||
|
||||
/// <summary>
|
||||
/// Used only for deserialization.
|
||||
/// </summary>
|
||||
public RazorError()
|
||||
: this(message: string.Empty, location: SourceLocation.Undefined, length: -1)
|
||||
{
|
||||
}
|
||||
|
||||
public RazorError(string message, int absoluteIndex, int lineIndex, int columnIndex, int length)
|
||||
: this(message, new SourceLocation(absoluteIndex, lineIndex, columnIndex), length)
|
||||
{
|
||||
}
|
||||
|
||||
public RazorError(string message, SourceLocation location, int length)
|
||||
{
|
||||
Message = message;
|
||||
Location = location;
|
||||
Length = length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets (or sets) the message describing the error.
|
||||
/// </summary>
|
||||
/// <remarks>Set property is only accessible for deserialization purposes.</remarks>
|
||||
public string Message { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets (or sets) the start position of the erroneous text.
|
||||
/// </summary>
|
||||
/// <remarks>Set property is only accessible for deserialization purposes.</remarks>
|
||||
public SourceLocation Location { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the length of the erroneous text.
|
||||
/// </summary>
|
||||
/// <remarks>Set property is only accessible for deserialization purposes.</remarks>
|
||||
public int Length { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format(CultureInfo.CurrentCulture, "Error @ {0}({2}) - [{1}]", Location, Message, Length);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
var error = obj as RazorError;
|
||||
return Equals(error);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
var hashCodeCombiner = HashCodeCombiner.Start();
|
||||
hashCodeCombiner.Add(Message, StringComparer.Ordinal);
|
||||
hashCodeCombiner.Add(Location);
|
||||
hashCodeCombiner.Add(Length);
|
||||
|
||||
return hashCodeCombiner;
|
||||
}
|
||||
|
||||
public bool Equals(RazorError other)
|
||||
{
|
||||
return other != null &&
|
||||
string.Equals(other.Message, Message, StringComparison.Ordinal) &&
|
||||
Location.Equals(other.Location) &&
|
||||
Length.Equals(other.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
// 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.Legacy;
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.Language
|
||||
{
|
||||
internal class LegacyRazorDiagnostic : RazorDiagnostic
|
||||
{
|
||||
private readonly RazorError _error;
|
||||
|
||||
public LegacyRazorDiagnostic(RazorError error)
|
||||
{
|
||||
_error = error;
|
||||
}
|
||||
|
||||
public override string Id => "RZ9999";
|
||||
|
||||
public override RazorDiagnosticSeverity Severity => RazorDiagnosticSeverity.Error;
|
||||
|
||||
public override SourceSpan Span => new SourceSpan(_error.Location, _error.Length);
|
||||
|
||||
public override string GetMessage(IFormatProvider formatProvider)
|
||||
{
|
||||
return _error.Message;
|
||||
}
|
||||
|
||||
public override bool Equals(RazorDiagnostic obj)
|
||||
{
|
||||
var other = obj as LegacyRazorDiagnostic;
|
||||
return other == null ? false : _error.Equals(other._error);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _error.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,6 @@
|
|||
// 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.Legacy;
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.Language
|
||||
{
|
||||
|
|
@ -45,16 +44,6 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
return new DefaultRazorDiagnostic(descriptor, span, args);
|
||||
}
|
||||
|
||||
internal static RazorDiagnostic Create(RazorError error)
|
||||
{
|
||||
if (error == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(error));
|
||||
}
|
||||
|
||||
return new LegacyRazorDiagnostic(error);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return ((IFormattable)this).ToString(null, null);
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Razor.Language;
|
||||
using Microsoft.AspNetCore.Razor.Language.Legacy;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
|
|
@ -47,12 +46,6 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor
|
|||
|
||||
return RazorDiagnostic.Create(descriptor, sourceSpan);
|
||||
}
|
||||
else if (string.Equals(typeName, typeof(LegacyRazorDiagnostic).FullName, StringComparison.Ordinal))
|
||||
{
|
||||
var error = new RazorError(message, absoluteIndex, lineIndex, characterIndex, length);
|
||||
|
||||
return RazorDiagnostic.Create(error);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -418,7 +418,9 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
|
||||
var originalTree = RazorSyntaxTree.Parse(sourceDocument);
|
||||
|
||||
var initialError = RazorDiagnostic.Create(new RazorError("Initial test error", SourceLocation.Zero, length: 1));
|
||||
var initialError = RazorDiagnostic.Create(
|
||||
new RazorDiagnosticDescriptor("RZ9999", () => "Initial test error", RazorDiagnosticSeverity.Error),
|
||||
new SourceSpan(SourceLocation.Zero, contentLength: 1));
|
||||
var expectedRewritingError = RazorDiagnosticFactory.CreateParsing_TagHelperFoundMalformedTagHelper(
|
||||
new SourceSpan(new SourceLocation(Environment.NewLine.Length * 2 + 30, 2, 1), contentLength: 4), "form");
|
||||
|
||||
|
|
|
|||
|
|
@ -1234,22 +1234,13 @@ catch(bar) { baz(); }", BlockKindInternal.Statement, SpanKindInternal.Code);
|
|||
ParseBlockTest(prefix + markup + suffix, expected);
|
||||
}
|
||||
|
||||
private void NamespaceImportTest(string content, string expectedNS, AcceptedCharactersInternal acceptedCharacters = AcceptedCharactersInternal.None, string errorMessage = null, SourceLocation? location = null)
|
||||
private void NamespaceImportTest(string content, string expectedNS, AcceptedCharactersInternal acceptedCharacters = AcceptedCharactersInternal.None, SourceLocation? location = null)
|
||||
{
|
||||
var errors = new RazorDiagnostic[0];
|
||||
if (!string.IsNullOrEmpty(errorMessage) && location.HasValue)
|
||||
{
|
||||
errors = new RazorDiagnostic[]
|
||||
{
|
||||
RazorDiagnostic.Create(new RazorError(errorMessage, location.Value, length: 1))
|
||||
};
|
||||
}
|
||||
ParseBlockTest(content,
|
||||
new DirectiveBlock(
|
||||
Factory.Code(content)
|
||||
.AsNamespaceImport(expectedNS)
|
||||
.Accepts(acceptedCharacters)),
|
||||
errors);
|
||||
.Accepts(acceptedCharacters)));
|
||||
}
|
||||
|
||||
private static StatementBlock CreateStatementBlock(MarkupBlock block)
|
||||
|
|
|
|||
|
|
@ -1955,11 +1955,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
|||
string document,
|
||||
IEnumerable<DirectiveDescriptor> descriptors,
|
||||
Block expected,
|
||||
params RazorError[] expectedErrors)
|
||||
params RazorDiagnostic[] expectedErrors)
|
||||
{
|
||||
var result = ParseCodeBlock(document, descriptors, designTime: false);
|
||||
|
||||
EvaluateResults(result, expected, expectedErrors.Select(error => RazorDiagnostic.Create(error)).ToList());
|
||||
EvaluateResults(result, expected, expectedErrors);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,75 +0,0 @@
|
|||
// 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 Newtonsoft.Json;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.Language.Legacy
|
||||
{
|
||||
public class RazorErrorTest
|
||||
{
|
||||
[Fact]
|
||||
public void RazorError_CanBeSerialized()
|
||||
{
|
||||
// Arrange
|
||||
var error = new RazorError(
|
||||
message: "Testing",
|
||||
location: new SourceLocation(absoluteIndex: 1, lineIndex: 2, characterIndex: 3),
|
||||
length: 456);
|
||||
var expectedSerializedError =
|
||||
$"{{\"{nameof(RazorError.Message)}\":\"Testing\",\"{nameof(RazorError.Location)}\":{{\"" +
|
||||
$"{nameof(SourceLocation.FilePath)}\":null,\"" +
|
||||
$"{nameof(SourceLocation.AbsoluteIndex)}\":1,\"{nameof(SourceLocation.LineIndex)}\":2,\"" +
|
||||
$"{nameof(SourceLocation.CharacterIndex)}\":3}},\"{nameof(RazorError.Length)}\":456}}";
|
||||
|
||||
// Act
|
||||
var serializedError = JsonConvert.SerializeObject(error);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedSerializedError, serializedError, StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RazorError_WithFilePath_CanBeSerialized()
|
||||
{
|
||||
// Arrange
|
||||
var error = new RazorError(
|
||||
message: "Testing",
|
||||
location: new SourceLocation("some-path", absoluteIndex: 1, lineIndex: 2, characterIndex: 56),
|
||||
length: 3);
|
||||
var expectedSerializedError =
|
||||
$"{{\"{nameof(RazorError.Message)}\":\"Testing\",\"{nameof(RazorError.Location)}\":{{\"" +
|
||||
$"{nameof(SourceLocation.FilePath)}\":\"some-path\",\"" +
|
||||
$"{nameof(SourceLocation.AbsoluteIndex)}\":1,\"{nameof(SourceLocation.LineIndex)}\":2,\"" +
|
||||
$"{nameof(SourceLocation.CharacterIndex)}\":56}},\"{nameof(RazorError.Length)}\":3}}";
|
||||
|
||||
// Act
|
||||
var serializedError = JsonConvert.SerializeObject(error);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expectedSerializedError, serializedError, StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RazorError_CanBeDeserialized()
|
||||
{
|
||||
// Arrange
|
||||
var error = new RazorError(
|
||||
message: "Testing",
|
||||
location: new SourceLocation("somepath", absoluteIndex: 1, lineIndex: 2, characterIndex: 3),
|
||||
length: 456);
|
||||
var serializedError = JsonConvert.SerializeObject(error);
|
||||
|
||||
// Act
|
||||
var deserializedError = JsonConvert.DeserializeObject<RazorError>(serializedError);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("Testing", deserializedError.Message, StringComparer.Ordinal);
|
||||
Assert.Equal(1, deserializedError.Location.AbsoluteIndex);
|
||||
Assert.Equal(2, deserializedError.Location.LineIndex);
|
||||
Assert.Equal(3, deserializedError.Location.CharacterIndex);
|
||||
Assert.Equal(456, deserializedError.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,170 +0,0 @@
|
|||
// 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.Globalization;
|
||||
using Microsoft.AspNetCore.Razor.Language.Legacy;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.Language
|
||||
{
|
||||
public class LegacyRazorDiagnosticTest
|
||||
{
|
||||
[Fact]
|
||||
public void LegacyRazorDiagnostic_Ctor()
|
||||
{
|
||||
// Arrange
|
||||
var span = new SourceSpan("test.cs", 15, 1, 8, 5);
|
||||
var error = new RazorError("This is an error", new SourceLocation("test.cs", 15, 1, 8), 5);
|
||||
|
||||
// Act
|
||||
var diagnostic = new LegacyRazorDiagnostic(error);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("RZ9999", diagnostic.Id);
|
||||
Assert.Equal(RazorDiagnosticSeverity.Error, diagnostic.Severity);
|
||||
Assert.Equal(span, diagnostic.Span);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LegacyRazorDiagnostic_GetMessage()
|
||||
{
|
||||
// Arrange
|
||||
var error = new RazorError("This is an error", SourceLocation.Zero, 5);
|
||||
var diagnostic = new LegacyRazorDiagnostic(error);
|
||||
|
||||
// Act
|
||||
var result = diagnostic.GetMessage();
|
||||
|
||||
// Assert
|
||||
Assert.Equal("This is an error", result);
|
||||
}
|
||||
|
||||
// RazorError doesn't support format strings.
|
||||
[Fact]
|
||||
public void LegacyRazorDiagnostic_GetMessage_FormatProvider()
|
||||
{
|
||||
// Arrange
|
||||
var error = new RazorError("This is an error", SourceLocation.Zero, 5);
|
||||
var diagnostic = new LegacyRazorDiagnostic(error);
|
||||
|
||||
// Act
|
||||
var result = diagnostic.GetMessage(CultureInfo.InvariantCulture);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("This is an error", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LegacyRazorDiagnostic_ToString()
|
||||
{
|
||||
// Arrange
|
||||
var error = new RazorError("This is an error", SourceLocation.Zero, 5);
|
||||
var diagnostic = new LegacyRazorDiagnostic(error);
|
||||
|
||||
// Act
|
||||
var result = diagnostic.ToString();
|
||||
|
||||
// Assert
|
||||
Assert.Equal("(1,1): Error RZ9999: This is an error", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LegacyRazorDiagnostic_ToString_FormatProvider()
|
||||
{
|
||||
// Arrange
|
||||
var error = new RazorError("This is an error", SourceLocation.Zero, 5);
|
||||
var diagnostic = new LegacyRazorDiagnostic(new RazorError("This is an error", SourceLocation.Zero, 5));
|
||||
|
||||
// Act
|
||||
var result = ((IFormattable)diagnostic).ToString("ignored", CultureInfo.InvariantCulture);
|
||||
|
||||
// Assert
|
||||
Assert.Equal("(1,1): Error RZ9999: This is an error", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LegacyRazorDiagnostic_Equals()
|
||||
{
|
||||
// Arrange
|
||||
var diagnostic1 = new LegacyRazorDiagnostic(new RazorError("This is an error", SourceLocation.Zero, 5));
|
||||
var diagnostic2 = new LegacyRazorDiagnostic(new RazorError("This is an error", SourceLocation.Zero, 5));
|
||||
|
||||
// Act
|
||||
var result = diagnostic1.Equals(diagnostic2);
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LegacyRazorDiagnostic_NotEquals_DifferentLocation()
|
||||
{
|
||||
// Arrange
|
||||
var diagnostic1 = new LegacyRazorDiagnostic(new RazorError("This is an error", SourceLocation.Zero, 5));
|
||||
var diagnostic2 = new LegacyRazorDiagnostic(new RazorError("This is an error", SourceLocation.Zero, 1));
|
||||
|
||||
// Act
|
||||
var result = diagnostic1.Equals(diagnostic2);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LegacyRazorDiagnostic_NotEquals_DifferentMessage()
|
||||
{
|
||||
// Arrange
|
||||
var diagnostic1 = new LegacyRazorDiagnostic(new RazorError("This is an error", SourceLocation.Zero, 5));
|
||||
var diagnostic2 = new LegacyRazorDiagnostic(new RazorError("This is maybe an error", SourceLocation.Zero, 5));
|
||||
|
||||
// Act
|
||||
var result = diagnostic1.Equals(diagnostic2);
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LegacyRazorDiagnostic_HashCodesEqual()
|
||||
{
|
||||
// Arrange
|
||||
var diagnostic1 = new LegacyRazorDiagnostic(new RazorError("This is an error", SourceLocation.Zero, 5));
|
||||
var diagnostic2 = new LegacyRazorDiagnostic(new RazorError("This is an error", SourceLocation.Zero, 5));
|
||||
|
||||
// Act
|
||||
var result = diagnostic1.GetHashCode() == diagnostic2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LegacyRazorDiagnostic_HashCodesNotEqual_DifferentLocation()
|
||||
{
|
||||
// Arrange
|
||||
var diagnostic1 = new LegacyRazorDiagnostic(new RazorError("This is an error", SourceLocation.Zero, 5));
|
||||
var diagnostic2 = new LegacyRazorDiagnostic(new RazorError("This is an error", SourceLocation.Zero, 2));
|
||||
|
||||
// Act
|
||||
var result = diagnostic1.GetHashCode() == diagnostic2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void LegacyRazorDiagnostic_HashCodesNotEqual_DifferentMessage()
|
||||
{
|
||||
// Arrange
|
||||
var diagnostic1 = new LegacyRazorDiagnostic(new RazorError("This is an error", SourceLocation.Zero, 5));
|
||||
var diagnostic2 = new LegacyRazorDiagnostic(new RazorError("This is maybe an error", SourceLocation.Zero, 5));
|
||||
|
||||
// Act
|
||||
var result = diagnostic1.GetHashCode() == diagnostic2.GetHashCode();
|
||||
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -41,22 +41,5 @@ namespace Microsoft.AspNetCore.Razor.Language
|
|||
Assert.Equal(RazorDiagnosticSeverity.Error, defaultDiagnostic.Severity);
|
||||
Assert.Equal(span, diagnostic.Span);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_WithRazorError_CreatesLegacyRazorDiagnostic()
|
||||
{
|
||||
// Arrange
|
||||
var span = new SourceSpan("test.cs", 15, 1, 8, 5);
|
||||
var error = new RazorError("This is an error", new SourceLocation("test.cs", 15, 1, 8), 5);
|
||||
|
||||
// Act
|
||||
var diagnostic = RazorDiagnostic.Create(error);
|
||||
|
||||
// Assert
|
||||
var legacyDiagnostic = Assert.IsType<LegacyRazorDiagnostic>(diagnostic);
|
||||
Assert.Equal("RZ9999", legacyDiagnostic.Id);
|
||||
Assert.Equal(RazorDiagnosticSeverity.Error, legacyDiagnostic.Severity);
|
||||
Assert.Equal(span, diagnostic.Span);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -88,8 +88,7 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor
|
|||
builder.AllowChildTag("allowed-child-one")
|
||||
.AddMetadata("foo", "bar")
|
||||
.AddDiagnostic(RazorDiagnostic.Create(
|
||||
new RazorDiagnosticDescriptor("id", () => "Test Message 1", RazorDiagnosticSeverity.Error), new SourceSpan(null, 10, 20, 30, 40)))
|
||||
.AddDiagnostic(RazorDiagnostic.Create(new RazorError("Test Message 2", 10, 20, 30, 40)));
|
||||
new RazorDiagnosticDescriptor("id", () => "Test Message", RazorDiagnosticSeverity.Error), new SourceSpan(null, 10, 20, 30, 40)));
|
||||
});
|
||||
|
||||
// Act
|
||||
|
|
|
|||
Loading…
Reference in New Issue