Skip to content

Commit

Permalink
Add global prefix to avoid name collisions with generated code (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
glennawatson authored Jun 10, 2019
1 parent f4e57a0 commit 74c3419
Show file tree
Hide file tree
Showing 14 changed files with 8,297 additions and 8,298 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static (ArrowExpressionClauseSyntax, TypeSyntax) GenerateFromEventExpres
SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName("System.Reactive.Linq.Observable"),
SyntaxFactory.IdentifierName("global::System.Reactive.Linq.Observable"),
SyntaxFactory.GenericName(SyntaxFactory.Identifier("FromEvent"))
.WithTypeArgumentList(fromEventTypeParameters)))
.WithArgumentList(
Expand All @@ -147,7 +147,7 @@ private static ArrowExpressionClauseSyntax GenerateUnitFromEventExpression(IEven
SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName("System.Reactive.Linq.Observable"),
SyntaxFactory.IdentifierName("global::System.Reactive.Linq.Observable"),
SyntaxFactory.IdentifierName("FromEvent")))
.WithArgumentList(
SyntaxFactory.ArgumentList(
Expand Down Expand Up @@ -178,7 +178,7 @@ private static (ArrowExpressionClauseSyntax, TypeSyntax) GenerateFromEventPatter
SyntaxFactory.InvocationExpression(
SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName("System.Reactive.Linq.Observable"),
SyntaxFactory.IdentifierName("global::System.Reactive.Linq.Observable"),
SyntaxFactory.GenericName(SyntaxFactory.Identifier("FromEventPattern"))
.WithTypeArgumentList(
SyntaxFactory.TypeArgumentList(
Expand Down
44 changes: 25 additions & 19 deletions src/Pharmacist.Core/Generation/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,6 @@ public static IImmutableList<ITypeDefinition> GetPublicNonGenericTypeDefinitions
.ToImmutableList());
}

/// <summary>
/// Gets a string form of the type and generic arguments for a type.
/// </summary>
/// <param name="currentType">The type to generate the arguments for.</param>
/// <returns>A type descriptor including the generic arguments.</returns>
public static string GenerateFullGenericName(this IType currentType)
{
var sb = new StringBuilder(GetBuiltInType(currentType.FullName));

if (currentType.TypeParameterCount > 0)
{
sb.Append("<")
.Append(string.Join(", ", currentType.TypeArguments.Select(GenerateFullGenericName)))
.Append(">");
}

return sb.ToString();
}

public static IType GetEventType(this IEvent eventDetails)
{
ICompilation compilation = eventDetails.Compilation;
Expand Down Expand Up @@ -170,6 +151,31 @@ public static IType GetRealType(this IType type, ICompilation compilation)
return type;
}

/// <summary>
/// Gets a string form of the type and generic arguments for a type.
/// </summary>
/// <param name="currentType">The type to generate the arguments for.</param>
/// <returns>A type descriptor including the generic arguments.</returns>
public static string GenerateFullGenericName(this IType currentType)
{
return GenerateFullGenericName(currentType, true);
}

private static string GenerateFullGenericName(this IType currentType, bool isStart)
{
var currentTypeFullName = GetBuiltInType(currentType.FullName);
var sb = new StringBuilder(isStart ? "global::" + currentTypeFullName : currentTypeFullName);

if (currentType.TypeParameterCount > 0)
{
sb.Append("<")
.Append(string.Join(", ", currentType.TypeArguments.Select(x => GenerateFullGenericName(x, false))))
.Append(">");
}

return sb.ToString();
}

private static IImmutableList<ITypeDefinition> GetPublicTypeDefinitionsWithEvents(ICompilation compilation)
{
return _publicEventsTypeMapping.GetOrAdd(
Expand Down
6 changes: 3 additions & 3 deletions src/Pharmacist.Core/Generation/RoslynGeneratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ public static TypeArgumentListSyntax GenerateObservableTypeArguments(this IMetho

public static TypeSyntax GenerateObservableType(this TypeArgumentListSyntax argumentList)
{
return SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("System"), SyntaxFactory.GenericName(SyntaxFactory.Identifier("IObservable")).WithTypeArgumentList(argumentList));
return SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("global::System"), SyntaxFactory.GenericName(SyntaxFactory.Identifier("IObservable")).WithTypeArgumentList(argumentList));
}

public static TypeSyntax GenerateObservableType(this TypeSyntax argumentList)
{
return SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("System"), SyntaxFactory.GenericName(SyntaxFactory.Identifier("IObservable")).WithTypeArgumentList(SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList(argumentList))));
return SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("global::System"), SyntaxFactory.GenericName(SyntaxFactory.Identifier("IObservable")).WithTypeArgumentList(SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList(argumentList))));
}

public static PropertyDeclarationSyntax WithObsoleteAttribute(this PropertyDeclarationSyntax syntax, IEntity eventDetails)
Expand Down Expand Up @@ -151,7 +151,7 @@ private static AttributeListSyntax GenerateObsoleteAttributeList(IEntity eventDe
var message = obsoleteAttribute.FixedArguments.FirstOrDefault().Value.ToString() ?? string.Empty;
var isError = bool.Parse(obsoleteAttribute.FixedArguments.ElementAtOrDefault(1).Value?.ToString() ?? bool.FalseString) ? SyntaxKind.TrueLiteralExpression : SyntaxKind.FalseLiteralExpression;
var attribute = SyntaxFactory.Attribute(
SyntaxFactory.IdentifierName("System.ObsoleteAttribute"),
SyntaxFactory.IdentifierName("global::System.ObsoleteAttribute"),
SyntaxFactory.AttributeArgumentList(SyntaxFactory.SeparatedList(new[] { SyntaxFactory.AttributeArgument(SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(message))), SyntaxFactory.AttributeArgument(SyntaxFactory.LiteralExpression(isError)) })));

return SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(attribute));
Expand Down
2 changes: 1 addition & 1 deletion src/Pharmacist.Core/Generation/RoslynHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Pharmacist.Core.Generation
/// </summary>
internal static class RoslynHelpers
{
internal const string ObservableUnitName = "System.Reactive.Unit";
internal const string ObservableUnitName = "global::System.Reactive.Unit";
internal const string VoidType = "System.Void";

/// <summary>
Expand Down
7 changes: 0 additions & 7 deletions src/Pharmacist.Core/Templates/HeaderTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,3 @@
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;

using Pharmacist.Common;
2 changes: 1 addition & 1 deletion src/Pharmacist.Tests/LibraryRangeIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static async Task CheckResultsAgainstTemplate(LibraryRange[] package, IR
using (var sr = new StreamReader(memoryStream))
{
var actualContents = sr.ReadToEnd();
var expectedContents = File.ReadAllText($"TestResults/{package[0].Name}.{bestPackageIdentity.Version}.txt");
var expectedContents = File.ReadAllText($"TestExpectedResults/{package[0].Name}.{bestPackageIdentity.Version}.txt");

string normalizedActual = _whitespaceRegex.Replace(actualContents, string.Empty);
string normalizedExpected = _whitespaceRegex.Replace(expectedContents, string.Empty);
Expand Down
2 changes: 1 addition & 1 deletion src/Pharmacist.Tests/PackageIdentityIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static async Task CheckResultsAgainstTemplate(PackageIdentity[] package,
using (var sr = new StreamReader(memoryStream))
{
var actualContents = sr.ReadToEnd();
var expectedContents = File.ReadAllText($"TestResults/{package[0].Id}.{package[0].Version}.txt");
var expectedContents = File.ReadAllText($"TestExpectedResults/{package[0].Id}.{package[0].Version}.txt");

string normalizedActual = _whitespaceRegex.Replace(actualContents, string.Empty);
string normalizedExpected = _whitespaceRegex.Replace(expectedContents, string.Empty);
Expand Down
6 changes: 3 additions & 3 deletions src/Pharmacist.Tests/Pharmacist.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
</ItemGroup>

<ItemGroup>
<None Update="TestResults\Tizen.Net.API4.4.0.1.14152.txt">
<None Update="TestExpectedResults\Tizen.Net.API4.4.0.1.14152.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestResults\Xamarin.Essentials.1.1.0.txt">
<None Update="TestExpectedResults\Xamarin.Essentials.1.1.0.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="TestResults\Xamarin.Forms.4.0.0.482894.txt">
<None Update="TestExpectedResults\Xamarin.Forms.4.0.0.482894.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="xunit.runner.json">
Expand Down
Loading

0 comments on commit 74c3419

Please sign in to comment.