├── .gitattributes ├── .gitignore ├── .vscode ├── launch.json └── tasks.json ├── DurableFunctionsAnalyzer.sln ├── DurableFunctionsAnalyzer ├── DurableFunctionsAnalyzer.Test │ ├── ArgumentTypeAnalyzerTests.cs │ ├── DurableFunctionsAnalyzer.Test.csproj │ ├── Extensions │ │ └── StringExtensionsTest.cs │ ├── Helpers │ │ ├── CodeFixVerifier.Helper.cs │ │ ├── DiagnosticResult.cs │ │ └── DiagnosticVerifier.Helper.cs │ ├── NameAnalyzerTests.cs │ ├── OrchestrationTriggerAnnotationAnalyzerTests.cs │ ├── ReturnTypeAnalyzerTests.cs │ └── Verifiers │ │ ├── CodeFixVerifier.cs │ │ └── DiagnosticVerifier.cs ├── DurableFunctionsAnalyzer.Vsix │ ├── DurableFunctionsAnalyzer.Vsix.csproj │ ├── packages.config │ └── source.extension.vsixmanifest └── DurableFunctionsAnalyzer │ ├── AnalyzerRegistration.cs │ ├── AnalyzerRegistration.cs.orig │ ├── Analyzers │ ├── ArgumentAnalyzer.cs │ ├── BaseFunctionAnalyzer.cs │ ├── IFunctionAnalyzer.cs │ ├── NameAnalyzer.cs │ ├── OrchestrationTriggerAnnotationAnalyzer.cs │ └── ReturnTypeAnalyzer.cs │ ├── DurableFunctionsAnalyzer.csproj │ ├── DurableFunctionsAnalyzerCodeFixProvider.cs │ ├── Extensions │ └── StringExtensions.cs │ ├── Models │ ├── FunctionCall.cs │ └── FunctionDefinition.cs │ ├── Resources.Designer.cs │ ├── Resources.Designer.cs.orig │ ├── Resources.resx │ ├── Resources.resx.orig │ └── tools │ ├── install.ps1 │ └── uninstall.ps1 ├── LICENSE ├── README.md └── images ├── poc.png ├── poc2.png ├── poc3.png ├── poc4.png └── settings.png /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.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 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to find out which attributes exist for C# debugging 3 | // Use hover for the description of the existing attributes 4 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": ".NET Core Launch (console)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Test/bin/Debug/netcoreapp2.0/DurableFunctionsAnalyzer.Test.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}/DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Test", 16 | // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window 17 | "console": "internalConsole", 18 | "stopAtEntry": false, 19 | "internalConsoleOptions": "openOnSessionStart" 20 | }, 21 | { 22 | "name": ".NET Core Attach", 23 | "type": "coreclr", 24 | "request": "attach", 25 | "processId": "${command:pickProcess}" 26 | } 27 | ,] 28 | } -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Test/DurableFunctionsAnalyzer.Test.csproj" 11 | ], 12 | "problemMatcher": "$msCompile" 13 | } 14 | ] 15 | } -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28307.168 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DurableFunctionsAnalyzer", "DurableFunctionsAnalyzer\DurableFunctionsAnalyzer\DurableFunctionsAnalyzer.csproj", "{4051DDC7-5ED3-4C0D-98D5-AA9DEE3FEB62}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DurableFunctionsAnalyzer.Test", "DurableFunctionsAnalyzer\DurableFunctionsAnalyzer.Test\DurableFunctionsAnalyzer.Test.csproj", "{AD729E25-7A16-4CDC-AB8D-7CB9EAE07554}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DurableFunctionsAnalyzer.Vsix", "DurableFunctionsAnalyzer\DurableFunctionsAnalyzer.Vsix\DurableFunctionsAnalyzer.Vsix.csproj", "{36A03A1A-2728-484D-8C3B-FBBE710207A5}" 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 | {4051DDC7-5ED3-4C0D-98D5-AA9DEE3FEB62}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {4051DDC7-5ED3-4C0D-98D5-AA9DEE3FEB62}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {4051DDC7-5ED3-4C0D-98D5-AA9DEE3FEB62}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {4051DDC7-5ED3-4C0D-98D5-AA9DEE3FEB62}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {AD729E25-7A16-4CDC-AB8D-7CB9EAE07554}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {AD729E25-7A16-4CDC-AB8D-7CB9EAE07554}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {AD729E25-7A16-4CDC-AB8D-7CB9EAE07554}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {AD729E25-7A16-4CDC-AB8D-7CB9EAE07554}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {36A03A1A-2728-484D-8C3B-FBBE710207A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {36A03A1A-2728-484D-8C3B-FBBE710207A5}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {36A03A1A-2728-484D-8C3B-FBBE710207A5}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {36A03A1A-2728-484D-8C3B-FBBE710207A5}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {A4477BE4-E416-46FD-8A51-A69727A89D26} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Test/ArgumentTypeAnalyzerTests.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 DurableFunctionsAnalyzer; 8 | 9 | namespace DurableFunctionsAnalyzer.Test 10 | { 11 | [TestClass] 12 | public class ArgumentAnalyzerTests : CodeFixVerifier 13 | { 14 | 15 | [TestMethod] 16 | public void Should_not_trigger_on_empty() 17 | { 18 | var test = @""; 19 | 20 | VerifyCSharpDiagnostic(test); 21 | } 22 | 23 | [TestMethod] 24 | public void Should_not_find_any_issue_with_tuple_parameter() 25 | { 26 | var test = @"using System.Collections.Generic; 27 | using System.Linq; 28 | using System.Net.Http; 29 | using System.Threading.Tasks; 30 | using Microsoft.Azure.WebJobs; 31 | using Microsoft.Azure.WebJobs.Extensions.Http; 32 | using Microsoft.Azure.WebJobs.Host; 33 | using Microsoft.Extensions.Logging; 34 | 35 | namespace ExternalInteraction 36 | { 37 | public static class HireEmployee 38 | { 39 | [FunctionName(""HireEmployee"")] 40 | public static async Task RunOrchestrator( 41 | [OrchestrationTrigger] DurableOrchestrationContext context, 42 | ILogger log) 43 | { 44 | var applications = context.GetInput>(); 45 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered"", (new String(""a string""), 1)); 46 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 47 | return approvals.OrderByDescending(x => x.Score).First(); 48 | } 49 | 50 | [FunctionName(""ApplicationsFiltered"")] 51 | public static async Task> Run( 52 | [ActivityTrigger] (String userName, int Length), 53 | [OrchestrationClient] DurableOrchestrationClient client) 54 | { 55 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 56 | } 57 | } 58 | }"; 59 | 60 | VerifyCSharpDiagnostic(test); 61 | } 62 | 63 | [TestMethod] 64 | public void Should_not_find_any_issue_with_correctly_function_parameter() 65 | { 66 | var test = @"using System.Collections.Generic; 67 | using System.Linq; 68 | using System.Net.Http; 69 | using System.Threading.Tasks; 70 | using Microsoft.Azure.WebJobs; 71 | using Microsoft.Azure.WebJobs.Extensions.Http; 72 | using Microsoft.Azure.WebJobs.Host; 73 | using Microsoft.Extensions.Logging; 74 | 75 | namespace ExternalInteraction 76 | { 77 | public static class HireEmployee 78 | { 79 | [FunctionName(""HireEmployee"")] 80 | public static async Task RunOrchestrator( 81 | [OrchestrationTrigger] DurableOrchestrationContext context, 82 | ILogger log) 83 | { 84 | var applications = context.GetInput>(); 85 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered"", new String("")); 86 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 87 | return approvals.OrderByDescending(x => x.Score).First(); 88 | } 89 | 90 | [FunctionName(""ApplicationsFiltered"")] 91 | public static async Task> Run( 92 | [ActivityTrigger] String userName, 93 | [OrchestrationClient] DurableOrchestrationClient client) 94 | { 95 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 96 | } 97 | } 98 | }"; 99 | 100 | VerifyCSharpDiagnostic(test); 101 | } 102 | 103 | 104 | [TestMethod] 105 | public void Should_not_find_any_issue_with_open_generic_function_parameter() 106 | { 107 | var test = @"using System.Collections.Generic; 108 | using System.Linq; 109 | using System.Net.Http; 110 | using System.Threading.Tasks; 111 | using Microsoft.Azure.WebJobs; 112 | using Microsoft.Azure.WebJobs.Extensions.Http; 113 | using Microsoft.Azure.WebJobs.Host; 114 | using Microsoft.Extensions.Logging; 115 | 116 | namespace ExternalInteraction 117 | { 118 | public class EatIceCream 119 | { 120 | public string Flavour { get; set; } 121 | } 122 | 123 | public class Command 124 | { 125 | public T WhatToEat { get; set; } 126 | public Command() 127 | { 128 | 129 | } 130 | } 131 | public static class HireEmployee 132 | { 133 | [FunctionName(""ConsumeDessert"")] 134 | public static async Task RunOrchestrator( 135 | [OrchestrationTrigger] DurableOrchestrationContext context, 136 | ILogger log) 137 | { 138 | var applications = context.GetInput>(); 139 | var approvals = await context.CallActivityAsync>(""Consume"", new Command()); 140 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 141 | return approvals.OrderByDescending(x => x.Score).First(); 142 | } 143 | 144 | [FunctionName(""Consume"")] 145 | public static async Task> Run( 146 | [ActivityTrigger] Command command, 147 | [OrchestrationClient] DurableOrchestrationClient client) 148 | { 149 | await client.RaiseEventAsync(approval.InstanceId, ""AllIceCreamEaten""); 150 | } 151 | } 152 | }"; 153 | 154 | VerifyCSharpDiagnostic(test); 155 | 156 | } 157 | 158 | 159 | [TestMethod] 160 | public void Should_not_crash_on_real_null_argument() 161 | { 162 | var test = @"using System; 163 | using System.Collections.Generic; 164 | using System.Net.Http; 165 | using System.Threading; 166 | using System.Threading.Tasks; 167 | using Microsoft.Azure.WebJobs; 168 | using Microsoft.Azure.WebJobs.Extensions.Http; 169 | using Microsoft.Azure.WebJobs.Host; 170 | using Microsoft.Extensions.Logging; 171 | 172 | namespace Timeout 173 | { 174 | public static class RunTransaction 175 | { 176 | [FunctionName(""RunTransaction"")] 177 | public static async Task> RunOrchestrator( 178 | [OrchestrationTrigger] DurableOrchestrationContext context, 179 | ILogger logger) 180 | { 181 | var outputs = new List(); 182 | using (var cts = new CancellationTokenSource()) 183 | { 184 | var timer = context.CreateTimer(context.CurrentUtcDateTime.AddSeconds(15), cts.Token); 185 | var action = context.CallActivityAsync(""SomethingTimeConsuming"", null); 186 | 187 | var result = await Task.WhenAny(timer, action); 188 | if (result == action) 189 | { 190 | cts.Cancel(); 191 | logger.LogInformation(""Approved""); 192 | } 193 | else 194 | { 195 | logger.LogInformation(""Task timed out""); 196 | } 197 | } 198 | return outputs; 199 | } 200 | 201 | [FunctionName(""Timeout"")] 202 | public static void Timeout([ActivityTrigger] string name, ILogger log) 203 | { 204 | log.LogInformation($""Request for approval timed out.""); 205 | } 206 | 207 | [FunctionName(""SomethingTimeConsuming"")] 208 | public async static Task SomethingTimeConsuming([ActivityTrigger] string name, ILogger log) 209 | { 210 | log.LogInformation($""Performing lengthy task.""); 211 | await Task.Delay(TimeSpan.FromSeconds(30)); 212 | return $""Approved""; 213 | } 214 | 215 | [FunctionName(""RunTransaction_HttpStart"")] 216 | public static async Task HttpStart( 217 | [HttpTrigger(AuthorizationLevel.Anonymous, ""get"", ""post"")]HttpRequestMessage req, 218 | [OrchestrationClient]DurableOrchestrationClient starter, 219 | ILogger log) 220 | { 221 | // Function input comes from the request content. 222 | string instanceId = await starter.StartNewAsync(""RunTransaction"", null); 223 | 224 | log.LogInformation($""Started orchestration with ID = '{instanceId}'.""); 225 | 226 | return starter.CreateCheckStatusResponse(req, instanceId); 227 | } 228 | } 229 | }"; 230 | VerifyCSharpDiagnostic(test); 231 | } 232 | 233 | [TestMethod] 234 | public void Should_not_crash_on_null_argument() 235 | { 236 | var test = @"using System.Collections.Generic; 237 | using System.Linq; 238 | using System.Net.Http; 239 | using System.Threading.Tasks; 240 | using Microsoft.Azure.WebJobs; 241 | using Microsoft.Azure.WebJobs.Extensions.Http; 242 | using Microsoft.Azure.WebJobs.Host; 243 | using Microsoft.Extensions.Logging; 244 | 245 | namespace ExternalInteraction 246 | { 247 | public static class HireEmployee 248 | { 249 | [FunctionName(""HireEmployee"")] 250 | public static async Task RunOrchestrator( 251 | [OrchestrationTrigger] DurableOrchestrationContext context, 252 | ILogger log) 253 | { 254 | var applications = context.GetInput>(); 255 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered"", null); 256 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 257 | return approvals.OrderByDescending(x => x.Score).First(); 258 | } 259 | 260 | [FunctionName(""ApplicationsFiltered"")] 261 | public static async Task> Run( 262 | [ActivityTrigger] String userName, 263 | [OrchestrationClient] DurableOrchestrationClient client) 264 | { 265 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 266 | } 267 | } 268 | }"; 269 | 270 | VerifyCSharpDiagnostic(test); 271 | 272 | } 273 | 274 | 275 | [TestMethod] 276 | public void Should_find_issue_with_incorrect_function_parameter() 277 | { 278 | var test = @"using System.Collections.Generic; 279 | using System.Linq; 280 | using System.Net.Http; 281 | using System.Threading.Tasks; 282 | using Microsoft.Azure.WebJobs; 283 | using Microsoft.Azure.WebJobs.Extensions.Http; 284 | using Microsoft.Azure.WebJobs.Host; 285 | using Microsoft.Extensions.Logging; 286 | using System; 287 | 288 | namespace ExternalInteraction 289 | { 290 | public static class HireEmployee 291 | { 292 | [FunctionName(""HireEmployee"")] 293 | public static async Task RunOrchestrator( 294 | [OrchestrationTrigger] DurableOrchestrationContext context, 295 | ILogger log) 296 | { 297 | var applications = context.GetInput>(); 298 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered"", Guid.NewGuid()); 299 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 300 | return approvals.OrderByDescending(x => x.Score).First(); 301 | } 302 | 303 | [FunctionName(""ApplicationsFiltered"")] 304 | public static async Task> Run( 305 | [ActivityTrigger] String userName, 306 | [OrchestrationClient] DurableOrchestrationClient client) 307 | { 308 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 309 | } 310 | } 311 | }"; 312 | var expected = new DiagnosticResult 313 | { 314 | Id = "DurableFunctionsArgumentAnalyzer", 315 | Message = String.Format("Azure function named '{0}' takes a '{1}' but was given a '{2}'", "ApplicationsFiltered", "System.String", "System.Guid"), 316 | Severity = DiagnosticSeverity.Warning, 317 | Locations = 318 | new[] { 319 | new DiagnosticResultLocation("Test0.cs", 21, 100) 320 | } 321 | }; 322 | 323 | VerifyCSharpDiagnostic(test, expected); 324 | } 325 | 326 | protected override CodeFixProvider GetCSharpCodeFixProvider() 327 | { 328 | return new DurableFunctionsAnalyzerCodeFixProvider(); 329 | } 330 | 331 | protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() 332 | { 333 | return new AnalyzerRegistration(); 334 | } 335 | } 336 | } 337 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Test/DurableFunctionsAnalyzer.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Test/Extensions/StringExtensionsTest.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace DurableFunctionsAnalyzer.Test.Extensions 7 | { 8 | [TestClass] 9 | public class StringExtensionsTest 10 | { 11 | [TestMethod] 12 | public void ProximityWorks() 13 | { 14 | var strings = new string[] { "cat", "cactus", "cattle" }; 15 | Assert.AreEqual(1, DurableFunctionsAnalyzer.Extensions.StringExtensions.LevenshteinDistance("cab", "cat")); 16 | Assert.AreEqual(4, DurableFunctionsAnalyzer.Extensions.StringExtensions.LevenshteinDistance("cab", "cactus")); 17 | Assert.AreEqual(4, DurableFunctionsAnalyzer.Extensions.StringExtensions.LevenshteinDistance("cab", "cattle")); 18 | Assert.AreEqual(17, DurableFunctionsAnalyzer.Extensions.StringExtensions.LevenshteinDistance("HireEmployee", "ApplicationsFiltered")); 19 | Assert.AreEqual(6, DurableFunctionsAnalyzer.Extensions.StringExtensions.LevenshteinDistance("ApplicationsFilteredNicely", "ApplicationsFiltered")); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.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 | int oldIndex = 0; 45 | int 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) 67 | { 68 | return document.GetSemanticModelAsync().Result.GetDiagnostics(); 69 | } 70 | 71 | /// 72 | /// Given a document, turn it into a string based on the syntax root 73 | /// 74 | /// The Document to be converted to a string 75 | /// A string containing the syntax of the Document after formatting 76 | private static string GetStringFromDocument(Document document) 77 | { 78 | var simplifiedDoc = Simplifier.ReduceAsync(document, Simplifier.Annotation).Result; 79 | var root = simplifiedDoc.GetSyntaxRootAsync().Result; 80 | root = Formatter.Format(root, Formatter.Annotation, simplifiedDoc.Project.Solution.Workspace); 81 | return root.GetText().ToString(); 82 | } 83 | } 84 | } 85 | 86 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.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(column), "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 64 | { 65 | get 66 | { 67 | return this.Locations.Length > 0 ? this.Locations[0].Path : ""; 68 | } 69 | } 70 | 71 | public int Line 72 | { 73 | get 74 | { 75 | return this.Locations.Length > 0 ? this.Locations[0].Line : -1; 76 | } 77 | } 78 | 79 | public int Column 80 | { 81 | get 82 | { 83 | return this.Locations.Length > 0 ? this.Locations[0].Column : -1; 84 | } 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.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 string DefaultFilePathPrefix = "Test"; 24 | internal static string CSharpDefaultFileExt = "cs"; 25 | internal static string VisualBasicDefaultExt = "vb"; 26 | internal static string TestProjectName = "TestProject"; 27 | 28 | #region Get Diagnostics 29 | 30 | /// 31 | /// Given classes in the form of strings, their language, and an IDiagnosticAnalyzer 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 | { 39 | return GetSortedDiagnosticsFromDocuments(analyzer, GetDocuments(sources, language)); 40 | } 41 | 42 | /// 43 | /// Given an analyzer and a document to apply it to, run the analyzer and gather an array of diagnostics found in it. 44 | /// The returned diagnostics are then ordered by location in the source document. 45 | /// 46 | /// The analyzer to run on the documents 47 | /// The Documents that the analyzer will be run on 48 | /// An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location 49 | protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer, Document[] documents) 50 | { 51 | var projects = new HashSet(); 52 | foreach (var document in documents) 53 | { 54 | projects.Add(document.Project); 55 | } 56 | 57 | var diagnostics = new List(); 58 | foreach (var project in projects) 59 | { 60 | var compilationWithAnalyzers = project.GetCompilationAsync().Result.WithAnalyzers(ImmutableArray.Create(analyzer)); 61 | var diags = compilationWithAnalyzers.GetAnalyzerDiagnosticsAsync().Result; 62 | foreach (var diag in diags) 63 | { 64 | if (diag.Location == Location.None || diag.Location.IsInMetadata) 65 | { 66 | diagnostics.Add(diag); 67 | } 68 | else 69 | { 70 | for (int i = 0; i < documents.Length; i++) 71 | { 72 | var document = documents[i]; 73 | var tree = document.GetSyntaxTreeAsync().Result; 74 | if (tree == diag.Location.SourceTree) 75 | { 76 | diagnostics.Add(diag); 77 | } 78 | } 79 | } 80 | } 81 | } 82 | 83 | var results = SortDiagnostics(diagnostics); 84 | diagnostics.Clear(); 85 | return results; 86 | } 87 | 88 | /// 89 | /// Sort diagnostics by location in source document 90 | /// 91 | /// The list of Diagnostics to be sorted 92 | /// An IEnumerable containing the Diagnostics in order of Location 93 | private static Diagnostic[] SortDiagnostics(IEnumerable diagnostics) 94 | { 95 | return diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray(); 96 | } 97 | 98 | #endregion 99 | 100 | #region Set up compilation and documents 101 | /// 102 | /// Given an array of strings as sources and a language, turn them into a project and return the documents and spans of it. 103 | /// 104 | /// Classes in the form of strings 105 | /// The language the source code is in 106 | /// A Tuple containing the Documents produced from the sources and their TextSpans if relevant 107 | private static Document[] GetDocuments(string[] sources, string language) 108 | { 109 | if (language != LanguageNames.CSharp && language != LanguageNames.VisualBasic) 110 | { 111 | throw new ArgumentException("Unsupported Language"); 112 | } 113 | 114 | var project = CreateProject(sources, language); 115 | var documents = project.Documents.ToArray(); 116 | 117 | if (sources.Length != documents.Length) 118 | { 119 | throw new InvalidOperationException("Amount of sources did not match amount of Documents created"); 120 | } 121 | 122 | return documents; 123 | } 124 | 125 | /// 126 | /// Create a Document from a string through creating a project that contains it. 127 | /// 128 | /// Classes in the form of a string 129 | /// The language the source code is in 130 | /// A Document created from the source string 131 | protected static Document CreateDocument(string source, string language = LanguageNames.CSharp) 132 | { 133 | return CreateProject(new[] { source }, language).Documents.First(); 134 | } 135 | 136 | /// 137 | /// Create a project using the inputted strings as sources. 138 | /// 139 | /// Classes in the form of strings 140 | /// The language the source code is in 141 | /// A Project created out of the Documents created from the source strings 142 | private static Project CreateProject(string[] sources, string language = LanguageNames.CSharp) 143 | { 144 | string fileNamePrefix = DefaultFilePathPrefix; 145 | string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt; 146 | 147 | var projectId = ProjectId.CreateNewId(debugName: TestProjectName); 148 | 149 | var solution = new AdhocWorkspace() 150 | .CurrentSolution 151 | .AddProject(projectId, TestProjectName, TestProjectName, language) 152 | .AddMetadataReference(projectId, CorlibReference) 153 | .AddMetadataReference(projectId, SystemCoreReference) 154 | .AddMetadataReference(projectId, CSharpSymbolsReference) 155 | .AddMetadataReference(projectId, CodeAnalysisReference); 156 | 157 | int count = 0; 158 | foreach (var source in sources) 159 | { 160 | var newFileName = fileNamePrefix + count + "." + fileExt; 161 | var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); 162 | solution = solution.AddDocument(documentId, newFileName, SourceText.From(source)); 163 | count++; 164 | } 165 | return solution.GetProject(projectId); 166 | } 167 | #endregion 168 | } 169 | } 170 | 171 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Test/NameAnalyzerTests.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 DurableFunctionsAnalyzer; 8 | 9 | namespace DurableFunctionsAnalyzer.Test 10 | { 11 | [TestClass] 12 | public class NameAnalyzerTests : CodeFixVerifier 13 | { 14 | 15 | [TestMethod] 16 | public void Should_not_trigger_on_empty() 17 | { 18 | var test = @""; 19 | 20 | VerifyCSharpDiagnostic(test); 21 | } 22 | [TestMethod] 23 | public void Should_not_find_any_issue_with_correctly_named_functions() 24 | { 25 | var test = @"using System.Collections.Generic; 26 | using System.Linq; 27 | using System.Net.Http; 28 | using System.Threading.Tasks; 29 | using Microsoft.Azure.WebJobs; 30 | using Microsoft.Azure.WebJobs.Extensions.Http; 31 | using Microsoft.Azure.WebJobs.Host; 32 | using Microsoft.Extensions.Logging; 33 | using System; 34 | 35 | namespace ExternalInteraction 36 | { 37 | public static class HireEmployee 38 | { 39 | [FunctionName(""HireEmployee"")] 40 | public static async Task RunOrchestrator( 41 | [OrchestrationTrigger] DurableOrchestrationContext context, 42 | ILogger log) 43 | { 44 | var applications = context.GetInput>(); 45 | var approvals = await context.CallActivityAsync(""ApplicationsFiltered"", ""An approval""); 46 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 47 | return approvals.OrderByDescending(x => x.Score).First(); 48 | } 49 | 50 | [FunctionName(""ApplicationsFiltered"")] 51 | public static async Task Run( 52 | [ActivityTrigger(""approval-queue"")] String approval, 53 | [OrchestrationClient] DurableOrchestrationClient client) 54 | { 55 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 56 | } 57 | } 58 | }"; 59 | 60 | 61 | VerifyCSharpDiagnostic(test); 62 | 63 | // var fixtest = @" 64 | //using System; 65 | //using System.Collections.Generic; 66 | //using System.Linq; 67 | //using System.Text; 68 | //using System.Threading.Tasks; 69 | //using System.Diagnostics; 70 | 71 | //namespace ConsoleApplication1 72 | //{ 73 | // class TYPENAME 74 | // { 75 | // } 76 | //}"; 77 | // VerifyCSharpFix(test, fixtest); 78 | } 79 | 80 | [TestMethod] 81 | public void Should_not_find_any_issue_with_name_of() 82 | { 83 | var test = @"using System.Collections.Generic; 84 | using System.Linq; 85 | using System.Net.Http; 86 | using System.Threading.Tasks; 87 | using Microsoft.Azure.WebJobs; 88 | using Microsoft.Azure.WebJobs.Extensions.Http; 89 | using Microsoft.Azure.WebJobs.Host; 90 | using Microsoft.Extensions.Logging; 91 | using System; 92 | 93 | namespace ExternalInteraction 94 | { 95 | public static class HireEmployee 96 | { 97 | [FunctionName(""HireEmployee"")] 98 | public static async Task RunOrchestrator( 99 | [OrchestrationTrigger] DurableOrchestrationContext context, 100 | ILogger log) 101 | { 102 | var applications = context.GetInput>(); 103 | var approvals = await context.CallActivityAsync>(nameof(ApplicationsFiltered)); 104 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 105 | return approvals.OrderByDescending(x => x.Score).First(); 106 | } 107 | 108 | [FunctionName(nameof(ApplicationsFiltered))] 109 | public static async Task ApplicationsFiltered( 110 | [QueueTrigger(""approval-queue"")] Approval approval, 111 | [OrchestrationClient] DurableOrchestrationClient client) 112 | { 113 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 114 | } 115 | } 116 | }"; 117 | 118 | 119 | VerifyCSharpDiagnostic(test); 120 | 121 | // var fixtest = @" 122 | //using System; 123 | //using System.Collections.Generic; 124 | //using System.Linq; 125 | //using System.Text; 126 | //using System.Threading.Tasks; 127 | //using System.Diagnostics; 128 | 129 | //namespace ConsoleApplication1 130 | //{ 131 | // class TYPENAME 132 | // { 133 | // } 134 | //}"; 135 | // VerifyCSharpFix(test, fixtest); 136 | } 137 | 138 | [TestMethod] 139 | public void Should_find_incorrectly_named_function() 140 | { 141 | var test = @"using System.Collections.Generic; 142 | using System.Linq; 143 | using System.Net.Http; 144 | using System.Threading.Tasks; 145 | using Microsoft.Azure.WebJobs; 146 | using Microsoft.Azure.WebJobs.Extensions.Http; 147 | using Microsoft.Azure.WebJobs.Host; 148 | using Microsoft.Extensions.Logging; 149 | using System; 150 | 151 | namespace ExternalInteraction 152 | { 153 | public static class HireEmployee 154 | { 155 | [FunctionName(""HireEmployee"")] 156 | public static async Task RunOrchestrator( 157 | [OrchestrationTrigger] DurableOrchestrationContext context, 158 | ILogger log) 159 | { 160 | var applications = context.GetInput>(); 161 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered""); 162 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 163 | return approvals.OrderByDescending(x => x.Score).First(); 164 | } 165 | 166 | [FunctionName(""ApplicationsFilteredNicely"")] 167 | public static async Task Run( 168 | [QueueTrigger(""approval-queue"")] Approval approval, 169 | [OrchestrationClient] DurableOrchestrationClient client) 170 | { 171 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 172 | } 173 | } 174 | }"; 175 | var expected = new DiagnosticResult 176 | { 177 | Id = "DurableFunctionsNameAnalyzer", 178 | Message = String.Format("Azure function named '{0}' does not exist. Did you mean 'ApplicationsFilteredNicely'?", "ApplicationsFiltered"), 179 | Severity = DiagnosticSeverity.Warning, 180 | Locations = 181 | new[] { 182 | new DiagnosticResultLocation("Test0.cs", 21, 84) 183 | } 184 | }; 185 | 186 | VerifyCSharpDiagnostic(test, expected); 187 | 188 | // var fixtest = @" 189 | //using System; 190 | //using System.Collections.Generic; 191 | //using System.Linq; 192 | //using System.Text; 193 | //using System.Threading.Tasks; 194 | //using System.Diagnostics; 195 | 196 | //namespace ConsoleApplication1 197 | //{ 198 | // class TYPENAME 199 | // { 200 | // } 201 | //}"; 202 | // VerifyCSharpFix(test, fixtest); 203 | } 204 | 205 | [TestMethod] 206 | public void Should_find_incorrectly_named_function_using_retry_policy() 207 | { 208 | var test = @"using System.Collections.Generic; 209 | using System.Linq; 210 | using System.Net.Http; 211 | using System.Threading.Tasks; 212 | using Microsoft.Azure.WebJobs; 213 | using Microsoft.Azure.WebJobs.Extensions.Http; 214 | using Microsoft.Azure.WebJobs.Host; 215 | using Microsoft.Extensions.Logging; 216 | using System; 217 | 218 | namespace ExternalInteraction 219 | { 220 | public static class HireEmployee 221 | { 222 | [FunctionName(""HireEmployee"")] 223 | public static async Task RunOrchestrator( 224 | [OrchestrationTrigger] DurableOrchestrationContext context, 225 | ILogger log) 226 | { 227 | var retryOptions = new RetryOptions(TimeSpan.FromSeconds(1), 3); 228 | var applications = context.GetInput>(); 229 | var approvals = await context.CallActivityWithRetryAsync>(""ApplicationsFiltered"", retryOptions, ""approved""); 230 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 231 | return approvals.OrderByDescending(x => x.Score).First(); 232 | } 233 | 234 | [FunctionName(""ApplicationsFilteredNicely"")] 235 | public static async Task Run( 236 | [ActivityTrigger(""approval-queue"")] String approval, 237 | [OrchestrationClient] DurableOrchestrationClient client) 238 | { 239 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 240 | } 241 | } 242 | }"; 243 | var expected = new DiagnosticResult 244 | { 245 | Id = "DurableFunctionsNameAnalyzer", 246 | Message = String.Format("Azure function named '{0}' does not exist. Did you mean 'ApplicationsFilteredNicely'?", "ApplicationsFiltered"), 247 | Severity = DiagnosticSeverity.Warning, 248 | Locations = 249 | new[] { 250 | new DiagnosticResultLocation("Test0.cs", 22, 93) 251 | } 252 | }; 253 | 254 | VerifyCSharpDiagnostic(test, expected); 255 | 256 | // var fixtest = @" 257 | //using System; 258 | //using System.Collections.Generic; 259 | //using System.Linq; 260 | //using System.Text; 261 | //using System.Threading.Tasks; 262 | //using System.Diagnostics; 263 | 264 | //namespace ConsoleApplication1 265 | //{ 266 | // class TYPENAME 267 | // { 268 | // } 269 | //}"; 270 | // VerifyCSharpFix(test, fixtest); 271 | } 272 | 273 | [TestMethod] 274 | public void Should_find_when_no_function_has_been_declared() 275 | { 276 | var test = @"using System.Collections.Generic; 277 | using System.Linq; 278 | using System.Net.Http; 279 | using System.Threading.Tasks; 280 | using Microsoft.Azure.WebJobs; 281 | using Microsoft.Azure.WebJobs.Extensions.Http; 282 | using Microsoft.Azure.WebJobs.Host; 283 | using Microsoft.Extensions.Logging; 284 | using System; 285 | 286 | namespace ExternalInteraction 287 | { 288 | public static class HireEmployee 289 | { 290 | public static async Task RunOrchestrator( 291 | [OrchestrationTrigger] DurableOrchestrationContext context, 292 | ILogger log) 293 | { 294 | var applications = context.GetInput>(); 295 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered""); 296 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 297 | return approvals.OrderByDescending(x => x.Score).First(); 298 | } 299 | 300 | public static async Task Run( 301 | [QueueTrigger(""approval-queue"")] Approval approval, 302 | [OrchestrationClient] DurableOrchestrationClient client) 303 | { 304 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 305 | } 306 | } 307 | }"; 308 | var expected = new DiagnosticResult 309 | { 310 | Id = "DurableFunctionsNameAnalyzer", 311 | Message = String.Format("Azure function named '{0}' does not exist. Could not find any function registrations.", "ApplicationsFiltered"), 312 | Severity = DiagnosticSeverity.Warning, 313 | Locations = 314 | new[] { 315 | new DiagnosticResultLocation("Test0.cs", 20, 84) 316 | } 317 | }; 318 | 319 | VerifyCSharpDiagnostic(test, expected); 320 | 321 | // var fixtest = @" 322 | //using System; 323 | //using System.Collections.Generic; 324 | //using System.Linq; 325 | //using System.Text; 326 | //using System.Threading.Tasks; 327 | //using System.Diagnostics; 328 | 329 | //namespace ConsoleApplication1 330 | //{ 331 | // class TYPENAME 332 | // { 333 | // } 334 | //}"; 335 | // VerifyCSharpFix(test, fixtest); 336 | } 337 | 338 | protected override CodeFixProvider GetCSharpCodeFixProvider() 339 | { 340 | return new DurableFunctionsAnalyzerCodeFixProvider(); 341 | } 342 | 343 | protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() 344 | { 345 | return new AnalyzerRegistration(); 346 | } 347 | } 348 | } 349 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Test/OrchestrationTriggerAnnotationAnalyzerTests.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 DurableFunctionsAnalyzer; 8 | 9 | namespace DurableFunctionsAnalyzer.Test 10 | { 11 | [TestClass] 12 | public class OrchestrationTriggerAnnotationAnalyzerTests : CodeFixVerifier 13 | { 14 | 15 | [TestMethod] 16 | public void Should_not_trigger_on_empty() 17 | { 18 | var test = @""; 19 | 20 | VerifyCSharpDiagnostic(test); 21 | } 22 | 23 | [TestMethod] 24 | public void Should_find_issue_with_object() 25 | { 26 | var test = @"using System.Collections.Generic; 27 | using System.Linq; 28 | using System.Net.Http; 29 | using System.Threading.Tasks; 30 | using Microsoft.Azure.WebJobs; 31 | using Microsoft.Azure.WebJobs.Extensions.Http; 32 | using Microsoft.Azure.WebJobs.Host; 33 | using Microsoft.Extensions.Logging; 34 | 35 | namespace ExternalInteraction 36 | { 37 | public static class HireEmployee 38 | { 39 | [FunctionName(""HireEmployee"")] 40 | public static async Task RunOrchestrator( 41 | [OrchestrationTrigger] Object context, 42 | ILogger log) 43 | { 44 | 45 | } 46 | }"; 47 | var expected = new DiagnosticResult 48 | { 49 | Id = "DurableFunctionOrchestrationTriggerAnalyzer", 50 | Message = String.Format("OrchestrationTrigger must be attached to a DurableOrchestrationContext", "ApplicationsFiltered", "System.Threading.Tasks.Task>", "System.Threading.Tasks.Task>"), 51 | Severity = DiagnosticSeverity.Warning, 52 | Locations = 53 | new[] { 54 | new DiagnosticResultLocation("Test0.cs", 16, 14) 55 | } 56 | }; 57 | VerifyCSharpDiagnostic(test, expected); 58 | } 59 | 60 | [TestMethod] 61 | public void Should_find_any_issue_with_key_word() 62 | { 63 | var test = @"using System.Collections.Generic; 64 | using System.Linq; 65 | using System.Net.Http; 66 | using System.Threading.Tasks; 67 | using Microsoft.Azure.WebJobs; 68 | using Microsoft.Azure.WebJobs.Extensions.Http; 69 | using Microsoft.Azure.WebJobs.Host; 70 | using Microsoft.Extensions.Logging; 71 | 72 | namespace ExternalInteraction 73 | { 74 | public static class HireEmployee 75 | { 76 | [FunctionName(""HireEmployee"")] 77 | public static async Task RunOrchestrator( 78 | [OrchestrationTrigger] string context, 79 | ILogger log) 80 | { 81 | 82 | } 83 | }"; 84 | var expected = new DiagnosticResult 85 | { 86 | Id = "DurableFunctionOrchestrationTriggerAnalyzer", 87 | Message = String.Format("OrchestrationTrigger must be attached to a DurableOrchestrationContext", "ApplicationsFiltered", "System.Threading.Tasks.Task>", "System.Threading.Tasks.Task>"), 88 | Severity = DiagnosticSeverity.Warning, 89 | Locations = 90 | new[] { 91 | new DiagnosticResultLocation("Test0.cs", 16, 14) 92 | } 93 | }; 94 | VerifyCSharpDiagnostic(test, expected); 95 | } 96 | 97 | 98 | [TestMethod] 99 | public void Should_not_find_any_issue_with_orchestration_trigger_on_context() 100 | { 101 | var test = @"using System.Collections.Generic; 102 | using System.Linq; 103 | using System.Net.Http; 104 | using System.Threading.Tasks; 105 | using Microsoft.Azure.WebJobs; 106 | using Microsoft.Azure.WebJobs.Extensions.Http; 107 | using Microsoft.Azure.WebJobs.Host; 108 | using Microsoft.Extensions.Logging; 109 | 110 | namespace ExternalInteraction 111 | { 112 | public static class HireEmployee 113 | { 114 | [FunctionName(""HireEmployee"")] 115 | public static async Task RunOrchestrator( 116 | [OrchestrationTrigger] DurableOrchestrationContext context, 117 | ILogger log) 118 | { 119 | 120 | } 121 | }"; 122 | 123 | VerifyCSharpDiagnostic(test); 124 | } 125 | 126 | 127 | [TestMethod] 128 | public void Should_not_find_any_issue_with_orchestration_trigger_on_context_base() 129 | { 130 | var test = @"using System.Collections.Generic; 131 | using System.Linq; 132 | using System.Net.Http; 133 | using System.Threading.Tasks; 134 | using Microsoft.Azure.WebJobs; 135 | using Microsoft.Azure.WebJobs.Extensions.Http; 136 | using Microsoft.Azure.WebJobs.Host; 137 | using Microsoft.Extensions.Logging; 138 | 139 | namespace ExternalInteraction 140 | { 141 | public static class HireEmployee 142 | { 143 | [FunctionName(""HireEmployee"")] 144 | public static async Task RunOrchestrator( 145 | [OrchestrationTrigger] DurableOrchestrationContextBase context, 146 | ILogger log) 147 | { 148 | 149 | } 150 | }"; 151 | 152 | VerifyCSharpDiagnostic(test); 153 | } 154 | 155 | protected override CodeFixProvider GetCSharpCodeFixProvider() 156 | { 157 | return new DurableFunctionsAnalyzerCodeFixProvider(); 158 | } 159 | 160 | protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() 161 | { 162 | return new AnalyzerRegistration(); 163 | } 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Test/ReturnTypeAnalyzerTests.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 DurableFunctionsAnalyzer; 8 | 9 | namespace DurableFunctionsAnalyzer.Test 10 | { 11 | [TestClass] 12 | public class ReturnTypeAnalyzerTests : CodeFixVerifier 13 | { 14 | 15 | [TestMethod] 16 | public void Should_not_trigger_on_empty() 17 | { 18 | var test = @""; 19 | 20 | VerifyCSharpDiagnostic(test); 21 | } 22 | [TestMethod] 23 | public void Should_not_find_any_issue_with_void_return() 24 | { 25 | var test = @"using System.Collections.Generic; 26 | using System.Linq; 27 | using System.Net.Http; 28 | using System.Threading.Tasks; 29 | using Microsoft.Azure.WebJobs; 30 | using Microsoft.Azure.WebJobs.Extensions.Http; 31 | using Microsoft.Azure.WebJobs.Host; 32 | using Microsoft.Extensions.Logging; 33 | 34 | namespace ExternalInteraction 35 | { 36 | public static class HireEmployee 37 | { 38 | [FunctionName(""HireEmployee"")] 39 | public static async Task RunOrchestrator( 40 | [OrchestrationTrigger] DurableOrchestrationContext context, 41 | ILogger log) 42 | { 43 | var applications = context.GetInput>(); 44 | var approvals = await context.CallActivityAsync(""ApplicationsFiltered"", new String("")); 45 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 46 | return approvals.OrderByDescending(x => x.Score).First(); 47 | } 48 | 49 | [FunctionName(""ApplicationsFiltered"")] 50 | public static void Run( 51 | [ActivityTrigger] String userName, 52 | [OrchestrationClient] DurableOrchestrationClient client) 53 | { 54 | client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 55 | } 56 | } 57 | }"; 58 | 59 | VerifyCSharpDiagnostic(test); 60 | 61 | } 62 | 63 | [TestMethod] 64 | public void Should_not_find_any_issue_with_untyped_task_return() 65 | { 66 | var test = @"using System.Collections.Generic; 67 | using System.Linq; 68 | using System.Net.Http; 69 | using System.Threading.Tasks; 70 | using Microsoft.Azure.WebJobs; 71 | using Microsoft.Azure.WebJobs.Extensions.Http; 72 | using Microsoft.Azure.WebJobs.Host; 73 | using Microsoft.Extensions.Logging; 74 | 75 | namespace ExternalInteraction 76 | { 77 | public static class HireEmployee 78 | { 79 | [FunctionName(""HireEmployee"")] 80 | public static async Task RunOrchestrator( 81 | [OrchestrationTrigger] DurableOrchestrationContext context, 82 | ILogger log) 83 | { 84 | var applications = context.GetInput>(); 85 | var approvals = await context.CallActivityAsync(""ApplicationsFiltered"", new String("")); 86 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 87 | return approvals.OrderByDescending(x => x.Score).First(); 88 | } 89 | 90 | [FunctionName(""ApplicationsFiltered"")] 91 | public static async Task Run( 92 | [ActivityTrigger] String userName, 93 | [OrchestrationClient] DurableOrchestrationClient client) 94 | { 95 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 96 | } 97 | } 98 | }"; 99 | 100 | VerifyCSharpDiagnostic(test); 101 | 102 | } 103 | [TestMethod] 104 | public void Should_not_find_any_issue_with_string_keyword() 105 | { 106 | var test = @"using System.Collections.Generic; 107 | using System.Linq; 108 | using System.Net.Http; 109 | using System.Threading.Tasks; 110 | using Microsoft.Azure.WebJobs; 111 | using Microsoft.Azure.WebJobs.Extensions.Http; 112 | using Microsoft.Azure.WebJobs.Host; 113 | using Microsoft.Extensions.Logging; 114 | 115 | namespace ExternalInteraction 116 | { 117 | public static class HireEmployee 118 | { 119 | [FunctionName(""HireEmployee"")] 120 | public static async Task RunOrchestrator( 121 | [OrchestrationTrigger] DurableOrchestrationContext context, 122 | ILogger log) 123 | { 124 | var applications = context.GetInput>(); 125 | var approvals = await context.CallActivityAsync(""ApplicationsFiltered"", new String("")); 126 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 127 | return approvals.OrderByDescending(x => x.Score).First(); 128 | } 129 | 130 | [FunctionName(""ApplicationsFiltered"")] 131 | public static async Task Run( 132 | [ActivityTrigger] string userName, 133 | [OrchestrationClient] DurableOrchestrationClient client) 134 | { 135 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 136 | } 137 | } 138 | }"; 139 | 140 | VerifyCSharpDiagnostic(test); 141 | 142 | } 143 | 144 | [TestMethod] 145 | public void Should_not_find_any_issue_with_different_long_representations() 146 | { 147 | var test = @"using System.Collections.Generic; 148 | using System.Linq; 149 | using System.Net.Http; 150 | using System.Threading.Tasks; 151 | using Microsoft.Azure.WebJobs; 152 | using Microsoft.Azure.WebJobs.Extensions.Http; 153 | using Microsoft.Azure.WebJobs.Host; 154 | using Microsoft.Extensions.Logging; 155 | 156 | namespace ExternalInteraction 157 | { 158 | public static class HireEmployee 159 | { 160 | [FunctionName(""HireEmployee"")] 161 | public static async Task RunOrchestrator( 162 | [OrchestrationTrigger] DurableOrchestrationContext context, 163 | ILogger log) 164 | { 165 | var applications = context.GetInput>(); 166 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered"", new String("")); 167 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 168 | return approvals.OrderByDescending(x => x.Score).First(); 169 | } 170 | 171 | [FunctionName(""ApplicationsFiltered"")] 172 | public static async Task> Run( 173 | [ActivityTrigger] String userName, 174 | [OrchestrationClient] DurableOrchestrationClient client) 175 | { 176 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 177 | } 178 | } 179 | }"; 180 | 181 | VerifyCSharpDiagnostic(test); 182 | 183 | } 184 | 185 | 186 | 187 | [TestMethod] 188 | public void Should_not_find_any_issue_with_int_representations() 189 | { 190 | var test = @"using System.Collections.Generic; 191 | using System.Linq; 192 | using System.Net.Http; 193 | using System.Threading.Tasks; 194 | using Microsoft.Azure.WebJobs; 195 | using Microsoft.Azure.WebJobs.Extensions.Http; 196 | using Microsoft.Azure.WebJobs.Host; 197 | using Microsoft.Extensions.Logging; 198 | 199 | namespace ExternalInteraction 200 | { 201 | public static class HireEmployee 202 | { 203 | [FunctionName(""HireEmployee"")] 204 | public static async Task RunOrchestrator( 205 | [OrchestrationTrigger] DurableOrchestrationContext context, 206 | ILogger log) 207 | { 208 | var applications = context.GetInput>(); 209 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered"", new String("")); 210 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 211 | return approvals.OrderByDescending(x => x.Score).First(); 212 | } 213 | 214 | [FunctionName(""ApplicationsFiltered"")] 215 | public static async Task> Run( 216 | [ActivityTrigger] String userName, 217 | [OrchestrationClient] DurableOrchestrationClient client) 218 | { 219 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 220 | } 221 | } 222 | }"; 223 | 224 | VerifyCSharpDiagnostic(test); 225 | 226 | } 227 | 228 | [TestMethod] 229 | public void Should_not_find_any_issue_with_double_representations() 230 | { 231 | var test = @"using System.Collections.Generic; 232 | using System.Linq; 233 | using System.Net.Http; 234 | using System.Threading.Tasks; 235 | using Microsoft.Azure.WebJobs; 236 | using Microsoft.Azure.WebJobs.Extensions.Http; 237 | using Microsoft.Azure.WebJobs.Host; 238 | using Microsoft.Extensions.Logging; 239 | 240 | namespace ExternalInteraction 241 | { 242 | public static class HireEmployee 243 | { 244 | [FunctionName(""HireEmployee"")] 245 | public static async Task RunOrchestrator( 246 | [OrchestrationTrigger] DurableOrchestrationContext context, 247 | ILogger log) 248 | { 249 | var applications = context.GetInput>(); 250 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered"", new String("")); 251 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 252 | return approvals.OrderByDescending(x => x.Score).First(); 253 | } 254 | 255 | [FunctionName(""ApplicationsFiltered"")] 256 | public static async Task> Run( 257 | [ActivityTrigger] String userName, 258 | [OrchestrationClient] DurableOrchestrationClient client) 259 | { 260 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 261 | } 262 | } 263 | }"; 264 | 265 | VerifyCSharpDiagnostic(test); 266 | 267 | } 268 | 269 | 270 | [TestMethod] 271 | public void Should_not_find_any_issue_with_activity_not_being_async() 272 | { 273 | var test = @"using System.Collections.Generic; 274 | using System.Linq; 275 | using System.Net.Http; 276 | using System.Threading.Tasks; 277 | using Microsoft.Azure.WebJobs; 278 | using Microsoft.Azure.WebJobs.Extensions.Http; 279 | using Microsoft.Azure.WebJobs.Host; 280 | using Microsoft.Extensions.Logging; 281 | 282 | namespace ExternalInteraction 283 | { 284 | public static class HireEmployee 285 | { 286 | [FunctionName(""HireEmployee"")] 287 | public static async Task RunOrchestrator( 288 | [OrchestrationTrigger] DurableOrchestrationContext context, 289 | ILogger log) 290 | { 291 | var applications = context.GetInput>(); 292 | var approvals = await context.CallActivityAsync(""ApplicationsFiltered"", new String("")); 293 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 294 | return approvals.OrderByDescending(x => x.Score).First(); 295 | } 296 | 297 | [FunctionName(""ApplicationsFiltered"")] 298 | public static string Run( 299 | [ActivityTrigger] String userName, 300 | [OrchestrationClient] DurableOrchestrationClient client) 301 | { 302 | client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 303 | } 304 | } 305 | }"; 306 | 307 | VerifyCSharpDiagnostic(test); 308 | 309 | } 310 | 311 | [TestMethod] 312 | public void Should_not_find_any_issue_with_different_bool_representations() 313 | { 314 | var test = @"using System.Collections.Generic; 315 | using System.Linq; 316 | using System.Net.Http; 317 | using System.Threading.Tasks; 318 | using Microsoft.Azure.WebJobs; 319 | using Microsoft.Azure.WebJobs.Extensions.Http; 320 | using Microsoft.Azure.WebJobs.Host; 321 | using Microsoft.Extensions.Logging; 322 | 323 | namespace ExternalInteraction 324 | { 325 | public static class HireEmployee 326 | { 327 | [FunctionName(""HireEmployee"")] 328 | public static async Task RunOrchestrator( 329 | [OrchestrationTrigger] DurableOrchestrationContext context, 330 | ILogger log) 331 | { 332 | var applications = context.GetInput>(); 333 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered"", new String("")); 334 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 335 | return approvals.OrderByDescending(x => x.Score).First(); 336 | } 337 | 338 | [FunctionName(""ApplicationsFiltered"")] 339 | public static async Task> Run( 340 | [ActivityTrigger] String userName, 341 | [OrchestrationClient] DurableOrchestrationClient client) 342 | { 343 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 344 | } 345 | } 346 | }"; 347 | 348 | VerifyCSharpDiagnostic(test); 349 | 350 | } 351 | [TestMethod] 352 | public void Should_not_find_any_issue_with_matching_return_type() 353 | { 354 | var test = @"using System.Collections.Generic; 355 | using System.Linq; 356 | using System.Net.Http; 357 | using System.Threading.Tasks; 358 | using Microsoft.Azure.WebJobs; 359 | using Microsoft.Azure.WebJobs.Extensions.Http; 360 | using Microsoft.Azure.WebJobs.Host; 361 | using Microsoft.Extensions.Logging; 362 | 363 | namespace ExternalInteraction 364 | { 365 | public static class HireEmployee 366 | { 367 | [FunctionName(""HireEmployee"")] 368 | public static async Task RunOrchestrator( 369 | [OrchestrationTrigger] DurableOrchestrationContext context, 370 | ILogger log) 371 | { 372 | var applications = context.GetInput>(); 373 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered"", new String("")); 374 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 375 | return approvals.OrderByDescending(x => x.Score).First(); 376 | } 377 | 378 | [FunctionName(""ApplicationsFiltered"")] 379 | public static async Task> Run( 380 | [ActivityTrigger] string userName, 381 | [OrchestrationClient] DurableOrchestrationClient client) 382 | { 383 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 384 | } 385 | } 386 | }"; 387 | 388 | VerifyCSharpDiagnostic(test); 389 | } 390 | 391 | [TestMethod] 392 | public void Should_find_issue_with_non_matching_return_type() 393 | { 394 | var test = @"using System.Collections.Generic; 395 | using System.Linq; 396 | using System.Net.Http; 397 | using System.Threading.Tasks; 398 | using Microsoft.Azure.WebJobs; 399 | using Microsoft.Azure.WebJobs.Extensions.Http; 400 | using Microsoft.Azure.WebJobs.Host; 401 | using Microsoft.Extensions.Logging; 402 | using System; 403 | 404 | namespace ExternalInteraction 405 | { 406 | public static class HireEmployee 407 | { 408 | [FunctionName(""HireEmployee"")] 409 | public static async Task RunOrchestrator( 410 | [OrchestrationTrigger] DurableOrchestrationContext context, 411 | ILogger log) 412 | { 413 | var applications = context.GetInput>(); 414 | var approvals = await context.CallActivityAsync>(""ApplicationsFiltered"", new String("")); 415 | log.LogInformation($""Approval received. {approvals.Count} applicants approved""); 416 | return approvals.First(); 417 | } 418 | 419 | [FunctionName(""ApplicationsFiltered"")] 420 | public static async Task> Run( 421 | [ActivityTrigger] string userName, 422 | [OrchestrationClient] DurableOrchestrationClient client) 423 | { 424 | await client.RaiseEventAsync(approval.InstanceId, ""ApplicationsFiltered"", approval.Applications); 425 | } 426 | } 427 | }"; 428 | var expected = new DiagnosticResult 429 | { 430 | Id = "DurableFunctionsReturnTypeAnalyzer", 431 | Message = String.Format("Azure function named '{0}' returns '{1}' but '{2}' is expected", "ApplicationsFiltered", "System.Threading.Tasks.Task>", "System.Threading.Tasks.Task>"), 432 | Severity = DiagnosticSeverity.Warning, 433 | Locations = 434 | new[] { 435 | new DiagnosticResultLocation("Test0.cs", 21, 39) 436 | } 437 | }; 438 | VerifyCSharpDiagnostic(test, expected); 439 | } 440 | 441 | 442 | protected override CodeFixProvider GetCSharpCodeFixProvider() 443 | { 444 | return new DurableFunctionsAnalyzerCodeFixProvider(); 445 | } 446 | 447 | protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() 448 | { 449 | return new AnalyzerRegistration(); 450 | } 451 | } 452 | } 453 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.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() 24 | { 25 | return null; 26 | } 27 | 28 | /// 29 | /// Returns the codefix being tested (VB) - to be implemented in non-abstract class 30 | /// 31 | /// The CodeFixProvider to be used for VisualBasic code 32 | protected virtual CodeFixProvider GetBasicCodeFixProvider() 33 | { 34 | return null; 35 | } 36 | 37 | /// 38 | /// Called to test a C# codefix when applied on the inputted string as a source 39 | /// 40 | /// A class in the form of a string before the CodeFix was applied to it 41 | /// A class in the form of a string after the CodeFix was applied to it 42 | /// Index determining which codefix to apply if there are multiple 43 | /// A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied 44 | protected void VerifyCSharpFix(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false) 45 | { 46 | VerifyFix(LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), GetCSharpCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics); 47 | } 48 | 49 | /// 50 | /// Called to test a VB codefix when applied on the inputted string as a source 51 | /// 52 | /// A class in the form of a string before the CodeFix was applied to it 53 | /// A class in the form of a string after the CodeFix was applied to it 54 | /// Index determining which codefix to apply if there are multiple 55 | /// A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied 56 | protected void VerifyBasicFix(string oldSource, string newSource, int? codeFixIndex = null, bool allowNewCompilerDiagnostics = false) 57 | { 58 | VerifyFix(LanguageNames.VisualBasic, GetBasicDiagnosticAnalyzer(), GetBasicCodeFixProvider(), oldSource, newSource, codeFixIndex, allowNewCompilerDiagnostics); 59 | } 60 | 61 | /// 62 | /// General verifier for codefixes. 63 | /// Creates a Document from the source string, then gets diagnostics on it and applies the relevant codefixes. 64 | /// Then gets the string after the codefix is applied and compares it with the expected result. 65 | /// Note: If any codefix causes new diagnostics to show up, the test fails unless allowNewCompilerDiagnostics is set to true. 66 | /// 67 | /// The language the source code is in 68 | /// The analyzer to be applied to the source code 69 | /// The codefix to be applied to the code wherever the relevant Diagnostic is found 70 | /// A class in the form of a string before the CodeFix was applied to it 71 | /// A class in the form of a string after the CodeFix was applied to it 72 | /// Index determining which codefix to apply if there are multiple 73 | /// A bool controlling whether or not the test will fail if the CodeFix introduces other warnings after being applied 74 | private void VerifyFix(string language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex, bool allowNewCompilerDiagnostics) 75 | { 76 | var document = CreateDocument(oldSource, language); 77 | var analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }); 78 | var compilerDiagnostics = GetCompilerDiagnostics(document); 79 | var attempts = analyzerDiagnostics.Length; 80 | 81 | for (int i = 0; i < attempts; ++i) 82 | { 83 | var actions = new List(); 84 | var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None); 85 | codeFixProvider.RegisterCodeFixesAsync(context).Wait(); 86 | 87 | if (!actions.Any()) 88 | { 89 | break; 90 | } 91 | 92 | if (codeFixIndex != null) 93 | { 94 | document = ApplyFix(document, actions.ElementAt((int)codeFixIndex)); 95 | break; 96 | } 97 | 98 | document = ApplyFix(document, actions.ElementAt(0)); 99 | analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document }); 100 | 101 | var newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document)); 102 | 103 | //check if applying the code fix introduced any new compiler diagnostics 104 | if (!allowNewCompilerDiagnostics && newCompilerDiagnostics.Any()) 105 | { 106 | // Format and get the compiler diagnostics again so that the locations make sense in the output 107 | document = document.WithSyntaxRoot(Formatter.Format(document.GetSyntaxRootAsync().Result, Formatter.Annotation, document.Project.Solution.Workspace)); 108 | newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document)); 109 | 110 | Assert.IsTrue(false, 111 | string.Format("Fix introduced new compiler diagnostics:\r\n{0}\r\n\r\nNew document:\r\n{1}\r\n", 112 | string.Join("\r\n", newCompilerDiagnostics.Select(d => d.ToString())), 113 | document.GetSyntaxRootAsync().Result.ToFullString())); 114 | } 115 | 116 | //check if there are analyzer diagnostics left after the code fix 117 | if (!analyzerDiagnostics.Any()) 118 | { 119 | break; 120 | } 121 | } 122 | 123 | //after applying all of the code fixes, compare the resulting string to the inputted one 124 | var actual = GetStringFromDocument(document); 125 | Assert.AreEqual(newSource, actual); 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.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() 21 | { 22 | return null; 23 | } 24 | 25 | /// 26 | /// Get the Visual Basic analyzer being tested (C#) - to be implemented in non-abstract class 27 | /// 28 | protected virtual DiagnosticAnalyzer GetBasicDiagnosticAnalyzer() 29 | { 30 | return null; 31 | } 32 | #endregion 33 | 34 | #region Verifier wrappers 35 | 36 | /// 37 | /// Called to test a C# DiagnosticAnalyzer when applied on the single inputted string as a source 38 | /// Note: input a DiagnosticResult for each Diagnostic expected 39 | /// 40 | /// A class in the form of a string to run the analyzer on 41 | /// DiagnosticResults that should appear after the analyzer is run on the source 42 | protected void VerifyCSharpDiagnostic(string source, params DiagnosticResult[] expected) 43 | { 44 | VerifyDiagnostics(new[] { source }, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), expected); 45 | } 46 | 47 | /// 48 | /// Called to test a VB DiagnosticAnalyzer when applied on the single inputted string as a source 49 | /// Note: input a DiagnosticResult for each Diagnostic expected 50 | /// 51 | /// A class in the form of a string to run the analyzer on 52 | /// DiagnosticResults that should appear after the analyzer is run on the source 53 | protected void VerifyBasicDiagnostic(string source, params DiagnosticResult[] expected) 54 | { 55 | VerifyDiagnostics(new[] { source }, LanguageNames.VisualBasic, GetBasicDiagnosticAnalyzer(), expected); 56 | } 57 | 58 | /// 59 | /// Called to test a C# DiagnosticAnalyzer when applied on the inputted strings as a source 60 | /// Note: input a DiagnosticResult for each Diagnostic expected 61 | /// 62 | /// An array of strings to create source documents from to run the analyzers on 63 | /// DiagnosticResults that should appear after the analyzer is run on the sources 64 | protected void VerifyCSharpDiagnostic(string[] sources, params DiagnosticResult[] expected) 65 | { 66 | VerifyDiagnostics(sources, LanguageNames.CSharp, GetCSharpDiagnosticAnalyzer(), expected); 67 | } 68 | 69 | /// 70 | /// Called to test a VB DiagnosticAnalyzer when applied on the inputted strings as a source 71 | /// Note: input a DiagnosticResult for each Diagnostic expected 72 | /// 73 | /// An array of strings to create source documents from to run the analyzers on 74 | /// DiagnosticResults that should appear after the analyzer is run on the sources 75 | protected void VerifyBasicDiagnostic(string[] sources, params DiagnosticResult[] expected) 76 | { 77 | VerifyDiagnostics(sources, LanguageNames.VisualBasic, GetBasicDiagnosticAnalyzer(), expected); 78 | } 79 | 80 | /// 81 | /// General method that gets a collection of actual diagnostics found in the source after the analyzer is run, 82 | /// then verifies each of them. 83 | /// 84 | /// An array of strings to create source documents from to run the analyzers on 85 | /// The language of the classes represented by the source strings 86 | /// The analyzer to be run on the source code 87 | /// DiagnosticResults that should appear after the analyzer is run on the sources 88 | private void VerifyDiagnostics(string[] sources, string language, DiagnosticAnalyzer analyzer, params DiagnosticResult[] expected) 89 | { 90 | var diagnostics = GetSortedDiagnostics(sources, language, analyzer); 91 | VerifyDiagnosticResults(diagnostics, analyzer, expected); 92 | } 93 | 94 | #endregion 95 | 96 | #region Actual comparisons and verifications 97 | /// 98 | /// Checks each of the actual Diagnostics found and compares them with the corresponding DiagnosticResult in the array of expected results. 99 | /// Diagnostics are considered equal only if the DiagnosticResultLocation, Id, Severity, and Message of the DiagnosticResult match the actual diagnostic. 100 | /// 101 | /// The Diagnostics found by the compiler after running the analyzer on the source code 102 | /// The analyzer that was being run on the sources 103 | /// Diagnostic Results that should have appeared in the code 104 | private static void VerifyDiagnosticResults(IEnumerable actualResults, DiagnosticAnalyzer analyzer, params DiagnosticResult[] expectedResults) 105 | { 106 | int expectedCount = expectedResults.Count(); 107 | int actualCount = actualResults.Count(); 108 | 109 | if (expectedCount != actualCount) 110 | { 111 | string diagnosticsOutput = actualResults.Any() ? FormatDiagnostics(analyzer, actualResults.ToArray()) : " NONE."; 112 | 113 | Assert.IsTrue(false, 114 | string.Format("Mismatch between number of diagnostics returned, expected \"{0}\" actual \"{1}\"\r\n\r\nDiagnostics:\r\n{2}\r\n", expectedCount, actualCount, diagnosticsOutput)); 115 | } 116 | 117 | for (int i = 0; i < expectedResults.Length; i++) 118 | { 119 | var actual = actualResults.ElementAt(i); 120 | var expected = expectedResults[i]; 121 | 122 | if (expected.Line == -1 && expected.Column == -1) 123 | { 124 | if (actual.Location != Location.None) 125 | { 126 | Assert.IsTrue(false, 127 | string.Format("Expected:\nA project diagnostic with No location\nActual:\n{0}", 128 | FormatDiagnostics(analyzer, actual))); 129 | } 130 | } 131 | else 132 | { 133 | VerifyDiagnosticLocation(analyzer, actual, actual.Location, expected.Locations.First()); 134 | var additionalLocations = actual.AdditionalLocations.ToArray(); 135 | 136 | if (additionalLocations.Length != expected.Locations.Length - 1) 137 | { 138 | Assert.IsTrue(false, 139 | string.Format("Expected {0} additional locations but got {1} for Diagnostic:\r\n {2}\r\n", 140 | expected.Locations.Length - 1, additionalLocations.Length, 141 | FormatDiagnostics(analyzer, actual))); 142 | } 143 | 144 | for (int j = 0; j < additionalLocations.Length; ++j) 145 | { 146 | VerifyDiagnosticLocation(analyzer, actual, additionalLocations[j], expected.Locations[j + 1]); 147 | } 148 | } 149 | 150 | if (actual.Id != expected.Id) 151 | { 152 | Assert.IsTrue(false, 153 | string.Format("Expected diagnostic id to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", 154 | expected.Id, actual.Id, FormatDiagnostics(analyzer, actual))); 155 | } 156 | 157 | if (actual.Severity != expected.Severity) 158 | { 159 | Assert.IsTrue(false, 160 | string.Format("Expected diagnostic severity to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", 161 | expected.Severity, actual.Severity, FormatDiagnostics(analyzer, actual))); 162 | } 163 | 164 | if (actual.GetMessage() != expected.Message) 165 | { 166 | Assert.IsTrue(false, 167 | string.Format("Expected diagnostic message to be \"{0}\" was \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", 168 | expected.Message, actual.GetMessage(), FormatDiagnostics(analyzer, actual))); 169 | } 170 | } 171 | } 172 | 173 | /// 174 | /// Helper method to VerifyDiagnosticResult that checks the location of a diagnostic and compares it with the location in the expected DiagnosticResult. 175 | /// 176 | /// The analyzer that was being run on the sources 177 | /// The diagnostic that was found in the code 178 | /// The Location of the Diagnostic found in the code 179 | /// The DiagnosticResultLocation that should have been found 180 | private static void VerifyDiagnosticLocation(DiagnosticAnalyzer analyzer, Diagnostic diagnostic, Location actual, DiagnosticResultLocation expected) 181 | { 182 | var actualSpan = actual.GetLineSpan(); 183 | 184 | Assert.IsTrue(actualSpan.Path == expected.Path || (actualSpan.Path != null && actualSpan.Path.Contains("Test0.") && expected.Path.Contains("Test.")), 185 | string.Format("Expected diagnostic to be in file \"{0}\" was actually in file \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", 186 | expected.Path, actualSpan.Path, FormatDiagnostics(analyzer, diagnostic))); 187 | 188 | var actualLinePosition = actualSpan.StartLinePosition; 189 | 190 | // Only check line position if there is an actual line in the real diagnostic 191 | if (actualLinePosition.Line > 0) 192 | { 193 | if (actualLinePosition.Line + 1 != expected.Line) 194 | { 195 | Assert.IsTrue(false, 196 | string.Format("Expected diagnostic to be on line \"{0}\" was actually on line \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", 197 | expected.Line, actualLinePosition.Line + 1, FormatDiagnostics(analyzer, diagnostic))); 198 | } 199 | } 200 | 201 | // Only check column position if there is an actual column position in the real diagnostic 202 | if (actualLinePosition.Character > 0) 203 | { 204 | if (actualLinePosition.Character + 1 != expected.Column) 205 | { 206 | Assert.IsTrue(false, 207 | string.Format("Expected diagnostic to start at column \"{0}\" was actually at column \"{1}\"\r\n\r\nDiagnostic:\r\n {2}\r\n", 208 | expected.Column, actualLinePosition.Character + 1, FormatDiagnostics(analyzer, diagnostic))); 209 | } 210 | } 211 | } 212 | #endregion 213 | 214 | #region Formatting Diagnostics 215 | /// 216 | /// Helper method to format a Diagnostic into an easily readable string 217 | /// 218 | /// The analyzer that this verifier tests 219 | /// The Diagnostics to be formatted 220 | /// The Diagnostics formatted as a string 221 | private static string FormatDiagnostics(DiagnosticAnalyzer analyzer, params Diagnostic[] diagnostics) 222 | { 223 | var builder = new StringBuilder(); 224 | for (int i = 0; i < diagnostics.Length; ++i) 225 | { 226 | builder.AppendLine("// " + diagnostics[i].ToString()); 227 | 228 | var analyzerType = analyzer.GetType(); 229 | var rules = analyzer.SupportedDiagnostics; 230 | 231 | foreach (var rule in rules) 232 | { 233 | if (rule != null && rule.Id == diagnostics[i].Id) 234 | { 235 | var location = diagnostics[i].Location; 236 | if (location == Location.None) 237 | { 238 | builder.AppendFormat("GetGlobalResult({0}.{1})", analyzerType.Name, rule.Id); 239 | } 240 | else 241 | { 242 | Assert.IsTrue(location.IsInSource, 243 | $"Test base does not currently handle diagnostics in metadata locations. Diagnostic in metadata: {diagnostics[i]}\r\n"); 244 | 245 | string resultMethodName = diagnostics[i].Location.SourceTree.FilePath.EndsWith(".cs") ? "GetCSharpResultAt" : "GetBasicResultAt"; 246 | var linePosition = diagnostics[i].Location.GetLineSpan().StartLinePosition; 247 | 248 | builder.AppendFormat("{0}({1}, {2}, {3}.{4})", 249 | resultMethodName, 250 | linePosition.Line + 1, 251 | linePosition.Character + 1, 252 | analyzerType.Name, 253 | rule.Id); 254 | } 255 | 256 | if (i != diagnostics.Length - 1) 257 | { 258 | builder.Append(','); 259 | } 260 | 261 | builder.AppendLine(); 262 | break; 263 | } 264 | } 265 | } 266 | return builder.ToString(); 267 | } 268 | #endregion 269 | } 270 | } 271 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Vsix/DurableFunctionsAnalyzer.Vsix.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 15.0 7 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 8 | 9 | 10 | 14.0 11 | 12 | 13 | 14 | 15 | 16 | 17 | Debug 18 | AnyCPU 19 | AnyCPU 20 | 2.0 21 | {82b43b9b-a64c-4715-b499-d71e9ca2bd60};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 22 | {36A03A1A-2728-484D-8C3B-FBBE710207A5} 23 | Library 24 | Properties 25 | DurableFunctionsAnalyzer.Vsix 26 | DurableFunctionsAnalyzer 27 | v4.6.1 28 | false 29 | false 30 | false 31 | false 32 | false 33 | false 34 | Roslyn 35 | 36 | 37 | true 38 | full 39 | false 40 | bin\Debug\ 41 | DEBUG;TRACE 42 | prompt 43 | 4 44 | 45 | 46 | pdbonly 47 | true 48 | bin\Release\ 49 | TRACE 50 | prompt 51 | 4 52 | 53 | 54 | Program 55 | $(DevEnvDir)devenv.exe 56 | /rootsuffix Roslyn 57 | 58 | 59 | 60 | 61 | Designer 62 | 63 | 64 | 65 | 66 | {4051DDC7-5ED3-4C0D-98D5-AA9DEE3FEB62} 67 | DurableFunctionsAnalyzer 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 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}. 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Vsix/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.Vsix/source.extension.vsixmanifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | DurableFunctionsAnalyzer 6 | This is a extension which adds a series of warnings and errors to your Azure Durable Functions project to catch common errors 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/AnalyzerRegistration.cs: -------------------------------------------------------------------------------- 1 | using DurableFunctionsAnalyzer.Analyzers; 2 | using Microsoft.CodeAnalysis; 3 | using Microsoft.CodeAnalysis.CSharp; 4 | using Microsoft.CodeAnalysis.Diagnostics; 5 | using System; 6 | using System.Collections.Immutable; 7 | using System.Linq; 8 | 9 | namespace DurableFunctionsAnalyzer 10 | { 11 | [DiagnosticAnalyzer(LanguageNames.CSharp)] 12 | public class AnalyzerRegistration : DiagnosticAnalyzer 13 | { 14 | public const string DiagnosticId = "DurableFunctionsAnalyzer"; 15 | 16 | 17 | public override ImmutableArray SupportedDiagnostics 18 | { 19 | get 20 | { 21 | return ImmutableArray.Create( 22 | NameAnalyzer.CloseRule, 23 | NameAnalyzer.MissingRule, 24 | ArgumentAnalyzer.Rule, 25 | ReturnTypeAnalyzer.Rule, 26 | OrchestrationTriggerAnnotationAnalyzer.Rule); 27 | } 28 | } 29 | 30 | public override void Initialize(AnalysisContext context) 31 | { 32 | var nameAnalyzer = new NameAnalyzer(); 33 | var argumentAnalyzer = new ArgumentAnalyzer(); 34 | var returnTypeAnalyzer = new ReturnTypeAnalyzer(); 35 | var baseAnalyzer = new BaseFunctionAnalyzer(); 36 | var orchestrationTriggerAnalyzer = new OrchestrationTriggerAnnotationAnalyzer(); 37 | baseAnalyzer.RegisterAnalyzer(nameAnalyzer); 38 | baseAnalyzer.RegisterAnalyzer(argumentAnalyzer); 39 | baseAnalyzer.RegisterAnalyzer(returnTypeAnalyzer); 40 | context.RegisterSyntaxNodeAction(orchestrationTriggerAnalyzer.FindOrchestrationTriggers, SyntaxKind.Attribute); 41 | context.RegisterCompilationStartAction(c => 42 | { 43 | c.RegisterCompilationEndAction(baseAnalyzer.ReportProblems); 44 | c.RegisterSyntaxNodeAction(baseAnalyzer.FindActivityCalls, SyntaxKind.InvocationExpression); 45 | c.RegisterSyntaxNodeAction(baseAnalyzer.FindActivities, SyntaxKind.Attribute); 46 | }); 47 | } 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/AnalyzerRegistration.cs.orig: -------------------------------------------------------------------------------- 1 | using DurableFunctionsAnalyzer.Analyzers; 2 | using Microsoft.CodeAnalysis; 3 | using Microsoft.CodeAnalysis.CSharp; 4 | using Microsoft.CodeAnalysis.Diagnostics; 5 | using System; 6 | using System.Collections.Immutable; 7 | using System.Linq; 8 | 9 | namespace DurableFunctionsAnalyzer 10 | { 11 | [DiagnosticAnalyzer(LanguageNames.CSharp)] 12 | public class AnalyzerRegistration : DiagnosticAnalyzer 13 | { 14 | public const string DiagnosticId = "DurableFunctionsAnalyzer"; 15 | 16 | 17 | public override ImmutableArray SupportedDiagnostics 18 | { 19 | get 20 | { 21 | <<<<<<< HEAD:DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/NameAnalyzerRegistration.cs 22 | return ImmutableArray.Create( 23 | NameAnalyzer.CloseRule, 24 | NameAnalyzer.MissingRule, 25 | ArgumentAnalyzer.Rule, 26 | ReturnTypeAnalyzer.Rule, 27 | OrchestrationTriggerAnnotationAnalyzer.Rule); 28 | ======= 29 | return ImmutableArray.Create(NameAnalyzer.CloseRule, 30 | NameAnalyzer.MissingRule, 31 | ArgumentAnalyzer.Rule, 32 | ReturnTypeAnalyzer.Rule); 33 | >>>>>>> origin/master:DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/AnalyzerRegistration.cs 34 | } 35 | } 36 | 37 | public override void Initialize(AnalysisContext context) 38 | { 39 | var nameAnalyzer = new NameAnalyzer(); 40 | var argumentAnalyzer = new ArgumentAnalyzer(); 41 | var returnTypeAnalyzer = new ReturnTypeAnalyzer(); 42 | var baseAnalyzer = new BaseFunctionAnalyzer(); 43 | var orchestrationTriggerAnalyzer = new OrchestrationTriggerAnnotationAnalyzer(); 44 | baseAnalyzer.RegisterAnalyzer(nameAnalyzer); 45 | baseAnalyzer.RegisterAnalyzer(argumentAnalyzer); 46 | baseAnalyzer.RegisterAnalyzer(returnTypeAnalyzer); 47 | <<<<<<< HEAD:DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/NameAnalyzerRegistration.cs 48 | context.RegisterSyntaxNodeAction(orchestrationTriggerAnalyzer.FindOrchestrationTriggers, SyntaxKind.Attribute); 49 | ======= 50 | >>>>>>> origin/master:DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/AnalyzerRegistration.cs 51 | context.RegisterCompilationStartAction(c => 52 | { 53 | c.RegisterCompilationEndAction(baseAnalyzer.ReportProblems); 54 | c.RegisterSyntaxNodeAction(baseAnalyzer.FindActivityCalls, SyntaxKind.InvocationExpression); 55 | c.RegisterSyntaxNodeAction(baseAnalyzer.FindActivities, SyntaxKind.Attribute); 56 | }); 57 | } 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/Analyzers/ArgumentAnalyzer.cs: -------------------------------------------------------------------------------- 1 | using DurableFunctionsAnalyzer.Models; 2 | using Microsoft.CodeAnalysis; 3 | using Microsoft.CodeAnalysis.Diagnostics; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | 8 | namespace DurableFunctionsAnalyzer.Analyzers 9 | { 10 | class ArgumentAnalyzer : IFunctionAnalyzer 11 | { 12 | public const string DiagnosticId = "DurableFunctionsArgumentAnalyzer"; 13 | private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.ArgumentAnalyzerTitle), Resources.ResourceManager, typeof(Resources)); 14 | private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.ArgumentAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources)); 15 | private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.ArgumentAnalyzerDescription), Resources.ResourceManager, typeof(Resources)); 16 | private const string Category = "Argument"; 17 | 18 | public static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description); 19 | 20 | 21 | public void ReportProblems(CompilationAnalysisContext cac, IEnumerable availableFunctions, IEnumerable calledFunctions) 22 | { 23 | foreach (var node in calledFunctions) 24 | { 25 | if (availableFunctions.Where(x => x.Name == node.Name).Any()) 26 | { 27 | var functionDefinition = availableFunctions.Where(x => x.Name == node.Name).SingleOrDefault(); 28 | if (functionDefinition.ActivityTriggerType != node.ParameterType) 29 | { 30 | cac.ReportDiagnostic(Diagnostic.Create(Rule, node.ParameterNode.GetLocation(), node.Name, functionDefinition.ActivityTriggerType, node.ParameterType)); 31 | } 32 | } 33 | 34 | } 35 | } 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/Analyzers/BaseFunctionAnalyzer.cs: -------------------------------------------------------------------------------- 1 | using DurableFunctionsAnalyzer.Models; 2 | using Microsoft.CodeAnalysis; 3 | using Microsoft.CodeAnalysis.CSharp; 4 | using Microsoft.CodeAnalysis.CSharp.Syntax; 5 | using Microsoft.CodeAnalysis.Diagnostics; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.IO; 9 | using System.Linq; 10 | 11 | namespace DurableFunctionsAnalyzer.Analyzers 12 | { 13 | class BaseFunctionAnalyzer 14 | { 15 | List _availableFunctions = new List(); 16 | List _calledFunctions = new List(); 17 | List _analyzers = new List(); 18 | 19 | public void ReportProblems(CompilationAnalysisContext cac) 20 | { 21 | try 22 | { 23 | foreach (var analyzer in _analyzers) 24 | analyzer.ReportProblems(cac, _availableFunctions, _calledFunctions); 25 | } 26 | catch (Exception ex) 27 | { 28 | File.WriteAllText(@"c:\temp\analyzer.txt", ex.ToString()); 29 | throw; 30 | } 31 | } 32 | 33 | public void RegisterAnalyzer(IFunctionAnalyzer analyzer) 34 | { 35 | _analyzers.Add(analyzer); 36 | } 37 | public void FindActivityCalls(SyntaxNodeAnalysisContext context) 38 | { 39 | var invocationExpression = context.Node as InvocationExpressionSyntax; 40 | if (invocationExpression != null) 41 | { 42 | 43 | var expression = invocationExpression.Expression as MemberAccessExpressionSyntax; 44 | if (expression != null) 45 | { 46 | var name = expression.Name; 47 | if (name.ToString().StartsWith("CallActivityAsync") || name.ToString().StartsWith("CallActivityWithRetryAsync")) 48 | { 49 | var functionName = invocationExpression.ArgumentList.Arguments.FirstOrDefault(); 50 | var argumentType = invocationExpression.ArgumentList.Arguments.Last(); 51 | var returnType = invocationExpression.ChildNodes().Where(x => x.IsKind(SyntaxKind.SimpleMemberAccessExpression)) 52 | .FirstOrDefault()? 53 | .ChildNodes() 54 | .Where(x => x.IsKind(SyntaxKind.GenericName)) 55 | .FirstOrDefault()? 56 | .ChildNodes() 57 | .Where(x => x.IsKind(SyntaxKind.TypeArgumentList))? 58 | .FirstOrDefault(); 59 | var returnTypeName = "System.Threading.Tasks.Task"; 60 | if (returnType != null) 61 | { 62 | returnTypeName = GetQualifiedTypeName(context.SemanticModel.GetTypeInfo(returnType.ChildNodes().FirstOrDefault()).Type); 63 | returnTypeName = "System.Threading.Tasks.Task<" + returnTypeName + ">"; 64 | 65 | } 66 | var typeInfo = context.SemanticModel.GetTypeInfo(argumentType.ChildNodes().First()); 67 | var typeName = ""; 68 | if (typeInfo.Type == null) 69 | return; 70 | typeName = GetQualifiedTypeName(typeInfo.Type); 71 | if (functionName != null && functionName.ToString().StartsWith("\"")) 72 | _calledFunctions.Add(new FunctionCall 73 | { 74 | Name = functionName.ToString().Trim('"'), 75 | NameNode = functionName, 76 | ParameterNode = argumentType, 77 | ParameterType = typeName, 78 | ExpectedReturnType = returnTypeName, 79 | ExpectedReturnTypeNode = invocationExpression 80 | }); 81 | } 82 | } 83 | } 84 | } 85 | 86 | private string GetQualifiedTypeName(ITypeSymbol typeInfo) 87 | { 88 | var tupleunderlyingtype = (typeInfo as INamedTypeSymbol).TupleUnderlyingType; 89 | if (tupleunderlyingtype != null) 90 | { 91 | return $"Tuple<{string.Join(", ", tupleunderlyingtype.TypeArguments.Select(x => GetQualifiedTypeName(x)))}>"; 92 | } 93 | 94 | var namedSymbol = typeInfo as INamedTypeSymbol; 95 | var genericType = ""; 96 | if (namedSymbol.TypeArguments.Any()) 97 | { 98 | genericType = "<" + GetQualifiedTypeName(namedSymbol.TypeArguments.First()) + ">"; 99 | } 100 | var typeName = ""; 101 | if (typeInfo.OriginalDefinition.ContainingNamespace.ToString() != "") 102 | typeName = typeInfo.OriginalDefinition.ContainingNamespace + "." + typeInfo.OriginalDefinition?.Name; 103 | else 104 | typeName = "System." + typeInfo.OriginalDefinition?.Name; 105 | var returnType = typeName + genericType; 106 | if (returnType == "System.Int") 107 | return returnType + "32"; 108 | return returnType; 109 | } 110 | 111 | public void FindActivities(SyntaxNodeAnalysisContext context) 112 | { 113 | var attributeExpression = context.Node as AttributeSyntax; 114 | if (attributeExpression != null && attributeExpression.ChildNodes().First().ToString() == "FunctionName") 115 | { 116 | var didAdd = false; 117 | var functionName = attributeExpression.ArgumentList.Arguments.First().ToString().Trim('"'); 118 | var parameterList = attributeExpression.Parent.Parent.ChildNodes().Where(x => x.IsKind(SyntaxKind.ParameterList)).SingleOrDefault(); 119 | var methodDeclaration = attributeExpression.Parent.Parent; 120 | while (!methodDeclaration.IsKind(SyntaxKind.MethodDeclaration)) 121 | { 122 | methodDeclaration = methodDeclaration.Parent; 123 | } 124 | if (parameterList != null) 125 | { 126 | foreach (var parameter in parameterList.ChildNodes().Where(x => x.IsKind(SyntaxKind.Parameter))) 127 | { 128 | foreach (var attributeList in parameter.ChildNodes().Where(x => x.IsKind(SyntaxKind.AttributeList))) 129 | { 130 | foreach (var attribute in attributeList.ChildNodes().Where(x => x.IsKind(SyntaxKind.Attribute))) 131 | { 132 | if ((attribute as AttributeSyntax).Name.ToString() == "ActivityTrigger") 133 | { 134 | var kindName = parameter.ChildNodes().Where(x => x.IsKind(SyntaxKind.IdentifierName) || 135 | x.IsKind(SyntaxKind.GenericName) || 136 | x.IsKind(SyntaxKind.TupleType)).SingleOrDefault(); 137 | if (kindName == null) 138 | { 139 | //predefined types 140 | kindName = parameter.ChildNodes().Where(x => x.IsKind(SyntaxKind.PredefinedType)).SingleOrDefault(); 141 | } 142 | if (kindName != null) 143 | { 144 | var typeInfo = context.SemanticModel.GetTypeInfo(kindName); 145 | //((Microsoft.CodeAnalysis.CSharp.Symbols.TupleTypeSymbol)typeInfo.Type).TupleElements 146 | string returnTypeName = ""; 147 | returnTypeName = GetQualifiedTypeName(context.SemanticModel.GetTypeInfo((methodDeclaration as MethodDeclarationSyntax).ReturnType).Type); 148 | _availableFunctions.Add(new FunctionDefinition 149 | { 150 | Name = functionName, 151 | ActivityTriggerType = GetQualifiedTypeName(typeInfo.Type), 152 | ReturnType = returnTypeName 153 | }); 154 | 155 | didAdd = true; 156 | } 157 | } 158 | } 159 | } 160 | } 161 | } 162 | if (!didAdd) 163 | { 164 | _availableFunctions.Add(new FunctionDefinition 165 | { 166 | Name = functionName 167 | }); 168 | } 169 | } 170 | } 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/Analyzers/IFunctionAnalyzer.cs: -------------------------------------------------------------------------------- 1 | using DurableFunctionsAnalyzer.Models; 2 | using Microsoft.CodeAnalysis.Diagnostics; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace DurableFunctionsAnalyzer.Analyzers 8 | { 9 | interface IFunctionAnalyzer 10 | { 11 | void ReportProblems(CompilationAnalysisContext cac, IEnumerable availableFunctions, IEnumerable calledFunctions); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/Analyzers/NameAnalyzer.cs: -------------------------------------------------------------------------------- 1 | using DurableFunctionsAnalyzer.Extensions; 2 | using DurableFunctionsAnalyzer.Models; 3 | using Microsoft.CodeAnalysis; 4 | using Microsoft.CodeAnalysis.Diagnostics; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | 9 | namespace DurableFunctionsAnalyzer.Analyzers 10 | { 11 | class NameAnalyzer : IFunctionAnalyzer 12 | { 13 | public const string DiagnosticId = "DurableFunctionsNameAnalyzer"; 14 | private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.NameAnalyzerTitle), Resources.ResourceManager, typeof(Resources)); 15 | private static readonly LocalizableString CloseMessageFormat = new LocalizableResourceString(nameof(Resources.NameAnalyzerCloseMessageFormat), Resources.ResourceManager, typeof(Resources)); 16 | private static readonly LocalizableString MissingMessageFormat = new LocalizableResourceString(nameof(Resources.NameAnalyzerMissingMessageFormat), Resources.ResourceManager, typeof(Resources)); 17 | private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.NameAnalyzerDescription), Resources.ResourceManager, typeof(Resources)); 18 | private const string Category = "Naming"; 19 | 20 | public static DiagnosticDescriptor CloseRule = new DiagnosticDescriptor(DiagnosticId, Title, CloseMessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description); 21 | public static DiagnosticDescriptor MissingRule = new DiagnosticDescriptor(DiagnosticId, Title, MissingMessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description); 22 | 23 | 24 | private string GetClosestString(string name, IEnumerable availableNames) 25 | { 26 | return availableNames.OrderBy(x => x.LevenshteinDistance(name)).First(); 27 | } 28 | 29 | public void ReportProblems(CompilationAnalysisContext cac, IEnumerable availableFunctions, IEnumerable calledFunctions) 30 | { 31 | foreach (var node in calledFunctions) 32 | { 33 | if (!availableFunctions.Any()) 34 | { 35 | cac.ReportDiagnostic(Diagnostic.Create(MissingRule, node.NameNode.GetLocation(), node.Name)); 36 | } 37 | else if (!availableFunctions.Select(x => x.Name).Contains(node.Name)) 38 | { 39 | cac.ReportDiagnostic(Diagnostic.Create(CloseRule, node.NameNode.GetLocation(), node.Name, GetClosestString(node.Name, availableFunctions.Select(x => x.Name)))); 40 | } 41 | } 42 | } 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/Analyzers/OrchestrationTriggerAnnotationAnalyzer.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis.Diagnostics; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using System.Linq; 6 | using Microsoft.CodeAnalysis.CSharp; 7 | using Microsoft.CodeAnalysis; 8 | 9 | namespace DurableFunctionsAnalyzer.Analyzers 10 | { 11 | public class OrchestrationTriggerAnnotationAnalyzer 12 | { 13 | public const string DiagnosticId = "DurableFunctionOrchestrationTriggerAnalyzer"; 14 | private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.OrchestrationTriggerAnalyzerTitle), Resources.ResourceManager, typeof(Resources)); 15 | private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.OrchestrationTriggerAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources)); 16 | private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.OrchestrationTriggerAnalyzerDescription), Resources.ResourceManager, typeof(Resources)); 17 | private const string Category = "Argument"; 18 | 19 | public static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description); 20 | 21 | public void FindOrchestrationTriggers(SyntaxNodeAnalysisContext context) 22 | { 23 | if (context.Node.ToString() == "OrchestrationTrigger") 24 | { 25 | var parameter = context.Node.Parent.Parent; 26 | var identifierNames = parameter.ChildNodes().Where(x => x.IsKind(SyntaxKind.IdentifierName)); 27 | if(!identifierNames.Any() || (identifierNames.First().ToString()!= "DurableOrchestrationContext" && identifierNames.First().ToString() != "DurableOrchestrationContextBase")) 28 | { 29 | context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation())); 30 | } 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/Analyzers/ReturnTypeAnalyzer.cs: -------------------------------------------------------------------------------- 1 | using DurableFunctionsAnalyzer.Models; 2 | using Microsoft.CodeAnalysis; 3 | using Microsoft.CodeAnalysis.Diagnostics; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | 8 | 9 | namespace DurableFunctionsAnalyzer.Analyzers 10 | { 11 | class ReturnTypeAnalyzer : IFunctionAnalyzer 12 | { 13 | public const string DiagnosticId = "DurableFunctionsReturnTypeAnalyzer"; 14 | private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.ReturnTypeAnalyzerTitle), Resources.ResourceManager, typeof(Resources)); 15 | private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.ReturnTypeAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources)); 16 | private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.ReturnTypeAnalyzerDescription), Resources.ResourceManager, typeof(Resources)); 17 | private const string Category = "Naming"; 18 | 19 | public static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description); 20 | 21 | 22 | public void ReportProblems(CompilationAnalysisContext cac, IEnumerable availableFunctions, IEnumerable calledFunctions) 23 | { 24 | foreach (var node in calledFunctions) 25 | { 26 | var functionDefinition = availableFunctions.Where(x => x.Name == node.Name).SingleOrDefault(); 27 | if (functionDefinition != null) 28 | { 29 | 30 | if (functionDefinition.ReturnType != node.ExpectedReturnType && 31 | !(functionDefinition.ReturnType == "System.Void" && node.ExpectedReturnType == "System.Threading.Tasks.Task"))//handle when the return type is void 32 | { 33 | if ($"System.Threading.Tasks.Task<{functionDefinition.ReturnType}>" != node.ExpectedReturnType)//Handle cases where the activity isn't async 34 | cac.ReportDiagnostic(Diagnostic.Create(Rule, node.ExpectedReturnTypeNode.GetLocation(), node.Name, functionDefinition.ReturnType, node.ExpectedReturnType)); 35 | } 36 | } 37 | } 38 | } 39 | 40 | 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/DurableFunctionsAnalyzer.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard1.3 5 | false 6 | True 7 | 8 | 9 | 10 | DurableFunctionsAnalyzer 11 | 1.0.0.0 12 | stimms 13 | https://github.com/stimms/DurableFunctionsAnalyzer/blob/master/LICENSE 14 | https://github.com/stimms/DurableFunctionsAnalyzer 15 | https://github.com/stimms/DurableFunctionsAnalyzer 16 | false 17 | DurableFunctionsAnalyzer 18 | Summary of changes made in this release of the package. 19 | Copyright 20 | DurableFunctionsAnalyzer, analyzers 21 | true 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/DurableFunctionsAnalyzerCodeFixProvider.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 | 16 | namespace DurableFunctionsAnalyzer 17 | { 18 | [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(DurableFunctionsAnalyzerCodeFixProvider)), Shared] 19 | public class DurableFunctionsAnalyzerCodeFixProvider : CodeFixProvider 20 | { 21 | private const string title = "Ma"; 22 | 23 | public sealed override ImmutableArray FixableDiagnosticIds 24 | { 25 | get { return ImmutableArray.Create(AnalyzerRegistration.DiagnosticId); } 26 | } 27 | 28 | public sealed override FixAllProvider GetFixAllProvider() 29 | { 30 | // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/FixAllProvider.md for more information on Fix All Providers 31 | return WellKnownFixAllProviders.BatchFixer; 32 | } 33 | 34 | public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) 35 | { 36 | var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); 37 | 38 | // TODO: Replace the following code with your own analysis, generating a CodeAction for each fix to suggest 39 | var diagnostic = context.Diagnostics.First(); 40 | var diagnosticSpan = diagnostic.Location.SourceSpan; 41 | 42 | // Find the type declaration identified by the diagnostic. 43 | var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType().First(); 44 | 45 | // Register a code action that will invoke the fix. 46 | context.RegisterCodeFix( 47 | CodeAction.Create( 48 | title: title, 49 | createChangedSolution: c => MakeUppercaseAsync(context.Document, declaration, c), 50 | equivalenceKey: title), 51 | diagnostic); 52 | } 53 | 54 | private async Task MakeUppercaseAsync(Document document, TypeDeclarationSyntax typeDecl, CancellationToken cancellationToken) 55 | { 56 | // Compute new uppercase name. 57 | var identifierToken = typeDecl.Identifier; 58 | var newName = identifierToken.Text.ToUpperInvariant(); 59 | 60 | // Get the symbol representing the type to be renamed. 61 | var semanticModel = await document.GetSemanticModelAsync(cancellationToken); 62 | var typeSymbol = semanticModel.GetDeclaredSymbol(typeDecl, cancellationToken); 63 | 64 | // Produce a new solution that has all references to that type renamed, including the declaration. 65 | var originalSolution = document.Project.Solution; 66 | var optionSet = originalSolution.Workspace.Options; 67 | var newSolution = await Renamer.RenameSymbolAsync(document.Project.Solution, typeSymbol, newName, optionSet, cancellationToken).ConfigureAwait(false); 68 | 69 | // Return the new solution with the now-uppercase type name. 70 | return newSolution; 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/Extensions/StringExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DurableFunctionsAnalyzer.Extensions 6 | { 7 | public static class StringExtensions 8 | { 9 | // This code is an implementation of the pseudocode from the Wikipedia, 10 | // showing a naive implementation. 11 | // You should research an algorithm with better space complexity. 12 | public static int LevenshteinDistance(this string baseString, string comparisonString) 13 | { 14 | int baseLength = baseString.Length; 15 | int comparisonLength = comparisonString.Length; 16 | int[,] d = new int[baseLength + 1, comparisonLength + 1]; 17 | if (baseLength == 0) 18 | { 19 | return comparisonLength; 20 | } 21 | if (comparisonLength == 0) 22 | { 23 | return baseLength; 24 | } 25 | for (int i = 0; i <= baseLength; d[i, 0] = i++) 26 | ; 27 | for (int j = 0; j <= comparisonLength; d[0, j] = j++) 28 | ; 29 | for (int i = 1; i <= baseLength; i++) 30 | { 31 | for (int j = 1; j <= comparisonLength; j++) 32 | { 33 | int cost = (comparisonString[j - 1] == baseString[i - 1]) ? 0 : 1; 34 | d[i, j] = Math.Min( 35 | Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), 36 | d[i - 1, j - 1] + cost); 37 | } 38 | } 39 | return d[baseLength, comparisonLength]; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/Models/FunctionCall.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace DurableFunctionsAnalyzer.Models 7 | { 8 | class FunctionCall 9 | { 10 | public string Name { get; set; } 11 | public SyntaxNode NameNode { get; set; } 12 | public SyntaxNode ParameterNode { get; set; } 13 | public String ParameterType { get; set; } 14 | public string ExpectedReturnType { get; set; } 15 | public SyntaxNode ExpectedReturnTypeNode { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/Models/FunctionDefinition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace DurableFunctionsAnalyzer.Models 6 | { 7 | class FunctionDefinition 8 | { 9 | public string Name { get; set; } 10 | public string ActivityTriggerType { get; set; } 11 | public string ReturnType { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/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 DurableFunctionsAnalyzer { 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", "15.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("DurableFunctionsAnalyzer.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 Function call parameters don't match function. 66 | /// 67 | internal static string ArgumentAnalyzerDescription { 68 | get { 69 | return ResourceManager.GetString("ArgumentAnalyzerDescription", resourceCulture); 70 | } 71 | } 72 | 73 | /// 74 | /// Looks up a localized string similar to Azure function named '{0}' takes a '{1}' but was given a '{2}'. 75 | /// 76 | internal static string ArgumentAnalyzerMessageFormat { 77 | get { 78 | return ResourceManager.GetString("ArgumentAnalyzerMessageFormat", resourceCulture); 79 | } 80 | } 81 | 82 | /// 83 | /// Looks up a localized string similar to Azure Durable Function Paramter Type Check. 84 | /// 85 | internal static string ArgumentAnalyzerTitle { 86 | get { 87 | return ResourceManager.GetString("ArgumentAnalyzerTitle", resourceCulture); 88 | } 89 | } 90 | 91 | /// 92 | /// Looks up a localized string similar to Azure function named '{0}' does not exist. Did you mean '{1}'?. 93 | /// 94 | internal static string NameAnalyzerCloseMessageFormat { 95 | get { 96 | return ResourceManager.GetString("NameAnalyzerCloseMessageFormat", resourceCulture); 97 | } 98 | } 99 | 100 | /// 101 | /// Looks up a localized string similar to Function call references unknown Azure Function.. 102 | /// 103 | internal static string NameAnalyzerDescription { 104 | get { 105 | return ResourceManager.GetString("NameAnalyzerDescription", resourceCulture); 106 | } 107 | } 108 | 109 | /// 110 | /// Looks up a localized string similar to Azure function named '{0}' does not exist. Could not find any function registrations.. 111 | /// 112 | internal static string NameAnalyzerMissingMessageFormat { 113 | get { 114 | return ResourceManager.GetString("NameAnalyzerMissingMessageFormat", resourceCulture); 115 | } 116 | } 117 | 118 | /// 119 | /// Looks up a localized string similar to Azure Durable Function Name Check. 120 | /// 121 | internal static string NameAnalyzerTitle { 122 | get { 123 | return ResourceManager.GetString("NameAnalyzerTitle", resourceCulture); 124 | } 125 | } 126 | 127 | /// 128 | /// Looks up a localized string similar to OrchestrationTrigger must be attached to a DurableOrchestrationContext. 129 | /// 130 | internal static string OrchestrationTriggerAnalyzerDescription { 131 | get { 132 | return ResourceManager.GetString("OrchestrationTriggerAnalyzerDescription", resourceCulture); 133 | } 134 | } 135 | 136 | /// 137 | /// Looks up a localized string similar to OrchestrationTrigger must be attached to a DurableOrchestrationContext. 138 | /// 139 | internal static string OrchestrationTriggerAnalyzerMessageFormat { 140 | get { 141 | return ResourceManager.GetString("OrchestrationTriggerAnalyzerMessageFormat", resourceCulture); 142 | } 143 | } 144 | 145 | /// 146 | /// Looks up a localized string similar to Orchestration Trigger Usage. 147 | /// 148 | internal static string OrchestrationTriggerAnalyzerTitle { 149 | get { 150 | return ResourceManager.GetString("OrchestrationTriggerAnalyzerTitle", resourceCulture); 151 | } 152 | } 153 | 154 | /// 155 | /// Looks up a localized string similar to Function return type doesn't match. 156 | /// 157 | internal static string ReturnTypeAnalyzerDescription { 158 | get { 159 | return ResourceManager.GetString("ReturnTypeAnalyzerDescription", resourceCulture); 160 | } 161 | } 162 | 163 | /// 164 | /// Looks up a localized string similar to Azure function named '{0}' returns '{1}' but '{2}' is expected. 165 | /// 166 | internal static string ReturnTypeAnalyzerMessageFormat { 167 | get { 168 | return ResourceManager.GetString("ReturnTypeAnalyzerMessageFormat", resourceCulture); 169 | } 170 | } 171 | 172 | /// 173 | /// Looks up a localized string similar to Azure Durable Function Return Type Check. 174 | /// 175 | internal static string ReturnTypeAnalyzerTitle { 176 | get { 177 | return ResourceManager.GetString("ReturnTypeAnalyzerTitle", resourceCulture); 178 | } 179 | } 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/Resources.Designer.cs.orig: -------------------------------------------------------------------------------- 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 DurableFunctionsAnalyzer { 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", "15.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("DurableFunctionsAnalyzer.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 Function call parameters don't match function. 66 | /// 67 | internal static string ArgumentAnalyzerDescription { 68 | get { 69 | return ResourceManager.GetString("ArgumentAnalyzerDescription", resourceCulture); 70 | } 71 | } 72 | 73 | /// 74 | /// Looks up a localized string similar to Azure function named '{0}' takes a '{1}' but was given a '{2}'. 75 | /// 76 | internal static string ArgumentAnalyzerMessageFormat { 77 | get { 78 | return ResourceManager.GetString("ArgumentAnalyzerMessageFormat", resourceCulture); 79 | } 80 | } 81 | 82 | /// 83 | /// Looks up a localized string similar to Azure Durable Function Paramter Type Check. 84 | /// 85 | internal static string ArgumentAnalyzerTitle { 86 | get { 87 | return ResourceManager.GetString("ArgumentAnalyzerTitle", resourceCulture); 88 | } 89 | } 90 | 91 | /// 92 | /// Looks up a localized string similar to Azure function named '{0}' does not exist. Did you mean '{1}'?. 93 | /// 94 | internal static string NameAnalyzerCloseMessageFormat { 95 | get { 96 | return ResourceManager.GetString("NameAnalyzerCloseMessageFormat", resourceCulture); 97 | } 98 | } 99 | 100 | /// 101 | /// Looks up a localized string similar to Function call references unknown Azure Function.. 102 | /// 103 | internal static string NameAnalyzerDescription { 104 | get { 105 | return ResourceManager.GetString("NameAnalyzerDescription", resourceCulture); 106 | } 107 | } 108 | 109 | /// 110 | /// Looks up a localized string similar to Azure function named '{0}' does not exist. Could not find any function registrations.. 111 | /// 112 | internal static string NameAnalyzerMissingMessageFormat { 113 | get { 114 | return ResourceManager.GetString("NameAnalyzerMissingMessageFormat", resourceCulture); 115 | } 116 | } 117 | 118 | /// 119 | /// Looks up a localized string similar to Azure Durable Function Name Check. 120 | /// 121 | internal static string NameAnalyzerTitle { 122 | get { 123 | return ResourceManager.GetString("NameAnalyzerTitle", resourceCulture); 124 | } 125 | } 126 | 127 | /// 128 | <<<<<<< HEAD 129 | /// Looks up a localized string similar to OrchestrationTrigger must be attached to a DurableOrchestrationContext. 130 | /// 131 | internal static string OrchestrationTriggerAnalyzerDescription { 132 | get { 133 | return ResourceManager.GetString("OrchestrationTriggerAnalyzerDescription", resourceCulture); 134 | } 135 | } 136 | 137 | /// 138 | /// Looks up a localized string similar to OrchestrationTrigger must be attached to a DurableOrchestrationContext. 139 | /// 140 | internal static string OrchestrationTriggerAnalyzerMessageFormat { 141 | get { 142 | return ResourceManager.GetString("OrchestrationTriggerAnalyzerMessageFormat", resourceCulture); 143 | } 144 | } 145 | 146 | /// 147 | /// Looks up a localized string similar to Orchestration Trigger Usage. 148 | /// 149 | internal static string OrchestrationTriggerAnalyzerTitle { 150 | get { 151 | return ResourceManager.GetString("OrchestrationTriggerAnalyzerTitle", resourceCulture); 152 | } 153 | } 154 | 155 | /// 156 | ======= 157 | >>>>>>> origin/master 158 | /// Looks up a localized string similar to Function return type doesn't match. 159 | /// 160 | internal static string ReturnTypeAnalyzerDescription { 161 | get { 162 | return ResourceManager.GetString("ReturnTypeAnalyzerDescription", resourceCulture); 163 | } 164 | } 165 | 166 | /// 167 | /// Looks up a localized string similar to Azure function named '{0}' returns '{1}' but '{2}' is expected. 168 | /// 169 | internal static string ReturnTypeAnalyzerMessageFormat { 170 | get { 171 | return ResourceManager.GetString("ReturnTypeAnalyzerMessageFormat", resourceCulture); 172 | } 173 | } 174 | 175 | /// 176 | /// Looks up a localized string similar to Azure Durable Function Return Type Check. 177 | /// 178 | internal static string ReturnTypeAnalyzerTitle { 179 | get { 180 | return ResourceManager.GetString("ReturnTypeAnalyzerTitle", resourceCulture); 181 | } 182 | } 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/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 | Function call parameters don't match function 122 | 123 | 124 | Azure function named '{0}' takes a '{1}' but was given a '{2}' 125 | 126 | 127 | Azure Durable Function Paramter Type Check 128 | 129 | 130 | Azure function named '{0}' does not exist. Did you mean '{1}'? 131 | The format-able message the diagnostic displays. 132 | 133 | 134 | Function call references unknown Azure Function. 135 | An optional longer localizable description of the diagnostic. 136 | 137 | 138 | Azure function named '{0}' does not exist. Could not find any function registrations. 139 | The format-able message when name analyzer can't find any function names 140 | 141 | 142 | Azure Durable Function Name Check 143 | The title of the diagnostic. 144 | 145 | 146 | OrchestrationTrigger must be attached to a DurableOrchestrationContext 147 | 148 | 149 | OrchestrationTrigger must be attached to a DurableOrchestrationContext 150 | 151 | 152 | Orchestration Trigger Usage 153 | 154 | 155 | Function return type doesn't match 156 | 157 | 158 | Azure function named '{0}' returns '{1}' but '{2}' is expected 159 | 160 | 161 | Azure Durable Function Return Type Check 162 | 163 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/Resources.resx.orig: -------------------------------------------------------------------------------- 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 | Function call parameters don't match function 122 | 123 | 124 | Azure function named '{0}' takes a '{1}' but was given a '{2}' 125 | 126 | 127 | Azure Durable Function Paramter Type Check 128 | 129 | 130 | Azure function named '{0}' does not exist. Did you mean '{1}'? 131 | The format-able message the diagnostic displays. 132 | 133 | 134 | Function call references unknown Azure Function. 135 | An optional longer localizable description of the diagnostic. 136 | 137 | 138 | Azure function named '{0}' does not exist. Could not find any function registrations. 139 | The format-able message when name analyzer can't find any function names 140 | 141 | 142 | Azure Durable Function Name Check 143 | The title of the diagnostic. 144 | 145 | <<<<<<< HEAD 146 | 147 | OrchestrationTrigger must be attached to a DurableOrchestrationContext 148 | 149 | 150 | OrchestrationTrigger must be attached to a DurableOrchestrationContext 151 | 152 | 153 | Orchestration Trigger Usage 154 | 155 | ======= 156 | >>>>>>> origin/master 157 | 158 | Function return type doesn't match 159 | 160 | 161 | Azure function named '{0}' returns '{1}' but '{2}' is expected 162 | 163 | 164 | Azure Durable Function Return Type Check 165 | 166 | -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/tools/install.ps1: -------------------------------------------------------------------------------- 1 | param($installPath, $toolsPath, $package, $project) 2 | 3 | if($project.Object.SupportsPackageDependencyResolution) 4 | { 5 | if($project.Object.SupportsPackageDependencyResolution()) 6 | { 7 | # Do not install analyzers via install.ps1, instead let the project system handle it. 8 | return 9 | } 10 | } 11 | 12 | $analyzersPaths = Join-Path (Join-Path (Split-Path -Path $toolsPath -Parent) "analyzers") * -Resolve 13 | 14 | foreach($analyzersPath in $analyzersPaths) 15 | { 16 | if (Test-Path $analyzersPath) 17 | { 18 | # Install the language agnostic analyzers. 19 | foreach ($analyzerFilePath in Get-ChildItem -Path "$analyzersPath\*.dll" -Exclude *.resources.dll) 20 | { 21 | if($project.Object.AnalyzerReferences) 22 | { 23 | $project.Object.AnalyzerReferences.Add($analyzerFilePath.FullName) 24 | } 25 | } 26 | } 27 | } 28 | 29 | # $project.Type gives the language name like (C# or VB.NET) 30 | $languageFolder = "" 31 | if($project.Type -eq "C#") 32 | { 33 | $languageFolder = "cs" 34 | } 35 | if($project.Type -eq "VB.NET") 36 | { 37 | $languageFolder = "vb" 38 | } 39 | if($languageFolder -eq "") 40 | { 41 | return 42 | } 43 | 44 | foreach($analyzersPath in $analyzersPaths) 45 | { 46 | # Install language specific analyzers. 47 | $languageAnalyzersPath = join-path $analyzersPath $languageFolder 48 | if (Test-Path $languageAnalyzersPath) 49 | { 50 | foreach ($analyzerFilePath in Get-ChildItem -Path "$languageAnalyzersPath\*.dll" -Exclude *.resources.dll) 51 | { 52 | if($project.Object.AnalyzerReferences) 53 | { 54 | $project.Object.AnalyzerReferences.Add($analyzerFilePath.FullName) 55 | } 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /DurableFunctionsAnalyzer/DurableFunctionsAnalyzer/tools/uninstall.ps1: -------------------------------------------------------------------------------- 1 | param($installPath, $toolsPath, $package, $project) 2 | 3 | if($project.Object.SupportsPackageDependencyResolution) 4 | { 5 | if($project.Object.SupportsPackageDependencyResolution()) 6 | { 7 | # Do not uninstall analyzers via uninstall.ps1, instead let the project system handle it. 8 | return 9 | } 10 | } 11 | 12 | $analyzersPaths = Join-Path (Join-Path (Split-Path -Path $toolsPath -Parent) "analyzers") * -Resolve 13 | 14 | foreach($analyzersPath in $analyzersPaths) 15 | { 16 | # Uninstall the language agnostic analyzers. 17 | if (Test-Path $analyzersPath) 18 | { 19 | foreach ($analyzerFilePath in Get-ChildItem -Path "$analyzersPath\*.dll" -Exclude *.resources.dll) 20 | { 21 | if($project.Object.AnalyzerReferences) 22 | { 23 | $project.Object.AnalyzerReferences.Remove($analyzerFilePath.FullName) 24 | } 25 | } 26 | } 27 | } 28 | 29 | # $project.Type gives the language name like (C# or VB.NET) 30 | $languageFolder = "" 31 | if($project.Type -eq "C#") 32 | { 33 | $languageFolder = "cs" 34 | } 35 | if($project.Type -eq "VB.NET") 36 | { 37 | $languageFolder = "vb" 38 | } 39 | if($languageFolder -eq "") 40 | { 41 | return 42 | } 43 | 44 | foreach($analyzersPath in $analyzersPaths) 45 | { 46 | # Uninstall language specific analyzers. 47 | $languageAnalyzersPath = join-path $analyzersPath $languageFolder 48 | if (Test-Path $languageAnalyzersPath) 49 | { 50 | foreach ($analyzerFilePath in Get-ChildItem -Path "$languageAnalyzersPath\*.dll" -Exclude *.resources.dll) 51 | { 52 | if($project.Object.AnalyzerReferences) 53 | { 54 | try 55 | { 56 | $project.Object.AnalyzerReferences.Remove($analyzerFilePath.FullName) 57 | } 58 | catch 59 | { 60 | 61 | } 62 | } 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Durable Functions Analyzer 2 | 3 | [![Build status](https://dev.azure.com/DurableFunctionAnalyzer/DurableFunctionAnalyzer/_apis/build/status/DurableFunctionAnalyzer)](https://dev.azure.com/DurableFunctionAnalyzer/DurableFunctionAnalyzer/_build/latest?definitionId=1) 4 | 5 | This is a collection of analyzers to save you from making some common mistakes with [Durable Azure Functions](https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview). 6 | 7 | ![An example of one of the analyzers finding an incorrectly named function call](images/poc.png) 8 | 9 | 10 | ![An example of one of the analyzers finding a function call with the wrong argument type](images/poc2.png) 11 | 12 | ## The why 13 | 14 | Function calls in durable functions are written in a way which can introduce errors in your code which won't be found until run time. In the interests of shifting warnings left these analyzers catch a number of common mistakes. 15 | 16 | * Using the wrong name to refer to a function call 17 | * Passing the wrong arguments 18 | * Casting to the wrong return type (not just yet) 19 | 20 | ## How to use them 21 | 22 | The analyzers are distributed as a nuget package. To install it all you need to do is run 23 | 24 | ``` 25 | dotnet add package DurableFunctionsAnalyzer 26 | ``` 27 | 28 | or 29 | 30 | ``` 31 | Install-Package DurableFunctionsAnalyzer 32 | ``` 33 | 34 | You also need to turn on `Enable full solution analysis` to see the warnings in Visual Studio. 35 | 36 | 37 | ![Enable full solution analysis](images/settings.png) 38 | 39 | -------------------------------------------------------------------------------- /images/poc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stimms/DurableFunctionsAnalyzer/ddd055b222c3bfa0d10ef6b736e611a1ee5b156d/images/poc.png -------------------------------------------------------------------------------- /images/poc2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stimms/DurableFunctionsAnalyzer/ddd055b222c3bfa0d10ef6b736e611a1ee5b156d/images/poc2.png -------------------------------------------------------------------------------- /images/poc3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stimms/DurableFunctionsAnalyzer/ddd055b222c3bfa0d10ef6b736e611a1ee5b156d/images/poc3.png -------------------------------------------------------------------------------- /images/poc4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stimms/DurableFunctionsAnalyzer/ddd055b222c3bfa0d10ef6b736e611a1ee5b156d/images/poc4.png -------------------------------------------------------------------------------- /images/settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stimms/DurableFunctionsAnalyzer/ddd055b222c3bfa0d10ef6b736e611a1ee5b156d/images/settings.png --------------------------------------------------------------------------------