Skip to content

Commit 846d8aa

Browse files
committed
Look for CancellationToken properties in base types too
Parse xs:boolean values for 0 and 1 Include readme in NuGet package
1 parent b873ac0 commit 846d8aa

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<AssemblyOriginatorKeyFile>../Analyzers.snk</AssemblyOriginatorKeyFile>
2222

2323
<!-- NOTE: Change the version in Vsix\source.extension.vsixmanifest to match this! -->
24-
<Version>3.3.0</Version>
24+
<Version>3.3.1</Version>
2525

2626
<RoslynVersion>3.11.0</RoslynVersion>
2727
</PropertyGroup>

src/Menees.Analyzers.CodeFixes/Menees.Analyzers.nuspec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<license type="expression">MIT</license>
1818
<projectUrl>https://github.com/menees/Analyzers</projectUrl>
1919
<requireLicenseAcceptance>false</requireLicenseAcceptance>
20+
<readme>docs\README.md</readme>
2021
<description>Provides analyzers for validating that:
2122
* Tabs are used for indentation.
2223
* Lengths of lines, methods, properties, and files are acceptable.
@@ -31,7 +32,6 @@
3132
* TryGetValue is used instead of ContainsKey and this[key].
3233
* Preferred terms are used in identifiers.
3334
</description>
34-
<!-- <releaseNotes></releaseNotes> -->
3535
<copyright>$copyright$</copyright>
3636
<tags>Analyzers, DotNetAnalyzers, Roslyn</tags>
3737
<frameworkAssemblies>
@@ -48,5 +48,8 @@
4848
<!-- Scripts -->
4949
<file src="tools\install.ps1" target="tools\" />
5050
<file src="tools\uninstall.ps1" target="tools\" />
51+
52+
<!-- Docs -->
53+
<file src="..\..\README.md" target="docs\" />
5154
</files>
5255
</package>

src/Menees.Analyzers/Men019SupportAsyncCancellationToken.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,9 @@ private bool ParameterHasCancellationTokenProperty(IParameterSymbol parameterSym
293293
&& typeHasCancellationTokenProperty.GetOrAdd(parameterSymbol.Type, type =>
294294
{
295295
bool hasCancellationTokenProperty = false;
296+
ITypeSymbol[] typeHierarchy = [.. GetTypeAndBaseTypes(type)];
296297
IEnumerable<IPropertySymbol> publicCancellationProperties = propertyNamesToCheck
297-
.SelectMany(propertyName => type.GetMembers(propertyName))
298+
.SelectMany(propertyName => typeHierarchy.SelectMany(t => t.GetMembers(propertyName)))
298299
.Where(m => m.Kind == SymbolKind.Property && m.DeclaredAccessibility == Accessibility.Public)
299300
.Cast<IPropertySymbol>();
300301
foreach (IPropertySymbol propertySymbol in publicCancellationProperties)

src/Menees.Analyzers/Settings.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private Settings(XElement xml)
140140
XElement? supportAsyncCancellationToken = xml.Element("SupportAsyncCancellationToken");
141141
if (supportAsyncCancellationToken != null)
142142
{
143-
if (bool.TryParse(supportAsyncCancellationToken.Attribute("CheckPrivateMethods")?.Value, out bool value))
143+
if (TryParseXsBoolean(supportAsyncCancellationToken.Attribute("CheckPrivateMethods")?.Value, out bool value))
144144
{
145145
this.CheckPrivateMethodsForCancellation = value;
146146
}
@@ -373,7 +373,7 @@ private static int GetSetting(XElement xml, string elementName, int defaultValue
373373
=> GetSetting(xml, elementName, defaultValue, (string text, out int value) => int.TryParse(text, out value) && value > 0);
374374

375375
private static bool GetSetting(XElement xml, string elementName, bool defaultValue)
376-
=> GetSetting(xml, elementName, defaultValue, bool.TryParse);
376+
=> GetSetting(xml, elementName, defaultValue, TryParseXsBoolean);
377377

378378
private static T GetSetting<T>(XElement xml, string elementName, T defaultValue, TryParse<T> tryParse)
379379
{
@@ -391,6 +391,25 @@ private static T GetSetting<T>(XElement xml, string elementName, T defaultValue,
391391
return result;
392392
}
393393

394+
/// <summary>
395+
/// Tries to parse an xs:boolean value, which allows true, false, 1, or 0.
396+
/// </summary>
397+
private static bool TryParseXsBoolean(string? text, out bool value)
398+
{
399+
bool result = bool.TryParse(text, out value);
400+
if (!result)
401+
{
402+
result = int.TryParse(text, out int number);
403+
if (result)
404+
{
405+
value = number != 0;
406+
}
407+
}
408+
409+
return result;
410+
}
411+
412+
394413
private static Predicate<string> CreateFileRegexPredicate(string fileRegex)
395414
=> value => Regex.IsMatch(value, fileRegex, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
396415

tests/Menees.Analyzers.Test/Men019UnitTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ public interface IThink1 { Task Think1(); }
9595
public interface IThink2 { Task Think2(); }
9696
#pragma warning restore MEN019
9797
98-
public sealed class Cancellable { public CancellationToken Cancel {get;}}
98+
public class Cancellable { public CancellationToken Cancel {get;}}
99+
public sealed class DerivedCancellable : Cancellable { public bool IsDerived {get;} = true; }
100+
99101
public class TestBase : IThink1
100102
{
101103
public virtual Task Think1() => Task.CompletedTask;
@@ -129,6 +131,7 @@ public class Test : TestBase, IFormattable, IThink2
129131
public Task UnitTest() => Task.CompletedTask;
130132
131133
public Task GetsCancelProperty(Cancellable cancellable) => Task.CompletedTask;
134+
public Task GetsInheritedCancelProperty(DerivedCancellable cancellable) => Task.CompletedTask;
132135
133136
// Normal methods
134137
public override string ToString() => nameof(Test);

0 commit comments

Comments
 (0)