From c34a99e188f69a989ae6a9947c333d8327f4b84e Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Thu, 6 Jul 2017 14:58:26 -0700 Subject: [PATCH] Update the TagHelperBinding API to use IReadOnlyList. - Changed `Attributes` to return `IReadOnlyList>`. - Changed `GetBoundRules` to return `IReadOnlyList`. - Updated tests to react to new signature. #1510 --- .../Legacy/TagHelperParseTreeRewriter.cs | 6 ++-- .../TagHelperBinder.cs | 8 +++--- .../TagHelperBinding.cs | 10 +++---- .../DefaultTagHelperFactsService.cs | 3 +- .../TagHelperBinderTest.cs | 28 +++++++++---------- 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperParseTreeRewriter.cs b/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperParseTreeRewriter.cs index b38c477b58..9bf9d2d77d 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperParseTreeRewriter.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/Legacy/TagHelperParseTreeRewriter.cs @@ -256,7 +256,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy { tagHelperBinding = _tagHelperBinder.GetBinding( tagName, - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: _currentParentTagName); // If there are not TagHelperDescriptors associated with the end tag block that also have no @@ -314,7 +314,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy } // Internal for testing - internal IEnumerable> GetAttributeNameValuePairs(Block tagBlock) + internal IReadOnlyList> GetAttributeNameValuePairs(Block tagBlock) { // Need to calculate how many children we should take that represent the attributes. var childrenOffset = IsPartialTag(tagBlock) ? 0 : 1; @@ -322,7 +322,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy if (childCount <= 1) { - return Enumerable.Empty>(); + return Array.Empty>(); } _htmlAttributeTracker.Clear(); diff --git a/src/Microsoft.AspNetCore.Razor.Language/TagHelperBinder.cs b/src/Microsoft.AspNetCore.Razor.Language/TagHelperBinder.cs index 529a80325f..043fc3f73d 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/TagHelperBinder.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/TagHelperBinder.cs @@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.Razor.Language /// Will return null if no s are a match. public TagHelperBinding GetBinding( string tagName, - IEnumerable> attributes, + IReadOnlyList> attributes, string parentTagName) { if (!string.IsNullOrEmpty(_tagHelperPrefix) && @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Razor.Language } var tagNameWithoutPrefix = _tagHelperPrefix != null ? tagName.Substring(_tagHelperPrefix.Length) : tagName; - Dictionary> applicableDescriptorMappings = null; + Dictionary> applicableDescriptorMappings = null; foreach (var descriptor in descriptors) { var applicableRules = descriptor.TagMatchingRules.Where( @@ -84,10 +84,10 @@ namespace Microsoft.AspNetCore.Razor.Language { if (applicableDescriptorMappings == null) { - applicableDescriptorMappings = new Dictionary>(); + applicableDescriptorMappings = new Dictionary>(); } - applicableDescriptorMappings[descriptor] = applicableRules; + applicableDescriptorMappings[descriptor] = applicableRules.ToList(); } } diff --git a/src/Microsoft.AspNetCore.Razor.Language/TagHelperBinding.cs b/src/Microsoft.AspNetCore.Razor.Language/TagHelperBinding.cs index cb1e76a4c0..a5590c536e 100644 --- a/src/Microsoft.AspNetCore.Razor.Language/TagHelperBinding.cs +++ b/src/Microsoft.AspNetCore.Razor.Language/TagHelperBinding.cs @@ -7,13 +7,13 @@ namespace Microsoft.AspNetCore.Razor.Language { public sealed class TagHelperBinding { - private IReadOnlyDictionary> _mappings; + private IReadOnlyDictionary> _mappings; internal TagHelperBinding( string tagName, - IEnumerable> attributes, + IReadOnlyList> attributes, string parentTagName, - IReadOnlyDictionary> mappings, + IReadOnlyDictionary> mappings, string tagHelperPrefix) { TagName = tagName; @@ -30,11 +30,11 @@ namespace Microsoft.AspNetCore.Razor.Language public string ParentTagName { get; } - public IEnumerable> Attributes { get; } + public IReadOnlyList> Attributes { get; } public string TagHelperPrefix { get; } - public IEnumerable GetBoundRules(TagHelperDescriptor descriptor) + public IReadOnlyList GetBoundRules(TagHelperDescriptor descriptor) { return _mappings[descriptor]; } diff --git a/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultTagHelperFactsService.cs b/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultTagHelperFactsService.cs index b5a9030964..439bf92aed 100644 --- a/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultTagHelperFactsService.cs +++ b/src/Microsoft.VisualStudio.LanguageServices.Razor/DefaultTagHelperFactsService.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.ComponentModel.Composition; +using System.Linq; using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language.Legacy; @@ -41,7 +42,7 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor var prefix = documentContext.Prefix; var tagHelperBinder = new TagHelperBinder(prefix, descriptors); - var binding = tagHelperBinder.GetBinding(tagName, attributes, parentTag); + var binding = tagHelperBinder.GetBinding(tagName, attributes.ToList(), parentTag); return binding; } diff --git a/test/Microsoft.AspNetCore.Razor.Language.Test/TagHelperBinderTest.cs b/test/Microsoft.AspNetCore.Razor.Language.Test/TagHelperBinderTest.cs index 3ad16f88d5..d05f37e85c 100644 --- a/test/Microsoft.AspNetCore.Razor.Language.Test/TagHelperBinderTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Language.Test/TagHelperBinderTest.cs @@ -108,7 +108,7 @@ namespace Microsoft.AspNetCore.Razor.Language // Act var bindingResult = tagHelperBinder.GetBinding( tagName, - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: parentTagName); // Assert @@ -172,7 +172,7 @@ namespace Microsoft.AspNetCore.Razor.Language return new TheoryData< string, // tagName - IEnumerable>, // providedAttributes + IReadOnlyList>, // providedAttributes IEnumerable, // availableDescriptors IEnumerable> // expectedDescriptors { @@ -264,12 +264,12 @@ namespace Microsoft.AspNetCore.Razor.Language [MemberData(nameof(RequiredAttributeData))] public void GetBinding_ReturnsBindingResultDescriptorsWithRequiredAttributes( string tagName, - IEnumerable> providedAttributes, + IReadOnlyList> providedAttributes, object availableDescriptors, object expectedDescriptors) { // Arrange - var tagHelperBinder = new TagHelperBinder(null, (IEnumerable)availableDescriptors); + var tagHelperBinder = new TagHelperBinder(null, (IReadOnlyList)availableDescriptors); // Act var bindingResult = tagHelperBinder.GetBinding(tagName, providedAttributes, parentTagName: "p"); @@ -291,7 +291,7 @@ namespace Microsoft.AspNetCore.Razor.Language // Act var bindingResult = tagHelperBinder.GetBinding( tagName: "th", - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: "p"); // Assert @@ -311,11 +311,11 @@ namespace Microsoft.AspNetCore.Razor.Language // Act var bindingResultDiv = tagHelperBinder.GetBinding( tagName: "th:div", - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: "p"); var bindingResultSpan = tagHelperBinder.GetBinding( tagName: "th:span", - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: "p"); // Assert @@ -338,7 +338,7 @@ namespace Microsoft.AspNetCore.Razor.Language // Act var bindingResult = tagHelperBinder.GetBinding( tagName: "th:div", - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: "p"); // Assert @@ -361,7 +361,7 @@ namespace Microsoft.AspNetCore.Razor.Language // Act var bindingResult = tagHelperBinder.GetBinding( tagName: "div", - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: "p"); // Assert @@ -384,7 +384,7 @@ namespace Microsoft.AspNetCore.Razor.Language // Act var tagHelperBinding = tagHelperBinder.GetBinding( tagName: "foo", - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: "p"); // Assert @@ -410,11 +410,11 @@ namespace Microsoft.AspNetCore.Razor.Language // Act var divBinding = tagHelperBinder.GetBinding( tagName: "div", - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: "p"); var spanBinding = tagHelperBinder.GetBinding( tagName: "span", - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: "p"); // Assert @@ -442,7 +442,7 @@ namespace Microsoft.AspNetCore.Razor.Language // Act var bindingResult = tagHelperBinder.GetBinding( tagName: "div", - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: "p"); // Assert @@ -469,7 +469,7 @@ namespace Microsoft.AspNetCore.Razor.Language // Act var binding = tagHelperBinder.GetBinding( tagName: "div", - attributes: Enumerable.Empty>(), + attributes: Array.Empty>(), parentTagName: "p"); // Assert