├── .gitignore ├── LICENSE ├── MoreEffectiveAnalyzers.sln ├── MoreEffectiveAnalyzers ├── MoreEffectiveAnalyzers.Test │ ├── 03-DesignPractices │ │ └── 24-NonVirtualEventsTests.cs │ ├── Helpers │ │ ├── CodeFixVerifier.Helper.cs │ │ ├── DiagnosticResult.cs │ │ └── DiagnosticVerifier.Helper.cs │ ├── MoreEffectiveAnalyzers.Test.csproj │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Verifiers │ │ ├── CodeFixVerifier.cs │ │ └── DiagnosticVerifier.cs │ └── packages.config ├── MoreEffectiveAnalyzers.Vsix │ ├── MoreEffectiveAnalyzers.Vsix.csproj │ └── source.extension.vsixmanifest └── MoreEffectiveAnalyzers │ ├── DeclareOnlyNonVirtualEventsAnalyzer.cs │ ├── DeclareOnlyNonVirtualEventsCodeFixProvider.cs │ ├── Diagnostic.nuspec │ ├── MoreEffectiveAnalyzers.csproj │ ├── Properties │ └── AssemblyInfo.cs │ ├── ReadMe.txt │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── packages.config │ └── tools │ ├── install.ps1 │ └── uninstall.ps1 └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studo 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | *_i.c 42 | *_p.c 43 | *_i.h 44 | *.ilk 45 | *.meta 46 | *.obj 47 | *.pch 48 | *.pdb 49 | *.pgc 50 | *.pgd 51 | *.rsp 52 | *.sbr 53 | *.tlb 54 | *.tli 55 | *.tlh 56 | *.tmp 57 | *.tmp_proj 58 | *.log 59 | *.vspscc 60 | *.vssscc 61 | .builds 62 | *.pidb 63 | *.svclog 64 | *.scc 65 | 66 | # Chutzpah Test files 67 | _Chutzpah* 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | *.cachefile 76 | 77 | # Visual Studio profiler 78 | *.psess 79 | *.vsp 80 | *.vspx 81 | 82 | # TFS 2012 Local Workspace 83 | $tf/ 84 | 85 | # Guidance Automation Toolkit 86 | *.gpState 87 | 88 | # ReSharper is a .NET coding add-in 89 | _ReSharper*/ 90 | *.[Rr]e[Ss]harper 91 | *.DotSettings.user 92 | 93 | # JustCode is a .NET coding addin-in 94 | .JustCode 95 | 96 | # TeamCity is a build add-in 97 | _TeamCity* 98 | 99 | # DotCover is a Code Coverage Tool 100 | *.dotCover 101 | 102 | # NCrunch 103 | _NCrunch_* 104 | .*crunch*.local.xml 105 | 106 | # MightyMoose 107 | *.mm.* 108 | AutoTest.Net/ 109 | 110 | # Web workbench (sass) 111 | .sass-cache/ 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.[Pp]ublish.xml 131 | *.azurePubxml 132 | # TODO: Comment the next line if you want to checkin your web deploy settings 133 | # but database connection strings (with potential passwords) will be unencrypted 134 | *.pubxml 135 | *.publishproj 136 | 137 | # NuGet Packages 138 | *.nupkg 139 | # The packages folder can be ignored because of Package Restore 140 | **/packages/* 141 | # except build/, which is used as an MSBuild target. 142 | !**/packages/build/ 143 | # Uncomment if necessary however generally it will be regenerated when needed 144 | #!**/packages/repositories.config 145 | 146 | # Windows Azure Build Output 147 | csx/ 148 | *.build.csdef 149 | 150 | # Windows Store app package directory 151 | AppPackages/ 152 | 153 | # Others 154 | *.[Cc]ache 155 | ClientBin/ 156 | [Ss]tyle[Cc]op.* 157 | ~$* 158 | *~ 159 | *.dbmdl 160 | *.dbproj.schemaview 161 | *.pfx 162 | *.publishsettings 163 | node_modules/ 164 | bower_components/ 165 | 166 | # RIA/Silverlight projects 167 | Generated_Code/ 168 | 169 | # Backup & report files from converting an old project file 170 | # to a newer Visual Studio version. Backup files are not needed, 171 | # because we have git ;-) 172 | _UpgradeReport_Files/ 173 | Backup*/ 174 | UpgradeLog*.XML 175 | UpgradeLog*.htm 176 | 177 | # SQL Server files 178 | *.mdf 179 | *.ldf 180 | 181 | # Business Intelligence projects 182 | *.rdl.data 183 | *.bim.layout 184 | *.bim_*.settings 185 | 186 | # Microsoft Fakes 187 | FakesAssemblies/ 188 | 189 | # Node.js Tools for Visual Studio 190 | .ntvs_analysis.dat 191 | 192 | # Visual Studio 6 build log 193 | *.plg 194 | 195 | # Visual Studio 6 workspace options file 196 | *.opt 197 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Bill Wagner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.24627.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MoreEffectiveAnalyzers", "MoreEffectiveAnalyzers\MoreEffectiveAnalyzers\MoreEffectiveAnalyzers.csproj", "{9084AD0E-41A5-4C2D-BD4B-C2BC67EB6E40}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MoreEffectiveAnalyzers.Test", "MoreEffectiveAnalyzers\MoreEffectiveAnalyzers.Test\MoreEffectiveAnalyzers.Test.csproj", "{809FE515-5B7A-48A3-BF9D-5BB8AF7D470A}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MoreEffectiveAnalyzers.Vsix", "MoreEffectiveAnalyzers\MoreEffectiveAnalyzers.Vsix\MoreEffectiveAnalyzers.Vsix.csproj", "{3C3625E2-85D5-4442-B2BA-36558A01C0CB}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {9084AD0E-41A5-4C2D-BD4B-C2BC67EB6E40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {9084AD0E-41A5-4C2D-BD4B-C2BC67EB6E40}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {9084AD0E-41A5-4C2D-BD4B-C2BC67EB6E40}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {9084AD0E-41A5-4C2D-BD4B-C2BC67EB6E40}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {809FE515-5B7A-48A3-BF9D-5BB8AF7D470A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {809FE515-5B7A-48A3-BF9D-5BB8AF7D470A}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {809FE515-5B7A-48A3-BF9D-5BB8AF7D470A}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {809FE515-5B7A-48A3-BF9D-5BB8AF7D470A}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {3C3625E2-85D5-4442-B2BA-36558A01C0CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {3C3625E2-85D5-4442-B2BA-36558A01C0CB}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {3C3625E2-85D5-4442-B2BA-36558A01C0CB}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {3C3625E2-85D5-4442-B2BA-36558A01C0CB}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | EndGlobal 35 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.Test/03-DesignPractices/24-NonVirtualEventsTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.CodeFixes; 3 | using Microsoft.CodeAnalysis.Diagnostics; 4 | using Microsoft.VisualStudio.TestTools.UnitTesting; 5 | using System; 6 | using TestHelper; 7 | using MoreEffectiveAnalyzers; 8 | 9 | namespace MoreEffectiveAnalyzers.Test 10 | { 11 | [TestClass] 12 | public class DeclareOnlyNonVirtualEventsTests : CodeFixVerifier 13 | { 14 | 15 | //No diagnostics expected to show up 16 | [TestMethod] 17 | public void NoDiagnosticOnEmptySpan() 18 | { 19 | const string test = @""; 20 | 21 | VerifyCSharpDiagnostic(test); 22 | } 23 | 24 | //Diagnostic and CodeFix both triggered and checked for 25 | [TestMethod] 26 | public void SuggestAndCreateFixOnVirtualFieldLikeEvent() 27 | { 28 | const string test = @"namespace VirtualEventTestCode 29 | { 30 | public class Driver 31 | { 32 | public virtual event EventHandler OnVirtualEvent; 33 | } 34 | }"; 35 | var expected = new DiagnosticResult 36 | { 37 | Id = "MoreEffectiveAnalyzersItem24Field", 38 | Message = "Event 'OnVirtualEvent' should not be virtual", 39 | Severity = DiagnosticSeverity.Warning, 40 | Locations = 41 | new[] { 42 | new DiagnosticResultLocation("Test0.cs", 5, 54) 43 | } 44 | }; 45 | 46 | VerifyCSharpDiagnostic(test, expected); 47 | 48 | const string fixtest = @"namespace VirtualEventTestCode 49 | { 50 | public class Driver 51 | { 52 | public event EventHandler OnVirtualEvent; 53 | } 54 | }"; 55 | VerifyCSharpFix(test, fixtest); 56 | } 57 | 58 | [TestMethod] 59 | public void AddVirtualRaiseEventMethodForFieldLikeEvents() 60 | { 61 | const string test = @"namespace VirtualEventTestCode 62 | { 63 | public class Driver 64 | { 65 | public virtual event EventHandler OnVirtualEvent; 66 | } 67 | }"; 68 | var expected = new DiagnosticResult 69 | { 70 | Id = "MoreEffectiveAnalyzersItem24Field", 71 | Message = "Event 'OnVirtualEvent' should not be virtual", 72 | Severity = DiagnosticSeverity.Warning, 73 | Locations = 74 | new[] { 75 | new DiagnosticResultLocation("Test0.cs", 5, 54) 76 | } 77 | }; 78 | 79 | VerifyCSharpDiagnostic(test, expected); 80 | 81 | const string fixtest = @"namespace VirtualEventTestCode 82 | { 83 | public class Driver 84 | { 85 | public event EventHandler OnVirtualEvent; 86 | 87 | protected virtual EventArgs RaiseVirtualEvent(EventArgs args) 88 | { 89 | OnVirtualEvent?.Invoke(this, args); 90 | return args; 91 | } 92 | } 93 | }"; 94 | VerifyCSharpFix(test, fixtest, 1); 95 | } 96 | 97 | [TestMethod] 98 | public void SuggestAndCreateFixOnVirtualPropertyLikeEvent() 99 | { 100 | const string test = @"namespace VirtualEventTestCode 101 | { 102 | public class Driver 103 | { 104 | protected event EventHandler eventField; 105 | 106 | public virtual event EventHandler OnVirtualEvent 107 | { 108 | add { eventField += value; } 109 | remove { eventField -= value; } 110 | } 111 | } 112 | }"; 113 | var expected = new DiagnosticResult 114 | { 115 | Id = "MoreEffectiveAnalyzersItem24Property", 116 | Message = "Event 'OnVirtualEvent' should not be virtual", 117 | Severity = DiagnosticSeverity.Warning, 118 | Locations = 119 | new[] { 120 | new DiagnosticResultLocation("Test0.cs", 7, 54) 121 | } 122 | }; 123 | 124 | VerifyCSharpDiagnostic(test, expected); 125 | 126 | const string fixtest = @"namespace VirtualEventTestCode 127 | { 128 | public class Driver 129 | { 130 | protected event EventHandler eventField; 131 | 132 | public event EventHandler OnVirtualEvent 133 | { 134 | add { eventField += value; } 135 | remove { eventField -= value; } 136 | } 137 | } 138 | }"; 139 | VerifyCSharpFix(test, fixtest); 140 | } 141 | 142 | [TestMethod] 143 | public void AddVirtualRaiseEventMethodForPropertyLikeEvent() 144 | { 145 | const string test = @"namespace VirtualEventTestCode 146 | { 147 | public class Driver 148 | { 149 | protected event EventHandler eventField; 150 | 151 | public virtual event EventHandler OnVirtualEvent 152 | { 153 | add { eventField += value; } 154 | remove { eventField -= value; } 155 | } 156 | } 157 | }"; 158 | var expected = new DiagnosticResult 159 | { 160 | Id = "MoreEffectiveAnalyzersItem24Property", 161 | Message = "Event 'OnVirtualEvent' should not be virtual", 162 | Severity = DiagnosticSeverity.Warning, 163 | Locations = 164 | new[] { 165 | new DiagnosticResultLocation("Test0.cs", 7, 54) 166 | } 167 | }; 168 | 169 | VerifyCSharpDiagnostic(test, expected); 170 | 171 | const string fixtest = @"namespace VirtualEventTestCode 172 | { 173 | public class Driver 174 | { 175 | protected event EventHandler eventField; 176 | 177 | public event EventHandler OnVirtualEvent 178 | { 179 | add { eventField += value; } 180 | remove { eventField -= value; } 181 | } 182 | 183 | protected virtual EventArgs RaiseVirtualEvent(EventArgs args) 184 | { 185 | eventField?.Invoke(this, args); 186 | return args; 187 | } 188 | } 189 | }"; 190 | VerifyCSharpFix(test, fixtest, 1); 191 | } 192 | 193 | protected override CodeFixProvider GetCSharpCodeFixProvider() => new DeclareOnlyNonVirtuaEventsCodeFixProvider(); 194 | 195 | protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() => new DeclareOnlyNonVirtualEventsAnalyzer(); 196 | } 197 | } -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.Test/Helpers/CodeFixVerifier.Helper.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.CodeActions; 3 | using Microsoft.CodeAnalysis.Formatting; 4 | using Microsoft.CodeAnalysis.Simplification; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Threading; 8 | 9 | namespace TestHelper 10 | { 11 | /// 12 | /// Diagnostic Producer class with extra methods dealing with applying codefixes 13 | /// All methods are static 14 | /// 15 | public abstract partial class CodeFixVerifier : DiagnosticVerifier 16 | { 17 | /// 18 | /// Apply the inputted CodeAction to the inputted document. 19 | /// Meant to be used to apply codefixes. 20 | /// 21 | /// The Document to apply the fix on 22 | /// A CodeAction that will be applied to the Document. 23 | /// A Document with the changes from the CodeAction 24 | private static Document ApplyFix(Document document, CodeAction codeAction) 25 | { 26 | var operations = codeAction.GetOperationsAsync(CancellationToken.None).Result; 27 | var solution = operations.OfType().Single().ChangedSolution; 28 | return solution.GetDocument(document.Id); 29 | } 30 | 31 | /// 32 | /// Compare two collections of Diagnostics,and return a list of any new diagnostics that appear only in the second collection. 33 | /// Note: Considers Diagnostics to be the same if they have the same Ids. In the case of multiple diagnostics with the same Id in a row, 34 | /// this method may not necessarily return the new one. 35 | /// 36 | /// The Diagnostics that existed in the code before the CodeFix was applied 37 | /// The Diagnostics that exist in the code after the CodeFix was applied 38 | /// A list of Diagnostics that only surfaced in the code after the CodeFix was applied 39 | private static IEnumerable GetNewDiagnostics(IEnumerable diagnostics, IEnumerable newDiagnostics) 40 | { 41 | var oldArray = diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); 42 | var newArray = newDiagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); 43 | 44 | var oldIndex = 0; 45 | var newIndex = 0; 46 | 47 | while (newIndex < newArray.Length) 48 | { 49 | if (oldIndex < oldArray.Length && oldArray[oldIndex].Id == newArray[newIndex].Id) 50 | { 51 | ++oldIndex; 52 | ++newIndex; 53 | } 54 | else 55 | { 56 | yield return newArray[newIndex++]; 57 | } 58 | } 59 | } 60 | 61 | /// 62 | /// Get the existing compiler diagnostics on the inputted document. 63 | /// 64 | /// The Document to run the compiler diagnostic analyzers on 65 | /// The compiler diagnostics that were found in the code 66 | private static IEnumerable GetCompilerDiagnostics(Document document) => document.GetSemanticModelAsync().Result.GetDiagnostics(); 67 | 68 | /// 69 | /// Given a document, turn it into a string based on the syntax root 70 | /// 71 | /// The Document to be converted to a string 72 | /// A string containing the syntax of the Document after formatting 73 | private static string GetStringFromDocument(Document document) 74 | { 75 | var simplifiedDoc = Simplifier.ReduceAsync(document, Simplifier.Annotation).Result; 76 | var root = simplifiedDoc.GetSyntaxRootAsync().Result; 77 | root = Formatter.Format(root, Formatter.Annotation, simplifiedDoc.Project.Solution.Workspace); 78 | return root.GetText().ToString(); 79 | } 80 | } 81 | } 82 | 83 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.Test/Helpers/DiagnosticResult.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using System; 3 | 4 | namespace TestHelper 5 | { 6 | /// 7 | /// Location where the diagnostic appears, as determined by path, line number, and column number. 8 | /// 9 | public struct DiagnosticResultLocation 10 | { 11 | public DiagnosticResultLocation(string path, int line, int column) 12 | { 13 | if (line < -1) 14 | { 15 | throw new ArgumentOutOfRangeException(nameof(line), "line must be >= -1"); 16 | } 17 | 18 | if (column < -1) 19 | { 20 | throw new ArgumentOutOfRangeException(nameof(line), "column must be >= -1"); 21 | } 22 | 23 | this.Path = path; 24 | this.Line = line; 25 | this.Column = column; 26 | } 27 | 28 | public string Path { get; } 29 | public int Line { get; } 30 | public int Column { get; } 31 | } 32 | 33 | /// 34 | /// Struct that stores information about a Diagnostic appearing in a source 35 | /// 36 | public struct DiagnosticResult 37 | { 38 | private DiagnosticResultLocation[] locations; 39 | 40 | public DiagnosticResultLocation[] Locations 41 | { 42 | get 43 | { 44 | if (this.locations == null) 45 | { 46 | this.locations = new DiagnosticResultLocation[] { }; 47 | } 48 | return this.locations; 49 | } 50 | 51 | set 52 | { 53 | this.locations = value; 54 | } 55 | } 56 | 57 | public DiagnosticSeverity Severity { get; set; } 58 | 59 | public string Id { get; set; } 60 | 61 | public string Message { get; set; } 62 | 63 | public string Path => this.Locations.Length > 0 ? this.Locations[0].Path : ""; 64 | 65 | public int Line => this.Locations.Length > 0 ? this.Locations[0].Line : -1; 66 | 67 | public int Column => this.Locations.Length > 0 ? this.Locations[0].Column : -1; 68 | } 69 | } -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.Test/Helpers/DiagnosticVerifier.Helper.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.CSharp; 3 | using Microsoft.CodeAnalysis.Diagnostics; 4 | using Microsoft.CodeAnalysis.Text; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Collections.Immutable; 8 | using System.Linq; 9 | 10 | namespace TestHelper 11 | { 12 | /// 13 | /// Class for turning strings into documents and getting the diagnostics on them 14 | /// All methods are static 15 | /// 16 | public abstract partial class DiagnosticVerifier 17 | { 18 | private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); 19 | private static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location); 20 | private static readonly MetadataReference CSharpSymbolsReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location); 21 | private static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location); 22 | 23 | internal static readonly string DefaultFilePathPrefix = @"Test"; 24 | internal static readonly string CSharpDefaultFileExt = "cs"; 25 | internal static readonly string VisualBasicDefaultExt = "vb"; 26 | internal static readonly string TestProjectName = "TestProject"; 27 | 28 | #region Get Diagnostics 29 | 30 | /// 31 | /// Given classes in the form of strings, their language, and an IDiagnosticAnlayzer to apply to it, return the diagnostics found in the string after converting it to a document. 32 | /// 33 | /// Classes in the form of strings 34 | /// The language the source classes are in 35 | /// The analyzer to be run on the sources 36 | /// An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location 37 | private static Diagnostic[] GetSortedDiagnostics(string[] sources, string language, DiagnosticAnalyzer analyzer) => 38 | GetSortedDiagnosticsFromDocuments(analyzer, GetDocuments(sources, language)); 39 | 40 | /// 41 | /// Given an analyzer and a document to apply it to, run the analyzer and gather an array of diagnostics found in it. 42 | /// The returned diagnostics are then ordered by location in the source document. 43 | /// 44 | /// The analyzer to run on the documents 45 | /// The Documents that the analyzer will be run on 46 | /// An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location 47 | protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer, Document[] documents) 48 | { 49 | var projects = new HashSet(); 50 | foreach (var document in documents) 51 | { 52 | projects.Add(document.Project); 53 | } 54 | 55 | var diagnostics = new List(); 56 | foreach (var project in projects) 57 | { 58 | var compilationWithAnalyzers = project.GetCompilationAsync().Result.WithAnalyzers(ImmutableArray.Create(analyzer)); 59 | var diags = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().Result; 60 | foreach (var diag in diags) 61 | { 62 | if (diag.Location == Location.None || diag.Location.IsInMetadata) 63 | { 64 | diagnostics.Add(diag); 65 | } 66 | else 67 | { 68 | foreach(var document in documents) 69 | { 70 | var tree = document.GetSyntaxTreeAsync().Result; 71 | if (tree == diag.Location.SourceTree) 72 | { 73 | diagnostics.Add(diag); 74 | } 75 | } 76 | } 77 | } 78 | } 79 | 80 | var results = SortDiagnostics(diagnostics); 81 | diagnostics.Clear(); 82 | return results; 83 | } 84 | 85 | /// 86 | /// Sort diagnostics by location in source document 87 | /// 88 | /// The list of Diagnostics to be sorted 89 | /// An IEnumerable containing the Diagnostics in order of Location 90 | private static Diagnostic[] SortDiagnostics(IEnumerable diagnostics) => 91 | diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); 92 | 93 | #endregion 94 | 95 | #region Set up compilation and documents 96 | /// 97 | /// Given an array of strings as sources and a language, turn them into a project and return the documents and spans of it. 98 | /// 99 | /// Classes in the form of strings 100 | /// The language the source code is in 101 | /// A Tuple containing the Documents produced from the sources and their TextSpans if relevant 102 | private static Document[] GetDocuments(string[] sources, string language) 103 | { 104 | if (language != LanguageNames.CSharp && language != LanguageNames.VisualBasic) 105 | { 106 | throw new ArgumentException("Unsupported Language"); 107 | } 108 | 109 | var project = CreateProject(sources, language); 110 | var documents = project.Documents.ToArray(); 111 | 112 | if (sources.Length != documents.Length) 113 | { 114 | throw new SystemException("Amount of sources did not match amount of Documents created"); 115 | } 116 | 117 | return documents; 118 | } 119 | 120 | /// 121 | /// Create a Document from a string through creating a project that contains it. 122 | /// 123 | /// Classes in the form of a string 124 | /// The language the source code is in 125 | /// A Document created from the source string 126 | protected static Document CreateDocument(string source, string language = LanguageNames.CSharp) => 127 | CreateProject(new[] { source }, language).Documents.First(); 128 | 129 | /// 130 | /// Create a project using the inputted strings as sources. 131 | /// 132 | /// Classes in the form of strings 133 | /// The language the source code is in 134 | /// A Project created out of the Documents created from the source strings 135 | private static Project CreateProject(string[] sources, string language = LanguageNames.CSharp) 136 | { 137 | var fileNamePrefix = DefaultFilePathPrefix; 138 | var fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; 139 | 140 | var projectId = ProjectId.CreateNewId(debugName: TestProjectName); 141 | 142 | using (var workspace = new AdhocWorkspace()) 143 | { 144 | var solution = workspace 145 | .CurrentSolution 146 | .AddProject(projectId, TestProjectName, TestProjectName, language) 147 | .AddMetadataReference(projectId, CorlibReference) 148 | .AddMetadataReference(projectId, SystemCoreReference) 149 | .AddMetadataReference(projectId, CSharpSymbolsReference) 150 | .AddMetadataReference(projectId, CodeAnalysisReference); 151 | 152 | var count = 0; 153 | foreach (var source in sources) 154 | { 155 | var newFileName = fileNamePrefix + count + "." + fileExt; 156 | var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); 157 | solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); 158 | count++; 159 | } 160 | return solution.GetProject(projectId); 161 | } 162 | } 163 | #endregion 164 | } 165 | } 166 | 167 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.Test/MoreEffectiveAnalyzers.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | 8.0.30703 7 | 2.0 8 | {809FE515-5B7A-48A3-BF9D-5BB8AF7D470A} 9 | Library 10 | Properties 11 | MoreEffectiveAnalyzers.Test 12 | MoreEffectiveAnalyzers.Test 13 | v4.5.2 14 | 512 15 | 16 | 17 | 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | false 26 | 27 | 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | false 35 | 36 | 37 | 38 | ..\..\packages\Microsoft.CodeAnalysis.Common.1.0.0\lib\net45\Microsoft.CodeAnalysis.dll 39 | True 40 | 41 | 42 | ..\..\packages\Microsoft.CodeAnalysis.CSharp.1.0.0\lib\net45\Microsoft.CodeAnalysis.CSharp.dll 43 | True 44 | 45 | 46 | ..\..\packages\Microsoft.CodeAnalysis.CSharp.Workspaces.1.0.0\lib\net45\Microsoft.CodeAnalysis.CSharp.Workspaces.dll 47 | True 48 | 49 | 50 | ..\..\packages\Microsoft.CodeAnalysis.VisualBasic.1.0.0\lib\net45\Microsoft.CodeAnalysis.VisualBasic.dll 51 | True 52 | 53 | 54 | ..\..\packages\Microsoft.CodeAnalysis.VisualBasic.Workspaces.1.0.0\lib\net45\Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll 55 | True 56 | 57 | 58 | ..\..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.0\lib\net45\Microsoft.CodeAnalysis.Workspaces.dll 59 | True 60 | 61 | 62 | ..\..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.0\lib\net45\Microsoft.CodeAnalysis.Workspaces.Desktop.dll 63 | True 64 | 65 | 66 | 67 | ..\..\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll 68 | True 69 | 70 | 71 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.AttributedModel.dll 72 | True 73 | 74 | 75 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Convention.dll 76 | True 77 | 78 | 79 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Hosting.dll 80 | True 81 | 82 | 83 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Runtime.dll 84 | True 85 | 86 | 87 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.TypedParts.dll 88 | True 89 | 90 | 91 | 92 | ..\..\packages\System.Reflection.Metadata.1.0.21\lib\portable-net45+win8\System.Reflection.Metadata.dll 93 | True 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | false 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | {9084AD0E-41A5-4C2D-BD4B-C2BC67EB6E40} 119 | MoreEffectiveAnalyzers 120 | 121 | 122 | 123 | 124 | 125 | 126 | 133 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.Test/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("MoreEffectiveAnalyzers.Test")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("MoreEffectiveAnalyzers.Test")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // Version information for an assembly consists of the following four values: 22 | // 23 | // Major Version 24 | // Minor Version 25 | // Build Number 26 | // Revision 27 | // 28 | // You can specify all the values or you can default the Build and Revision Numbers 29 | // by using the '*' as shown below: 30 | // [assembly: AssemblyVersion("1.0.*")] 31 | [assembly: AssemblyVersion("1.0.0.0")] 32 | [assembly: AssemblyFileVersion("1.0.0.0")] -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.Test/Verifiers/CodeFixVerifier.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.CodeActions; 3 | using Microsoft.CodeAnalysis.CodeFixes; 4 | using Microsoft.CodeAnalysis.Diagnostics; 5 | using Microsoft.CodeAnalysis.Formatting; 6 | using Microsoft.VisualStudio.TestTools.UnitTesting; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Threading; 10 | 11 | namespace TestHelper 12 | { 13 | /// 14 | /// Superclass of all Unit tests made for diagnostics with codefixes. 15 | /// Contains methods used to verify correctness of codefixes 16 | /// 17 | public abstract partial class CodeFixVerifier : DiagnosticVerifier 18 | { 19 | /// 20 | /// Returns the codefix being tested (C#) - to be implemented in non-abstract class 21 | /// 22 | /// The CodeFixProvider to be used for CSharp code 23 | protected virtual CodeFixProvider GetCSharpCodeFixProvider() => null; 24 | 25 | /// 26 | /// Returns the codefix being tested (VB) - to be implemented in non-abstract class 27 | /// 28 | /// The CodeFixProvider to be used for VisualBasic code 29 | protected virtual CodeFixProvider GetBasicCodeFixProvider() => null; 30 | 31 | /// 32 | /// Called to test a C# codefix when applied on the inputted string as a source 33 | /// 34 | /// A class in the form of a string before the CodeFix was applied to it 35 | /// A class in the form of a string after the CodeFix was applied to it 36 | /// Index determining which codefix to apply if there are multiple 37 | /// A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied 38 | protected void VerifyCSharpFix(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false) 39 | { 40 | VerifyFix(LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), GetCSharpCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics); 41 | } 42 | 43 | /// 44 | /// Called to test a VB codefix when applied on the inputted string as a source 45 | /// 46 | /// A class in the form of a string before the CodeFix was applied to it 47 | /// A class in the form of a string after the CodeFix was applied to it 48 | /// Index determining which codefix to apply if there are multiple 49 | /// A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied 50 | protected void VerifyBasicFix(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false) 51 | { 52 | VerifyFix(LanguageNames.VisualBasic, GetBasicDiagnosticAnalyzer(), GetBasicCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics); 53 | } 54 | 55 | /// 56 | /// General verifier for codefixes. 57 | /// Creates a Document from the source string, then gets diagnostics on it and applies the relevant codefixes. 58 | /// Then gets the string after the codefix is applied and compares it with the expected result. 59 | /// Note: If any codefix causes new diagnostics to show up, the test fails unless allowNewCompilerDiagnostics is set to true. 60 | /// 61 | /// The language the source code is in 62 | /// The analyzer to be applied to the source code 63 | /// The codefix to be applied to the code wherever the relevant Diagnostic is found 64 | /// A class in the form of a string before the CodeFix was applied to it 65 | /// A class in the form of a string after the CodeFix was applied to it 66 | /// Index determining which codefix to apply if there are multiple 67 | /// A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied 68 | private static void VerifyFix(string language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex, bool allowNewCompilerDiagnostics) 69 | { 70 | var document = CreateDocument(oldSource, language); 71 | var analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }); 72 | var compilerDiagnostics = GetCompilerDiagnostics(document); 73 | var attempts = analyzerDiagnostics.Length; 74 | 75 | for (int i = 0; i < attempts; ++i) 76 | { 77 | var actions = new List(); 78 | var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None); 79 | codeFixProvider.RegisterCodeFixesAsync(context).Wait(); 80 | 81 | if (!actions.Any()) 82 | { 83 | break; 84 | } 85 | 86 | if (codeFixIndex != null) 87 | { 88 | document = ApplyFix(document, actions.ElementAt((int)codeFixIndex)); 89 | break; 90 | } 91 | 92 | document = ApplyFix(document, actions.ElementAt(0)); 93 | analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }); 94 | 95 | var newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document)); 96 | 97 | //check if applying the code fix introduced any new compiler diagnostics 98 | if (!allowNewCompilerDiagnostics && newCompilerDiagnostics.Any()) 99 | { 100 | // Format and get the compiler diagnostics again so that the locations make sense in the output 101 | document = document.WithSyntaxRoot(Formatter.Format(document.GetSyntaxRootAsync().Result, Formatter.Annotation, document.Project.Solution.Workspace)); 102 | newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document)); 103 | 104 | Assert.IsTrue(false, 105 | $"Fix introduced new compiler diagnostics:\r\n{string.Join("\r\n", newCompilerDiagnostics.Select(d => d.ToString()))}\r\n\r\nNew document:\r\n{document.GetSyntaxRootAsync().Result.ToFullString()}\r\n"); 106 | } 107 | 108 | //check if there are analyzer diagnostics left after the code fix 109 | if (!analyzerDiagnostics.Any()) 110 | { 111 | break; 112 | } 113 | } 114 | 115 | //after applying all of the code fixes, compare the resulting string to the inputted one 116 | var actual = GetStringFromDocument(document); 117 | Assert.AreEqual(newSource, actual); 118 | } 119 | } 120 | } -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.Test/Verifiers/DiagnosticVerifier.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.CSharp; 3 | using Microsoft.CodeAnalysis.Diagnostics; 4 | using Microsoft.VisualStudio.TestTools.UnitTesting; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | 9 | namespace TestHelper 10 | { 11 | /// 12 | /// Superclass of all Unit Tests for DiagnosticAnalyzers 13 | /// 14 | public abstract partial class DiagnosticVerifier 15 | { 16 | #region To be implemented by Test classes 17 | /// 18 | /// Get the CSharp analyzer being tested - to be implemented in non-abstract class 19 | /// 20 | protected virtual DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() => null; 21 | 22 | /// 23 | /// Get the Visual Basic analyzer being tested (C#) - to be implemented in non-abstract class 24 | /// 25 | protected virtual DiagnosticAnalyzer GetBasicDiagnosticAnalyzer() => null; 26 | #endregion 27 | 28 | #region Verifier wrappers 29 | 30 | /// 31 | /// Called to test a C# DiagnosticAnalyzer when applied on the single inputted string as a source 32 | /// Note: input a DiagnosticResult for each Diagnostic expected 33 | /// 34 | /// A class in the form of a string to run the analyzer on 35 | /// DiagnosticResults that should appear after the analyzer is run on the source 36 | protected void VerifyCSharpDiagnostic(string source, params DiagnosticResult[] expected) 37 | { 38 | VerifyDiagnostics(new[] { source }, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), expected); 39 | } 40 | 41 | /// 42 | /// Called to test a VB DiagnosticAnalyzer when applied on the single inputted string as a source 43 | /// Note: input a DiagnosticResult for each Diagnostic expected 44 | /// 45 | /// A class in the form of a string to run the analyzer on 46 | /// DiagnosticResults that should appear after the analyzer is run on the source 47 | protected void VerifyBasicDiagnostic(string source, params DiagnosticResult[] expected) 48 | { 49 | VerifyDiagnostics(new[] { source }, LanguageNames.VisualBasic, GetBasicDiagnosticAnalyzer(), expected); 50 | } 51 | 52 | /// 53 | /// Called to test a C# DiagnosticAnalyzer when applied on the inputted strings as a source 54 | /// Note: input a DiagnosticResult for each Diagnostic expected 55 | /// 56 | /// An array of strings to create source documents from to run the analyzers on 57 | /// DiagnosticResults that should appear after the analyzer is run on the sources 58 | protected void VerifyCSharpDiagnostic(string[] sources, params DiagnosticResult[] expected) 59 | { 60 | VerifyDiagnostics(sources, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), expected); 61 | } 62 | 63 | /// 64 | /// Called to test a VB DiagnosticAnalyzer when applied on the inputted strings as a source 65 | /// Note: input a DiagnosticResult for each Diagnostic expected 66 | /// 67 | /// An array of strings to create source documents from to run the analyzers on 68 | /// DiagnosticResults that should appear after the analyzer is run on the sources 69 | protected void VerifyBasicDiagnostic(string[] sources, params DiagnosticResult[] expected) 70 | { 71 | VerifyDiagnostics(sources, LanguageNames.VisualBasic, GetBasicDiagnosticAnalyzer(), expected); 72 | } 73 | 74 | /// 75 | /// General method that gets a collection of actual diagnostics found in the source after the analyzer is run, 76 | /// then verifies each of them. 77 | /// 78 | /// An array of strings to create source documents from to run the analyzers on 79 | /// The language of the classes represented by the source strings 80 | /// The analyzer to be run on the source code 81 | /// DiagnosticResults that should appear after the analyzer is run on the sources 82 | private static void VerifyDiagnostics(string[] sources, string language, DiagnosticAnalyzer analyzer, params DiagnosticResult[] expected) 83 | { 84 | var diagnostics = GetSortedDiagnostics(sources, language, analyzer); 85 | VerifyDiagnosticResults(diagnostics, analyzer, expected); 86 | } 87 | 88 | #endregion 89 | 90 | #region Actual comparisons and verifications 91 | /// 92 | /// Checks each of the actual Diagnostics found and compares them with the corresponding DiagnosticResult in the array of expected results. 93 | /// Diagnostics are considered equal only if the DiagnosticResultLocation, Id, Severity, and Message of the DiagnosticResult match the actual diagnostic. 94 | /// 95 | /// The Diagnostics found by the compiler after running the analyzer on the source code 96 | /// The analyzer that was being run on the sources 97 | /// Diagnostic Results that should have appeared in the code 98 | private static void VerifyDiagnosticResults(IEnumerable actualResults, DiagnosticAnalyzer analyzer, params DiagnosticResult[] expectedResults) 99 | { 100 | var expectedCount = expectedResults.Count(); 101 | var actualCount = actualResults.Count(); 102 | 103 | if (expectedCount != actualCount) 104 | { 105 | var diagnosticsOutput = actualResults.Any() ? FormatDiagnostics(analyzer, actualResults.ToArray()) : " NONE."; 106 | 107 | Assert.IsTrue(false, 108 | $"Mismatch between number of diagnostics returned, expected \"{expectedCount}\" actual \"{actualCount}\"\r\n\r\nDiagnostics:\r\n{diagnosticsOutput}\r\n"); 109 | } 110 | 111 | for (int i = 0; i < expectedResults.Length; i++) 112 | { 113 | var actual = actualResults.ElementAt(i); 114 | var expected = expectedResults[i]; 115 | 116 | if (expected.Line == -1 && expected.Column == -1) 117 | { 118 | if (actual.Location != Location.None) 119 | { 120 | Assert.IsTrue(false, 121 | $"Expected:\nA project diagnostic with No location\nActual:\n{FormatDiagnostics(analyzer, actual)}"); 122 | } 123 | } 124 | else 125 | { 126 | VerifyDiagnosticLocation(analyzer, actual, actual.Location, expected.Locations.First()); 127 | var additionalLocations = actual.AdditionalLocations.ToArray(); 128 | 129 | if (additionalLocations.Length != expected.Locations.Length - 1) 130 | { 131 | Assert.IsTrue(false, 132 | $"Expected {expected.Locations.Length - 1} additional locations but got {additionalLocations.Length} for Diagnostic:\r\n {FormatDiagnostics(analyzer, actual)}\r\n"); 133 | } 134 | 135 | for (int j = 0; j < additionalLocations.Length; ++j) 136 | { 137 | VerifyDiagnosticLocation(analyzer, actual, additionalLocations[j], expected.Locations[j + 1]); 138 | } 139 | } 140 | 141 | if (actual.Id != expected.Id) 142 | { 143 | Assert.IsTrue(false, 144 | $"Expected diagnostic id to be \"{expected.Id}\" was \"{actual.Id}\"\r\n\r\nDiagnostic:\r\n {FormatDiagnostics(analyzer, actual)}\r\n"); 145 | } 146 | 147 | if (actual.Severity != expected.Severity) 148 | { 149 | Assert.IsTrue(false, 150 | $"Expected diagnostic severity to be \"{expected.Severity}\" was \"{actual.Severity}\"\r\n\r\nDiagnostic:\r\n {FormatDiagnostics(analyzer, actual)}\r\n"); 151 | } 152 | 153 | if (actual.GetMessage() != expected.Message) 154 | { 155 | Assert.IsTrue(false, 156 | $"Expected diagnostic message to be \"{expected.Message}\" was \"{actual.GetMessage()}\"\r\n\r\nDiagnostic:\r\n {FormatDiagnostics(analyzer, actual)}\r\n"); 157 | } 158 | } 159 | } 160 | 161 | /// 162 | /// Helper method to VerifyDiagnosticResult that checks the location of a diagnostic and compares it with the location in the expected DiagnosticResult. 163 | /// 164 | /// The analyzer that was being run on the sources 165 | /// The diagnostic that was found in the code 166 | /// The Location of the Diagnostic found in the code 167 | /// The DiagnosticResultLocation that should have been found 168 | private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer, Diagnostic diagnostic, Location actual, DiagnosticResultLocation expected) 169 | { 170 | var actualSpan = actual.GetLineSpan(); 171 | 172 | Assert.IsTrue(actualSpan.Path == expected.Path || (actualSpan.Path != null && actualSpan.Path.Contains("Test0.") && expected.Path.Contains("Test.")), 173 | $"Expected diagnostic to be in file \"{expected.Path}\" was actually in file \"{actualSpan.Path}\"\r\n\r\nDiagnostic:\r\n {FormatDiagnostics(analyzer, diagnostic)}\r\n"); 174 | 175 | var actualLinePosition = actualSpan.StartLinePosition; 176 | 177 | // Only check line position if there is an actual line in the real diagnostic 178 | if (actualLinePosition.Line > 0) 179 | { 180 | if (actualLinePosition.Line + 1 != expected.Line) 181 | { 182 | Assert.IsTrue(false, 183 | $"Expected diagnostic to be on line \"{expected.Line}\" was actually on line \"{actualLinePosition.Line + 1}\"\r\n\r\nDiagnostic:\r\n {FormatDiagnostics(analyzer, diagnostic)}\r\n"); 184 | } 185 | } 186 | 187 | // Only check column position if there is an actual column position in the real diagnostic 188 | if (actualLinePosition.Character > 0) 189 | { 190 | if (actualLinePosition.Character + 1 != expected.Column) 191 | { 192 | Assert.IsTrue(false, 193 | $"Expected diagnostic to start at column \"{expected.Column}\" was actually at column \"{actualLinePosition.Character + 1}\"\r\n\r\nDiagnostic:\r\n {FormatDiagnostics(analyzer, diagnostic)}\r\n"); 194 | } 195 | } 196 | } 197 | #endregion 198 | 199 | #region Formatting Diagnostics 200 | /// 201 | /// Helper method to format a Diagnostic into an easily readable string 202 | /// 203 | /// The analyzer that this verifier tests 204 | /// The Diagnostics to be formatted 205 | /// The Diagnostics formatted as a string 206 | private static string FormatDiagnostics(DiagnosticAnalyzer analyzer, params Diagnostic[] diagnostics) 207 | { 208 | var builder = new StringBuilder(); 209 | for (int i = 0; i < diagnostics.Length; ++i) 210 | { 211 | builder.AppendLine("// " + diagnostics[i].ToString()); 212 | 213 | var analyzerType = analyzer.GetType(); 214 | var rules = analyzer.SupportedDiagnostics; 215 | 216 | foreach (var rule in rules) 217 | { 218 | if (rule != null && rule.Id == diagnostics[i].Id) 219 | { 220 | var location = diagnostics[i].Location; 221 | if (location == Location.None) 222 | { 223 | builder.AppendFormat("GetGlobalResult({0}.{1})", analyzerType.Name, rule.Id); 224 | } 225 | else 226 | { 227 | Assert.IsTrue(location.IsInSource, 228 | $"Test base does not currently handle diagnostics in metadata locations. Diagnostic in metadata: {diagnostics[i]}\r\n"); 229 | 230 | var resultMethodName = diagnostics[i].Location.SourceTree.FilePath.EndsWith(".cs") ? "GetCSharpResultAt" : "GetBasicResultAt"; 231 | var linePosition = diagnostics[i].Location.GetLineSpan().StartLinePosition; 232 | 233 | builder.AppendFormat("{0}({1}, {2}, {3}.{4})", 234 | resultMethodName, 235 | linePosition.Line + 1, 236 | linePosition.Character + 1, 237 | analyzerType.Name, 238 | rule.Id); 239 | } 240 | 241 | if (i != diagnostics.Length - 1) 242 | { 243 | builder.Append(','); 244 | } 245 | 246 | builder.AppendLine(); 247 | break; 248 | } 249 | } 250 | } 251 | return builder.ToString(); 252 | } 253 | #endregion 254 | } 255 | } 256 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.Test/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.Vsix/MoreEffectiveAnalyzers.Vsix.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 14.0 5 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 6 | 7 | 8 | 9 | Debug 10 | AnyCPU 11 | 2.0 12 | {82b43b9b-a64c-4715-b499-d71e9ca2bd60};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 13 | {3C3625E2-85D5-4442-B2BA-36558A01C0CB} 14 | Library 15 | Properties 16 | MoreEffectiveAnalyzers 17 | MoreEffectiveAnalyzers 18 | v4.5.2 19 | false 20 | false 21 | false 22 | false 23 | false 24 | false 25 | Roslyn 26 | 27 | 28 | true 29 | full 30 | false 31 | bin\Debug\ 32 | DEBUG;TRACE 33 | prompt 34 | 4 35 | 36 | 37 | pdbonly 38 | true 39 | bin\Release\ 40 | TRACE 41 | prompt 42 | 4 43 | 44 | 45 | Program 46 | $(DevEnvDir)devenv.exe 47 | /rootsuffix Roslyn 48 | 49 | 50 | 51 | Designer 52 | 53 | 54 | 55 | 56 | {9084AD0E-41A5-4C2D-BD4B-C2BC67EB6E40} 57 | MoreEffectiveAnalyzers 58 | 59 | 60 | 61 | 62 | 69 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.Vsix/source.extension.vsixmanifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | MoreEffectiveAnalyzers.Vsix 6 | This is a sample diagnostic extension for the .NET Compiler Platform ("Roslyn"). 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers/DeclareOnlyNonVirtualEventsAnalyzer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Immutable; 2 | using System.Linq; 3 | using Microsoft.CodeAnalysis; 4 | using Microsoft.CodeAnalysis.CSharp; 5 | using Microsoft.CodeAnalysis.CSharp.Syntax; 6 | using Microsoft.CodeAnalysis.Diagnostics; 7 | 8 | namespace MoreEffectiveAnalyzers 9 | { 10 | [DiagnosticAnalyzer(LanguageNames.CSharp)] 11 | public class DeclareOnlyNonVirtualEventsAnalyzer : DiagnosticAnalyzer 12 | { 13 | public const string FieldEventDiagnosticId = "MoreEffectiveAnalyzersItem24Field"; 14 | public const string PropertyEventDiagnosticId = "MoreEffectiveAnalyzersItem24Property"; 15 | 16 | // You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat. 17 | private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.AnalyzerTitle), Resources.ResourceManager, typeof(Resources)); 18 | private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.AnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources)); 19 | private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.AnalyzerDescription), Resources.ResourceManager, typeof(Resources)); 20 | private const string Category = "DesignPractices"; 21 | 22 | private static readonly DiagnosticDescriptor RuleField = new DiagnosticDescriptor(FieldEventDiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description); 23 | private static readonly DiagnosticDescriptor RuleProperty = new DiagnosticDescriptor(PropertyEventDiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description); 24 | 25 | public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(RuleField, RuleProperty); 26 | 27 | public override void Initialize(AnalysisContext context) 28 | { 29 | context.RegisterSyntaxNodeAction(AnalyzeEventDeclaration, 30 | SyntaxKind.EventDeclaration, 31 | SyntaxKind.EventFieldDeclaration); 32 | } 33 | 34 | private static void AnalyzeEventDeclaration(SyntaxNodeAnalysisContext eventDeclarationSyntaxContext) 35 | { 36 | var n = eventDeclarationSyntaxContext.Node; 37 | var modifiers = default(SyntaxTokenList); 38 | var eventName = default(string); 39 | var location = default(Location); 40 | var descriptor = default(DiagnosticDescriptor); 41 | if (n.Kind() == SyntaxKind.EventFieldDeclaration) 42 | { 43 | var eventNode = eventDeclarationSyntaxContext.Node as EventFieldDeclarationSyntax; 44 | modifiers = eventNode.Modifiers; 45 | var variable = eventNode.Declaration.Variables.Single(); 46 | eventName = variable.Identifier.ValueText; 47 | location = variable.GetLocation(); 48 | descriptor = RuleField; 49 | } 50 | else if (n.Kind() == SyntaxKind.EventDeclaration) 51 | { 52 | var eventNode = eventDeclarationSyntaxContext.Node as EventDeclarationSyntax; 53 | eventName = eventNode.Identifier.ValueText; 54 | modifiers = eventNode.Modifiers; 55 | location = eventNode.Identifier.GetLocation(); 56 | descriptor = RuleProperty; 57 | } 58 | var isVirtual = modifiers.Any(m => m.Kind() == SyntaxKind.VirtualKeyword); 59 | if (isVirtual) 60 | { 61 | var diagnostic = Diagnostic.Create(descriptor, location, eventName); 62 | eventDeclarationSyntaxContext.ReportDiagnostic(diagnostic); 63 | } 64 | } 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers/DeclareOnlyNonVirtualEventsCodeFixProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Collections.Immutable; 4 | using System.Composition; 5 | using System.Linq; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using Microsoft.CodeAnalysis; 9 | using Microsoft.CodeAnalysis.CodeFixes; 10 | using Microsoft.CodeAnalysis.CodeActions; 11 | using Microsoft.CodeAnalysis.CSharp; 12 | using Microsoft.CodeAnalysis.CSharp.Syntax; 13 | using Microsoft.CodeAnalysis.Rename; 14 | using Microsoft.CodeAnalysis.Text; 15 | using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; 16 | 17 | namespace MoreEffectiveAnalyzers 18 | { 19 | [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DeclareOnlyNonVirtuaEventsCodeFixProvider)), Shared] 20 | public class DeclareOnlyNonVirtuaEventsCodeFixProvider : CodeFixProvider 21 | { 22 | private const string removeVirtualTitle = "Remove virtual keyword"; 23 | private const string implementVirtualRaiseEvent = "Implement Virtual Method to Raise Event"; 24 | private const string argumentName = @"args"; 25 | 26 | public sealed override ImmutableArray FixableDiagnosticIds => ImmutableArray.Create(DeclareOnlyNonVirtualEventsAnalyzer.FieldEventDiagnosticId, DeclareOnlyNonVirtualEventsAnalyzer.PropertyEventDiagnosticId); 27 | 28 | public sealed override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; 29 | 30 | public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) 31 | { 32 | var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); 33 | 34 | var diagnostic = context.Diagnostics.First(); 35 | var diagnosticSpan = diagnostic.Location.SourceSpan; 36 | 37 | // Find the type declaration identified by the diagnostic. 38 | if (diagnostic.Id == DeclareOnlyNonVirtualEventsAnalyzer.FieldEventDiagnosticId) 39 | { 40 | var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf() 41 | .OfType().First(); 42 | 43 | // We'll register two actions here. 44 | // One will simply remove the virtual keyword. 45 | // The second will remove the virtual keyword, 46 | // and add a virtual method to raise the event. 47 | context.RegisterCodeFix( 48 | CodeAction.Create( 49 | title: removeVirtualTitle, 50 | createChangedDocument: c => RemoveVirtualEventFieldAsync(context.Document, declaration, c), 51 | equivalenceKey: removeVirtualTitle), 52 | diagnostic); 53 | context.RegisterCodeFix( 54 | CodeAction.Create( 55 | title: implementVirtualRaiseEvent, 56 | createChangedDocument: c => ImplementVirtualRaiseEventFieldAsync(context.Document, declaration, c), 57 | equivalenceKey: implementVirtualRaiseEvent), 58 | diagnostic); 59 | } 60 | else if (diagnostic.Id == DeclareOnlyNonVirtualEventsAnalyzer.PropertyEventDiagnosticId) 61 | { 62 | var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf() 63 | .OfType().First(); 64 | 65 | // We'll register two actions here. 66 | // One will simply remove the virtual keyword. 67 | // The second will remove the virtual keyword, 68 | // and add a virtual method to raise the event. 69 | context.RegisterCodeFix( 70 | CodeAction.Create( 71 | title: removeVirtualTitle, 72 | createChangedDocument: c => RemoveVirtualEventPropertyAsync(context.Document, declaration, c), 73 | equivalenceKey: removeVirtualTitle), 74 | diagnostic); 75 | context.RegisterCodeFix( 76 | CodeAction.Create( 77 | title: implementVirtualRaiseEvent, 78 | createChangedDocument: c => ImplementVirtualEventPropertyAsync(context.Document, declaration, c), 79 | equivalenceKey: implementVirtualRaiseEvent), 80 | diagnostic); 81 | } 82 | } 83 | 84 | private async static Task ImplementVirtualEventPropertyAsync(Document document, EventDeclarationSyntax declaration, CancellationToken c) 85 | { 86 | // Need the left (IdentifierNameSyntax) of the Accessor Statement for the AssignmentExpressionSyntax 87 | var accessor = declaration.AccessorList.Accessors.First(); 88 | var statements = accessor.Body.Statements.OfType(); 89 | // Need to fine where the right side of an Add statement is "value": 90 | var eventFieldName = statements.Where(s => (s.Expression as AssignmentExpressionSyntax)?.OperatorToken.Kind() == SyntaxKind.PlusEqualsToken) 91 | .Where(s => ((s.Expression as AssignmentExpressionSyntax)?.Right as IdentifierNameSyntax)?.Identifier.ValueText == "value") 92 | .Select(s => ((s.Expression as AssignmentExpressionSyntax)?.Left as IdentifierNameSyntax)?.Identifier.ValueText).First(); 93 | 94 | var raiseMethod = CreateRaiseMethod(declaration.Identifier.ValueText, eventFieldName, (declaration.Type as GenericNameSyntax)); 95 | 96 | var root = await document.GetSyntaxRootAsync(c); 97 | var newRoot = root.InsertNodesAfter(declaration, new SyntaxNode[] { raiseMethod }); 98 | // Note that we need to find the node again 99 | declaration = newRoot.FindToken(declaration.Span.Start).Parent.AncestorsAndSelf() 100 | .OfType().First(); 101 | 102 | var modifiers = declaration.Modifiers; 103 | var virtualToken = modifiers.Single(m => m.Kind() == SyntaxKind.VirtualKeyword); 104 | 105 | var newDeclaration = declaration.ReplaceToken(virtualToken, Token(SyntaxKind.None)); 106 | newRoot = newRoot.ReplaceNode(declaration, newDeclaration 107 | .WithTrailingTrivia(TriviaList(CarriageReturnLineFeed, CarriageReturnLineFeed))); 108 | return document.WithSyntaxRoot(newRoot); 109 | } 110 | 111 | private async static Task ImplementVirtualRaiseEventFieldAsync(Document document, EventFieldDeclarationSyntax declaration, CancellationToken c) 112 | { 113 | var eventName = declaration.Declaration.Variables.Single().Identifier.ValueText; 114 | var raiseMethod = CreateRaiseMethod(eventName, eventName, 115 | (declaration.Declaration.Type as GenericNameSyntax)); 116 | 117 | var root = await document.GetSyntaxRootAsync(c); 118 | var newRoot = root.InsertNodesAfter(declaration, new SyntaxNode[] { raiseMethod }); 119 | // Note that we need to find the node again 120 | declaration = newRoot.FindToken(declaration.Span.Start).Parent.AncestorsAndSelf() 121 | .OfType().First(); 122 | 123 | var modifiers = declaration.Modifiers; 124 | var virtualToken = modifiers.Single(m => m.Kind() == SyntaxKind.VirtualKeyword); 125 | 126 | var newDeclaration = declaration.ReplaceToken(virtualToken, Token(SyntaxKind.None)); 127 | newRoot = newRoot.ReplaceNode(declaration, newDeclaration 128 | .WithTrailingTrivia(TriviaList(CarriageReturnLineFeed, CarriageReturnLineFeed))); 129 | return document.WithSyntaxRoot(newRoot); 130 | } 131 | 132 | private static Task RemoveVirtualEventPropertyAsync(Document document, EventDeclarationSyntax declaration, CancellationToken c) 133 | { 134 | var modifiers = declaration.Modifiers; 135 | var virtualToken = modifiers.Single(m => m.Kind() == SyntaxKind.VirtualKeyword); 136 | return RemoveVirtualTokenAsync(document, virtualToken, c); 137 | } 138 | 139 | private static Task RemoveVirtualEventFieldAsync(Document document, EventFieldDeclarationSyntax declaration, CancellationToken c) 140 | { 141 | var modifiers = declaration.Modifiers; 142 | var virtualToken = modifiers.Single(m => m.Kind() == SyntaxKind.VirtualKeyword); 143 | return RemoveVirtualTokenAsync(document, virtualToken, c); 144 | } 145 | 146 | private static async Task RemoveVirtualTokenAsync(Document document, SyntaxToken virtualToken, CancellationToken c) 147 | { 148 | var root = await document.GetSyntaxRootAsync(c); 149 | var newRoot = root.ReplaceToken(virtualToken, SyntaxFactory.Token(SyntaxKind.None)); 150 | return document.WithSyntaxRoot(newRoot); 151 | } 152 | 153 | private static MethodDeclarationSyntax CreateRaiseMethod(string eventName, string eventFieldName, GenericNameSyntax argType) 154 | { 155 | var shortendEventName = eventName.Replace("On", ""); 156 | var arg = (argType.TypeArgumentList.Arguments.First() as IdentifierNameSyntax); 157 | var argTypeName = arg.Identifier.ValueText; 158 | return MethodDeclaration(arg, $"Raise{shortendEventName}") 159 | .WithModifiers(TokenList( 160 | Token(SyntaxKind.ProtectedKeyword), 161 | Token(SyntaxKind.VirtualKeyword))) 162 | .WithParameterList( 163 | ParameterList(SingletonSeparatedList( 164 | Parameter(Identifier(argumentName)) 165 | .WithType(IdentifierName(argTypeName)))) 166 | .WithOpenParenToken(Token(SyntaxKind.OpenParenToken)) 167 | .WithCloseParenToken(Token(SyntaxKind.CloseParenToken))) 168 | .WithBody(Block( 169 | List( 170 | new StatementSyntax[]{ 171 | ExpressionStatement( 172 | ConditionalAccessExpression( 173 | IdentifierName(eventFieldName), 174 | InvocationExpression( 175 | MemberBindingExpression(IdentifierName(@"Invoke")) 176 | .WithOperatorToken(Token(SyntaxKind.DotToken))) 177 | .WithArgumentList(ArgumentList(SeparatedList( 178 | new SyntaxNodeOrToken[]{ 179 | Argument(ThisExpression().WithToken(Token(SyntaxKind.ThisKeyword))), 180 | Token(SyntaxKind.CommaToken), 181 | Argument(IdentifierName(argumentName)) 182 | })) 183 | .WithOpenParenToken(Token(SyntaxKind.OpenParenToken)) 184 | .WithCloseParenToken(Token(SyntaxKind.CloseParenToken)))) 185 | .WithOperatorToken(Token(SyntaxKind.QuestionToken))) 186 | .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)), 187 | ReturnStatement(IdentifierName(argumentName)) 188 | .WithReturnKeyword(Token(SyntaxKind.ReturnKeyword)) 189 | .WithSemicolonToken(Token(SyntaxKind.SemicolonToken)) 190 | })) 191 | .WithOpenBraceToken(Token(SyntaxKind.OpenBraceToken)) 192 | .WithCloseBraceToken(Token(SyntaxKind.CloseBraceToken))); 193 | } 194 | } 195 | } -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers/Diagnostic.nuspec: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | MoreEffectiveAnalyzers 5 | 1.0.0.0 6 | MoreEffectiveAnalyzers 7 | billw 8 | billw 9 | http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE 10 | http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE 11 | http://ICON_URL_HERE_OR_DELETE_THIS_LINE 12 | false 13 | MoreEffectiveAnalyzers 14 | Summary of changes made in this release of the package. 15 | Copyright 16 | MoreEffectiveAnalyzers, analyzers 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers/MoreEffectiveAnalyzers.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 11.0 6 | Debug 7 | AnyCPU 8 | {9084AD0E-41A5-4C2D-BD4B-C2BC67EB6E40} 9 | Library 10 | Properties 11 | MoreEffectiveAnalyzers 12 | MoreEffectiveAnalyzers 13 | {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | Profile7 15 | v4.5 16 | 17 | 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | 37 | 38 | 39 | True 40 | True 41 | Resources.resx 42 | 43 | 44 | 45 | 46 | ResXFileCodeGenerator 47 | Resources.Designer.cs 48 | 49 | 50 | 51 | 52 | Designer 53 | PreserveNewest 54 | 55 | 56 | 57 | PreserveNewest 58 | 59 | 60 | PreserveNewest 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | ..\..\packages\Microsoft.CodeAnalysis.Common.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.dll 71 | False 72 | 73 | 74 | ..\..\packages\Microsoft.CodeAnalysis.CSharp.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.CSharp.dll 75 | False 76 | 77 | 78 | ..\..\packages\Microsoft.CodeAnalysis.CSharp.Workspaces.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.CSharp.Workspaces.dll 79 | False 80 | 81 | 82 | ..\..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.Workspaces.dll 83 | False 84 | 85 | 86 | ..\..\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll 87 | False 88 | 89 | 90 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.AttributedModel.dll 91 | False 92 | 93 | 94 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Convention.dll 95 | False 96 | 97 | 98 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Hosting.dll 99 | False 100 | 101 | 102 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Runtime.dll 103 | False 104 | 105 | 106 | ..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.TypedParts.dll 107 | False 108 | 109 | 110 | ..\..\packages\System.Reflection.Metadata.1.0.21\lib\portable-net45+win8\System.Reflection.Metadata.dll 111 | False 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 130 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | // General Information about an assembly is controlled through the following 5 | // set of attributes. Change these attribute values to modify the information 6 | // associated with an assembly. 7 | [assembly: AssemblyTitle("MoreEffectiveAnalyzers")] 8 | [assembly: AssemblyDescription("")] 9 | [assembly: AssemblyConfiguration("")] 10 | [assembly: AssemblyCompany("")] 11 | [assembly: AssemblyProduct("MoreEffectiveAnalyzers")] 12 | [assembly: AssemblyCopyright("Copyright © 2015")] 13 | [assembly: AssemblyTrademark("")] 14 | [assembly: AssemblyCulture("")] 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // Version information for an assembly consists of the following four values: 22 | // 23 | // Major Version 24 | // Minor Version 25 | // Build Number 26 | // Revision 27 | // 28 | // You can specify all the values or you can default the Build and Revision Numbers 29 | // by using the '*' as shown below: 30 | [assembly: AssemblyVersion("1.0.*")] 31 | [assembly: AssemblyFileVersion("1.0.0.0")] 32 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers/ReadMe.txt: -------------------------------------------------------------------------------- 1 |  2 | Building this project will produce an analyzer .dll, as well as the 3 | following two ways you may wish to package that analyzer: 4 | * A NuGet package (.nupkg file) that will add your assembly as a 5 | project-local analyzer that participates in builds. 6 | * A VSIX extension (.vsix file) that will apply your analyzer to all projects 7 | and works just in the IDE. 8 | 9 | To debug your analyzer, make sure the default project is the VSIX project and 10 | start debugging. This will deploy the analyzer as a VSIX into another instance 11 | of Visual Studio, which is useful for debugging, even if you intend to produce 12 | a NuGet package. 13 | 14 | 15 | TRYING OUT YOUR NUGET PACKAGE 16 | 17 | To try out the NuGet package: 18 | 1. Create a local NuGet feed by following the instructions here: 19 | > http://docs.nuget.org/docs/creating-packages/hosting-your-own-nuget-feeds 20 | 2. Copy the .nupkg file into that folder. 21 | 3. Open the target project in Visual Studio 2015. 22 | 4. Right-click on the project node in Solution Explorer and choose Manage 23 | NuGet Packages. 24 | 5. Select the NuGet feed you created on the left. 25 | 6. Choose your analyzer from the list and click Install. 26 | 27 | If you want to automatically deploy the .nupkg file to the local feed folder 28 | when you build this project, follow these steps: 29 | 1. Right-click on this project in Solution Explorer and choose 'Unload Project'. 30 | 2. Right-click on this project and click "Edit". 31 | 3. Scroll down to the "AfterBuild" target. 32 | 4. In the "Exec" task, change the value inside "Command" after the -OutputDirectory 33 | path to point to your local NuGet feed folder. -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace MoreEffectiveAnalyzers { 12 | using System; 13 | using System.Reflection; 14 | 15 | 16 | /// 17 | /// A strongly-typed resource class, for looking up localized strings, etc. 18 | /// 19 | // This class was auto-generated by the StronglyTypedResourceBuilder 20 | // class via a tool like ResGen or Visual Studio. 21 | // To add or remove a member, edit your .ResX file then rerun ResGen 22 | // with the /str option, or rebuild your VS project. 23 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 24 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 25 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 26 | internal class Resources { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() { 34 | } 35 | 36 | /// 37 | /// Returns the cached ResourceManager instance used by this class. 38 | /// 39 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 40 | internal static global::System.Resources.ResourceManager ResourceManager { 41 | get { 42 | if (object.ReferenceEquals(resourceMan, null)) { 43 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MoreEffectiveAnalyzers.Resources", typeof(Resources).GetTypeInfo().Assembly); 44 | resourceMan = temp; 45 | } 46 | return resourceMan; 47 | } 48 | } 49 | 50 | /// 51 | /// Overrides the current thread's CurrentUICulture property for all 52 | /// resource lookups using this strongly typed resource class. 53 | /// 54 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 55 | internal static global::System.Globalization.CultureInfo Culture { 56 | get { 57 | return resourceCulture; 58 | } 59 | set { 60 | resourceCulture = value; 61 | } 62 | } 63 | 64 | /// 65 | /// Looks up a localized string similar to Vritual Events can cause hard to diagnose bugs. Use a virtual method to raise a non-virtual event instead.. 66 | /// 67 | internal static string AnalyzerDescription { 68 | get { 69 | return ResourceManager.GetString("AnalyzerDescription", resourceCulture); 70 | } 71 | } 72 | 73 | /// 74 | /// Looks up a localized string similar to Event '{0}' should not be virtual. 75 | /// 76 | internal static string AnalyzerMessageFormat { 77 | get { 78 | return ResourceManager.GetString("AnalyzerMessageFormat", resourceCulture); 79 | } 80 | } 81 | 82 | /// 83 | /// Looks up a localized string similar to Declare only non-virtual events. 84 | /// 85 | internal static string AnalyzerTitle { 86 | get { 87 | return ResourceManager.GetString("AnalyzerTitle", resourceCulture); 88 | } 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | Vritual Events can cause hard to diagnose bugs. Use a virtual method to raise a non-virtual event instead. 122 | An optional longer localizable description of the diagnostic. 123 | 124 | 125 | Event '{0}' should not be virtual 126 | The format-able message the diagnostic displays. 127 | 128 | 129 | Declare only non-virtual events 130 | The title of the diagnostic. 131 | 132 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers/tools/install.ps1: -------------------------------------------------------------------------------- 1 | param($installPath, $toolsPath, $package, $project) 2 | 3 | $analyzersPaths = Join-Path (Join-Path (Split-Path -Path $toolsPath -Parent) "analyzers" ) * -Resolve 4 | 5 | foreach($analyzersPath in $analyzersPaths) 6 | { 7 | # Install the language agnostic analyzers. 8 | if (Test-Path $analyzersPath) 9 | { 10 | foreach ($analyzerFilePath in Get-ChildItem $analyzersPath -Filter *.dll) 11 | { 12 | if($project.Object.AnalyzerReferences) 13 | { 14 | $project.Object.AnalyzerReferences.Add($analyzerFilePath.FullName) 15 | } 16 | } 17 | } 18 | } 19 | 20 | # $project.Type gives the language name like (C# or VB.NET) 21 | $languageFolder = "" 22 | if($project.Type -eq "C#") 23 | { 24 | $languageFolder = "cs" 25 | } 26 | if($project.Type -eq "VB.NET") 27 | { 28 | $languageFolder = "vb" 29 | } 30 | if($languageFolder -eq "") 31 | { 32 | return 33 | } 34 | 35 | foreach($analyzersPath in $analyzersPaths) 36 | { 37 | # Install language specific analyzers. 38 | $languageAnalyzersPath = join-path $analyzersPath $languageFolder 39 | if (Test-Path $languageAnalyzersPath) 40 | { 41 | foreach ($analyzerFilePath in Get-ChildItem $languageAnalyzersPath -Filter *.dll) 42 | { 43 | if($project.Object.AnalyzerReferences) 44 | { 45 | $project.Object.AnalyzerReferences.Add($analyzerFilePath.FullName) 46 | } 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /MoreEffectiveAnalyzers/MoreEffectiveAnalyzers/tools/uninstall.ps1: -------------------------------------------------------------------------------- 1 | param($installPath, $toolsPath, $package, $project) 2 | 3 | $analyzersPaths = Join-Path (Join-Path (Split-Path -Path $toolsPath -Parent) "analyzers" ) * -Resolve 4 | 5 | foreach($analyzersPath in $analyzersPaths) 6 | { 7 | # Uninstall the language agnostic analyzers. 8 | if (Test-Path $analyzersPath) 9 | { 10 | foreach ($analyzerFilePath in Get-ChildItem $analyzersPath -Filter *.dll) 11 | { 12 | if($project.Object.AnalyzerReferences) 13 | { 14 | $project.Object.AnalyzerReferences.Remove($analyzerFilePath.FullName) 15 | } 16 | } 17 | } 18 | } 19 | 20 | # $project.Type gives the language name like (C# or VB.NET) 21 | $languageFolder = "" 22 | if($project.Type -eq "C#") 23 | { 24 | $languageFolder = "cs" 25 | } 26 | if($project.Type -eq "VB.NET") 27 | { 28 | $languageFolder = "vb" 29 | } 30 | if($languageFolder -eq "") 31 | { 32 | return 33 | } 34 | 35 | foreach($analyzersPath in $analyzersPaths) 36 | { 37 | # Uninstall language specific analyzers. 38 | $languageAnalyzersPath = join-path $analyzersPath $languageFolder 39 | if (Test-Path $languageAnalyzersPath) 40 | { 41 | foreach ($analyzerFilePath in Get-ChildItem $languageAnalyzersPath -Filter *.dll) 42 | { 43 | if($project.Object.AnalyzerReferences) 44 | { 45 | try 46 | { 47 | $project.Object.AnalyzerReferences.Remove($analyzerFilePath.FullName) 48 | } 49 | catch 50 | { 51 | 52 | } 53 | } 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MoreEffectiveCSharpAnalyzers 2 | This repository contains a set of Analyzers and CodeFixes that enforce the items written in More Effective C#, the 2nd edition 3 | --------------------------------------------------------------------------------