Skip to content
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

Extension bug #156

Merged
merged 2 commits into from Nov 22, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/PublicApiGenerator/CodeNormalizer.cs
Expand Up @@ -60,8 +60,8 @@ public static string NormalizeGeneratedCode(StringWriter writer)
RegexOptions.IgnorePatternWhitespace); // SingleLine is required for multi line params arrays

gennedClass = Regex.Replace(gennedClass, @"(public|protected) (.*) _(.*)_292C96C3C42E4C07BEED73F5DAA0A6DF_(.*)", EventModifierMatcher);
gennedClass = Regex.Replace(gennedClass, @"(public|protected) (abstract|static|new static|virtual|override|new|unsafe) (.*) _(.*)_5DB9F56043FF464997155541DA321AD4_(.*)", PropertyModifierMatcher);
gennedClass = Regex.Replace(gennedClass, @"(public|protected) (abstract|static|new static|virtual|override|new|unsafe) (.*) _(.*)_3C0D97CD952D40AA8B6E1ECB98FFC79F_(.*)", MethodModifierMatcher);
gennedClass = Regex.Replace(gennedClass, @"(public|protected)( abstract | static | new static | virtual | override | new | unsafe | )(.*) _(.*)_5DB9F56043FF464997155541DA321AD4_(.*)", PropertyModifierMatcher);
gennedClass = Regex.Replace(gennedClass, @"(public|protected)( abstract | static | new static | virtual | override | new | unsafe | )(.*) _(.*)_3C0D97CD952D40AA8B6E1ECB98FFC79F_(.*)", MethodModifierMatcher);
gennedClass = gennedClass.Replace("class " + StaticMarker, "static class ");
gennedClass = gennedClass.Replace("struct " + ReadonlyMarker, "readonly struct ");
gennedClass = gennedClass.Replace(ReadonlyMarker, string.Empty); // remove magic marker from readonly struct ctor
Expand Down Expand Up @@ -97,17 +97,17 @@ static string PropertyModifierMatcher(Match match)
var oldModifier = match.Groups[2].Value;
var modifier = match.Groups[4].Value;

return match.ToString().Replace(string.Format(PropertyModifierMarkerTemplate, modifier), string.Empty)
.Replace(oldModifier, modifier);
var s = match.ToString().Replace(string.Format(PropertyModifierMarkerTemplate, modifier), string.Empty);
return string.IsNullOrWhiteSpace(oldModifier) ? s.Insert(s.IndexOf(oldModifier, StringComparison.Ordinal), modifier) : s.Replace(oldModifier, modifier);
}

static string MethodModifierMatcher(Match match)
{
var oldModifier = match.Groups[2].Value;
var modifier = match.Groups[4].Value;

return match.ToString().Replace(string.Format(MethodModifierMarkerTemplate, modifier), string.Empty)
.Replace(oldModifier, modifier);
var s = match.ToString().Replace(string.Format(MethodModifierMarkerTemplate, modifier), string.Empty);
return string.IsNullOrWhiteSpace(oldModifier) ? s.Insert(s.IndexOf(oldModifier, StringComparison.Ordinal), modifier) : s.Replace(oldModifier, modifier);
}

static string RemoveUnnecessaryWhiteSpace(string publicApi)
Expand Down
20 changes: 10 additions & 10 deletions src/PublicApiGenerator/ModifierMarkerNameBuilder.cs
Expand Up @@ -11,17 +11,17 @@ public static string Build(MethodDefinition methodDefinition, MemberAttributes g
return (getAccessorAttributes & MemberAttributes.ScopeMask, isNew, methodDefinition.IsVirtual,
methodDefinition.IsAbstract) switch
{
(MemberAttributes.Static, null, _, _) => Format(markerTemplate, "static") + name,
(MemberAttributes.Static, true, _, _) => Format(markerTemplate, "new static") + name,
(MemberAttributes.Override, _, _, _) => Format(markerTemplate, "override") + name,
(MemberAttributes.Final | MemberAttributes.Override, _, _, _) => Format(markerTemplate,"override sealed") + name,
(MemberAttributes.Final, true, _, _) => Format(markerTemplate, "new") + name,
(MemberAttributes.Abstract, null, _, _) => Format(markerTemplate, "abstract") + name,
(MemberAttributes.Abstract, true, _, _) => Format(markerTemplate, "new abstract") + name,
(MemberAttributes.Const, _, _, _) => Format(markerTemplate, "abstract override") + name,
(MemberAttributes.Static, null, _, _) => Format(markerTemplate, " static ") + name,
(MemberAttributes.Static, true, _, _) => Format(markerTemplate, " new static ") + name,
(MemberAttributes.Override, _, _, _) => Format(markerTemplate, " override ") + name,
(MemberAttributes.Final | MemberAttributes.Override, _, _, _) => Format(markerTemplate," override sealed ") + name,
(MemberAttributes.Final, true, _, _) => Format(markerTemplate, " new ") + name,
(MemberAttributes.Abstract, null, _, _) => Format(markerTemplate, " abstract ") + name,
(MemberAttributes.Abstract, true, _, _) => Format(markerTemplate, " new abstract ") + name,
(MemberAttributes.Const, _, _, _) => Format(markerTemplate, " abstract override ") + name,
(MemberAttributes.Final, null, true, false) => name,
(_, null, true, false) => Format(markerTemplate, "virtual") + name,
(_, true, true, false) => Format(markerTemplate, "new virtual") + name,
(_, null, true, false) => Format(markerTemplate, " virtual ") + name,
(_, true, true, false) => Format(markerTemplate, " new virtual ") + name,
_ => name
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/PublicApiGenerator/PropertyNameBuilder.cs
Expand Up @@ -16,7 +16,7 @@ public static class PropertyNameBuilder
}

var isNew = propertyDefinition.IsNew(typeDef => typeDef?.Properties, e =>
e.Name.Equals(propertyDefinition.Name, StringComparison.Ordinal) && e.PropertyType.Equals(propertyDefinition.PropertyType));
e.Name.Equals(propertyDefinition.Name, StringComparison.Ordinal));

return ModifierMarkerNameBuilder.Build(propertyDefinition.GetMethod, getAccessorAttributes, isNew, name,
CodeNormalizer.PropertyModifierMarkerTemplate);
Expand Down
30 changes: 30 additions & 0 deletions src/PublicApiGeneratorTests/Method_modifiers.cs
Expand Up @@ -257,6 +257,20 @@ public class ClassWithMethodHiding : PublicApiGeneratorTests.Examples.ClassWithS
}");
}

[Fact]
public void Should_output_new_modifier_for_generics()
{
AssertPublicApi(typeof(ClassWithMethodExtensions<>),
@"namespace PublicApiGeneratorTests.Examples
{
public class ClassWithMethodExtensions<T> : PublicApiGeneratorTests.Examples.ClassWithMethodExtensions
{
public ClassWithMethodExtensions() { }
public new PublicApiGeneratorTests.Examples.ClassWithMethodExtensions<T> Extend(string parameter) { }
}
}");
}

[Fact]
public void Should_output_protected_new_modifier()
{
Expand Down Expand Up @@ -517,6 +531,22 @@ protected AbstractClassRedeclaringAbstractMethod()
}
public new abstract void DoSomething();
}

public class ClassWithMethodExtensions
{
public ClassWithMethodExtensions Extend(string parameter)
{
return this;
}
}

public class ClassWithMethodExtensions<T> : ClassWithMethodExtensions
{
public new ClassWithMethodExtensions<T> Extend(string parameter)
{
return this;
}
}
}
// ReSharper restore ClassWithVirtualMembersNeverInherited.Global
// ReSharper restore RedundantOverridenMember
Expand Down
26 changes: 25 additions & 1 deletion src/PublicApiGeneratorTests/Property_modifiers.cs
@@ -1,4 +1,4 @@
using PublicApiGeneratorTests.Examples;
using PublicApiGeneratorTests.Examples;
using Xunit;

namespace PublicApiGeneratorTests
Expand Down Expand Up @@ -215,6 +215,20 @@ public class ClassWithPropertyHiding : PublicApiGeneratorTests.Examples.ClassWit
}");
}

[Fact]
public void Should_output_new_modifier_for_generics()
{
AssertPublicApi(typeof(ClassWithPropertyExtensions<>),
@"namespace PublicApiGeneratorTests.Examples
{
public class ClassWithPropertyExtensions<T> : PublicApiGeneratorTests.Examples.ClassWithPropertyExtensions
{
public ClassWithPropertyExtensions() { }
public new PublicApiGeneratorTests.Examples.ClassWithPropertyExtensions<T> Extension { get; set; }
}
}");
}

[Fact]
public void Should_output_protected_new_modifier()
{
Expand Down Expand Up @@ -462,6 +476,16 @@ protected AbstractClassRedeclaringAbstractProperty()
}
public new abstract string Value { get; set; }
}

public class ClassWithPropertyExtensions
{
public ClassWithMethodExtensions Extension { get; set; }
}

public class ClassWithPropertyExtensions<T> : ClassWithPropertyExtensions
{
public new ClassWithPropertyExtensions<T> Extension { get; set; }
}
}
// ReSharper restore ValueParameterNotUsed
// ReSharper restore ClassNeverInstantiated.Global
Expand Down