Update the TagHelperBinding API to use IReadOnlyList.

- Changed `Attributes` to return `IReadOnlyList<KeyValuePair<string, string>>`.
- Changed `GetBoundRules` to return `IReadOnlyList<TagMatchingRuleDescriptor>`.
- Updated tests to react to new signature.

#1510
This commit is contained in:
N. Taylor Mullen 2017-07-06 14:58:26 -07:00
parent 93875f973b
commit c34a99e188
5 changed files with 28 additions and 27 deletions

View File

@ -256,7 +256,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
{ {
tagHelperBinding = _tagHelperBinder.GetBinding( tagHelperBinding = _tagHelperBinder.GetBinding(
tagName, tagName,
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: _currentParentTagName); parentTagName: _currentParentTagName);
// If there are not TagHelperDescriptors associated with the end tag block that also have no // 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 for testing
internal IEnumerable<KeyValuePair<string, string>> GetAttributeNameValuePairs(Block tagBlock) internal IReadOnlyList<KeyValuePair<string, string>> GetAttributeNameValuePairs(Block tagBlock)
{ {
// Need to calculate how many children we should take that represent the attributes. // Need to calculate how many children we should take that represent the attributes.
var childrenOffset = IsPartialTag(tagBlock) ? 0 : 1; var childrenOffset = IsPartialTag(tagBlock) ? 0 : 1;
@ -322,7 +322,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Legacy
if (childCount <= 1) if (childCount <= 1)
{ {
return Enumerable.Empty<KeyValuePair<string, string>>(); return Array.Empty<KeyValuePair<string, string>>();
} }
_htmlAttributeTracker.Clear(); _htmlAttributeTracker.Clear();

View File

@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.Razor.Language
/// Will return <c>null</c> if no <see cref="TagHelperDescriptor"/>s are a match.</returns> /// Will return <c>null</c> if no <see cref="TagHelperDescriptor"/>s are a match.</returns>
public TagHelperBinding GetBinding( public TagHelperBinding GetBinding(
string tagName, string tagName,
IEnumerable<KeyValuePair<string, string>> attributes, IReadOnlyList<KeyValuePair<string, string>> attributes,
string parentTagName) string parentTagName)
{ {
if (!string.IsNullOrEmpty(_tagHelperPrefix) && if (!string.IsNullOrEmpty(_tagHelperPrefix) &&
@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Razor.Language
} }
var tagNameWithoutPrefix = _tagHelperPrefix != null ? tagName.Substring(_tagHelperPrefix.Length) : tagName; var tagNameWithoutPrefix = _tagHelperPrefix != null ? tagName.Substring(_tagHelperPrefix.Length) : tagName;
Dictionary<TagHelperDescriptor, IEnumerable<TagMatchingRuleDescriptor>> applicableDescriptorMappings = null; Dictionary<TagHelperDescriptor, IReadOnlyList<TagMatchingRuleDescriptor>> applicableDescriptorMappings = null;
foreach (var descriptor in descriptors) foreach (var descriptor in descriptors)
{ {
var applicableRules = descriptor.TagMatchingRules.Where( var applicableRules = descriptor.TagMatchingRules.Where(
@ -84,10 +84,10 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
if (applicableDescriptorMappings == null) if (applicableDescriptorMappings == null)
{ {
applicableDescriptorMappings = new Dictionary<TagHelperDescriptor, IEnumerable<TagMatchingRuleDescriptor>>(); applicableDescriptorMappings = new Dictionary<TagHelperDescriptor, IReadOnlyList<TagMatchingRuleDescriptor>>();
} }
applicableDescriptorMappings[descriptor] = applicableRules; applicableDescriptorMappings[descriptor] = applicableRules.ToList();
} }
} }

View File

@ -7,13 +7,13 @@ namespace Microsoft.AspNetCore.Razor.Language
{ {
public sealed class TagHelperBinding public sealed class TagHelperBinding
{ {
private IReadOnlyDictionary<TagHelperDescriptor, IEnumerable<TagMatchingRuleDescriptor>> _mappings; private IReadOnlyDictionary<TagHelperDescriptor, IReadOnlyList<TagMatchingRuleDescriptor>> _mappings;
internal TagHelperBinding( internal TagHelperBinding(
string tagName, string tagName,
IEnumerable<KeyValuePair<string, string>> attributes, IReadOnlyList<KeyValuePair<string, string>> attributes,
string parentTagName, string parentTagName,
IReadOnlyDictionary<TagHelperDescriptor, IEnumerable<TagMatchingRuleDescriptor>> mappings, IReadOnlyDictionary<TagHelperDescriptor, IReadOnlyList<TagMatchingRuleDescriptor>> mappings,
string tagHelperPrefix) string tagHelperPrefix)
{ {
TagName = tagName; TagName = tagName;
@ -30,11 +30,11 @@ namespace Microsoft.AspNetCore.Razor.Language
public string ParentTagName { get; } public string ParentTagName { get; }
public IEnumerable<KeyValuePair<string, string>> Attributes { get; } public IReadOnlyList<KeyValuePair<string, string>> Attributes { get; }
public string TagHelperPrefix { get; } public string TagHelperPrefix { get; }
public IEnumerable<TagMatchingRuleDescriptor> GetBoundRules(TagHelperDescriptor descriptor) public IReadOnlyList<TagMatchingRuleDescriptor> GetBoundRules(TagHelperDescriptor descriptor)
{ {
return _mappings[descriptor]; return _mappings[descriptor];
} }

View File

@ -4,6 +4,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language; using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Legacy; using Microsoft.AspNetCore.Razor.Language.Legacy;
@ -41,7 +42,7 @@ namespace Microsoft.VisualStudio.LanguageServices.Razor
var prefix = documentContext.Prefix; var prefix = documentContext.Prefix;
var tagHelperBinder = new TagHelperBinder(prefix, descriptors); var tagHelperBinder = new TagHelperBinder(prefix, descriptors);
var binding = tagHelperBinder.GetBinding(tagName, attributes, parentTag); var binding = tagHelperBinder.GetBinding(tagName, attributes.ToList(), parentTag);
return binding; return binding;
} }

View File

@ -108,7 +108,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Act // Act
var bindingResult = tagHelperBinder.GetBinding( var bindingResult = tagHelperBinder.GetBinding(
tagName, tagName,
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: parentTagName); parentTagName: parentTagName);
// Assert // Assert
@ -172,7 +172,7 @@ namespace Microsoft.AspNetCore.Razor.Language
return new TheoryData< return new TheoryData<
string, // tagName string, // tagName
IEnumerable<KeyValuePair<string, string>>, // providedAttributes IReadOnlyList<KeyValuePair<string, string>>, // providedAttributes
IEnumerable<TagHelperDescriptor>, // availableDescriptors IEnumerable<TagHelperDescriptor>, // availableDescriptors
IEnumerable<TagHelperDescriptor>> // expectedDescriptors IEnumerable<TagHelperDescriptor>> // expectedDescriptors
{ {
@ -264,12 +264,12 @@ namespace Microsoft.AspNetCore.Razor.Language
[MemberData(nameof(RequiredAttributeData))] [MemberData(nameof(RequiredAttributeData))]
public void GetBinding_ReturnsBindingResultDescriptorsWithRequiredAttributes( public void GetBinding_ReturnsBindingResultDescriptorsWithRequiredAttributes(
string tagName, string tagName,
IEnumerable<KeyValuePair<string, string>> providedAttributes, IReadOnlyList<KeyValuePair<string, string>> providedAttributes,
object availableDescriptors, object availableDescriptors,
object expectedDescriptors) object expectedDescriptors)
{ {
// Arrange // Arrange
var tagHelperBinder = new TagHelperBinder(null, (IEnumerable<TagHelperDescriptor>)availableDescriptors); var tagHelperBinder = new TagHelperBinder(null, (IReadOnlyList<TagHelperDescriptor>)availableDescriptors);
// Act // Act
var bindingResult = tagHelperBinder.GetBinding(tagName, providedAttributes, parentTagName: "p"); var bindingResult = tagHelperBinder.GetBinding(tagName, providedAttributes, parentTagName: "p");
@ -291,7 +291,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Act // Act
var bindingResult = tagHelperBinder.GetBinding( var bindingResult = tagHelperBinder.GetBinding(
tagName: "th", tagName: "th",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p"); parentTagName: "p");
// Assert // Assert
@ -311,11 +311,11 @@ namespace Microsoft.AspNetCore.Razor.Language
// Act // Act
var bindingResultDiv = tagHelperBinder.GetBinding( var bindingResultDiv = tagHelperBinder.GetBinding(
tagName: "th:div", tagName: "th:div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p"); parentTagName: "p");
var bindingResultSpan = tagHelperBinder.GetBinding( var bindingResultSpan = tagHelperBinder.GetBinding(
tagName: "th:span", tagName: "th:span",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p"); parentTagName: "p");
// Assert // Assert
@ -338,7 +338,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Act // Act
var bindingResult = tagHelperBinder.GetBinding( var bindingResult = tagHelperBinder.GetBinding(
tagName: "th:div", tagName: "th:div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p"); parentTagName: "p");
// Assert // Assert
@ -361,7 +361,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Act // Act
var bindingResult = tagHelperBinder.GetBinding( var bindingResult = tagHelperBinder.GetBinding(
tagName: "div", tagName: "div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p"); parentTagName: "p");
// Assert // Assert
@ -384,7 +384,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Act // Act
var tagHelperBinding = tagHelperBinder.GetBinding( var tagHelperBinding = tagHelperBinder.GetBinding(
tagName: "foo", tagName: "foo",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p"); parentTagName: "p");
// Assert // Assert
@ -410,11 +410,11 @@ namespace Microsoft.AspNetCore.Razor.Language
// Act // Act
var divBinding = tagHelperBinder.GetBinding( var divBinding = tagHelperBinder.GetBinding(
tagName: "div", tagName: "div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p"); parentTagName: "p");
var spanBinding = tagHelperBinder.GetBinding( var spanBinding = tagHelperBinder.GetBinding(
tagName: "span", tagName: "span",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p"); parentTagName: "p");
// Assert // Assert
@ -442,7 +442,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Act // Act
var bindingResult = tagHelperBinder.GetBinding( var bindingResult = tagHelperBinder.GetBinding(
tagName: "div", tagName: "div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p"); parentTagName: "p");
// Assert // Assert
@ -469,7 +469,7 @@ namespace Microsoft.AspNetCore.Razor.Language
// Act // Act
var binding = tagHelperBinder.GetBinding( var binding = tagHelperBinder.GetBinding(
tagName: "div", tagName: "div",
attributes: Enumerable.Empty<KeyValuePair<string, string>>(), attributes: Array.Empty<KeyValuePair<string, string>>(),
parentTagName: "p"); parentTagName: "p");
// Assert // Assert