Skip to content

Commit 59df64a

Browse files
committed
Update stylecop analyzers
1 parent 7ad77f4 commit 59df64a

21 files changed

Lines changed: 236 additions & 162 deletions

StyleCop.Analyzers.Status.Generator/CodeFixStatus.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace StyleCop.Analyzers.Status.Generator
1+
// Copyright (c) Dennis Fischer. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Status.Generator
25
{
36
/// <summary>
47
/// This enum is used to indicate whether or not a code fix is implemented

StyleCop.Analyzers.Status.Generator/CompilationFailedException.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace StyleCop.Analyzers.Status.Generator
1+
// Copyright (c) Dennis Fischer. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Status.Generator
25
{
36
using System;
47

@@ -13,26 +16,25 @@ public class CompilationFailedException : Exception
1316
/// </summary>
1417
public CompilationFailedException()
1518
{
16-
1719
}
1820

1921
/// <summary>
2022
/// Initializes a new instance of the <see cref="CompilationFailedException"/> class.
2123
/// </summary>
2224
/// <param name="message">The message that should be reported</param>
23-
public CompilationFailedException(string message) : base(message)
25+
public CompilationFailedException(string message)
26+
: base(message)
2427
{
25-
2628
}
2729

2830
/// <summary>
2931
/// Initializes a new instance of the <see cref="CompilationFailedException"/> class.
3032
/// </summary>
3133
/// <param name="message">The message that should be reported</param>
3234
/// <param name="inner">The exception that caused this exception to be thrown</param>
33-
public CompilationFailedException(string message, Exception inner) : base(message, inner)
35+
public CompilationFailedException(string message, Exception inner)
36+
: base(message, inner)
3437
{
35-
3638
}
3739
}
3840
}

StyleCop.Analyzers.Status.Generator/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace StyleCop.Analyzers.Status.Generator
1+
// Copyright (c) Dennis Fischer. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Status.Generator
25
{
36
using System;
47
using System.IO;

StyleCop.Analyzers.Status.Generator/Properties/AssemblyInfo.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Reflection;
1+
// Copyright (c) Dennis Fischer. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
using System.Reflection;
25
using System.Runtime.CompilerServices;
36
using System.Runtime.InteropServices;
47

StyleCop.Analyzers.Status.Generator/ResourceReader.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
3-
using System.Collections.Immutable;
4-
using System.IO;
5-
using System.Resources;
6-
using Microsoft.CodeAnalysis;
1+
// Copyright (c) Dennis Fischer. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
73

84
namespace StyleCop.Analyzers.Status.Generator
95
{
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
using System.Collections.Immutable;
9+
using System.IO;
10+
using System.Resources;
11+
using Microsoft.CodeAnalysis;
12+
1013
/// <summary>
1114
/// Provides a method that creates binary resources in memory
1215
/// </summary>
@@ -16,11 +19,13 @@ internal class ResourceReader
1619
/// Creates a <see cref="ResourceDescription"/> from a resx file im memory.
1720
/// </summary>
1821
/// <param name="name">The name of the resource.</param>
19-
/// < param name="path">The path to the resource</param>
22+
/// <param name="path">The path to the resource</param>
2023
/// <returns>A resource description for the resx file</returns>
2124
internal static ResourceDescription ReadResource(string name, string path)
2225
{
23-
return new ResourceDescription(name, () =>
26+
return new ResourceDescription(
27+
name,
28+
() =>
2429
{
2530
using (ResXResourceReader reader = new ResXResourceReader(path))
2631
{
@@ -35,7 +40,8 @@ internal static ResourceDescription ReadResource(string name, string path)
3540
}
3641
return new MemoryStream(memStream.ToArray());
3742
}
38-
}, false);
43+
},
44+
false);
3945
}
4046

4147
/// <summary>
@@ -56,7 +62,8 @@ internal static ImmutableArray<ResourceDescription> GetResourcesRecursive(string
5662

5763
foreach (var path in Directory.EnumerateFiles(root, "*.resx", SearchOption.AllDirectories))
5864
{
59-
string relativePath = Path.ChangeExtension(Path.GetFullPath(path)
65+
string relativePath = Path.ChangeExtension(
66+
Path.GetFullPath(path)
6067
.Substring(root.Length)
6168
.Replace(Path.DirectorySeparatorChar, '.')
6269
.Replace(Path.AltDirectorySeparatorChar, '.'), "resources")

StyleCop.Analyzers.Status.Generator/SolutionReader.cs

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
namespace StyleCop.Analyzers.Status.Generator
1+
// Copyright (c) Dennis Fischer. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Status.Generator
25
{
36
using System;
4-
using System.Collections.Immutable;
57
using System.Collections.Generic;
8+
using System.Collections.Immutable;
69
using System.IO;
710
using System.Linq;
811
using System.Reflection;
912
using System.Text.RegularExpressions;
1013
using System.Threading.Tasks;
1114
using Microsoft.CodeAnalysis;
1215
using Microsoft.CodeAnalysis.CodeFixes;
13-
using Microsoft.CodeAnalysis.MSBuild;
14-
using Microsoft.CodeAnalysis.Diagnostics;
1516
using Microsoft.CodeAnalysis.CSharp;
1617
using Microsoft.CodeAnalysis.CSharp.Syntax;
18+
using Microsoft.CodeAnalysis.Diagnostics;
19+
using Microsoft.CodeAnalysis.MSBuild;
1720

1821
/// <summary>
1922
/// A class that is used to parse the StyleCop.Analyzers solution to get an overview
@@ -65,7 +68,6 @@ private SolutionReader()
6568
/// <returns>A <see cref="Task{SolutionReader}"/> representing the asynchronous operation</returns>
6669
public static async Task<SolutionReader> CreateAsync(string pathToSln, string analyzerProjectName = "StyleCop.Analyzers", string codeFixProjectName = "StyleCop.Analyzers.CodeFixes")
6770
{
68-
6971
SolutionReader reader = new SolutionReader();
7072

7173
reader.SlnPath = pathToSln;
@@ -78,67 +80,10 @@ public static async Task<SolutionReader> CreateAsync(string pathToSln, string an
7880
return reader;
7981
}
8082

81-
private async Task InitializeAsync()
82-
{
83-
this.solution = await this.workspace.OpenSolutionAsync(this.SlnPath);
84-
85-
this.analyzerProject = this.solution.Projects.Single(x => x.Name == this.AnalyzerProjectName);
86-
this.analyzerCompilation = await this.analyzerProject.GetCompilationAsync();
87-
this.analyzerCompilation = this.analyzerCompilation.WithOptions(this.analyzerCompilation.Options.WithOutputKind(OutputKind.DynamicallyLinkedLibrary));
88-
89-
this.codeFixProject = this.solution.Projects.Single(x => x.Name == this.CodeFixProjectName);
90-
this.codeFixCompilation = await this.codeFixProject.GetCompilationAsync();
91-
this.codeFixCompilation = this.codeFixCompilation.WithOptions(this.codeFixCompilation.Options.WithOutputKind(OutputKind.DynamicallyLinkedLibrary));
92-
93-
this.booleanType = this.analyzerCompilation.GetSpecialType(SpecialType.System_Boolean);
94-
95-
this.Compile();
96-
97-
this.noCodeFixAttributeTypeSymbol = this.analyzerCompilation.GetTypeByMetadataName("StyleCop.Analyzers.NoCodeFixAttribute");
98-
this.diagnosticAnalyzerTypeSymbol = this.analyzerCompilation.GetTypeByMetadataName(typeof(DiagnosticAnalyzer).FullName);
99-
100-
this.InitializeCodeFixTypes();
101-
}
102-
103-
private void InitializeCodeFixTypes()
104-
{
105-
var codeFixTypes = this.codeFixAssembly.GetTypes().Where(x => x.FullName.EndsWith("CodeFixProvider"));
106-
107-
this.CodeFixProviders = ImmutableArray.Create(
108-
codeFixTypes
109-
.Select(t => Activator.CreateInstance(t, true))
110-
.OfType<CodeFixProvider>()
111-
.Where(x => x != null)
112-
.Where(x => x.GetType().Name != "SettingsFileCodeFixProvider")
113-
.ToArray());
114-
}
115-
116-
private void Compile()
117-
{
118-
string path = Path.Combine(Path.GetDirectoryName(this.SlnPath), this.AnalyzerProjectName);
119-
this.analyzerAssembly = this.GetAssembly(this.analyzerCompilation, ResourceReader.GetResourcesRecursive(path));
120-
121-
this.codeFixAssembly = this.GetAssembly(this.codeFixCompilation);
122-
}
123-
124-
private Assembly GetAssembly(Compilation compilation, IEnumerable<ResourceDescription> manifestResources = null)
125-
{
126-
MemoryStream memStream = new MemoryStream();
127-
128-
var emitResult = compilation.Emit(memStream, manifestResources: manifestResources);
129-
130-
if (!emitResult.Success)
131-
{
132-
throw new CompilationFailedException();
133-
}
134-
135-
return Assembly.Load(memStream.ToArray());
136-
}
137-
13883
/// <summary>
13984
/// Analyzes the project and returns information about the diagnostics in it.
14085
/// </summary>
141-
/// <returns>A <see cref="Task{ImmutableList{StyleCopDiagnostic}}"/> representing the asynchronous operation</returns>
86+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
14287
public async Task<ImmutableList<StyleCopDiagnostic>> GetDiagnosticsAsync()
14388
{
14489
var diagnostics = ImmutableList.CreateBuilder<StyleCopDiagnostic>();
@@ -228,6 +173,63 @@ private static bool HasImplementation(SyntaxNode syntaxRoot)
228173
return hasImplementation;
229174
}
230175

176+
private async Task InitializeAsync()
177+
{
178+
this.solution = await this.workspace.OpenSolutionAsync(this.SlnPath);
179+
180+
this.analyzerProject = this.solution.Projects.Single(x => x.Name == this.AnalyzerProjectName);
181+
this.analyzerCompilation = await this.analyzerProject.GetCompilationAsync();
182+
this.analyzerCompilation = this.analyzerCompilation.WithOptions(this.analyzerCompilation.Options.WithOutputKind(OutputKind.DynamicallyLinkedLibrary));
183+
184+
this.codeFixProject = this.solution.Projects.Single(x => x.Name == this.CodeFixProjectName);
185+
this.codeFixCompilation = await this.codeFixProject.GetCompilationAsync();
186+
this.codeFixCompilation = this.codeFixCompilation.WithOptions(this.codeFixCompilation.Options.WithOutputKind(OutputKind.DynamicallyLinkedLibrary));
187+
188+
this.booleanType = this.analyzerCompilation.GetSpecialType(SpecialType.System_Boolean);
189+
190+
this.Compile();
191+
192+
this.noCodeFixAttributeTypeSymbol = this.analyzerCompilation.GetTypeByMetadataName("StyleCop.Analyzers.NoCodeFixAttribute");
193+
this.diagnosticAnalyzerTypeSymbol = this.analyzerCompilation.GetTypeByMetadataName(typeof(DiagnosticAnalyzer).FullName);
194+
195+
this.InitializeCodeFixTypes();
196+
}
197+
198+
private void InitializeCodeFixTypes()
199+
{
200+
var codeFixTypes = this.codeFixAssembly.GetTypes().Where(x => x.FullName.EndsWith("CodeFixProvider"));
201+
202+
this.CodeFixProviders = ImmutableArray.Create(
203+
codeFixTypes
204+
.Select(t => Activator.CreateInstance(t, true))
205+
.OfType<CodeFixProvider>()
206+
.Where(x => x != null)
207+
.Where(x => x.GetType().Name != "SettingsFileCodeFixProvider")
208+
.ToArray());
209+
}
210+
211+
private void Compile()
212+
{
213+
string path = Path.Combine(Path.GetDirectoryName(this.SlnPath), this.AnalyzerProjectName);
214+
this.analyzerAssembly = this.GetAssembly(this.analyzerCompilation, ResourceReader.GetResourcesRecursive(path));
215+
216+
this.codeFixAssembly = this.GetAssembly(this.codeFixCompilation);
217+
}
218+
219+
private Assembly GetAssembly(Compilation compilation, IEnumerable<ResourceDescription> manifestResources = null)
220+
{
221+
MemoryStream memStream = new MemoryStream();
222+
223+
var emitResult = compilation.Emit(memStream, manifestResources: manifestResources);
224+
225+
if (!emitResult.Success)
226+
{
227+
throw new CompilationFailedException();
228+
}
229+
230+
return Assembly.Load(memStream.ToArray());
231+
}
232+
231233
private string GetStatus(INamedTypeSymbol classSymbol, SyntaxNode root, SemanticModel model, DiagnosticDescriptor descriptor)
232234
{
233235
// Some analyzers use multiple descriptors. We analyze the first one and hope that

StyleCop.Analyzers.Status.Generator/StyleCop.Analyzers.Status.Generator.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<ErrorReport>prompt</ErrorReport>
2727
<WarningLevel>4</WarningLevel>
2828
<CodeAnalysisRuleSet>Rules.ruleset</CodeAnalysisRuleSet>
29+
<DocumentationFile>bin\Debug\StyleCop.Analyzers.Status.Generator.XML</DocumentationFile>
2930
</PropertyGroup>
3031
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3132
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -118,19 +119,24 @@
118119
<SubType>Designer</SubType>
119120
</None>
120121
<None Include="Rules.ruleset" />
122+
<None Include="..\stylecop.json" />
121123
</ItemGroup>
122124
<ItemGroup>
123125
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.1.0.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll" />
124126
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.1.0.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll" />
125-
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-alpha007\tools\analyzers\C#\StyleCop.Analyzers.dll" />
127+
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta015\analyzers\dotnet\cs\Newtonsoft.Json.dll" />
128+
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta015\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
129+
<Analyzer Include="..\packages\StyleCop.Analyzers.1.0.0-beta015\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
126130
</ItemGroup>
127131
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
128132
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
129133
<PropertyGroup>
130134
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
131135
</PropertyGroup>
132136
<Error Condition="!Exists('..\packages\LibGit2Sharp.0.21.0.176\build\net40\LibGit2Sharp.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\LibGit2Sharp.0.21.0.176\build\net40\LibGit2Sharp.props'))" />
137+
<Error Condition="!Exists('..\packages\StyleCop.Analyzers.1.0.0-beta015\build\StyleCop.Analyzers.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\StyleCop.Analyzers.1.0.0-beta015\build\StyleCop.Analyzers.targets'))" />
133138
</Target>
139+
<Import Project="..\packages\StyleCop.Analyzers.1.0.0-beta015\build\StyleCop.Analyzers.targets" Condition="Exists('..\packages\StyleCop.Analyzers.1.0.0-beta015\build\StyleCop.Analyzers.targets')" />
134140
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
135141
Other similar extension points exist, see Microsoft.Common.targets.
136142
<Target Name="BeforeBuild">

StyleCop.Analyzers.Status.Generator/StyleCopDiagnostic.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace StyleCop.Analyzers.Status.Generator
1+
// Copyright (c) Dennis Fischer. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Status.Generator
25
{
36
using Newtonsoft.Json;
47
using Newtonsoft.Json.Converters;
@@ -89,6 +92,17 @@ public class StyleCopDiagnostic
8992
/// </value>
9093
public string HelpLink { get; set; }
9194

95+
/// <summary>
96+
/// Creates an instance of the <see cref="StyleCopDiagnostic"/> class
97+
/// that is populated with the data stored in <paramref name="value"/>.
98+
/// </summary>
99+
/// <param name="value">A json representing a <see cref="StyleCopDiagnostic"/></param>
100+
/// <returns>A <see cref="StyleCopDiagnostic"/> that is populated with the data stored in <paramref name="value"/>.</returns>
101+
public static StyleCopDiagnostic FromJson(string value)
102+
{
103+
return JsonConvert.DeserializeObject<StyleCopDiagnostic>(value);
104+
}
105+
92106
/// <summary>
93107
/// Returns a string representing this diagnostic
94108
/// </summary>
@@ -108,16 +122,5 @@ public string ToJson()
108122
{
109123
return JsonConvert.SerializeObject(this);
110124
}
111-
112-
/// <summary>
113-
/// Creates an instance of the <see cref="StyleCopDiagnostic"/> class
114-
/// that is populated with the data stored in <paramref name="value"/>.
115-
/// </summary>
116-
/// <param name="value">A json representing a <see cref="StyleCopDiagnostic"/></param>
117-
/// <returns>A <see cref="StyleCopDiagnostic"/> that is populated with the data stored in <paramref name="value"/>.</returns>
118-
public static StyleCopDiagnostic FromJson(string value)
119-
{
120-
return JsonConvert.DeserializeObject<StyleCopDiagnostic>(value);
121-
}
122125
}
123126
}

StyleCop.Analyzers.Status.Generator/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="1.0.0" targetFramework="net452" />
99
<package id="Microsoft.Composition" version="1.0.27" targetFramework="net45" />
1010
<package id="Newtonsoft.Json" version="7.0.1-beta3" targetFramework="net45" />
11-
<package id="StyleCop.Analyzers" version="1.0.0-alpha007" targetFramework="net45" />
11+
<package id="StyleCop.Analyzers" version="1.0.0-beta015" targetFramework="net452" developmentDependency="true" />
1212
<package id="System.Collections.Immutable" version="1.1.36" targetFramework="net452" />
1313
<package id="System.Reflection.Metadata" version="1.0.21" targetFramework="net452" />
1414
</packages>

StyleCop.Analyzers.Status.Web/App_Start/BundleConfig.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
namespace StyleCop.Analyzers.Status.Web
1+
// Copyright (c) Dennis Fischer. All Rights Reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Status.Web
25
{
36
using System.Web.Optimization;
47

5-
public class BundleConfig
8+
internal class BundleConfig
69
{
7-
public static void RegisterBundles(BundleCollection bundles)
10+
internal static void RegisterBundles(BundleCollection bundles)
811
{
912
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
1013
"~/Scripts/jquery-{version}.js"));

0 commit comments

Comments
 (0)