Skip to content

Commit

Permalink
Fix #477: Port CA1711, IdentifiersShouldNotHaveIncorrectSuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Golding committed Apr 14, 2016
1 parent 3dd5126 commit 9a22626
Show file tree
Hide file tree
Showing 11 changed files with 1,886 additions and 44 deletions.
9 changes: 9 additions & 0 deletions src/Analyzer.Utilities/AnalyzerUtilitiesResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Analyzer.Utilities/AnalyzerUtilitiesResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,7 @@
<data name="CategorySecurity" xml:space="preserve">
<value>Security</value>
</data>
<data name="ErrorStringDoesNotEndWithSuffix" xml:space="preserve">
<value>The string "{0}" does not end with the suffix "{1}".</value>
</data>
</root>
48 changes: 48 additions & 0 deletions src/Analyzer.Utilities/Extensions/INamedTypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
Expand All @@ -18,6 +19,53 @@ public static IEnumerable<INamedTypeSymbol> GetBaseTypesAndThis(this INamedTypeS
}
}

/// <summary>
/// Returns a value indicating whether <paramref name="type"/> derives from, or implements
/// any generic construction of, the type defined by <paramref name="parentType"/>.
/// </summary>
/// <remarks>
/// This method only works when <paramref name="parentType"/> is a definition,
/// not a constructed type.
/// </remarks>
/// <example>
/// <para>
/// If <paramref name="parentType"/> is the class <code>Stack&gt;T></code>, then this
/// method will return <code>true</code> when called on <code>Stack&gt;int></code>
/// or any type derived it, because <code>Stack&gt;int></code> is constructed from
/// <code>Stack&gt;T></code>.
/// </para>
/// <para>
/// Similarly, if <paramref name="parentType"/> is the interface <code>IList&gt;T></code>,
/// then this method will return <code>true</code> for <code>List&gt;int></code>
/// or any other class that extends <code>IList&gt;></code> or an class that implements it,
/// because <code>IList&gt;int></code> is constructed from <code>IList&gt;T></code>.
/// </para>
/// </example>
public static bool DerivesFromOrImplementsAnyConstructionOf(this INamedTypeSymbol type, INamedTypeSymbol parentType, Compilation compilation)
{
if (!parentType.IsDefinition)
{
throw new ArgumentException($"The type {nameof(parentType)} is not a definition; it is a constructed type", nameof(parentType));
}

for (INamedTypeSymbol baseType = type.OriginalDefinition;
baseType != null;
baseType = baseType.BaseType?.OriginalDefinition)
{
if (baseType.Equals(parentType))
{
return true;
}
}

if (type.OriginalDefinition.AllInterfaces.Any(baseInterface => baseInterface.OriginalDefinition.Equals(parentType)))
{
return true;
}

return false;
}

public static bool ImplementsOperator(this INamedTypeSymbol symbol, string op)
{
// TODO: should this filter on the right-hand-side operator type?
Expand Down
32 changes: 32 additions & 0 deletions src/Analyzer.Utilities/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Globalization;

namespace Analyzer.Utilities.Extensions
{
Expand All @@ -22,5 +23,36 @@ public static bool IsPlural(this string word)
}
return true;
}

public static bool HasSuffix(this string str, string suffix)
{
if (str == null)
{
throw new ArgumentNullException(nameof(str));
}

if (suffix == null)
{
throw new ArgumentNullException(nameof(suffix));
}

return str.EndsWith(suffix, StringComparison.Ordinal);
}

public static string WithoutSuffix(this string str, string suffix)
{
if (!str.HasSuffix(suffix))
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
AnalyzerUtilitiesResources.ErrorStringDoesNotEndWithSuffix,
str,
suffix),
nameof(str));
}

return str.Substring(0, str.Length - suffix.Length);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@
<Compile Include="CSharpImplementIDisposableCorrectly.Fixer.cs" />
<Compile Include="CSharpIdentifiersShouldNotContainUnderscores.Fixer.cs" />
<Compile Include="CSharpIdentifiersShouldHaveCorrectSuffix.Fixer.cs" />
<Compile Include="CSharpIdentifiersShouldNotHaveIncorrectSuffix.cs" />
<Compile Include="CSharpIdentifiersShouldNotHaveIncorrectSuffix.Fixer.cs" />
<Compile Include="CSharpEnumsShouldHavePluralNames.Fixer.cs" />
<Compile Include="CSharpIdentifiersShouldNotMatchKeywords.Fixer.cs" />
Expand Down
Loading

0 comments on commit 9a22626

Please sign in to comment.