Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: roslyn code analyzer for API usages #679

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Analyzer~/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
obj
bin

13 changes: 13 additions & 0 deletions Analyzer~/Analyzer.Sample/Analyzer.Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Analyzer\Analyzer.csproj"
OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions Analyzer~/Analyzer.Sample/Examples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// ReSharper disable UnusedType.Global
// ReSharper disable UnusedMember.Global
namespace Analyzer.Sample;

// If you don't see warnings, build the Analyzers Project.

public class Examples
{
public class MyCompanyClass // Try to apply quick fix using the IDE.
{
}

public void ToStars()
{
var spaceship = new Spaceship();
spaceship.SetSpeed(300000000); // Invalid value, it should be highlighted.
spaceship.SetSpeed(42);
}
}
12 changes: 12 additions & 0 deletions Analyzer~/Analyzer.Sample/Spaceship.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Analyzer.Sample;

public class Spaceship
{
public void SetSpeed(long speed)
{
if (speed > 299_792_458)
throw new ArgumentOutOfRangeException(nameof(speed));
}
}
25 changes: 25 additions & 0 deletions Analyzer~/Analyzer.Tests/Analyzer.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.1"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.1"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.2"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Analyzer\Analyzer.csproj"/>
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions Analyzer~/Analyzer.Tests/SampleCodeFixProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Threading.Tasks;
using Xunit;
using Verifier =
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.CodeFixVerifier<Analyzer.SampleSyntaxAnalyzer,
Analyzer.SampleCodeFixProvider>;

namespace Analyzer.Tests;

public class SampleCodeFixProviderTests
{
[Fact]
public async Task ClassWithMyCompanyTitle_ReplaceWithCommonKeyword()
{
const string text = @"
public class MyCompanyClass
{
}
";

const string newText = @"
public class CommonClass
{
}
";

var expected = Verifier.Diagnostic()
.WithLocation(2, 14)
.WithArguments("MyCompanyClass");
await Verifier.VerifyCodeFixAsync(text, expected, newText).ConfigureAwait(false);
}
}
35 changes: 35 additions & 0 deletions Analyzer~/Analyzer.Tests/SampleSemanticAnalyzerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Threading.Tasks;
using Xunit;
using Verifier =
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.AnalyzerVerifier<
Analyzer.SampleSemanticAnalyzer>;

namespace Analyzer.Tests;

public class SampleSemanticAnalyzerTests
{
[Fact]
public async Task SetSpeedHugeSpeedSpecified_AlertDiagnostic()
{
const string text = @"
public class Program
{
public void Main()
{
var spaceship = new Spaceship();
spaceship.SetSpeed(300000000);
}
}

public class Spaceship
{
public void SetSpeed(long speed) {}
}
";

var expected = Verifier.Diagnostic()
.WithLocation(7, 28)
.WithArguments("300000000");
await Verifier.VerifyAnalyzerAsync(text, expected);
}
}
25 changes: 25 additions & 0 deletions Analyzer~/Analyzer.Tests/SampleSyntaxAnalyzerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Threading.Tasks;
using Xunit;
using Verifier =
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.AnalyzerVerifier<
Analyzer.SampleSyntaxAnalyzer>;

namespace Analyzer.Tests;

public class SampleSyntaxAnalyzerTests
{
[Fact]
public async Task ClassWithMyCompanyTitle_AlertDiagnostic()
{
const string text = @"
public class MyCompanyClass
{
}
";

var expected = Verifier.Diagnostic()
.WithLocation(2, 14)
.WithArguments("MyCompanyClass");
await Verifier.VerifyAnalyzerAsync(text, expected).ConfigureAwait(false);
}
}
28 changes: 28 additions & 0 deletions Analyzer~/Analyzer.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Analyzer", "Analyzer\Analyzer.csproj", "{12E3ABF3-F611-4E37-9EFF-AD900923A9C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Analyzer.Sample", "Analyzer.Sample\Analyzer.Sample.csproj", "{CBCF75A5-9510-4F0F-B33F-0444868434AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Analyzer.Tests", "Analyzer.Tests\Analyzer.Tests.csproj", "{E621C24B-1CF0-475E-A395-8FF01A739475}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{12E3ABF3-F611-4E37-9EFF-AD900923A9C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{12E3ABF3-F611-4E37-9EFF-AD900923A9C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{12E3ABF3-F611-4E37-9EFF-AD900923A9C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{12E3ABF3-F611-4E37-9EFF-AD900923A9C8}.Release|Any CPU.Build.0 = Release|Any CPU
{CBCF75A5-9510-4F0F-B33F-0444868434AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBCF75A5-9510-4F0F-B33F-0444868434AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBCF75A5-9510-4F0F-B33F-0444868434AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBCF75A5-9510-4F0F-B33F-0444868434AE}.Release|Any CPU.Build.0 = Release|Any CPU
{E621C24B-1CF0-475E-A395-8FF01A739475}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E621C24B-1CF0-475E-A395-8FF01A739475}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E621C24B-1CF0-475E-A395-8FF01A739475}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E621C24B-1CF0-475E-A395-8FF01A739475}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
40 changes: 40 additions & 0 deletions Analyzer~/Analyzer/Analyzer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>

<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsRoslynComponent>true</IsRoslynComponent>

<RootNamespace>Analyzer</RootNamespace>
<AssemblyName>Analyzer</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.6.0"/>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<Compile Update="Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions Analyzer~/Analyzer/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Release 1.0

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|------------------------------------------------
AB0001 | Naming | Warning | Type names should not contain the company name.
AB0002 | Usage | Warning | The speed must be lower than the Speed of Light.
4 changes: 4 additions & 0 deletions Analyzer~/Analyzer/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
9 changes: 9 additions & 0 deletions Analyzer~/Analyzer/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"DebugRoslynAnalyzers": {
"commandName": "DebugRoslynComponent",
"targetProject": "../Analyzer.Sample/Analyzer.Sample.csproj"
}
}
}
29 changes: 29 additions & 0 deletions Analyzer~/Analyzer/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Roslyn Analyzers Sample

A set of three sample projects that includes Roslyn analyzers with code fix providers. Enjoy this template to learn from and modify analyzers for your own needs.

## Content
### Analyzer
A .NET Standard project with implementations of sample analyzers and code fix providers.
**You must build this project to see the results (warnings) in the IDE.**

- [SampleSemanticAnalyzer.cs](SampleSemanticAnalyzer.cs): An analyzer that reports invalid values used for the `speed` parameter of the `SetSpeed` function.
- [SampleSyntaxAnalyzer.cs](SampleSyntaxAnalyzer.cs): An analyzer that reports the company name used in class definitions.
- [SampleCodeFixProvider.cs](SampleCodeFixProvider.cs): A code fix that renames classes with company name in their definition. The fix is linked to [SampleSyntaxAnalyzer.cs](SampleSyntaxAnalyzer.cs).

### Analyzer.Sample
A project that references the sample analyzers. Note the parameters of `ProjectReference` in [Analyzer.Sample.csproj](../Analyzer.Sample/Analyzer.Sample.csproj), they make sure that the project is referenced as a set of analyzers.

### Analyzer.Tests
Unit tests for the sample analyzers and code fix provider. The easiest way to develop language-related features is to start with unit tests.

## How To?
### How to debug?
- Use the [launchSettings.json](Properties/launchSettings.json) profile.
- Debug tests.

### How can I determine which syntax nodes I should expect?
Consider installing the Roslyn syntax tree viewer plugin [Rossynt](https://plugins.jetbrains.com/plugin/16902-rossynt/).

### Learn more about wiring analyzers
The complete set of information is available at [roslyn github repo wiki](https://github.com/dotnet/roslyn/blob/main/docs/wiki/README.md).
90 changes: 90 additions & 0 deletions Analyzer~/Analyzer/Resources.Designer.cs

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

Loading