-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d707cb
commit 28c6270
Showing
18 changed files
with
686 additions
and
0 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
20 changes: 20 additions & 0 deletions
20
src/Hussy.Net.Analyzers/Hussy.Net.Analyzers.Sample/Examples.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,20 @@ | ||
// ReSharper disable UnusedType.Global | ||
// ReSharper disable UnusedMember.Global | ||
|
||
namespace Hussy.Net.Analyzers.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. | ||
Check warning on line 10 in src/Hussy.Net.Analyzers/Hussy.Net.Analyzers.Sample/Examples.cs
|
||
{ | ||
} | ||
|
||
public void ToStars() | ||
{ | ||
var spaceship = new Spaceship(); | ||
spaceship.SetSpeed(300000000); // Invalid value, it should be highlighted. | ||
Check warning on line 17 in src/Hussy.Net.Analyzers/Hussy.Net.Analyzers.Sample/Examples.cs
|
||
spaceship.SetSpeed(42); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Hussy.Net.Analyzers/Hussy.Net.Analyzers.Sample/Hussy.Net.Analyzers.Sample.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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Hussy.Net\Hussy.Net.csproj" /> | ||
<ProjectReference Include="..\Hussy.Net.Analyzers\Hussy.Net.Analyzers.csproj" | ||
OutputItemType="Analyzer" ReferenceOutputAssembly="false"/> | ||
</ItemGroup> | ||
|
||
</Project> |
12 changes: 12 additions & 0 deletions
12
src/Hussy.Net.Analyzers/Hussy.Net.Analyzers.Sample/Spaceship.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,12 @@ | ||
using System; | ||
|
||
namespace Hussy.Net.Analyzers.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
25
src/Hussy.Net.Analyzers/Hussy.Net.Analyzers.Tests/Hussy.Net.Analyzers.Tests.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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.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="..\Hussy.Net.Analyzers\Hussy.Net.Analyzers.csproj"/> | ||
</ItemGroup> | ||
|
||
</Project> |
31 changes: 31 additions & 0 deletions
31
src/Hussy.Net.Analyzers/Hussy.Net.Analyzers.Tests/SampleCodeFixProviderTests.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,31 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Verifier = | ||
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.CodeFixVerifier<Hussy.Net.Analyzers.SampleSyntaxAnalyzer, | ||
Hussy.Net.Analyzers.SampleCodeFixProvider>; | ||
|
||
namespace Hussy.Net.Analyzers.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
35
src/Hussy.Net.Analyzers/Hussy.Net.Analyzers.Tests/SampleSemanticAnalyzerTests.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,35 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Verifier = | ||
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.AnalyzerVerifier< | ||
Hussy.Net.Analyzers.SampleSemanticAnalyzer>; | ||
|
||
namespace Hussy.Net.Analyzers.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
25
src/Hussy.Net.Analyzers/Hussy.Net.Analyzers.Tests/SampleSyntaxAnalyzerTests.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,25 @@ | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Verifier = | ||
Microsoft.CodeAnalysis.CSharp.Testing.XUnit.AnalyzerVerifier< | ||
Hussy.Net.Analyzers.SampleSyntaxAnalyzer>; | ||
|
||
namespace Hussy.Net.Analyzers.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); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Hussy.Net.Analyzers/Hussy.Net.Analyzers/AnalyzerReleases.Shipped.md
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,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
4
src/Hussy.Net.Analyzers/Hussy.Net.Analyzers/AnalyzerReleases.Unshipped.md
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,4 @@ | ||
### New Rules | ||
|
||
Rule ID | Category | Severity | Notes | ||
--------|----------|----------|------- |
43 changes: 43 additions & 0 deletions
43
src/Hussy.Net.Analyzers/Hussy.Net.Analyzers/Hussy.Net.Analyzers.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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<IsPackable>false</IsPackable> | ||
<Nullable>enable</Nullable> | ||
<LangVersion>latest</LangVersion> | ||
|
||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
<IsRoslynComponent>true</IsRoslynComponent> | ||
|
||
<RootNamespace>Hussy.Net.Analyzers</RootNamespace> | ||
<AssemblyName>Hussy.Net.Analyzers</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.3.0"/> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.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> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Hussy.Net\Hussy.Net.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
9 changes: 9 additions & 0 deletions
9
src/Hussy.Net.Analyzers/Hussy.Net.Analyzers/Properties/launchSettings.json
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,9 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"DebugRoslynAnalyzers": { | ||
"commandName": "DebugRoslynComponent", | ||
"targetProject": "../Hussy.Net.Analyzers.Sample/Hussy.Net.Analyzers.Sample.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 |
---|---|---|
@@ -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 | ||
### Hussy.Net.Analyzers | ||
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). | ||
|
||
### Hussy.Net.Analyzers.Sample | ||
A project that references the sample analyzers. Note the parameters of `ProjectReference` in [Hussy.Net.Analyzers.Sample.csproj](../Hussy.Net.Analyzers.Sample/Hussy.Net.Analyzers.Sample.csproj), they make sure that the project is referenced as a set of analyzers. | ||
|
||
### Hussy.Net.Analyzers.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). |
Oops, something went wrong.