-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SP1003 analyzer that validates correct return statements formatting
- Loading branch information
1 parent
f997c3f
commit 805c324
Showing
20 changed files
with
341 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using StyleCopPlus.Analyzers; | ||
using StyleCopPlus.Test.Helpers; | ||
using TestHelper; | ||
|
||
namespace StyleCopPlus.Test.Analyzers | ||
{ | ||
[TestClass] | ||
public class SP1003AnalyzerTests : DiagnosticVerifier | ||
{ | ||
[TestMethod] | ||
public void DoesNotReport_ValidReturnStatements() | ||
{ | ||
string test = DataHelper.GetEmbeddedResource( | ||
DataHelper.SP1003FormattedReturnStatements, | ||
out _, | ||
out _); | ||
|
||
VerifyCSharpDiagnostic(test); | ||
} | ||
|
||
[TestMethod] | ||
public void Reports_UnformattedBlockReturnStatement() | ||
{ | ||
string test = DataHelper.GetEmbeddedResource( | ||
DataHelper.SP1003UnformattedBlockReturnStatement, | ||
out int line, | ||
out int column); | ||
|
||
DiagnosticResult expected = CreateResult(line, column); | ||
|
||
VerifyCSharpDiagnostic(test, expected); | ||
} | ||
|
||
[TestMethod] | ||
public void Reports_UnformattedMethodReturnStatement() | ||
{ | ||
string test = DataHelper.GetEmbeddedResource( | ||
DataHelper.SP1003UnformattedMethodReturnStatement, | ||
out int line, | ||
out int column); | ||
|
||
DiagnosticResult expected = CreateResult(line, column); | ||
|
||
VerifyCSharpDiagnostic(test, expected); | ||
} | ||
|
||
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() | ||
{ | ||
return new SP1003SeparatedReturnStatementAnalyzer(); | ||
} | ||
|
||
private DiagnosticResult CreateResult(int lineNumber, int column) | ||
{ | ||
return new DiagnosticResult | ||
{ | ||
Id = SP1003SeparatedReturnStatementAnalyzer.DiagnosticId, | ||
Message = SP1003SeparatedReturnStatementAnalyzer.MessageFormat, | ||
Severity = DiagnosticSeverity.Warning, | ||
Locations = | ||
[ | ||
new DiagnosticResultLocation("Test0.cs", lineNumber, column) | ||
] | ||
}; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 12 additions & 100 deletions
112
src/StyleCopPlus.Test/Playground/AnalyzerPlayground.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{E1FA01A7-84FB-4BAA-8A2F-2B70E223965E}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>AnalyzerPlayground</RootNamespace> | ||
<AssemblyName>AnalyzerPlayground</AssemblyName> | ||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<TargetFrameworkProfile /> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="CheckParameters\GenericParameter.cs" /> | ||
<Compile Include="CheckParameters\OutParameter.cs" /> | ||
<Compile Include="CheckParameters\ReferenceParameter.cs" /> | ||
<Compile Include="CheckParameters\RefParameter.cs" /> | ||
<Compile Include="CheckParameters\ValueTypeParameter.cs" /> | ||
<Compile Include="CheckParameters\ConstructorParameter.cs" /> | ||
<Compile Include="CreateVariable\ConstructorCall.cs" /> | ||
<Compile Include="CreateVariable\FluentApiCalls.cs" /> | ||
<Compile Include="CreateVariable\PropertyCall.cs" /> | ||
<Compile Include="CreateVariable\StaticCall.cs" /> | ||
<Compile Include="CreateVariable\VoidCall.cs" /> | ||
<Compile Include="IntroduceField\ReadonlyField.cs" /> | ||
<Compile Include="SP1001\ThrowExpression.cs" /> | ||
<Compile Include="SP1001\ThrowStatement.cs" /> | ||
<Compile Include="SP1001\ThrowWithoutMessage.cs" /> | ||
<Compile Include="SP1001\ValidThrowExpression.cs" /> | ||
<Compile Include="SP1001\ValidThrowStatement.cs" /> | ||
<Compile Include="SP1002\InvalidParameterName.cs" /> | ||
<Compile Include="SP1002\ValidParameterName.cs" /> | ||
<Compile Include="SP1131\OperandWithinLambda.cs" /> | ||
<Compile Include="SP2100\All.cs" /> | ||
<Compile Include="SP2100\ConstructorDefinition.cs" /> | ||
<Compile Include="SP2100\ConstructorInvocation.cs" /> | ||
<Compile Include="SP2100\MethodDefinition.cs" /> | ||
<Compile Include="SP2100\MethodInvocation.cs" /> | ||
<Compile Include="SP2100\MethodInvocationWithAssignment.cs" /> | ||
<Compile Include="SP2100\FluentApi.cs" /> | ||
<Compile Include="SP2101\LongConstructor.cs" /> | ||
<Compile Include="SP2101\LongFinalizer.cs" /> | ||
<Compile Include="SP2101\LongMethod.cs" /> | ||
<Compile Include="SP2101\LongStaticConstructor.cs" /> | ||
<Compile Include="SP2101\ValidClass.cs" /> | ||
<Compile Include="SP2102\LongGetter.cs" /> | ||
<Compile Include="SP2102\LongSetter.cs" /> | ||
<Compile Include="SP2102\ValidClass.cs" /> | ||
<Compile Include="SP2103\LongClass.cs" /> | ||
<Compile Include="SP2103\ValidClass.cs" /> | ||
<Compile Include="SP1131\CorrectOperandsWithStatic.cs" /> | ||
<Compile Include="SP1131\IncorrectOperands.cs" /> | ||
<Compile Include="SP1131\IncorrectOperandsFixed.cs" /> | ||
<Compile Include="SP1131\IncorrectOperandsWithConst.cs" /> | ||
<Compile Include="SP1131\IncorrectOperandsWithConstFixed.cs" /> | ||
<Compile Include="SP1131\InvertedOperands.cs" /> | ||
<Compile Include="SP1131\InvertedOperandsFixed.cs" /> | ||
<Compile Include="SP1131\NotEqualsOperator.cs" /> | ||
<Compile Include="SP1131\NotEqualsOperatorFixed.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Folder Include="Properties\" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<AdditionalFiles Include="stylecop.json" Link="stylecop.json" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> | ||
|
||
</Project> | ||
|
||
|
||
|
29 changes: 29 additions & 0 deletions
29
src/StyleCopPlus.Test/Playground/SP1003/FormattedReturnStatements.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
|
||
namespace AnalyzerPlayground.SP1003 | ||
{ | ||
internal class FormattedReturnStatements | ||
{ | ||
public int M() | ||
{ | ||
Console.WriteLine("test"); | ||
|
||
if (DateTime.Now.Second % 2 == 0) | ||
{ | ||
return 1; | ||
} | ||
else if (DateTime.Now.Second % 2 == 1) | ||
{ | ||
Console.Write("test"); | ||
|
||
return 2; | ||
} | ||
else if (DateTime.Now.Second % 3 == 1) | ||
return 5; | ||
|
||
Console.WriteLine("test"); | ||
|
||
return 0; | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/StyleCopPlus.Test/Playground/SP1003/UnformattedBlockReturnStatement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace AnalyzerPlayground.SP1003 | ||
{ | ||
internal class UnformattedBlockReturnStatement | ||
{ | ||
public int M() | ||
{ | ||
if (DateTime.Now.Second % 2 == 0) | ||
{ | ||
Console.WriteLine("test"); | ||
/*C*/return 1; | ||
} | ||
|
||
return 0; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/StyleCopPlus.Test/Playground/SP1003/UnformattedMethodReturnStatement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace AnalyzerPlayground.SP1003 | ||
{ | ||
internal class UnformattedMethodReturnStatement | ||
{ | ||
public int M() | ||
{ | ||
Console.WriteLine("test"); | ||
/*C*/return 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.