Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Feb 9, 2022
1 parent 9012e10 commit 0649b05
Show file tree
Hide file tree
Showing 29 changed files with 181 additions and 189 deletions.
1 change: 1 addition & 0 deletions AssemblyToProcess/AssemblyToProcess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<AssemblyVersion>1.0</AssemblyVersion>
</PropertyGroup>
<ItemGroup>
<Using Remove="System.Net.Http" />
<ProjectReference Include="..\Obsolete\Obsolete.csproj" />
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion AssemblyToProcess/ClassToMark.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
// ReSharper disable NotAccessedField.Local
// ReSharper disable EventNeverSubscribedTo.Global
#pragma warning disable 67
Expand Down
2 changes: 0 additions & 2 deletions AssemblyToProcess/ClassWithObsoleteAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

[Obsolete]
public class ClassWithObsoleteAttribute
{
Expand Down
2 changes: 0 additions & 2 deletions AssemblyToProcess/ClassWithObsoleteAttributeToSkip.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

[Obsolete]
[DoNotWarnAboutObsoleteUsage]
public class ClassWithObsoleteAttributeToSkip
Expand Down
2 changes: 0 additions & 2 deletions AssemblyToProcess/InterfaceToMark.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

[ObsoleteEx(
TreatAsErrorFromVersion = "2.0",
RemoveInVersion = "4.0",
Expand Down
2 changes: 0 additions & 2 deletions AssemblyToProcess/Sample.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Before
{
[ObsoleteEx(
Expand Down
1 change: 0 additions & 1 deletion AssemblyToProcess/StructToMark.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
#pragma warning disable 67

[ObsoleteEx(
Expand Down
3 changes: 2 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>5.2.1</Version>
<LangVersion>latest</LangVersion>
<LangVersion>10</LangVersion>
<NoWarn>NU5118</NoWarn>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
12 changes: 6 additions & 6 deletions Obsolete.Fody/AssemblyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ public partial class ModuleWeaver
{
public void ProcessAssembly()
{
foreach (var typeDefinition in ModuleDefinition.GetTypes())
foreach (var type in ModuleDefinition.GetTypes())
{
ProcessAttributes(typeDefinition);
foreach (var property in typeDefinition.Properties)
ProcessAttributes(type);
foreach (var property in type.Properties)
{
ProcessAttributes(property);
}
foreach (var method in typeDefinition.Methods)
foreach (var method in type.Methods)
{
ProcessAttributes(method);
}
foreach (var field in typeDefinition.Fields)
foreach (var field in type.Fields)
{
ProcessAttributes(field);
}
foreach (var @event in typeDefinition.Events)
foreach (var @event in type.Events)
{
ProcessAttributes(@event);
}
Expand Down
1 change: 0 additions & 1 deletion Obsolete.Fody/AttributeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using Mono.Cecil;

public static class AttributeExtensions
Expand Down
47 changes: 23 additions & 24 deletions Obsolete.Fody/AttributeFixer.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
using System.Linq;
using Fody;
using Mono.Cecil;
using Mono.Collections.Generic;

public partial class ModuleWeaver
{
void ProcessAttributes(IMemberDefinition memberDefinition)
void ProcessAttributes(IMemberDefinition member)
{
CheckForNormalAttribute(memberDefinition);
CheckForNormalAttribute(member);

InnerProcess(memberDefinition);
InnerProcess(member);
}

void InnerProcess(IMemberDefinition memberDefinition)
void InnerProcess(IMemberDefinition member)
{
var customAttributes = memberDefinition.CustomAttributes;
var customAttributes = member.CustomAttributes;
var obsoleteExAttribute = customAttributes.FirstOrDefault(x => x.AttributeType.Name == "ObsoleteExAttribute");
if (obsoleteExAttribute == null)
{
return;
}
var throwsNotImplemented = false;
if (memberDefinition is MethodDefinition methodDefinition)
if (member is MethodDefinition method)
{
throwsNotImplemented = ThrowsNotImplemented(methodDefinition);
if (methodDefinition.IsGetter || methodDefinition.IsSetter)
throwsNotImplemented = ThrowsNotImplemented(method);
if (method.IsGetter || method.IsSetter)
{
var error = $"ObsoleteExAttribute is not valid on property gets or sets. Member: `{memberDefinition.FullName}`.";
var error = $"ObsoleteExAttribute is not valid on property gets or sets. Member: `{member.FullName}`.";
WriteError(error);
}
}
else if (memberDefinition is PropertyDefinition propertyDefinition)
else if (member is PropertyDefinition property)
{
throwsNotImplemented = ThrowsNotImplemented(propertyDefinition);
throwsNotImplemented = ThrowsNotImplemented(property);
}

customAttributes.Remove(obsoleteExAttribute);
Expand All @@ -46,37 +45,37 @@ void InnerProcess(IMemberDefinition memberDefinition)
}
catch (WeavingException exception)
{
throw new WeavingException($"Could not process {memberDefinition.FullName}. {exception.Message}");
throw new WeavingException($"Could not process {member.FullName}. {exception.Message}");
}

ValidateVersion(memberDefinition, attributeData);
ValidateVersion(member, attributeData);

AddObsoleteAttribute(attributeData, customAttributes);
AddEditorBrowsableAttribute(customAttributes, HideObsoleteMembers);
}

static bool ThrowsNotImplemented(PropertyDefinition propertyDefinition)
static bool ThrowsNotImplemented(PropertyDefinition property)
{
if (propertyDefinition.SetMethod != null)
if (property.SetMethod != null)
{
if (ThrowsNotImplemented(propertyDefinition.SetMethod))
if (ThrowsNotImplemented(property.SetMethod))
{
return true;
}
}
if (propertyDefinition.GetMethod != null)
if (property.GetMethod != null)
{
if (ThrowsNotImplemented(propertyDefinition.GetMethod))
if (ThrowsNotImplemented(property.GetMethod))
{
return true;
}
}
return false;
}

static bool ThrowsNotImplemented(MethodDefinition methodDefinition)
static bool ThrowsNotImplemented(MethodDefinition method)
{
return methodDefinition.HasBody && methodDefinition.Body.Instructions
return method.HasBody && method.Body.Instructions
.Select(instruction => instruction.Operand)
.OfType<MethodReference>()
.Select(methodReference => methodReference.FullName)
Expand Down Expand Up @@ -117,17 +116,17 @@ void AddObsoleteAttribute(AttributeData attributeData, Collection<CustomAttribut
customAttributes.Add(customAttribute);
}

void ValidateVersion(IMemberDefinition memberDefinition, AttributeData attributeData)
void ValidateVersion(IMemberDefinition member, AttributeData attributeData)
{
if (attributeData.RemoveInVersion < attributeData.TreatAsErrorFromVersion)
{
var message = $"Cannot process '{memberDefinition.FullName}'. The version specified in 'RemoveInVersion' {attributeData.RemoveInVersion} is less than the version specified in 'TreatAsErrorFromVersion' {attributeData.TreatAsErrorFromVersion}. The member should be removed or 'RemoveInVersion' increased.";
var message = $"Cannot process '{member.FullName}'. The version specified in 'RemoveInVersion' {attributeData.RemoveInVersion} is less than the version specified in 'TreatAsErrorFromVersion' {attributeData.TreatAsErrorFromVersion}. The member should be removed or 'RemoveInVersion' increased.";
throw new WeavingException(message);
}

if (assemblyVersion >= attributeData.RemoveInVersion)
{
var message = $"Cannot process '{memberDefinition.FullName}'. The assembly version {assemblyVersion} is equal to or greater than version specified in 'RemoveInVersion' {attributeData.RemoveInVersion}. The member should be removed or 'RemoveInVersion' increased.";
var message = $"Cannot process '{member.FullName}'. The assembly version {assemblyVersion} is equal to or greater than version specified in 'RemoveInVersion' {attributeData.RemoveInVersion}. The member should be removed or 'RemoveInVersion' increased.";
throw new WeavingException(message);
}
}
Expand Down
17 changes: 9 additions & 8 deletions Obsolete.Fody/BrowsableAttributeAdder.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using System.Linq;
using Mono.Cecil;
using Mono.Cecil;
using Mono.Collections.Generic;

public partial class ModuleWeaver
{
public void AddEditorBrowsableAttribute(Collection<CustomAttribute> customAttributes, HideObsoleteMembersState state)
public void AddEditorBrowsableAttribute(Collection<CustomAttribute> attributes, HideObsoleteMembersState state)
{
if (customAttributes.Any(x => x.AttributeType.Name == "EditorBrowsableAttribute") || state == HideObsoleteMembersState.Off)
if (attributes.Any(x => x.AttributeType.Name == "EditorBrowsableAttribute") || state == HideObsoleteMembersState.Off)
{
return;
}
var customAttribute = new CustomAttribute(EditorBrowsableConstructor);
var customAttributeArgument = new CustomAttributeArgument(EditorBrowsableStateType, state == HideObsoleteMembersState.Advanced ? AdvancedStateConstant : NeverStateConstant);
customAttribute.ConstructorArguments.Add(customAttributeArgument);
customAttributes.Add(customAttribute);

var attribute = new CustomAttribute(EditorBrowsableConstructor);
var memberState = state == HideObsoleteMembersState.Advanced ? AdvancedStateConstant : NeverStateConstant;
var attributeArgument = new CustomAttributeArgument(EditorBrowsableStateType, memberState);
attribute.ConstructorArguments.Add(attributeArgument);
attributes.Add(attribute);
}
}
8 changes: 6 additions & 2 deletions Obsolete.Fody/ConfigReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Linq;
using Fody;

public partial class ModuleWeaver
Expand Down Expand Up @@ -27,6 +25,7 @@ void ReadFormats()
{
TreatAsErrorFormat = treatAsErrorFormat.Value;
}

var throwsNotImplementedText = Config.Attributes("ThrowsNotImplementedText").FirstOrDefault();
if (throwsNotImplementedText != null)
{
Expand All @@ -38,6 +37,7 @@ void ReadFormats()
{
RemoveInVersionFormat = removeInVersionFormat.Value;
}

var replacementFormat = Config.Attributes("ReplacementFormat").FirstOrDefault();
if (replacementFormat != null)
{
Expand All @@ -58,6 +58,7 @@ void ReadHideObsoleteMembers()
HideObsoleteMembers = state;
return;
}

throw new Exception($"Could not parse 'HideObsoleteMembers' from '{xAttribute.Value}'.");
}

Expand All @@ -68,6 +69,7 @@ void ReadVersionIncrement()
{
return;
}

throw new WeavingException("VersionIncrement is no longer supported. Use StepType instead.");
}

Expand All @@ -78,10 +80,12 @@ void ReadStepType()
{
return;
}

if (Enum.TryParse(xAttribute.Value, out StepType))
{
return;
}

throw new Exception($"Could not parse 'StepType' from '{xAttribute.Value}'.");
}
}
16 changes: 9 additions & 7 deletions Obsolete.Fody/DataFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,32 @@ public partial class ModuleWeaver
{
public string ConvertToMessage(AttributeData attributeData)
{
var stringBuilder = new StringBuilder();
var builder = new StringBuilder();
var message = attributeData.Message;
if (message != null)
{
message = message.Trim();
message = message.Trim('.');
stringBuilder.AppendFormat("{0}. ", message);
builder.AppendFormat("{0}. ", message);
}

if (attributeData.Replacement != null)
{
stringBuilder.AppendFormat(ReplacementFormat, attributeData.Replacement);
builder.AppendFormat(ReplacementFormat, attributeData.Replacement);
}

if (assemblyVersion < attributeData.TreatAsErrorFromVersion)
{
stringBuilder.AppendFormat(TreatAsErrorFormat, attributeData.TreatAsErrorFromVersion.ToSemVer());
builder.AppendFormat(TreatAsErrorFormat, attributeData.TreatAsErrorFromVersion.ToSemVer());
}

if (attributeData.ThrowsNotImplemented)
{
stringBuilder.Append(ThrowsNotImplementedText);
builder.Append(ThrowsNotImplementedText);
}
stringBuilder.AppendFormat(RemoveInVersionFormat, attributeData.RemoveInVersion.ToSemVer());

return stringBuilder.ToString().Trim();
builder.AppendFormat(RemoveInVersionFormat, attributeData.RemoveInVersion.ToSemVer());

return builder.ToString().Trim();
}
}
10 changes: 7 additions & 3 deletions Obsolete.Fody/EditorBrowsableAttributeFinder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Linq;
using Mono.Cecil;

public partial class ModuleWeaver
Expand All @@ -14,13 +13,14 @@ void FindEditorBrowsableTypes()
{
return;
}

var attributeType = FindTypeDefinition("System.ComponentModel.EditorBrowsableAttribute");
EditorBrowsableConstructor = ModuleDefinition.ImportReference(attributeType.Methods.First(IsDesiredConstructor));
EditorBrowsableStateType = FindTypeDefinition("System.ComponentModel.EditorBrowsableState");
var advancedFieldDefinition = EditorBrowsableStateType.Fields.First(x => x.Name == "Advanced");
AdvancedStateConstant = (int) advancedFieldDefinition.Constant;
AdvancedStateConstant = (int)advancedFieldDefinition.Constant;
var neverFieldDefinition = EditorBrowsableStateType.Fields.First(x => x.Name == "Never");
NeverStateConstant = (int) neverFieldDefinition.Constant;
NeverStateConstant = (int)neverFieldDefinition.Constant;
}

static bool IsDesiredConstructor(MethodDefinition x)
Expand All @@ -29,20 +29,24 @@ static bool IsDesiredConstructor(MethodDefinition x)
{
return false;
}

if (x.Parameters.Count != 1)
{
return false;
}

return x.Parameters[0].ParameterType.Name == "EditorBrowsableState";
}

public enum HideObsoleteMembersState
{
Advanced,

// some dirty trickery to be backward compatible
True = Advanced,
Never,
Off,

// some dirty trickery to be backward compatible
False = Off,
}
Expand Down
3 changes: 1 addition & 2 deletions Obsolete.Fody/ModuleWeaver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using Fody;
using Fody;

public partial class ModuleWeaver :
BaseModuleWeaver
Expand Down
Loading

0 comments on commit 0649b05

Please sign in to comment.