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

Add diagnostic and code fix verification helpers #131

Closed
wants to merge 9 commits into from
7 changes: 7 additions & 0 deletions Samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "VisualBasicToCSharpConverte
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SolutionExplorer", "samples\CSharp\SolutionExplorer\SolutionExplorer.csproj", "{B0474F4F-A6A9-4F10-BC49-CE2957201DBB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Roslyn.UnitTestFramework.Test", "samples\Shared\UnitTestFramework.Test\Roslyn.UnitTestFramework.Test.csproj", "{04DABE2E-7230-4992-8E8B-F6BEACB11AA5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -281,6 +283,10 @@ Global
{B0474F4F-A6A9-4F10-BC49-CE2957201DBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0474F4F-A6A9-4F10-BC49-CE2957201DBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0474F4F-A6A9-4F10-BC49-CE2957201DBB}.Release|Any CPU.Build.0 = Release|Any CPU
{04DABE2E-7230-4992-8E8B-F6BEACB11AA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04DABE2E-7230-4992-8E8B-F6BEACB11AA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04DABE2E-7230-4992-8E8B-F6BEACB11AA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04DABE2E-7230-4992-8E8B-F6BEACB11AA5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -339,6 +345,7 @@ Global
{ECB83742-8023-4609-B139-D7B78DD66ED9} = {8E1C9AEC-6EF1-43A8-A378-52C5C0E40532}
{5B7D7569-B5EE-4C01-9AFA-BC1958588160} = {8E1C9AEC-6EF1-43A8-A378-52C5C0E40532}
{B0474F4F-A6A9-4F10-BC49-CE2957201DBB} = {C3FB27E9-C8EE-4F76-B0AA-7CD67A7E652B}
{04DABE2E-7230-4992-8E8B-F6BEACB11AA5} = {A54A1AB7-DBD6-4C31-A22E-C53674137C53}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B849838B-3D7A-4B6B-BE07-285DCB1588F4}
Expand Down
25 changes: 25 additions & 0 deletions THIRD-PARTY-NOTICES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Roslyn SDK uses third-party libraries or other resources that may be
distributed under licenses different than the Roslyn SDK software.

In the event that we accidentally failed to list a required notice, please
bring it to our attention. Post an issue or email us:

dotnet@microsoft.com

The attached notices are provided for information only.

License notice for StyleCop Analyzers
-------------------------------------

Copyright (c) Tunnel Vision Laboratories, LLC. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
these files except in compliance with the License. You may obtain a copy of the
License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<OutputPath>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)\..\..\bin\CSharp\ConvertToConditional.Test'))</OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="2.6.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Roslyn.UnitTestFramework.Test
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
internal class CSharpSyntaxTreeDiagnosticAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "TEST01";
public static readonly LocalizableString Title = "Test title";
public static readonly LocalizableString MessageFormat = "Semicolons should be {0} by a space";
public static readonly string Category = "Test";
public static readonly DiagnosticSeverity DefaultSeverity = DiagnosticSeverity.Warning;
public static readonly bool IsEnabledByDefault = true;
public static readonly LocalizableString Description = "Test description";
public static readonly string HelpLinkUri = "https://github.com/dotnet/roslyn-sdk";
public static readonly ImmutableArray<string> CustomTags = ImmutableArray.Create(WellKnownDiagnosticTags.Unnecessary);

private static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DefaultSeverity, IsEnabledByDefault, Description, HelpLinkUri, CustomTags.ToArray());

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; }
= ImmutableArray.Create(Descriptor);

public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();

context.RegisterSyntaxTreeAction(HandleSyntaxTree);
}

private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
{
SyntaxNode root = context.Tree.GetCompilationUnitRoot(context.CancellationToken);
foreach (SyntaxToken token in root.DescendantTokens())
{
switch (token.Kind())
{
case SyntaxKind.SemicolonToken:
HandleSemicolonToken(context, token);
break;

default:
break;
}
}
}

private static void HandleSemicolonToken(SyntaxTreeAnalysisContext context, SyntaxToken token)
{
// check for a following space
bool missingFollowingSpace = true;
if (token.HasTrailingTrivia)
{
if (token.TrailingTrivia.First().IsKind(SyntaxKind.EndOfLineTrivia))
{
missingFollowingSpace = false;
}
}

if (missingFollowingSpace)
{
// semicolon should{} be {followed} by a space
context.ReportDiagnostic(Diagnostic.Create(Descriptor, token.GetLocation(), TokenSpacingProperties.InsertFollowing, "followed"));
}
}

internal static class TokenSpacingProperties
{
internal const string LocationKey = "location";
internal const string ActionKey = "action";
internal const string LocationFollowing = "following";
internal const string ActionInsert = "insert";

internal static ImmutableDictionary<string, string> InsertFollowing { get; } =
ImmutableDictionary<string, string>.Empty
.SetItem(LocationKey, LocationFollowing)
.SetItem(ActionKey, ActionInsert);
}
}
}
Loading