Skip to content

Commit 07e2705

Browse files
authored
Prefix all code generated types with global:: (#92)
* Prefix all code generated types with global:: * at * ok * ? * - * . * fix
1 parent a1b3517 commit 07e2705

25 files changed

Lines changed: 864 additions & 552 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ProxyInterfaceSourceGenerator;
2+
3+
internal static class Constants
4+
{
5+
internal const string GlobalPrefix = "global::";
6+
}

src/ProxyInterfaceSourceGenerator/Extensions/ParameterSymbolExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static string GetDefaultValue(this IParameterSymbol ps)
4747
{
4848
defaultValue = ps.Type.IsReferenceType
4949
? ParameterValueNull : // The parameter is a ReferenceType, so use "null".
50-
$"default({ps.Type})"; // The parameter is not a ReferenceType, so use "default(T)".
50+
$"default({Constants.GlobalPrefix}{ps.Type})"; // The parameter is not a ReferenceType, so use "default(T)".
5151
}
5252
}
5353
else

src/ProxyInterfaceSourceGenerator/Extensions/SymbolExtensions.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,53 @@ public static IReadOnlyList<string> GetAttributesAsList(this ISymbol symbol)
1717
{
1818
return symbol
1919
.GetAttributes()
20-
.Where(a => a.AttributeClass.IsPublic() && !ExcludedAttributes.Contains(a.AttributeClass?.ToString(), StringComparer.OrdinalIgnoreCase))
21-
.Select(a => $"[{a}]")
20+
.Where(a => a.AttributeClass.IsPublic() && !ExcludedAttributes.Contains(a.AttributeClass!.ToString(), StringComparer.OrdinalIgnoreCase))
21+
.Select(a =>
22+
{
23+
var sb = new StringBuilder();
24+
sb.Append($"[{a.AttributeClass!.ToFullyQualifiedDisplayString()}");
25+
26+
var args = a.ConstructorArguments.Select(FormatAttributeArgument).ToList();
27+
args.AddRange(a.NamedArguments.Select(kvp => $"{kvp.Key} = {FormatAttributeArgument(kvp.Value)}"));
28+
29+
if (args.Count > 0)
30+
{
31+
sb.Append($"({string.Join(", ", args)})");
32+
}
33+
34+
sb.Append("]");
35+
return sb.ToString();
36+
})
2237
.ToArray();
2338
}
2439

40+
private static string FormatAttributeArgument(TypedConstant arg)
41+
{
42+
if (arg.IsNull)
43+
{
44+
return "null";
45+
}
46+
47+
if (arg.Kind == TypedConstantKind.Type)
48+
{
49+
return $"typeof({Constants.GlobalPrefix}{arg.Value})";
50+
}
51+
52+
if (arg.Kind == TypedConstantKind.Enum)
53+
{
54+
var enumType = arg.Type;
55+
if (enumType is null)
56+
{
57+
return arg.Value?.ToString() ?? string.Empty;
58+
}
59+
60+
var member = enumType.GetMembers().OfType<IFieldSymbol>().FirstOrDefault(f => f.ConstantValue is not null && f.ConstantValue.Equals(arg.Value));
61+
return $"{enumType.ToFullyQualifiedDisplayString()}.{member?.Name ?? arg.Value?.ToString()}";
62+
}
63+
64+
return arg.ToCSharpString();
65+
}
66+
2567
public static string GetAttributesPrefix(this ISymbol symbol)
2668
{
2769
var attributes = symbol.GetAttributesAsList();

src/ProxyInterfaceSourceGenerator/FileGenerators/BaseGenerator.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,17 @@ private void TryAddDirect(string typeSymbolAsString, ProxyData existing)
190190
var found = Context.ReplacedTypes.FirstOrDefault(r => r.Direct && r.ClassType == typeSymbolAsString);
191191
if (found == null)
192192
{
193-
var proxy = $"global::{existing.NamespaceDot}{existing.ShortMetadataName}Proxy"; // global::ProxyInterfaceSourceGeneratorTests.Source.TimeProviderProxy
193+
var proxy = $"{Constants.GlobalPrefix}{existing.NamespaceDot}{existing.ShortMetadataName}Proxy"; // global::ProxyInterfaceSourceGeneratorTests.Source.TimeProviderProxy
194194
Context.ReplacedTypes.Add(new(typeSymbolAsString, existing.FullInterfaceName, string.Empty, string.Empty, proxy, true));
195195
}
196196
}
197197

198198
protected bool TryGetNamedTypeSymbolByFullName(TypeKind kind, string name, IEnumerable<string> usings, [NotNullWhen(true)] out ClassSymbol? classSymbol)
199199
{
200200
classSymbol = default;
201-
const string globalPrefix = "global::";
202-
if (name.StartsWith(globalPrefix, StringComparison.Ordinal))
201+
if (name.StartsWith(Constants.GlobalPrefix, StringComparison.Ordinal))
203202
{
204-
name = name.Substring(globalPrefix.Length);
203+
name = name.Substring(Constants.GlobalPrefix.Length);
205204
}
206205

207206
// The GetTypeByMetadataName method returns null if no type matches the full name or if 2 or more types (in different assemblies) match the full name.

src/ProxyInterfaceSourceGenerator/FileGenerators/ProxyClassesGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private string CreateProxyClassCode(
6868

6969
if (firstExtends is not null)
7070
{
71-
extends = $"global::{firstExtends.NamespaceDot}{firstExtends.ShortMetadataName}Proxy, ";
71+
extends = $"{Constants.GlobalPrefix}{firstExtends.NamespaceDot}{firstExtends.ShortMetadataName}Proxy, ";
7272
@base = " : base(instance)";
7373
@new = "new ";
7474
instanceBaseDefinition = $"public {firstExtends.FullQualifiedTypeName} _Instance{firstExtends.FullQualifiedTypeName.GetLastPart()} {{ get; }}";

src/ProxyInterfaceSourceGenerator/SyntaxReceiver/AttributeArgumentListParser.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static ProxyInterfaceGeneratorAttributeArguments Parse(AttributeSyntax? a
4141
}
4242
else
4343
{
44-
throw new ArgumentException("The first argument from the ProxyAttribute should be a Type.");
44+
throw new ArgumentException("The first argument from the ProxyAttribute should be a valid resolvable Type.");
4545
}
4646

4747
var array = attributeSyntax.ArgumentList?.Arguments.ToArray() ?? [];
@@ -129,6 +129,14 @@ private static bool TryParseAsType(
129129
}
130130

131131
var typeInfo = semanticModel.GetTypeInfo(typeSyntax);
132+
133+
if (typeInfo.Type is IErrorTypeSymbol)
134+
{
135+
// The Roslyn compiler could not resolve the type represented by typeSyntax.
136+
// There was an error in the code being analyzed, the type doesn't exist or cannot be resolved in the current context.
137+
return false;
138+
}
139+
132140
var typeSymbol = typeInfo.Type!;
133141

134142
info = new(typeSymbol.ToFullyQualifiedDisplayString(), typeSymbol.GetFullMetadataName(), isGeneric);

src/ProxyInterfaceSourceGenerator/SyntaxReceiver/ProxySyntaxReceiver.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace ProxyInterfaceSourceGenerator.SyntaxReceiver;
88

99
internal class ProxySyntaxReceiver : ISyntaxContextReceiver
1010
{
11-
private const string GlobalPrefix = "global::";
1211
private static readonly string[] Modifiers = ["public", "partial"];
1312
public IDictionary<InterfaceDeclarationSyntax, ProxyData> CandidateInterfaces { get; } = new Dictionary<InterfaceDeclarationSyntax, ProxyData>();
1413

@@ -65,7 +64,7 @@ private static bool TryGet(InterfaceDeclarationSyntax interfaceDeclarationSyntax
6564
var fluentBuilderAttributeArguments = AttributeArgumentListParser.Parse(attributeList.Attributes.FirstOrDefault(), semanticModel);
6665

6766
var metadataName = fluentBuilderAttributeArguments.MetadataName;
68-
var globalNamespace = string.IsNullOrEmpty(ns) ? string.Empty : $"{GlobalPrefix}{ns}";
67+
var globalNamespace = string.IsNullOrEmpty(ns) ? string.Empty : $"{Constants.GlobalPrefix}{ns}";
6968
var namespaceDot = string.IsNullOrEmpty(ns) ? string.Empty : $"{ns}.";
7069

7170
data = new ProxyData(

tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientObjectProxy.g.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,39 @@ public partial class ClientObjectProxy : global::ProxyInterfaceSourceGeneratorTe
4343

4444
public object Tag { get => _Instance.Tag; set => _Instance.Tag = value; }
4545

46-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
46+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
4747
public global::Microsoft.SharePoint.Client.ObjectPath Path { get => _Instance.Path; }
4848

49-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
49+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
5050
public string ObjectVersion { get => _Instance.ObjectVersion; set => _Instance.ObjectVersion = value; }
5151

52-
[Microsoft.SharePoint.Client.PseudoRemoteAttribute]
52+
[global::Microsoft.SharePoint.Client.PseudoRemoteAttribute]
5353
public bool? ServerObjectIsNull { get => _Instance.ServerObjectIsNull; }
5454

5555
public global::ProxyInterfaceSourceGeneratorTests.Source.PnP.IClientObject TypedObject { get => MapToInterface(_Instance.TypedObject); }
5656

57-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
57+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
5858
public virtual void FromJson(global::Microsoft.SharePoint.Client.JsonReader reader)
5959
{
6060
global::Microsoft.SharePoint.Client.JsonReader reader_ = reader;
6161
_Instance.FromJson(reader_);
6262
}
6363

64-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
64+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
6565
public virtual bool CustomFromJson(global::Microsoft.SharePoint.Client.JsonReader reader)
6666
{
6767
global::Microsoft.SharePoint.Client.JsonReader reader_ = reader;
6868
var result__636829107 = _Instance.CustomFromJson(reader_);
6969
return result__636829107;
7070
}
7171

72-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
72+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
7373
public void Retrieve()
7474
{
7575
_Instance.Retrieve();
7676
}
7777

78-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
78+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
7979
public void Retrieve(params string[] propertyNames)
8080
{
8181
string[] propertyNames_ = propertyNames;

tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.ClientRuntimeContextProxy.g.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public partial class ClientRuntimeContextProxy : global::ProxyInterfaceSourceGen
8383

8484
public int RequestTimeout { get => _Instance.RequestTimeout; set => _Instance.RequestTimeout = value; }
8585

86-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
86+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
8787
public global::System.Collections.Generic.Dictionary<string, object> StaticObjects { get => _Instance.StaticObjects; }
8888

8989
public global::System.Version ServerSchemaVersion { get => _Instance.ServerSchemaVersion; }
@@ -125,14 +125,14 @@ public T CastTo<T>(global::ProxyInterfaceSourceGeneratorTests.Source.PnP.IClient
125125
return result_366781530;
126126
}
127127

128-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
128+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
129129
public void AddQuery(global::Microsoft.SharePoint.Client.ClientAction query)
130130
{
131131
global::Microsoft.SharePoint.Client.ClientAction query_ = query;
132132
_Instance.AddQuery(query_);
133133
}
134134

135-
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
135+
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Never)]
136136
public void AddQueryIdAndResultObject(long id, object obj)
137137
{
138138
long id_ = id;

tests/ProxyInterfaceSourceGeneratorTests/Destination/Microsoft.SharePoint.Client.SecurableObjectProxy.g.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ public partial class SecurableObjectProxy : global::ProxyInterfaceSourceGenerato
4949

5050
public new global::Microsoft.SharePoint.Client.SecurableObject _Instance { get; }
5151
public global::Microsoft.SharePoint.Client.ClientObject _InstanceClientObject { get; }
52-
[Microsoft.SharePoint.Client.RemoteAttribute]
52+
[global::Microsoft.SharePoint.Client.RemoteAttribute]
5353
public global::ProxyInterfaceSourceGeneratorTests.Source.PnP.ISecurableObject FirstUniqueAncestorSecurableObject { get => MapToInterface(_Instance.FirstUniqueAncestorSecurableObject); }
5454

55-
[Microsoft.SharePoint.Client.RemoteAttribute]
55+
[global::Microsoft.SharePoint.Client.RemoteAttribute]
5656
public bool HasUniqueRoleAssignments { get => _Instance.HasUniqueRoleAssignments; }
5757

58-
[Microsoft.SharePoint.Client.RemoteAttribute]
58+
[global::Microsoft.SharePoint.Client.RemoteAttribute]
5959
public global::Microsoft.SharePoint.Client.RoleAssignmentCollection RoleAssignments { get => _Instance.RoleAssignments; }
6060

61-
[Microsoft.SharePoint.Client.RemoteAttribute]
61+
[global::Microsoft.SharePoint.Client.RemoteAttribute]
6262
public virtual void ResetRoleInheritance()
6363
{
6464
_Instance.ResetRoleInheritance();
6565
}
6666

67-
[Microsoft.SharePoint.Client.RemoteAttribute]
67+
[global::Microsoft.SharePoint.Client.RemoteAttribute]
6868
public virtual void BreakRoleInheritance(bool copyRoleAssignments, bool clearSubscopes)
6969
{
7070
bool copyRoleAssignments_ = copyRoleAssignments;

0 commit comments

Comments
 (0)