Move DefaultTagHelperTargetExtension errors to use RazorDiagnosticFactory.
- Added tests validating these errors because we didn't have any before. #1827
This commit is contained in:
parent
b900a82bc3
commit
926dae0d88
|
|
@ -550,7 +550,8 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RenderTagHelperAttributeInline(
|
// Internal for testing
|
||||||
|
internal void RenderTagHelperAttributeInline(
|
||||||
CodeRenderingContext context,
|
CodeRenderingContext context,
|
||||||
DefaultTagHelperPropertyIntermediateNode property,
|
DefaultTagHelperPropertyIntermediateNode property,
|
||||||
IntermediateNode node,
|
IntermediateNode node,
|
||||||
|
|
@ -574,20 +575,14 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions
|
||||||
}
|
}
|
||||||
else if (node is CSharpCodeIntermediateNode)
|
else if (node is CSharpCodeIntermediateNode)
|
||||||
{
|
{
|
||||||
var error = new RazorError(
|
var diagnostic = RazorDiagnosticFactory.CreateTagHelper_CodeBlocksNotSupportedInAttributes(span ?? SourceSpan.Undefined);
|
||||||
LegacyResources.TagHelpers_CodeBlocks_NotSupported_InAttributes,
|
context.Diagnostics.Add(diagnostic);
|
||||||
SourceLocation.FromSpan(span),
|
|
||||||
span == null ? -1 : span.Value.Length);
|
|
||||||
context.Diagnostics.Add(RazorDiagnostic.Create(error));
|
|
||||||
}
|
}
|
||||||
else if (node is TemplateIntermediateNode)
|
else if (node is TemplateIntermediateNode)
|
||||||
{
|
{
|
||||||
var expectedTypeName = property.IsIndexerNameMatch ? property.BoundAttribute.IndexerTypeName : property.BoundAttribute.TypeName;
|
var expectedTypeName = property.IsIndexerNameMatch ? property.BoundAttribute.IndexerTypeName : property.BoundAttribute.TypeName;
|
||||||
var error = new RazorError(
|
var diagnostic = RazorDiagnosticFactory.CreateTagHelper_InlineMarkupBlocksNotSupportedInAttributes(expectedTypeName, span ?? SourceSpan.Undefined);
|
||||||
LegacyResources.FormatTagHelpers_InlineMarkupBlocks_NotSupported_InAttributes(expectedTypeName),
|
context.Diagnostics.Add(diagnostic);
|
||||||
SourceLocation.FromSpan(span),
|
|
||||||
span == null ? -1 : span.Value.Length);
|
|
||||||
context.Diagnostics.Add(RazorDiagnostic.Create(error));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,32 @@ namespace Microsoft.AspNetCore.Razor.Language
|
||||||
return RazorDiagnostic.Create(Parsing_SectionsCannotBeNested, location, LegacyResources.SectionExample_CS);
|
return RazorDiagnostic.Create(Parsing_SectionsCannotBeNested, location, LegacyResources.SectionExample_CS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static readonly RazorDiagnosticDescriptor TagHelper_CodeBlocksNotSupportedInAttributes =
|
||||||
|
new RazorDiagnosticDescriptor(
|
||||||
|
$"{DiagnosticPrefix}2003",
|
||||||
|
() => LegacyResources.TagHelpers_CodeBlocks_NotSupported_InAttributes,
|
||||||
|
RazorDiagnosticSeverity.Error);
|
||||||
|
public static RazorDiagnostic CreateTagHelper_CodeBlocksNotSupportedInAttributes(SourceSpan location)
|
||||||
|
{
|
||||||
|
var diagnostic = RazorDiagnostic.Create(TagHelper_CodeBlocksNotSupportedInAttributes, location);
|
||||||
|
return diagnostic;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static readonly RazorDiagnosticDescriptor TagHelper_InlineMarkupBlocksNotSupportedInAttributes =
|
||||||
|
new RazorDiagnosticDescriptor(
|
||||||
|
$"{DiagnosticPrefix}2004",
|
||||||
|
() => LegacyResources.TagHelpers_InlineMarkupBlocks_NotSupported_InAttributes,
|
||||||
|
RazorDiagnosticSeverity.Error);
|
||||||
|
public static RazorDiagnostic CreateTagHelper_InlineMarkupBlocksNotSupportedInAttributes(string expectedTypeName, SourceSpan location)
|
||||||
|
{
|
||||||
|
var diagnostic = RazorDiagnostic.Create(
|
||||||
|
TagHelper_InlineMarkupBlocksNotSupportedInAttributes,
|
||||||
|
location,
|
||||||
|
expectedTypeName);
|
||||||
|
|
||||||
|
return diagnostic;
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region TagHelper Errors
|
#region TagHelper Errors
|
||||||
|
|
|
||||||
|
|
@ -359,6 +359,74 @@ EndAddHtmlAttributeValues(__tagHelperExecutionContext);
|
||||||
ignoreLineEndingDifferences: true);
|
ignoreLineEndingDifferences: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RenderTagHelperAttributeInline_NonString_StatementInAttribute_Errors()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var extension = new DefaultTagHelperTargetExtension();
|
||||||
|
var context = TestCodeRenderingContext.CreateRuntime();
|
||||||
|
var node = new DefaultTagHelperPropertyIntermediateNode()
|
||||||
|
{
|
||||||
|
BoundAttribute = IntPropertyTagHelper.BoundAttributes.Single(),
|
||||||
|
IsIndexerNameMatch = false,
|
||||||
|
};
|
||||||
|
var expectedLocation = new SourceSpan(100, 10);
|
||||||
|
var expectedDiagnostic = RazorDiagnosticFactory.CreateTagHelper_CodeBlocksNotSupportedInAttributes(expectedLocation);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
extension.RenderTagHelperAttributeInline(context, node, new CSharpCodeIntermediateNode(), expectedLocation);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var diagnostic = Assert.Single(context.Diagnostics);
|
||||||
|
Assert.Equal(expectedDiagnostic, diagnostic);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RenderTagHelperAttributeInline_NonStringIndexerMatch_TemplateInAttribute_Errors()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var extension = new DefaultTagHelperTargetExtension();
|
||||||
|
var context = TestCodeRenderingContext.CreateRuntime();
|
||||||
|
var node = new DefaultTagHelperPropertyIntermediateNode()
|
||||||
|
{
|
||||||
|
BoundAttribute = IntIndexerTagHelper.BoundAttributes.Single(),
|
||||||
|
IsIndexerNameMatch = true,
|
||||||
|
};
|
||||||
|
var expectedLocation = new SourceSpan(100, 10);
|
||||||
|
var expectedDiagnostic = RazorDiagnosticFactory.CreateTagHelper_InlineMarkupBlocksNotSupportedInAttributes("System.Int32", expectedLocation);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
extension.RenderTagHelperAttributeInline(context, node, new TemplateIntermediateNode(), expectedLocation);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var diagnostic = Assert.Single(context.Diagnostics);
|
||||||
|
Assert.Equal(expectedDiagnostic, diagnostic);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RenderTagHelperAttributeInline_NonString_TemplateInAttribute_Errors()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var extension = new DefaultTagHelperTargetExtension();
|
||||||
|
var context = TestCodeRenderingContext.CreateRuntime();
|
||||||
|
var node = new DefaultTagHelperPropertyIntermediateNode()
|
||||||
|
{
|
||||||
|
BoundAttribute = IntIndexerTagHelper.BoundAttributes.Single(),
|
||||||
|
IsIndexerNameMatch = false,
|
||||||
|
};
|
||||||
|
var expectedLocation = new SourceSpan(100, 10);
|
||||||
|
var expectedDiagnostic = RazorDiagnosticFactory.CreateTagHelper_InlineMarkupBlocksNotSupportedInAttributes(
|
||||||
|
"System.Collections.Generic.Dictionary<System.String, System.Int32>",
|
||||||
|
expectedLocation);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
extension.RenderTagHelperAttributeInline(context, node, new TemplateIntermediateNode(), expectedLocation);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
var diagnostic = Assert.Single(context.Diagnostics);
|
||||||
|
Assert.Equal(expectedDiagnostic, diagnostic);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WriteTagHelperProperty_DesignTime_StringProperty_HtmlContent_RendersCorrectly()
|
public void WriteTagHelperProperty_DesignTime_StringProperty_HtmlContent_RendersCorrectly()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue