Add duplicate directive error for th prefix.

- Regenerated diagnostics for integration tests.
- Added a duplicate directive unit test.
- Added a new RazorDiagnostic entry in the factory.

#942
This commit is contained in:
N. Taylor Mullen 2017-06-28 15:17:36 -07:00
parent e70ce98213
commit 05cc4123a7
5 changed files with 57 additions and 1 deletions

View File

@ -1877,9 +1877,28 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
protected virtual void TagHelperPrefixDirective()
{
RazorDiagnostic duplicateDiagnostic = null;
if (Context.SeenDirectives.Contains(SyntaxConstants.CSharp.TagHelperPrefixKeyword))
{
// There wil always be at least 1 child because of the `@` transition.
var directiveStart = Context.Builder.CurrentBlock.Children.First().Start;
var errorLength = /* @ */ 1 + SyntaxConstants.CSharp.TagHelperPrefixKeyword.Length;
duplicateDiagnostic = RazorDiagnosticFactory.CreateParsing_DuplicateDirective(
SyntaxConstants.CSharp.TagHelperPrefixKeyword,
new SourceSpan(directiveStart, errorLength));
}
TagHelperDirective(
SyntaxConstants.CSharp.TagHelperPrefixKeyword,
(prefix, errors) => new TagHelperPrefixDirectiveChunkGenerator(prefix, errors));
(prefix, errors) =>
{
if (duplicateDiagnostic != null)
{
errors.Add(duplicateDiagnostic);
}
return new TagHelperPrefixDirectiveChunkGenerator(prefix, errors);
});
}
protected virtual void AddTagHelperDirective()

View File

@ -45,6 +45,16 @@ namespace Microsoft.AspNetCore.Razor.Language
return RazorDiagnostic.Create(CodeTarget_UnsupportedExtension, SourceSpan.Undefined, documentKind, extensionType.Name);
}
internal static readonly RazorDiagnosticDescriptor Parsing_DuplicateDirective =
new RazorDiagnosticDescriptor(
$"{DiagnosticPrefix}2001",
() => Resources.DuplicateDirective,
RazorDiagnosticSeverity.Error);
public static RazorDiagnostic CreateParsing_DuplicateDirective(string directive, SourceSpan location)
{
return RazorDiagnostic.Create(Parsing_DuplicateDirective, location, directive);
}
#endregion
#region TagHelper Errors

View File

@ -1113,6 +1113,29 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
.Accepts(AcceptedCharactersInternal.None)));
}
[Fact]
public void TagHelperPrefixDirective_DuplicatesCauseError()
{
// Arrange
var expectedDiagnostic = RazorDiagnosticFactory.CreateParsing_DuplicateDirective(
"tagHelperPrefix",
new SourceSpan(null, 22 + Environment.NewLine.Length, 1, 0, 16));
// Act
var document = ParseDocument(
@"@tagHelperPrefix ""th:""
@tagHelperPrefix ""th""",
directives: null,
designTime: false);
// Assert
var directive = document.Root.Children.OfType<Block>().Last();
var erroredSpan = (Span)directive.Children.Last();
var chunkGenerator = Assert.IsType<TagHelperPrefixDirectiveChunkGenerator>(erroredSpan.ChunkGenerator);
var diagnostic = Assert.Single(chunkGenerator.Diagnostics);
Assert.Equal(expectedDiagnostic, diagnostic);
}
[Fact]
public void TagHelperPrefixDirective_NoValueSucceeds()
{

View File

@ -6,7 +6,9 @@ TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cs
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(9,18): Error RZ9999: Unterminated string literal. Strings that start with a quotation mark (") must be terminated before the end of the line. However, strings that start with @ and a quotation mark (@") can span multiple lines.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(11,2): Error RZ9999: Directive 'tagHelperPrefix' must have a value.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(12,2): Error RZ9999: Directive 'tagHelperPrefix' must have a value.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(12,1): Error RZ2001: The 'tagHelperPrefix' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(13,18): Error RZ9999: Unterminated string literal. Strings that start with a quotation mark (") must be terminated before the end of the line. However, strings that start with @ and a quotation mark (@") can span multiple lines.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(13,1): Error RZ2001: The 'tagHelperPrefix' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(15,10): Error RZ9999: The 'inherits' directive expects a type name.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(16,1): Error RZ9999: The 'inherits' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(16,11): Error RZ9999: The 'inherits' directive expects a type name.

View File

@ -6,7 +6,9 @@ TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cs
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(9,18): Error RZ9999: Unterminated string literal. Strings that start with a quotation mark (") must be terminated before the end of the line. However, strings that start with @ and a quotation mark (@") can span multiple lines.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(11,2): Error RZ9999: Directive 'tagHelperPrefix' must have a value.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(12,2): Error RZ9999: Directive 'tagHelperPrefix' must have a value.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(12,1): Error RZ2001: The 'tagHelperPrefix' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(13,18): Error RZ9999: Unterminated string literal. Strings that start with a quotation mark (") must be terminated before the end of the line. However, strings that start with @ and a quotation mark (@") can span multiple lines.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(13,1): Error RZ2001: The 'tagHelperPrefix' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(15,10): Error RZ9999: The 'inherits' directive expects a type name.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(16,1): Error RZ9999: The 'inherits' directive may only occur once per document.
TestFiles/IntegrationTests/CodeGenerationIntegrationTest/IncompleteDirectives.cshtml(16,11): Error RZ9999: The 'inherits' directive expects a type name.