Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ public ComponentDirectiveVisitor(string filePath, IReadOnlyList<TagHelperDescrip
{
// If this is a child content tag helper, we want to add it if it's original type is in scope.
// E.g, if the type name is `Test.MyComponent.ChildContent`, we want to add it if `Test.MyComponent` is in scope.
TrySplitNamespaceAndType(typeName, out typeName, out var _);
TrySplitNamespaceAndType(typeName, out var typeNameTextSpan, out var _);
typeName = GetTextSpanContent(typeNameTextSpan, typeName);
}

if (currentNamespace != null && IsTypeInScope(typeName, currentNamespace))
Expand Down Expand Up @@ -336,7 +337,8 @@ public override void VisitRazorDirective(RazorDirectiveSyntax node)
{
// If this is a child content tag helper, we want to add it if it's original type is in scope of the given namespace.
// E.g, if the type name is `Test.MyComponent.ChildContent`, we want to add it if `Test.MyComponent` is in this namespace.
TrySplitNamespaceAndType(typeName, out typeName, out var _);
TrySplitNamespaceAndType(typeName, out var typeNameTextSpan, out var _);
typeName = GetTextSpanContent(typeNameTextSpan, typeName);
}
if (typeName != null && IsTypeInNamespace(typeName, @namespace))
{
Expand All @@ -350,13 +352,13 @@ public override void VisitRazorDirective(RazorDirectiveSyntax node)

internal static bool IsTypeInNamespace(string typeName, string @namespace)
{
if (!TrySplitNamespaceAndType(typeName, out var typeNamespace, out var _) || typeNamespace == string.Empty)
if (!TrySplitNamespaceAndType(typeName, out var typeNamespace, out var _) || typeNamespace.Length == 0)
{
// Either the typeName is not the full type name or this type is at the top level.
return true;
}

return typeNamespace.Equals(@namespace, StringComparison.Ordinal);
return @namespace.Length == typeNamespace.Length && 0 == string.CompareOrdinal(typeName, typeNamespace.Start, @namespace, 0, @namespace.Length);
}

// Check if the given type is already in scope given the namespace of the current document.
Expand All @@ -366,12 +368,13 @@ internal static bool IsTypeInNamespace(string typeName, string @namespace)
// Whereas `MyComponents.SomethingElse.OtherComponent` is not in scope.
internal static bool IsTypeInScope(string typeName, string currentNamespace)
{
if (!TrySplitNamespaceAndType(typeName, out var typeNamespace, out var _) || typeNamespace == string.Empty)
if (!TrySplitNamespaceAndType(typeName, out var typeNamespaceTextSpan, out var _) || typeNamespaceTextSpan.Length == 0)
{
// Either the typeName is not the full type name or this type is at the top level.
return true;
}

var typeNamespace = GetTextSpanContent(typeNamespaceTextSpan, typeName);
var typeNamespaceSegments = typeNamespace.Split(NamespaceSeparators, StringSplitOptions.RemoveEmptyEntries);
var currentNamespaceSegments = currentNamespace.Split(NamespaceSeparators, StringSplitOptions.RemoveEmptyEntries);
if (typeNamespaceSegments.Length > currentNamespaceSegments.Length)
Expand Down Expand Up @@ -399,21 +402,23 @@ internal static bool IsTagHelperFromMangledClass(TagHelperDescriptor tagHelper)
{
// If this is a child content tag helper, we want to look at it's original type.
// E.g, if the type name is `Test.__generated__MyComponent.ChildContent`, we want to look at `Test.__generated__MyComponent`.
TrySplitNamespaceAndType(typeName, out typeName, out var _);
TrySplitNamespaceAndType(typeName, out var typeNameTextSpan, out var _);
typeName = GetTextSpanContent(typeNameTextSpan, typeName);
}
if (!TrySplitNamespaceAndType(typeName, out var _, out var className))
if (!TrySplitNamespaceAndType(typeName, out var _, out var classNameTextSpan))
Copy link
Contributor

@dougbu dougbu Jun 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor but this method could exit earlier if the original typeName were empty. No reason to get the string content of an empty span. #WontFix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to affect the functionality of this code, as there are some subtle behaviors here. Let me know if you think it's worth pursuing.


In reply to: 439606336 [](ancestors = 439606336)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not my code. I defer to other reviewers 😺

{
return false;
}
var className = GetTextSpanContent(classNameTextSpan, typeName);

return ComponentMetadata.IsMangledClass(className);
}

// Internal for testing.
internal static bool TrySplitNamespaceAndType(string fullTypeName, out string @namespace, out string typeName)
internal static bool TrySplitNamespaceAndType(string fullTypeName, out TextSpan @namespace, out TextSpan typeName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

{
@namespace = string.Empty;
typeName = string.Empty;
@namespace = default;
typeName = default;

if (string.IsNullOrEmpty(fullTypeName))
{
Expand Down Expand Up @@ -442,20 +447,26 @@ internal static bool TrySplitNamespaceAndType(string fullTypeName, out string @n

if (splitLocation == -1)
{
typeName = fullTypeName;
typeName = new TextSpan(0, fullTypeName.Length);
return true;
}

@namespace = fullTypeName.Substring(0, splitLocation);
@namespace = new TextSpan(0, splitLocation);

var typeNameStartLocation = splitLocation + 1;
if (typeNameStartLocation < fullTypeName.Length)
{
typeName = fullTypeName.Substring(typeNameStartLocation, fullTypeName.Length - typeNameStartLocation);
typeName = new TextSpan(typeNameStartLocation, fullTypeName.Length - typeNameStartLocation);
}

return true;
}

// Internal for testing.
internal static string GetTextSpanContent(TextSpan textSpan, string s)
{
return s.Substring(textSpan.Start, textSpan.Length);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1359,8 +1359,8 @@ public void TrySplitNamespaceAndType_WorksAsExpected(string fullTypeName, bool e

// Assert
Assert.Equal(expectedResult, result);
Assert.Equal(expectedNamespace, @namespace);
Assert.Equal(expectedTypeName, typeName);
Assert.Equal(expectedNamespace, DefaultRazorTagHelperBinderPhase.ComponentDirectiveVisitor.GetTextSpanContent(@namespace, fullTypeName));
Assert.Equal(expectedTypeName, DefaultRazorTagHelperBinderPhase.ComponentDirectiveVisitor.GetTextSpanContent(typeName, fullTypeName));
}

private static RazorSourceDocument CreateComponentTestSourceDocument(string content, string filePath = null)
Expand Down