Skip to content

Commit

Permalink
[GH-11] - attempting generate suppression file
Browse files Browse the repository at this point in the history
  • Loading branch information
tpodolak committed Jun 21, 2018
1 parent 97ead42 commit e1c34f6
Show file tree
Hide file tree
Showing 19 changed files with 321 additions and 33 deletions.
7 changes: 7 additions & 0 deletions NSubstitute.Analyzers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NSubstitute.Analyzers.Visua
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NSubstitute.Analyzers.Tests.VisualBasic", "tests\NSubstitute.Analyzers.Tests.VisualBasic\NSubstitute.Analyzers.Tests.VisualBasic.csproj", "{00A000B8-B887-4216-92B2-E810616CE7A8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NSubstitute.Analyzers.CSharp.Vsix", "src\NSubstitute.Analyzers.CSharp.Vsix\NSubstitute.Analyzers.CSharp.Vsix.csproj", "{4DF03310-98B3-450E-90FE-2EF423A6CC59}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -46,6 +48,10 @@ Global
{00A000B8-B887-4216-92B2-E810616CE7A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00A000B8-B887-4216-92B2-E810616CE7A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00A000B8-B887-4216-92B2-E810616CE7A8}.Release|Any CPU.Build.0 = Release|Any CPU
{4DF03310-98B3-450E-90FE-2EF423A6CC59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4DF03310-98B3-450E-90FE-2EF423A6CC59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4DF03310-98B3-450E-90FE-2EF423A6CC59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4DF03310-98B3-450E-90FE-2EF423A6CC59}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2B7BF4D2-4964-4674-9268-70AE5A3C98DB} = {FB46CD9F-6E27-4767-AD1E-8C21CD08F993}
Expand All @@ -54,5 +60,6 @@ Global
{4FA76F86-FB64-4DD9-8F62-3508FB4544C8} = {0ED2F2A2-CF6E-4EED-8166-F22B5D7631F4}
{94139977-A98F-4B19-B3AE-E1001F1EDF00} = {FB46CD9F-6E27-4767-AD1E-8C21CD08F993}
{00A000B8-B887-4216-92B2-E810616CE7A8} = {0ED2F2A2-CF6E-4EED-8166-F22B5D7631F4}
{4DF03310-98B3-450E-90FE-2EF423A6CC59} = {FB46CD9F-6E27-4767-AD1E-8C21CD08F993}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -->
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MinimumVisualStudioVersion>15.0</MinimumVisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<FileUpgradeFlags>
</FileUpgradeFlags>
<OldToolsVersion>14.0</OldToolsVersion>
<UpgradeBackupLocation />
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
<Platform Condition="'$(Platform)' == ''">AnyCPU</Platform>
<PlatformTarget>AnyCPU</PlatformTarget>
<SchemaVersion>2.0</SchemaVersion>
<ProjectTypeGuids>{82b43b9b-a64c-4715-b499-d71e9ca2bd60};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>{4DF03310-98B3-450E-90FE-2EF423A6CC59}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NSubstitute.Analyzers2.CSharp.Vsix</RootNamespace>
<AssemblyName>NSubstitute.Analyzers2.CSharp</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<GeneratePkgDefFile>false</GeneratePkgDefFile>
<IncludeAssemblyInVSIXContainer>false</IncludeAssemblyInVSIXContainer>
<IncludeDebugSymbolsInVSIXContainer>false</IncludeDebugSymbolsInVSIXContainer>
<IncludeDebugSymbolsInLocalVSIXDeployment>false</IncludeDebugSymbolsInLocalVSIXDeployment>
<CopyBuildOutputToOutputDirectory>false</CopyBuildOutputToOutputDirectory>
<CopyOutputSymbolsToOutputDirectory>false</CopyOutputSymbolsToOutputDirectory>
<VSSDKTargetPlatformRegRootSuffix>Roslyn</VSSDKTargetPlatformRegRootSuffix>
</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>
<PropertyGroup>
<StartAction>Program</StartAction>
<StartProgram>$(DevEnvDir)devenv.exe</StartProgram>
<StartArguments>/rootsuffix Roslyn</StartArguments>
</PropertyGroup>
<ItemGroup>
<None Include="source.extension.vsixmanifest">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NSubstitute.Analyzers.CSharp\NSubstitute.Analyzers.CSharp.csproj">
<Project>{27CBFCE9-8E54-4EA1-8417-70BEB8F97E5B}</Project>
<Name>NSubstitute.Analyzers.CSharp</Name>
<IncludeOutputGroupsInVSIX>BuiltProjectOutputGroup%3bBuiltProjectOutputGroupDependencies%3bGetCopyToOutputDirectoryItems%3bSatelliteDllsProjectOutputGroup%3b</IncludeOutputGroupsInVSIX>
<IncludeOutputGroupsInVSIXLocalOnly>DebugSymbolsProjectOutputGroup%3b</IncludeOutputGroupsInVSIXLocalOnly>
</ProjectReference>
<ProjectReference Include="..\NSubstitute.Analyzers.Shared\NSubstitute.Analyzers.Shared.csproj">
<Project>{2b7bf4d2-4964-4674-9268-70ae5a3c98db}</Project>
<Name>NSubstitute.Analyzers.Shared</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json">
<Version>11.0.2</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\VSSDK\Microsoft.VsSDK.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="NSubstitute.Analyzers.CSharp.d429c94d-b39b-4294-99cd-205607d7d485" Version="1.0" Language="en-US" Publisher="Tomek"/>
<DisplayName>NSubstitute.Analyzers.CSharp</DisplayName>
<Description xml:space="preserve">This is a sample diagnostic extension for the .NET Compiler Platform ("Roslyn").</Description>
</Metadata>
<Installation>
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[15.0,)" />
</Installation>
<Dependencies>
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
</Dependencies>
<Assets>
<Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="NSubstitute.Analyzers2.CSharp" Path="|NSubstitute.Analyzers2.CSharp|"/>
<Asset Type="Microsoft.VisualStudio.Analyzer" d:Source="Project" d:ProjectName="NSubstitute.Analyzers2.CSharp" Path="|NSubstitute.Analyzers2.CSharp|"/>
</Assets>
<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[15.0,16.0)" DisplayName="Visual Studio core editor" />
<Prerequisite Id="Microsoft.VisualStudio.Component.Roslyn.LanguageServices" Version="[15.0,16.0)" DisplayName="Roslyn Language Services" />
</Prerequisites>
</PackageManifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="NSubstitute.Analyzers.CSharp.d429c94d-b39b-4294-99cd-205607d7d485" Version="1.0" Language="en-US" Publisher="Tomek"/>
<DisplayName>NSubstitute.Analyzers.CSharp</DisplayName>
<Description xml:space="preserve">This is a sample diagnostic extension for the .NET Compiler Platform ("Roslyn").</Description>
</Metadata>
<Installation>
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[15.0,)" />
</Installation>
<Dependencies>
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
</Dependencies>
<Assets>
<Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="NSubstitute.Analyzers.CSharp" Path="|NSubstitute.Analyzers.CSharp|"/>
<Asset Type="Microsoft.VisualStudio.Analyzer" d:Source="Project" d:ProjectName="NSubstitute.Analyzers.CSharp" Path="|NSubstitute.Analyzers.CSharp|"/>
<Asset Type="Microsoft.VisualStudio.Analyzer" d:Source="Project" d:ProjectName="NSubstitute.Analyzers.Shared" Path="|NSubstitute.Analyzers.Shared|"/>
</Assets>
<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[15.0,16.0)" DisplayName="Visual Studio core editor" />
<Prerequisite Id="Microsoft.VisualStudio.Component.Roslyn.LanguageServices" Version="[15.0,16.0)" DisplayName="Roslyn Language Services" />
</Prerequisites>
</PackageManifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Newtonsoft.Json;
using NSubstitute.Analyzers.Shared;
using NSubstitute.Analyzers.Shared.Extensions;
using NSubstitute.Analyzers.Shared.Settings;
using NSubstitute.Analyzers.Shared.Threading;

namespace NSubstitute.Analyzers.CSharp.CodeFixProviders
{
[ExportCodeFixProvider(LanguageNames.CSharp)]
public class SupressDiagnosticCodeFixProvider : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create(DiagnosticIdentifiers.NonVirtualSetupSpecification);

public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
var project = context.Document.Project;
var workspace = project.Solution.Workspace;

// check if we are allowed to add it
if (!workspace.CanApplyChange(ApplyChangesKind.AddAdditionalDocument))
{
return SpecializedTasks.CompletedTask;
}

foreach (var diagnostic in context.Diagnostics)
{
context.RegisterCodeFix(
CodeAction.Create(
"Suppress in nsubstitute.json", // TODO proper message
cancellationToken => GetTransformedSolutionAsync(context, diagnostic),
nameof(SupressDiagnosticCodeFixProvider)),
diagnostic);
}

return SpecializedTasks.CompletedTask;
}

private async Task<Solution> GetTransformedSolutionAsync(CodeFixContext context, Diagnostic diagnostic)
{
var options = context.Document.Project.AnalyzerOptions.GetSettings(default(CancellationToken));
var project = context.Document.Project;
var solution = project.Solution;
var root = await context.Document.GetSyntaxRootAsync().ConfigureAwait(false);
var model = await context.Document.GetSemanticModelAsync();

var forPartsOfNode = (InvocationExpressionSyntax)root.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
var symbol = model.GetSymbolInfo(forPartsOfNode);

var newDocumentId = DocumentId.CreateNewId(project.Id);

options.Suppressions.Add(new Suppression
{
Target = DocumentationCommentId.CreateDeclarationId(symbol.Symbol),
Rules = new List<string>
{
DiagnosticIdentifiers.NonVirtualSetupSpecification
}
});

var newSolution = solution.AddAdditionalDocument(newDocumentId, AnalyzersSettings.AnalyzerFileName, JsonConvert.SerializeObject(options));

return newSolution;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>
<PropertyGroup>
<PackageId>NSubstitute.Analyzers.CSharp</PackageId>
<PackageVersion>$(VersionSuffix)</PackageVersion>
<PackageVersion>7.0.0</PackageVersion>
<Authors>Tomasz Podolak, NSubstitute.Analyzers contributors</Authors>
<PackageLicenseUrl>https://github.com/nsubstitute/NSubstitute.Analyzers/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/nsubstitute/NSubstitute.Analyzers</PackageProjectUrl>
Expand All @@ -23,7 +23,7 @@
<ItemGroup>
<PackageReference Update="NetStandard.Library" Version="$(NetStandardImplicitPackageVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="1.3.2" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" PrivateAssets="all"/>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
<ItemGroup>
<None Update="tools\*.ps1" CopyToOutputDirectory="Always" Pack="true" PackagePath="" />
Expand Down
2 changes: 1 addition & 1 deletion src/NSubstitute.Analyzers.CSharp/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:element name="root" msdata:IsSettingsFileDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using NSubstitute.Analyzers.Shared.Threading;
using Document = Microsoft.CodeAnalysis.Document;

namespace NSubstitute.Analyzers.Shared.CodeFixProviders
Expand All @@ -26,7 +27,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(codeAction, diagnostic);
}

return Task.FromResult(1);
return SpecializedTasks.CompletedTask;
}

protected abstract TInvocationExpressionSyntax GetInvocationExpressionSyntaxWithEmptyArgumentList(TInvocationExpressionSyntax invocationExpressionSyntax);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using NSubstitute.Analyzers.Shared.Threading;

namespace NSubstitute.Analyzers.Shared.CodeFixProviders
{
Expand All @@ -27,7 +28,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(codeAction, diagnostic);
}

return Task.FromResult(1);
return SpecializedTasks.CompletedTask;
}

protected abstract TGenericNameSyntax GetGenericNameSyntax(TInvocationExpression methodInvocationNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ private bool IsSupressed(SyntaxNodeAnalysisContext syntaxNodeContext, ISymbol sy

private bool IsSupressed(SyntaxNodeAnalysisContext syntaxNodeContext, AnalyzersSettings settings, ISymbol symbol)
{
foreach (var supressedSymbolName in settings.NonVirtualSetupSettings.SupressedSymbols)
foreach (var supression in settings.Suppressions.Where(suppression => suppression.Rules.Contains(DiagnosticDescriptorsProvider.NonVirtualSetupSpecification.Id)))
{
foreach (var supressedSymbol in DocumentationCommentId.GetSymbolsForDeclarationId(supressedSymbolName, syntaxNodeContext.Compilation))
foreach (var supressedSymbol in DocumentationCommentId.GetSymbolsForDeclarationId(supression.Target, syntaxNodeContext.Compilation))
{
if (supressedSymbol.Equals(symbol) ||
supressedSymbol.Equals(symbol.ContainingType) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.IO;
using Microsoft.CodeAnalysis;
using NSubstitute.Analyzers.Shared.Settings;

namespace NSubstitute.Analyzers.Shared.Extensions
{
public static class AdditionalTextExtensions
{
public static bool IsSettingsFile(this AdditionalText additionalText)
{
return Path.GetFileName(additionalText.Path).Equals(AnalyzersSettings.AnalyzerFileName, StringComparison.CurrentCultureIgnoreCase);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.Diagnostics;
using Newtonsoft.Json;
using NSubstitute.Analyzers.Shared.Settings;

namespace NSubstitute.Analyzers.Shared.Extensions
Expand All @@ -12,8 +11,7 @@ internal static class AnalyzerOptionsExtensions
{
public static AnalyzersSettings GetSettings(this AnalyzerOptions options, CancellationToken cancellationToken)
{
var settingsText = options.AdditionalFiles.FirstOrDefault(file =>
Path.GetFileName(file.Path).Equals(AnalyzersSettings.AnalyzerFileName, StringComparison.CurrentCultureIgnoreCase));
var settingsText = options.AdditionalFiles.FirstOrDefault(file => file.IsSettingsFile());

if (settingsText == null)
{
Expand All @@ -23,7 +21,10 @@ public static AnalyzersSettings GetSettings(this AnalyzerOptions options, Cancel
try
{
var sourceText = settingsText.GetText(cancellationToken);
return JsonConvert.DeserializeObject<AnalyzersSettings>(sourceText.ToString());

return AnalyzersSettings.Default;

// return JsonConvert.DeserializeObject<AnalyzersSettings>(sourceText.ToString());
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<Version>$(VersionSuffix)</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="1.3.2" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="1.3.2" PrivateAssets="all"/>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="1.3.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="1.3.2" PrivateAssets="all" />
<PackageReference Update="NetStandard.Library" Version="$(NetStandardImplicitPackageVersion)" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" PrivateAssets="all"/>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
</Project>
Loading

0 comments on commit e1c34f6

Please sign in to comment.