From c6a35e7b2647d51b2b5187cd0516b518557340e3 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Tue, 21 Mar 2017 12:06:22 -0700 Subject: [PATCH] Added HasIndexer property to BoundAttributeDescriptor --- .../BoundAttributeDescriptor.cs | 4 +- .../BoundAttributeDescriptorComparer.cs | 2 + ...agHelperBoundAttributeDescriptorBuilder.cs | 5 ++ .../DefaultTagHelperDescriptorFactoryTest.cs | 63 ++++++++++++++----- 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/BoundAttributeDescriptor.cs b/src/Microsoft.AspNetCore.Razor.Evolution/BoundAttributeDescriptor.cs index 08c1231e0c..915da152ff 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/BoundAttributeDescriptor.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/BoundAttributeDescriptor.cs @@ -27,12 +27,14 @@ namespace Microsoft.AspNetCore.Razor.Evolution public string Name { get; protected set; } - public string IndexerNamePrefix { get; set; } + public string IndexerNamePrefix { get; protected set; } public string TypeName { get; protected set; } public string IndexerTypeName { get; protected set; } + public bool HasIndexer { get; protected set; } + public string Documentation { get; protected set; } public string DisplayName { get; protected set; } diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/BoundAttributeDescriptorComparer.cs b/src/Microsoft.AspNetCore.Razor.Evolution/BoundAttributeDescriptorComparer.cs index 43ef4abb66..0955c1ddee 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/BoundAttributeDescriptorComparer.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/BoundAttributeDescriptorComparer.cs @@ -54,6 +54,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution string.Equals(descriptorX.Kind, descriptorY.Kind, StringComparison.Ordinal) && descriptorX.IsIndexerStringProperty == descriptorY.IsIndexerStringProperty && descriptorX.IsEnum == descriptorY.IsEnum && + descriptorX.HasIndexer == descriptorY.HasIndexer && string.Equals(descriptorX.Name, descriptorY.Name, _stringComparison) && string.Equals(descriptorX.IndexerNamePrefix, descriptorY.IndexerNamePrefix, _stringComparison) && string.Equals(descriptorX.TypeName, descriptorY.TypeName, StringComparison.Ordinal) && @@ -77,6 +78,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution hashCodeCombiner.Add(descriptor.Kind); hashCodeCombiner.Add(descriptor.IsIndexerStringProperty); hashCodeCombiner.Add(descriptor.IsEnum); + hashCodeCombiner.Add(descriptor.HasIndexer); hashCodeCombiner.Add(descriptor.Name, _stringComparer); hashCodeCombiner.Add(descriptor.IndexerNamePrefix, _stringComparer); hashCodeCombiner.Add(descriptor.TypeName, StringComparer.Ordinal); diff --git a/src/Microsoft.AspNetCore.Razor.Evolution/ITagHelperBoundAttributeDescriptorBuilder.cs b/src/Microsoft.AspNetCore.Razor.Evolution/ITagHelperBoundAttributeDescriptorBuilder.cs index 2c3c4efc04..a3cb37b7f2 100644 --- a/src/Microsoft.AspNetCore.Razor.Evolution/ITagHelperBoundAttributeDescriptorBuilder.cs +++ b/src/Microsoft.AspNetCore.Razor.Evolution/ITagHelperBoundAttributeDescriptorBuilder.cs @@ -35,6 +35,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution new[] { '@', '!', '<', '/', '?', '[', '>', ']', '=', '"', '\'', '*' }); private bool _isEnum; + private bool _hasIndexer; private string _indexerValueTypeName; private string _name; private string _propertyName; @@ -88,6 +89,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution { _indexerNamePrefix = attributeNamePrefix; _indexerValueTypeName = valueTypeName; + _hasIndexer = true; return this; } @@ -136,6 +138,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution _typeName, _indexerNamePrefix, _indexerValueTypeName, + _hasIndexer, _documentation, displayName, _metadata, @@ -256,6 +259,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution string typeName, string dictionaryAttributeNamePrefix, string dictionaryValueTypeName, + bool hasIndexer, string documentation, string displayName, Dictionary metadata, @@ -268,6 +272,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution TypeName = typeName; IndexerNamePrefix = dictionaryAttributeNamePrefix; IndexerTypeName = dictionaryValueTypeName; + HasIndexer = hasIndexer; Documentation = documentation; DisplayName = displayName; Diagnostics = new List(diagnostics); diff --git a/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorFactoryTest.cs b/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorFactoryTest.cs index ee5260cf68..1ea30a9965 100644 --- a/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorFactoryTest.cs +++ b/test/Microsoft.CodeAnalysis.Razor.Test/DefaultTagHelperDescriptorFactoryTest.cs @@ -1732,8 +1732,8 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces var dictionaryNamespace = typeof(IDictionary<,>).FullName; dictionaryNamespace = dictionaryNamespace.Substring(0, dictionaryNamespace.IndexOf('`')); - // tagTelperType, expectedAttributeDescriptors - return new TheoryData> + // tagHelperType, expectedAttributeDescriptors, expectedDiagnostics + return new TheoryData, IEnumerable> { { typeof(DefaultValidHtmlAttributePrefix), @@ -1745,7 +1745,8 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .TypeName($"{dictionaryNamespace}") .AsDictionary("dictionary-property-", typeof(string).FullName) .Build() - } + }, + Enumerable.Empty() }, { typeof(SingleValidHtmlAttributePrefix), @@ -1757,7 +1758,8 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .TypeName($"{dictionaryNamespace}") .AsDictionary("valid-name-", typeof(string).FullName) .Build() - } + }, + Enumerable.Empty() }, { typeof(MultipleValidHtmlAttributePrefix), @@ -1808,7 +1810,8 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .TypeName($"{dictionaryNamespace}") .AsDictionary("valid-prefix6", typeof(string).FullName) .Build() - } + }, + Enumerable.Empty() }, { typeof(SingleInvalidHtmlAttributePrefix), @@ -1818,11 +1821,16 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .Name("valid-name") .PropertyName(nameof(SingleInvalidHtmlAttributePrefix.StringProperty)) .TypeName(typeof(string).FullName) - .AddDiagnostic( - RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( - typeof(SingleInvalidHtmlAttributePrefix).FullName, - nameof(SingleInvalidHtmlAttributePrefix.StringProperty))) + .AddDiagnostic(RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( + typeof(SingleInvalidHtmlAttributePrefix).FullName, + nameof(SingleInvalidHtmlAttributePrefix.StringProperty))) .Build(), + }, + new[] + { + RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( + typeof(SingleInvalidHtmlAttributePrefix).FullName, + nameof(SingleInvalidHtmlAttributePrefix.StringProperty)) } }, { @@ -1841,7 +1849,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .AsDictionary("valid-prefix2-", typeof(string).FullName) .AddDiagnostic( RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( - typeof(MultipleInvalidHtmlAttributePrefix).FullName, + typeof(MultipleInvalidHtmlAttributePrefix).FullName, nameof(MultipleInvalidHtmlAttributePrefix.DictionaryOfIntProperty))) .Build(), ITagHelperBoundAttributeDescriptorBuilder.Create(typeof(MultipleInvalidHtmlAttributePrefix).FullName) @@ -1850,7 +1858,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .TypeName($"{typeof(IReadOnlyDictionary<,>).Namespace}.IReadOnlyDictionary") .AddDiagnostic( RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( - typeof(MultipleInvalidHtmlAttributePrefix).FullName, + typeof(MultipleInvalidHtmlAttributePrefix).FullName, nameof(MultipleInvalidHtmlAttributePrefix.ReadOnlyDictionaryProperty))) .Build(), ITagHelperBoundAttributeDescriptorBuilder.Create(typeof(MultipleInvalidHtmlAttributePrefix).FullName) @@ -1859,7 +1867,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .TypeName(typeof(int).FullName) .AddDiagnostic( RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( - typeof(MultipleInvalidHtmlAttributePrefix).FullName, + typeof(MultipleInvalidHtmlAttributePrefix).FullName, nameof(MultipleInvalidHtmlAttributePrefix.IntProperty))) .Build(), ITagHelperBoundAttributeDescriptorBuilder.Create(typeof(MultipleInvalidHtmlAttributePrefix).FullName) @@ -1869,7 +1877,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .AsDictionary("valid-prefix5-", typeof(string).FullName) .AddDiagnostic( RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( - typeof(MultipleInvalidHtmlAttributePrefix).FullName, + typeof(MultipleInvalidHtmlAttributePrefix).FullName, nameof(MultipleInvalidHtmlAttributePrefix.DictionaryOfIntSubclassProperty))) .Build(), ITagHelperBoundAttributeDescriptorBuilder.Create(typeof(MultipleInvalidHtmlAttributePrefix).FullName) @@ -1878,7 +1886,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .AsDictionary("valid-prefix6", typeof(string).FullName) .AddDiagnostic( RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( - typeof(MultipleInvalidHtmlAttributePrefix).FullName, + typeof(MultipleInvalidHtmlAttributePrefix).FullName, nameof(MultipleInvalidHtmlAttributePrefix.GetOnlyDictionaryAttributePrefix))) .Build(), ITagHelperBoundAttributeDescriptorBuilder.Create(typeof(MultipleInvalidHtmlAttributePrefix).FullName) @@ -1887,9 +1895,30 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces .AsDictionary("invalid-name7-", typeof(object).FullName) .AddDiagnostic( RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNull( - typeof(MultipleInvalidHtmlAttributePrefix).FullName, + typeof(MultipleInvalidHtmlAttributePrefix).FullName, nameof(MultipleInvalidHtmlAttributePrefix.GetOnlyDictionaryPropertyWithAttributeName))) .Build(), + }, + new[] + { + RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( + typeof(MultipleInvalidHtmlAttributePrefix).FullName, + nameof(MultipleInvalidHtmlAttributePrefix.DictionaryOfIntProperty)), + RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( + typeof(MultipleInvalidHtmlAttributePrefix).FullName, + nameof(MultipleInvalidHtmlAttributePrefix.ReadOnlyDictionaryProperty)), + RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( + typeof(MultipleInvalidHtmlAttributePrefix).FullName, + nameof(MultipleInvalidHtmlAttributePrefix.IntProperty)), + RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( + typeof(MultipleInvalidHtmlAttributePrefix).FullName, + nameof(MultipleInvalidHtmlAttributePrefix.DictionaryOfIntSubclassProperty)), + RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNotNull( + typeof(MultipleInvalidHtmlAttributePrefix).FullName, + nameof(MultipleInvalidHtmlAttributePrefix.GetOnlyDictionaryAttributePrefix)), + RazorDiagnosticFactory.CreateTagHelper_InvalidAttributePrefixNull( + typeof(MultipleInvalidHtmlAttributePrefix).FullName, + nameof(MultipleInvalidHtmlAttributePrefix.GetOnlyDictionaryPropertyWithAttributeName)), } }, }; @@ -1900,7 +1929,8 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces [MemberData(nameof(TagHelperWithPrefixData))] public void CreateDescriptor_WithPrefixes_ReturnsExpectedAttributeDescriptors( Type tagHelperType, - IEnumerable expectedAttributeDescriptors) + IEnumerable expectedAttributeDescriptors, + IEnumerable expectedDiagnostics) { // Arrange var factory = new DefaultTagHelperDescriptorFactory(Compilation, designTime: false); @@ -1914,6 +1944,7 @@ namespace Microsoft.CodeAnalysis.Razor.Workspaces expectedAttributeDescriptors, descriptor.BoundAttributes, BoundAttributeDescriptorComparer.CaseSensitive); + Assert.Equal(expectedDiagnostics, descriptor.GetAllDiagnostics()); } public static TheoryData HtmlConversionData