Skip to content

Commit

Permalink
[GH-30] - basic arg analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
tpodolak committed Aug 24, 2018
1 parent 9300e00 commit a5302a3
Show file tree
Hide file tree
Showing 12 changed files with 1,096 additions and 60 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers
{
internal class CallInfoContext
{
public List<ElementAccessExpressionSyntax> IndexerAccesses { get; }

public List<InvocationExpressionSyntax> ArgAtInvocations { get; }

public List<InvocationExpressionSyntax> ArgInvocations { get; }

public CallInfoContext(List<InvocationExpressionSyntax> argAtInvocations, List<InvocationExpressionSyntax> argInvocations, List<ElementAccessExpressionSyntax> indexerAccesses)
{
IndexerAccesses = indexerAccesses;
ArgAtInvocations = argAtInvocations;
ArgInvocations = argInvocations;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ internal class AbstractDiagnosticDescriptorsProvider<T> : IDiagnosticDescriptors

public DiagnosticDescriptor ReEntrantSubstituteCall { get; } = DiagnosticDescriptors<T>.ReEntrantSubstituteCall;

public DiagnosticDescriptor CallInfoArgumentOutOfRange { get; } = DiagnosticDescriptors<T>.ReEntrantSubstituteCall;
public DiagnosticDescriptor CallInfoArgumentOutOfRange { get; } = DiagnosticDescriptors<T>.CallInfoArgumentOutOfRange;

public DiagnosticDescriptor CallInfoCouldNotConvertParameterAtPosition { get; } = DiagnosticDescriptors<T>.CallInfoCouldNotConvertParameterAtPosition;

public DiagnosticDescriptor CallInfoCouldNotFindArgumentToThisCall { get; } = DiagnosticDescriptors<T>.CallInfoCouldNotFindArgumentToThisCall;

public DiagnosticDescriptor CallInfoMoreThanOneArgumentOfType { get; } = DiagnosticDescriptors<T>.CallInfoMoreThanOneArgumentOfType;

public DiagnosticDescriptor CallInfoArgumentSetWithIncompatibleValue { get; } = DiagnosticDescriptors<T>.CallInfoArgumentSetWithIncompatibleValue;

public DiagnosticDescriptor CallInfoArgumentIsNotOutOrRef { get; } = DiagnosticDescriptors<T>.CallInfoArgumentIsNotOutOrRef;
}
}
39 changes: 39 additions & 0 deletions src/NSubstitute.Analyzers.Shared/DiagnosticDescriptors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,45 @@ internal class DiagnosticDescriptors<T>
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);

public static DiagnosticDescriptor CallInfoCouldNotConvertParameterAtPosition { get; } =
CreateDiagnosticDescriptor(
name: nameof(CallInfoCouldNotConvertParameterAtPosition),
id: DiagnosticIdentifiers.CallInfoCouldNotConvertParameterAtPosition,
category: DiagnosticCategories.Usage,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);

public static DiagnosticDescriptor CallInfoCouldNotFindArgumentToThisCall { get; } =
CreateDiagnosticDescriptor(
name: nameof(CallInfoCouldNotFindArgumentToThisCall),
id: DiagnosticIdentifiers.CallInfoCouldNotFindArgumentToThisCall,
category: DiagnosticCategories.Usage,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);

public static DiagnosticDescriptor CallInfoMoreThanOneArgumentOfType { get; } =
CreateDiagnosticDescriptor(
name: nameof(CallInfoMoreThanOneArgumentOfType),
id: DiagnosticIdentifiers.CallInfoMoreThanOneArgumentOfType,
category: DiagnosticCategories.Usage,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);

public static DiagnosticDescriptor CallInfoArgumentSetWithIncompatibleValue { get; } =
CreateDiagnosticDescriptor(
name: nameof(CallInfoArgumentSetWithIncompatibleValue),
id: DiagnosticIdentifiers.CallInfoArgumentSetWithIncompatibleValue,
category: DiagnosticCategories.Usage,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);

public static DiagnosticDescriptor CallInfoArgumentIsNotOutOrRef { get; } = CreateDiagnosticDescriptor(
name: nameof(CallInfoArgumentIsNotOutOrRef),
id: DiagnosticIdentifiers.CallInfoArgumentIsNotOutOrRef,
category: DiagnosticCategories.Usage,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);

private static DiagnosticDescriptor CreateDiagnosticDescriptor(
string name, string id, string category, DiagnosticSeverity defaultSeverity, bool isEnabledByDefault)
{
Expand Down
5 changes: 5 additions & 0 deletions src/NSubstitute.Analyzers.Shared/DiagnosticIdentifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ internal class DiagnosticIdentifiers
public static readonly string NonVirtualWhenSetupSpecification = "NS012";
public static readonly string ReEntrantSubstituteCall = "NS013";
public static readonly string CallInfoArgumentOutOfRange = "NS014";
public static readonly string CallInfoCouldNotConvertParameterAtPosition = "NS015";
public static readonly string CallInfoCouldNotFindArgumentToThisCall = "NS016";
public static readonly string CallInfoMoreThanOneArgumentOfType = "NS017";
public static readonly string CallInfoArgumentSetWithIncompatibleValue = "NS018";
public static readonly string CallInfoArgumentIsNotOutOrRef = "NS019";
}
}
10 changes: 10 additions & 0 deletions src/NSubstitute.Analyzers.Shared/IDiagnosticDescriptorsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,15 @@ internal interface IDiagnosticDescriptorsProvider
DiagnosticDescriptor ReEntrantSubstituteCall { get; }

DiagnosticDescriptor CallInfoArgumentOutOfRange { get; }

DiagnosticDescriptor CallInfoCouldNotConvertParameterAtPosition { get; }

DiagnosticDescriptor CallInfoCouldNotFindArgumentToThisCall { get; }

DiagnosticDescriptor CallInfoMoreThanOneArgumentOfType { get; }

DiagnosticDescriptor CallInfoArgumentSetWithIncompatibleValue { get; }

DiagnosticDescriptor CallInfoArgumentIsNotOutOrRef { get; }
}
}
4 changes: 4 additions & 0 deletions src/NSubstitute.Analyzers.Shared/MetadataNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ internal class MetadataNames
public const string NSubstituteWhenMethod = "When";
public const string NSubstituteWhenForAnyArgsMethod = "WhenForAnyArgs";
public const string NSubstituteWhenCalledType = "WhenCalled";
public const string CallInfoArgAtMethod = "ArgAt";
public const string CallInfoArgMethod = "Arg";
public const string CallInfoArgsMethod = "Args";
public const string CallInfoArgTypesMethod = "ArgTypes";
}
}
135 changes: 135 additions & 0 deletions src/NSubstitute.Analyzers.Shared/Resources.Designer.cs

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

60 changes: 60 additions & 0 deletions src/NSubstitute.Analyzers.Shared/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,64 @@
<value>Unable to find matching argument.</value>
<comment>The title of the diagnostic.</comment>
</data>
<data name="CallInfoCouldNotConvertParameterAtPositionDescription" xml:space="preserve">
<value>Couldn't convert parameter.</value>
<comment>An optional longer localizable description of the diagnostic.</comment>
</data>
<data name="CallInfoCouldNotConvertParameterAtPositionMessageFormat" xml:space="preserve">
<value>Couldn't convert parameter at position {0} to type {1}.</value>
<comment>The format-able message the diagnostic displays.</comment>
</data>
<data name="CallInfoCouldNotConvertParameterAtPositionTitle" xml:space="preserve">
<value>Couldn't convert parameter.</value>
<comment>The title of the diagnostic.</comment>
</data>
<data name="CallInfoCouldNotFindArgumentToThisCallDescription" xml:space="preserve">
<value>Can not find an argument to this call.</value>
<comment>An optional longer localizable description of the diagnostic.</comment>
</data>
<data name="CallInfoCouldNotFindArgumentToThisCallMessageFormat" xml:space="preserve">
<value>Can not find an argument of type {0} to this call.</value>
<comment>The format-able message the diagnostic displays.</comment>
</data>
<data name="CallInfoCouldNotFindArgumentToThisCallTitle" xml:space="preserve">
<value>Can not find an argument to this call.</value>
<comment>The title of the diagnostic.</comment>
</data>
<data name="CallInfoMoreThanOneArgumentOfTypeDescription" xml:space="preserve">
<value>There is more than one argument of given type to this call.</value>
<comment>An optional longer localizable description of the diagnostic.</comment>
</data>
<data name="CallInfoMoreThanOneArgumentOfTypeMessageFormat" xml:space="preserve">
<value>There is more than one argument of type {0} to this call.</value>
<comment>The format-able message the diagnostic displays.</comment>
</data>
<data name="CallInfoMoreThanOneArgumentOfTypeTitle" xml:space="preserve">
<value>There is more than one argument of given type to this call.</value>
<comment>The title of the diagnostic.</comment>
</data>
<data name="CallInfoArgumentSetWithIncompatibleValueDescription" xml:space="preserve">
<value>Could not set value.</value>
<comment>An optional longer localizable description of the diagnostic.</comment>
</data>
<data name="CallInfoArgumentSetWithIncompatibleValueMessageFormat" xml:space="preserve">
<value>Could not set value of type {0} to argument {1} ({2}) because the types are incompatible.</value>
<comment>The format-able message the diagnostic displays.</comment>
</data>
<data name="CallInfoArgumentSetWithIncompatibleValueTitle" xml:space="preserve">
<value>Could not set value.</value>
<comment>The title of the diagnostic.</comment>
</data>
<data name="CallInfoArgumentIsNotOutOrRefDescription" xml:space="preserve">
<value>Could not set argument.</value>
<comment>An optional longer localizable description of the diagnostic.</comment>
</data>
<data name="CallInfoArgumentIsNotOutOrRefMessageFormat" xml:space="preserve">
<value>Could not set argument {0} ({1}) as it is not an out or ref argument.</value>
<comment>The format-able message the diagnostic displays.</comment>
</data>
<data name="CallInfoArgumentIsNotOutOrRefTitle" xml:space="preserve">
<value>Could not set argument.</value>
<comment>The title of the diagnostic.</comment>
</data>
</root>

This file was deleted.

Loading

0 comments on commit a5302a3

Please sign in to comment.