-
Notifications
You must be signed in to change notification settings - Fork 229
Allow components in global namespace #10086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
8a05492
9de3c17
b225060
acf1208
a7713a7
e9f30fb
61ead07
d5eeae8
7f6c75d
cfb9176
ae4fc31
8586250
1836b11
0d0bf2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -115,8 +115,10 @@ private static TagHelperDescriptor CreateNameMatchingDescriptor( | |||||||||||||||
|
|
||||||||||||||||
| if (fullyQualified) | ||||||||||||||||
| { | ||||||||||||||||
| var containingNamespace = type.ContainingNamespace.ToDisplayString(); | ||||||||||||||||
| var fullName = $"{containingNamespace}.{type.Name}"; | ||||||||||||||||
| var containingNamespace = type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat); | ||||||||||||||||
| var fullName = string.IsNullOrEmpty(containingNamespace) | ||||||||||||||||
| ? type.Name | ||||||||||||||||
| : $"{containingNamespace}.{type.Name}"; | ||||||||||||||||
|
||||||||||||||||
| var containingNamespace = type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat); | |
| var fullName = string.IsNullOrEmpty(containingNamespace) | |
| ? type.Name | |
| : $"{containingNamespace}.{type.Name}"; | |
| var fullName = type.ContainingNamespace.IsGlobalNamespace | |
| ? type.Name | |
| : $"{type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat)}.{type.Name}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, is it possible to have a component in a nested type? If so, will this handle that correctly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, is it possible to have a component in a nested type? If so, will this handle that correctly?
I don't think it's possible. (You've already asked me this previously in #9689 (comment) :D)
In any case, the previous code did "{namespace}.{typeName}" and I've just changed it to omit the "{namespace}." prefix when needed, so nothing should change at this front, right?
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -303,12 +303,15 @@ public static bool TryComputeClassName(this RazorCodeDocument codeDocument, [Not | |
| } | ||
| #nullable disable | ||
|
|
||
| public static bool TryComputeNamespace(this RazorCodeDocument document, bool fallbackToRootNamespace, out string @namespace) | ||
| => TryComputeNamespace(document, fallbackToRootNamespace: fallbackToRootNamespace, allowEmptyRootNamespace: false, out @namespace); | ||
|
||
|
|
||
| // In general documents will have a relative path (relative to the project root). | ||
| // We can only really compute a nice namespace when we know a relative path. | ||
| // | ||
| // However all kinds of thing are possible in tools. We shouldn't barf here if the document isn't | ||
| // set up correctly. | ||
| public static bool TryComputeNamespace(this RazorCodeDocument document, bool fallbackToRootNamespace, out string @namespace) | ||
| internal static bool TryComputeNamespace(this RazorCodeDocument document, bool fallbackToRootNamespace, bool allowEmptyRootNamespace, out string @namespace) | ||
| { | ||
| if (document == null) | ||
| { | ||
|
|
@@ -318,22 +321,22 @@ public static bool TryComputeNamespace(this RazorCodeDocument document, bool fal | |
| @namespace = (string)document.Items[NamespaceKey]; | ||
| if (@namespace is null) | ||
| { | ||
| var result = TryComputeNamespaceCore(document, fallbackToRootNamespace, out @namespace); | ||
| var result = TryComputeNamespaceCore(document, fallbackToRootNamespace: fallbackToRootNamespace, allowEmptyRootNamespace: allowEmptyRootNamespace, out @namespace); | ||
| document.Items[NamespaceKey] = @namespace; | ||
| return result; | ||
| } | ||
|
|
||
| #if DEBUG | ||
| // In debug mode, even if we're cached, lets take the hit to run this again and make sure the cached value is correct. | ||
| // This is to help us find issues with caching logic during development. | ||
| var validateResult = TryComputeNamespaceCore(document, fallbackToRootNamespace, out var validateNamespace); | ||
| var validateResult = TryComputeNamespaceCore(document, fallbackToRootNamespace: fallbackToRootNamespace, allowEmptyRootNamespace: allowEmptyRootNamespace, out var validateNamespace); | ||
| Debug.Assert(validateResult, "We couldn't compute the namespace, but have a cached value, so something has gone wrong"); | ||
| Debug.Assert(validateNamespace == @namespace, $"We cached a namespace of {@namespace} but calculated that it should be {validateNamespace}"); | ||
| #endif | ||
|
|
||
| return true; | ||
|
|
||
| bool TryComputeNamespaceCore(RazorCodeDocument document, bool fallbackToRootNamespace, out string @namespace) | ||
| bool TryComputeNamespaceCore(RazorCodeDocument document, bool fallbackToRootNamespace, bool allowEmptyRootNamespace, out string @namespace) | ||
| { | ||
| var filePath = document.Source.FilePath; | ||
| if (filePath == null || document.Source.RelativePath == null || filePath.Length < document.Source.RelativePath.Length) | ||
|
|
@@ -397,11 +400,18 @@ bool TryComputeNamespaceCore(RazorCodeDocument document, bool fallbackToRootName | |
| var options = document.GetCodeGenerationOptions() ?? document.GetDocumentIntermediateNode()?.Options; | ||
| baseNamespace = options?.RootNamespace; | ||
| appendSuffix = true; | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(baseNamespace)) | ||
| // null means RootNamespace wasn't set yet, so we fail for now, tooling seems to rely on this | ||
| if (baseNamespace is null || | ||
| (!allowEmptyRootNamespace && string.IsNullOrEmpty(baseNamespace))) | ||
| { | ||
| @namespace = null; | ||
| return false; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // There was no valid @namespace directive and we couldn't compute the RootNamespace. | ||
| // There was no valid @namespace directive. | ||
| @namespace = null; | ||
| return false; | ||
| } | ||
|
|
@@ -444,7 +454,11 @@ bool TryComputeNamespaceCore(RazorCodeDocument document, bool fallbackToRootName | |
|
|
||
| previousLength = builder.Length; | ||
|
|
||
| builder.Append('.'); | ||
| if (previousLength != 0) | ||
| { | ||
| builder.Append('.'); | ||
| } | ||
|
|
||
| CSharpIdentifier.AppendSanitized(builder, token); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_type.ContainingNamespace.ToDisplayString()would be"<global namespace>"; withSymbolExtensions.FullNameTypeDisplayFormatwe get an empty string instead (this format was already used in other places, just not everywhere)