Skip to content

Commit

Permalink
Merge pull request #541 from distantcam/SuppressOnPropertyNameChanged…
Browse files Browse the repository at this point in the history
…Config

Suppress on property name changed config
  • Loading branch information
ltrzesniewski committed Mar 17, 2020
2 parents c5140b6 + 4b6e332 commit 04af646
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Linq;
using System.Xml;

public partial class ModuleWeaver
{
public bool SuppressOnPropertyNameChangedWarning;

public void ResolveSuppressOnPropertyNameChangedWarningConfig()
{
var value = Config?.Attributes("SuppressOnPropertyNameChangedWarning")
.Select(a => a.Value)
.SingleOrDefault();
if (value != null)
{
SuppressOnPropertyNameChangedWarning = XmlConvert.ToBoolean(value.ToLowerInvariant());
}
}
}
1 change: 1 addition & 0 deletions PropertyChanged.Fody/ModuleWeaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public override void Execute()
ResolveCheckForEqualityUsingBaseEqualsConfig();
ResolveUseStaticEqualsFromBaseConfig();
ResolveSuppressWarningsConfig();
ResolveSuppressOnPropertyNameChangedWarningConfig();
ResolveEventInvokerName();
FindCoreReferences();
FindInterceptor();
Expand Down
22 changes: 16 additions & 6 deletions PropertyChanged.Fody/OnChangedWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ OnChangedMethod FindOnChangedMethod(TypeNode notifyNode, string methodName)

if (foundMethod != null)
{
throw new WeavingException($"The type {notifyNode.TypeDefinition.FullName} has multiple valid overloads of a On_PropertyName_Changed method named {methodName}).");}
throw new WeavingException($"The type {notifyNode.TypeDefinition.FullName} has multiple valid overloads of a On_PropertyName_Changed method named {methodName}).");
}

foundMethod = method;
}
Expand All @@ -115,13 +116,19 @@ OnChangedMethod CreateOnChangedMethod(TypeNode notifyNode, MethodDefinition meth
{
if (methodDefinition.IsStatic)
{
EmitConditionalWarning(methodDefinition, $"The type {notifyNode.TypeDefinition.FullName} has a On_PropertyName_Changed method ({methodDefinition.Name}) which is static.");
if (!SuppressOnPropertyNameChangedWarning)
{
EmitConditionalWarning(methodDefinition, $"The type {notifyNode.TypeDefinition.FullName} has a On_PropertyName_Changed method ({methodDefinition.Name}) which is static.");
}
return null;
}

if (methodDefinition.ReturnType.FullName != "System.Void")
{
EmitConditionalWarning(methodDefinition, $"The type {notifyNode.TypeDefinition.FullName} has a On_PropertyName_Changed method ({methodDefinition.Name}) that has a non void return value. Ensure the return type void.");
if (!SuppressOnPropertyNameChangedWarning)
{
EmitConditionalWarning(methodDefinition, $"The type {notifyNode.TypeDefinition.FullName} has a On_PropertyName_Changed method ({methodDefinition.Name}) that has a non void return value. Ensure the return type void.");
}
return null;
}

Expand Down Expand Up @@ -151,7 +158,7 @@ OnChangedMethod CreateOnChangedMethod(TypeNode notifyNode, MethodDefinition meth
};
}

if (!EventInvokerNames.Contains(methodDefinition.Name))
if (!EventInvokerNames.Contains(methodDefinition.Name) && !SuppressOnPropertyNameChangedWarning)
{
EmitConditionalWarning(methodDefinition, $"Unsupported signature for a On_PropertyName_Changed method: {methodDefinition.Name} in {methodDefinition.DeclaringType.FullName}");
}
Expand Down Expand Up @@ -190,6 +197,9 @@ void ValidateOnChangedMethod(TypeNode notifyNode, MethodDefinition method, bool
{
return;
}
EmitConditionalWarning(method, $"Type {method.DeclaringType.FullName} does not contain a {propertyName} property with an injected change notification, and therefore the {method.Name} method will not be called.");
if (!SuppressOnPropertyNameChangedWarning)
{
EmitConditionalWarning(method, $"Type {method.DeclaringType.FullName} does not contain a {propertyName} property with an injected change notification, and therefore the {method.Name} method will not be called.");
}
}
}
}
10 changes: 10 additions & 0 deletions PropertyChanged.Fody/PropertyChanged.Fody.xcf
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@
<xs:documentation>Used to control if equality checks should use the static Equals method resolved from the base class.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="SuppressWarnings" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to turn off build warnings from this weaver.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="SuppressOnPropertyNameChangedWarning" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to turn off build warnings about mismatched On_PropertyName_Changed methods.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
2 changes: 1 addition & 1 deletion PropertyChanged.Fody/WarningChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ public void EmitConditionalWarning(ICustomAttributeProvider member, string messa

WriteWarning($"{message} You can suppress this warning with [SuppressPropertyChangedWarnings].", sequencePoint);
}
}
}
10 changes: 10 additions & 0 deletions SmokeTest/FodyWeavers.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
<xs:documentation>Used to control if equality checks should use the static Equals method resolved from the base class.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="SuppressWarnings" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to turn off build warnings from this weaver.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="SuppressOnPropertyNameChangedWarning" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to turn off build warnings about mismatched On_PropertyName_Changed methods.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:all>
Expand Down
57 changes: 57 additions & 0 deletions Tests/SuppressOnPropertyNameChangedWarningConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Xml.Linq;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;

public class SuppressOnPropertyNameChangedWarningConfigTests :
VerifyBase
{
[Fact]
public void False()
{
var xElement = XElement.Parse("<PropertyChanged SuppressOnPropertyNameChangedWarning='false'/>");
var moduleWeaver = new ModuleWeaver { Config = xElement };
moduleWeaver.ResolveSuppressOnPropertyNameChangedWarningConfig();
Assert.False(moduleWeaver.SuppressOnPropertyNameChangedWarning);
}

[Fact]
public void False0()
{
var xElement = XElement.Parse("<PropertyChanged SuppressOnPropertyNameChangedWarning='0'/>");
var moduleWeaver = new ModuleWeaver { Config = xElement };
moduleWeaver.ResolveSuppressOnPropertyNameChangedWarningConfig();
Assert.False(moduleWeaver.SuppressOnPropertyNameChangedWarning);
}

[Fact]
public void True()
{
var xElement = XElement.Parse("<PropertyChanged SuppressOnPropertyNameChangedWarning='True'/>");
var moduleWeaver = new ModuleWeaver { Config = xElement };
moduleWeaver.ResolveSuppressOnPropertyNameChangedWarningConfig();
Assert.True(moduleWeaver.SuppressOnPropertyNameChangedWarning);
}

[Fact]
public void True1()
{
var xElement = XElement.Parse("<PropertyChanged SuppressOnPropertyNameChangedWarning='1'/>");
var moduleWeaver = new ModuleWeaver { Config = xElement };
moduleWeaver.ResolveSuppressOnPropertyNameChangedWarningConfig();
Assert.True(moduleWeaver.SuppressOnPropertyNameChangedWarning);
}

[Fact]
public void Default()
{
var moduleWeaver = new ModuleWeaver();
moduleWeaver.ResolveSuppressOnPropertyNameChangedWarningConfig();
Assert.False(moduleWeaver.SuppressOnPropertyNameChangedWarning);
}

public SuppressOnPropertyNameChangedWarningConfigTests(ITestOutputHelper output) :
base(output)
{
}
}

0 comments on commit 04af646

Please sign in to comment.