forked from dotnet/roslyn-analyzers
/
CodeFixProvider.cs
2470 lines (2081 loc) · 158 KB
/
CodeFixProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Text;
#pragma warning disable CA1820 // Test for empty strings using string length
namespace MetaCompilation.Analyzers
{
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(MetaCompilationCodeFixProvider)), Shared]
public class MetaCompilationCodeFixProvider : CodeFixProvider
{
public const string MessagePrefix = "T: ";
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(
MetaCompilationAnalyzer.MissingId,
MetaCompilationAnalyzer.MissingInit,
MetaCompilationAnalyzer.MissingRegisterStatement,
MetaCompilationAnalyzer.TooManyInitStatements,
MetaCompilationAnalyzer.IncorrectInitSig,
MetaCompilationAnalyzer.InvalidStatement,
MetaCompilationAnalyzer.MissingAnalysisMethod,
MetaCompilationAnalyzer.IncorrectAnalysisAccessibility,
MetaCompilationAnalyzer.IncorrectAnalysisParameter,
MetaCompilationAnalyzer.IncorrectAnalysisReturnType,
MetaCompilationAnalyzer.IncorrectSigSuppDiag,
MetaCompilationAnalyzer.MissingAccessor,
MetaCompilationAnalyzer.TooManyAccessors,
MetaCompilationAnalyzer.IncorrectAccessorReturn,
MetaCompilationAnalyzer.SuppDiagReturnValue,
MetaCompilationAnalyzer.SupportedRules,
MetaCompilationAnalyzer.IdDeclTypeError,
MetaCompilationAnalyzer.MissingIdDeclaration,
MetaCompilationAnalyzer.DefaultSeverityError,
MetaCompilationAnalyzer.EnabledByDefaultError,
MetaCompilationAnalyzer.InternalAndStaticError,
MetaCompilationAnalyzer.MissingRule,
MetaCompilationAnalyzer.IfStatementMissing,
MetaCompilationAnalyzer.IfStatementIncorrect,
MetaCompilationAnalyzer.IfKeywordMissing,
MetaCompilationAnalyzer.IfKeywordIncorrect,
MetaCompilationAnalyzer.TrailingTriviaCheckMissing,
MetaCompilationAnalyzer.TrailingTriviaCheckIncorrect,
MetaCompilationAnalyzer.TrailingTriviaVarMissing,
MetaCompilationAnalyzer.TrailingTriviaVarIncorrect,
MetaCompilationAnalyzer.WhitespaceCheckMissing,
MetaCompilationAnalyzer.WhitespaceCheckIncorrect,
MetaCompilationAnalyzer.ReturnStatementMissing,
MetaCompilationAnalyzer.ReturnStatementIncorrect,
MetaCompilationAnalyzer.OpenParenIncorrect,
MetaCompilationAnalyzer.OpenParenMissing,
MetaCompilationAnalyzer.StartSpanIncorrect,
MetaCompilationAnalyzer.StartSpanMissing,
MetaCompilationAnalyzer.EndSpanIncorrect,
MetaCompilationAnalyzer.EndSpanMissing,
MetaCompilationAnalyzer.SpanIncorrect,
MetaCompilationAnalyzer.SpanMissing,
MetaCompilationAnalyzer.LocationIncorrect,
MetaCompilationAnalyzer.LocationMissing,
MetaCompilationAnalyzer.DiagnosticMissing,
MetaCompilationAnalyzer.DiagnosticIncorrect,
MetaCompilationAnalyzer.DiagnosticReportIncorrect,
MetaCompilationAnalyzer.DiagnosticReportMissing,
MetaCompilationAnalyzer.TrailingTriviaKindCheckMissing,
MetaCompilationAnalyzer.TrailingTriviaKindCheckIncorrect,
MetaCompilationAnalyzer.MissingSuppDiag,
MetaCompilationAnalyzer.IncorrectKind,
MetaCompilationAnalyzer.IncorrectRegister,
MetaCompilationAnalyzer.IncorrectArguments,
MetaCompilationAnalyzer.TrailingTriviaCountMissing,
MetaCompilationAnalyzer.TrailingTriviaCountIncorrect,
MetaCompilationAnalyzer.IdStringLiteral,
MetaCompilationAnalyzer.Title,
MetaCompilationAnalyzer.Message,
MetaCompilationAnalyzer.Category);
public sealed override FixAllProvider GetFixAllProvider()
{
return WellKnownFixAllProviders.BatchFixer;
}
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
SyntaxNode root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
foreach (Diagnostic diagnostic in context.Diagnostics)
{
TextSpan diagnosticSpan = diagnostic.Location.SourceSpan;
switch (diagnostic.Id)
{
case MetaCompilationAnalyzer.MissingId:
IEnumerable<ClassDeclarationSyntax> idDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>();
if (idDeclarations.Any())
{
ClassDeclarationSyntax declaration = idDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Give the diagnostic a unique string ID distinguishing it from other diagnostics", c => MissingIdAsync(context.Document, declaration, c), "Give the diagnostic a unique string ID distinguishing it from other diagnostics"), diagnostic);
}
break;
case MetaCompilationAnalyzer.MissingInit:
IEnumerable<ClassDeclarationSyntax> initDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>();
if (initDeclarations.Any())
{
ClassDeclarationSyntax declaration = initDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Insert the missing Initialize method", c => MissingInitAsync(context.Document, declaration, c), "Insert the missing Initialize method"), diagnostic);
}
break;
case MetaCompilationAnalyzer.MissingRegisterStatement:
IEnumerable<MethodDeclarationSyntax> registerDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (registerDeclarations.Any())
{
MethodDeclarationSyntax declaration = registerDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Register an action to analyze code when changes occur", c => MissingRegisterAsync(context.Document, declaration, c), "Register an action to analyze code when changes occur"), diagnostic);
}
break;
case MetaCompilationAnalyzer.TooManyInitStatements:
IEnumerable<MethodDeclarationSyntax> manyDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (manyDeclarations.Any())
{
MethodDeclarationSyntax declaration = manyDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Remove multiple registered actions from the Initialize method", c => MultipleStatementsAsync(context.Document, declaration, c), "Remove multiple registered actions from the Initialize method"), diagnostic);
}
break;
case MetaCompilationAnalyzer.InvalidStatement:
IEnumerable<StatementSyntax> invalidDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<StatementSyntax>();
if (invalidDeclarations.Any())
{
StatementSyntax declaration = invalidDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Remove invalid statements from the Initialize method", c => InvalidStatementAsync(context.Document, declaration, c), "Remove invalid statements from the Initialize method"), diagnostic);
}
break;
case MetaCompilationAnalyzer.MissingAnalysisMethod:
IEnumerable<MethodDeclarationSyntax> analysisDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (analysisDeclarations.Any())
{
MethodDeclarationSyntax declaration = analysisDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Generate the method called by actions registered in Initialize", c => MissingAnalysisMethodAsync(context.Document, declaration, c), "Generate the method called by actions registered in Initialize"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IncorrectAnalysisAccessibility:
IEnumerable<MethodDeclarationSyntax> incorrectDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (incorrectDeclarations.Any())
{
MethodDeclarationSyntax declaration = incorrectDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Add the private keyword to this method", c => IncorrectAnalysisAccessibilityAsync(context.Document, declaration, c), "Add the private keyword to this method"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IncorrectAnalysisReturnType:
IEnumerable<MethodDeclarationSyntax> returnDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (returnDeclarations.Any())
{
MethodDeclarationSyntax declaration = returnDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Declare a void return type for this method", c => IncorrectAnalysisReturnTypeAsync(context.Document, declaration, c), "Declare a void return type for this method"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IncorrectAnalysisParameter:
IEnumerable<MethodDeclarationSyntax> parameterDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (parameterDeclarations.Any())
{
MethodDeclarationSyntax declaration = parameterDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Have this method take one parameter of type SyntaxNodeAnalysisContext", c => IncorrectAnalysisParameterAsync(context.Document, declaration, c), "Have this method take one parameter of type SyntaxNodeAnalysisContext"), diagnostic);
}
break;
case MetaCompilationAnalyzer.InternalAndStaticError:
IEnumerable<FieldDeclarationSyntax> internalDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<FieldDeclarationSyntax>();
if (internalDeclarations.Any())
{
FieldDeclarationSyntax declaration = internalDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Make the DiagnosticDescriptor rule both internal and static", c => InternalStaticAsync(context.Document, declaration, c), "Make the DiagnosticDescriptor rule both internal and static"), diagnostic);
}
break;
case MetaCompilationAnalyzer.EnabledByDefaultError:
IEnumerable<ArgumentSyntax> enabledDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ArgumentSyntax>();
if (enabledDeclarations.Any())
{
ArgumentSyntax declaration = enabledDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Set 'isEnabledByDefault' parameter to true", c => EnabledByDefaultAsync(context.Document, declaration, c), "Set 'isEnabledByDefault' parameter to true"), diagnostic);
}
break;
case MetaCompilationAnalyzer.DefaultSeverityError:
IEnumerable<ArgumentSyntax> severityDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ArgumentSyntax>();
if (severityDeclarations.Any())
{
ArgumentSyntax declaration = severityDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Set the severity to 'Error' if something is not allowed", c => DiagnosticSeverityError(context.Document, declaration, c), "Set the severity to 'Error' if something is not allowed"), diagnostic);
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Set the severity to 'Warning' if something is suspicious but allowed", c => DiagnosticSeverityWarning(context.Document, declaration, c), "Set the severity to 'Warning' if something is suspicious but allowed"), diagnostic);
}
break;
case MetaCompilationAnalyzer.MissingIdDeclaration:
IEnumerable<VariableDeclaratorSyntax> missingIdDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<VariableDeclaratorSyntax>();
if (missingIdDeclarations.Any())
{
VariableDeclaratorSyntax declaration = missingIdDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Declare the diagnostic ID as a public constant string", c => MissingIdDeclarationAsync(context.Document, declaration, c), "Declare the diagnostic ID as a public constant string"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IdStringLiteral:
case MetaCompilationAnalyzer.IdDeclTypeError:
IEnumerable<ClassDeclarationSyntax> declDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>();
if (declDeclarations.Any())
{
ClassDeclarationSyntax classDeclaration = declDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Declare the diagnostic ID as a public constant string", c => IdDeclTypeAsync(context.Document, classDeclaration, c), "Declare the diagnostic ID as a public constant string"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IfStatementIncorrect:
IEnumerable<StatementSyntax> ifDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<StatementSyntax>();
if (ifDeclarations.Any())
{
StatementSyntax declaration = ifDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Extract the IfStatementSyntax Node from the context", c => IncorrectIfAsync(context.Document, declaration, c), "Extract the IfStatementSyntax Node from the context"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IfStatementMissing:
IEnumerable<MethodDeclarationSyntax> ifMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (ifMissingDeclarations.Any())
{
MethodDeclarationSyntax declaration = ifMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Extract the IfStatementSyntax Node from the context", c => MissingIfAsync(context.Document, declaration, c), "Extract the IfStatementSyntax Node from the context"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IncorrectInitSig:
IEnumerable<MethodDeclarationSyntax> initSigDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (initSigDeclarations.Any())
{
MethodDeclarationSyntax declaration = initSigDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Implement the correct signature for the Initialize method", c => IncorrectSigAsync(context.Document, declaration, c), "Implement the correct signature for the Initialize method"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IfKeywordIncorrect:
IEnumerable<StatementSyntax> keywordDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<StatementSyntax>();
if (keywordDeclarations.Any())
{
StatementSyntax declaration = keywordDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Extract the if-keyword from the if-statement", c => IncorrectKeywordAsync(context.Document, declaration, c), "Extract the if-keyword from the if-statement"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IfKeywordMissing:
IEnumerable<MethodDeclarationSyntax> keywordMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (keywordMissingDeclarations.Any())
{
MethodDeclarationSyntax declaration = keywordMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Extract the if-keyword from the if-statement", c => MissingKeywordAsync(context.Document, declaration, c), "Extract the if-keyword from the if-statement"), diagnostic);
}
break;
case MetaCompilationAnalyzer.TrailingTriviaCheckIncorrect:
IEnumerable<MethodDeclarationSyntax> checkDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (checkDeclarations.Any())
{
MethodDeclarationSyntax declaration = checkDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Check if the if-keyword has trailing trivia", c => TrailingCheckIncorrectAsync(context.Document, declaration, c), "Check if the if-keyword has trailing trivia"), diagnostic);
}
break;
case MetaCompilationAnalyzer.TrailingTriviaCheckMissing:
IEnumerable<MethodDeclarationSyntax> checkMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (checkMissingDeclarations.Any())
{
MethodDeclarationSyntax declaration = checkMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Check if the if-keyword has trailing trivia", c => TrailingCheckMissingAsync(context.Document, declaration, c), "Check if the if-keyword has trailing trivia"), diagnostic);
}
break;
case MetaCompilationAnalyzer.TrailingTriviaVarMissing:
IEnumerable<IfStatementSyntax> varMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<IfStatementSyntax>();
if (varMissingDeclarations.Any())
{
IfStatementSyntax declaration = varMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Extract the first trailing trivia into a variable", c => TrailingVarMissingAsync(context.Document, declaration, c), "Extract the first trailing trivia into a variable"), diagnostic);
}
break;
case MetaCompilationAnalyzer.TrailingTriviaVarIncorrect:
IEnumerable<IfStatementSyntax> varDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<IfStatementSyntax>();
if (varDeclarations.Any())
{
IfStatementSyntax declaration = varDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Extract the first trailing trivia into a variable", c => TrailingVarIncorrectAsync(context.Document, declaration, c), "Extract the first trailing trivia into a variable"), diagnostic);
}
break;
case MetaCompilationAnalyzer.TrailingTriviaKindCheckIncorrect:
IEnumerable<IfStatementSyntax> kindDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<IfStatementSyntax>();
if (kindDeclarations.Any())
{
IfStatementSyntax declaration = kindDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Check the kind of the first trailing trivia", c => TrailingKindCheckIncorrectAsync(context.Document, declaration, c), "Check the kind of the first trailing trivia"), diagnostic);
}
break;
case MetaCompilationAnalyzer.TrailingTriviaKindCheckMissing:
IEnumerable<IfStatementSyntax> kindMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<IfStatementSyntax>();
if (kindMissingDeclarations.Any())
{
IfStatementSyntax declaration = kindMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Check the kind of the first trailing trivia", c => TrailingKindCheckMissingAsync(context.Document, declaration, c), "Check the kind of the first trailing trivia"), diagnostic);
}
break;
case MetaCompilationAnalyzer.WhitespaceCheckIncorrect:
IEnumerable<IfStatementSyntax> whitespaceDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<IfStatementSyntax>();
if (whitespaceDeclarations.Any())
{
IfStatementSyntax declaration = whitespaceDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Check if the whitespace is a single space", c => WhitespaceCheckIncorrectAsync(context.Document, declaration, c), "Check if the whitespace is a single space"), diagnostic);
}
break;
case MetaCompilationAnalyzer.ReturnStatementIncorrect:
IEnumerable<IfStatementSyntax> statementDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<IfStatementSyntax>();
if (statementDeclarations.Any())
{
IfStatementSyntax declaration = statementDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Return from the method", c => ReturnIncorrectAsync(context.Document, declaration, c), "Return from the method"), diagnostic);
}
break;
case MetaCompilationAnalyzer.ReturnStatementMissing:
IEnumerable<IfStatementSyntax> statementMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<IfStatementSyntax>();
if (statementMissingDeclarations.Any())
{
IfStatementSyntax declaration = statementMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Return from the method", c => ReturnMissingAsync(context.Document, declaration, c), "Return from the method"), diagnostic);
}
break;
case MetaCompilationAnalyzer.WhitespaceCheckMissing:
IEnumerable<IfStatementSyntax> whitespaceMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<IfStatementSyntax>();
if (whitespaceMissingDeclarations.Any())
{
IfStatementSyntax declaration = whitespaceMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Check if the whitespace is a single space", c => WhitespaceCheckMissingAsync(context.Document, declaration, c), "Check if the whitespace is a single space"), diagnostic);
}
break;
case MetaCompilationAnalyzer.LocationMissing:
IEnumerable<MethodDeclarationSyntax> locationMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (locationMissingDeclarations.Any())
{
MethodDeclarationSyntax declaration = locationMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Create a diagnostic location", c => AddLocationAsync(context.Document, declaration, c), "Create a diagnostic location"), diagnostic);
}
break;
case MetaCompilationAnalyzer.LocationIncorrect:
IEnumerable<StatementSyntax> locationDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<StatementSyntax>();
if (locationDeclarations.Any())
{
StatementSyntax declaration = locationDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Create a diagnostic location", c => ReplaceLocationAsync(context.Document, declaration, c), "Create a diagnostic location"), diagnostic);
}
break;
case MetaCompilationAnalyzer.SpanMissing:
IEnumerable<MethodDeclarationSyntax> spanMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (spanMissingDeclarations.Any())
{
MethodDeclarationSyntax declaration = spanMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Create a diagnostic span", c => AddSpanAsync(context.Document, declaration, c), "Create a diagnostic span"), diagnostic);
}
break;
case MetaCompilationAnalyzer.SpanIncorrect:
IEnumerable<StatementSyntax> spanDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<StatementSyntax>();
if (spanDeclarations.Any())
{
StatementSyntax declaration = spanDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Create a diagnostic span", c => ReplaceSpanAsync(context.Document, declaration, c), "Create a diagnostic span"), diagnostic);
}
break;
case MetaCompilationAnalyzer.EndSpanMissing:
IEnumerable<MethodDeclarationSyntax> endMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (endMissingDeclarations.Any())
{
MethodDeclarationSyntax declaration = endMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Create a variable representing the end of the diagnostic span", c => AddEndSpanAsync(context.Document, declaration, c), "Create a variable representing the end of the diagnostic span"), diagnostic);
}
break;
case MetaCompilationAnalyzer.EndSpanIncorrect:
IEnumerable<StatementSyntax> endDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<StatementSyntax>();
if (endDeclarations.Any())
{
StatementSyntax declaration = endDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Create a variable representing the end of the diagnostic span", c => ReplaceEndSpanAsync(context.Document, declaration, c), "Create a variable representing the end of the diagnostic span"), diagnostic);
}
break;
case MetaCompilationAnalyzer.StartSpanMissing:
IEnumerable<MethodDeclarationSyntax> startMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (startMissingDeclarations.Any())
{
MethodDeclarationSyntax declaration = startMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Create a variable representing the start of the diagnostic span", c => AddStartSpanAsync(context.Document, declaration, c), "Create a variable representing the start of the diagnostic span"), diagnostic);
}
break;
case MetaCompilationAnalyzer.StartSpanIncorrect:
IEnumerable<StatementSyntax> startDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<StatementSyntax>();
if (startDeclarations.Any())
{
StatementSyntax declaration = startDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Create a variable representing the start of the diagnostic span", c => ReplaceStartSpanAsync(context.Document, declaration, c), "Create a variable representing the start of the diagnostic span"), diagnostic);
}
break;
case MetaCompilationAnalyzer.OpenParenMissing:
IEnumerable<MethodDeclarationSyntax> openMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (openMissingDeclarations.Any())
{
MethodDeclarationSyntax declaration = openMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Extract the open parenthesis from the if-statement", c => AddOpenParenAsync(context.Document, declaration, c), "Extract the open parenthesis from the if-statement"), diagnostic);
}
break;
case MetaCompilationAnalyzer.OpenParenIncorrect:
IEnumerable<StatementSyntax> openDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<StatementSyntax>();
if (openDeclarations.Any())
{
StatementSyntax declaration = openDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Extract the open parenthesis from the if-statement", c => ReplaceOpenParenAsync(context.Document, declaration, c), "Extract the open parenthesis from the if-statement"), diagnostic);
}
break;
case MetaCompilationAnalyzer.DiagnosticMissing:
IEnumerable<ClassDeclarationSyntax> diagnosticMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>();
if (diagnosticMissingDeclarations.Any())
{
ClassDeclarationSyntax declaration = diagnosticMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Create the diagnostic that is going to be reported", c => AddDiagnosticAsync(context.Document, declaration, c), "Create the diagnostic that is going to be reported"), diagnostic);
}
break;
case MetaCompilationAnalyzer.DiagnosticIncorrect:
IEnumerable<StatementSyntax> diagnosticDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<StatementSyntax>();
if (diagnosticDeclarations.Any())
{
StatementSyntax declaration = diagnosticDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Create the diagnostic that is going to be reported", c => ReplaceDiagnosticAsync(context.Document, declaration, c), "Create the diagnostic that is going to be reported"), diagnostic);
}
break;
case MetaCompilationAnalyzer.DiagnosticReportMissing:
IEnumerable<MethodDeclarationSyntax> reportMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (reportMissingDeclarations.Any())
{
MethodDeclarationSyntax declaration = reportMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Report the diagnostic to the SyntaxNodeAnalysisContext", c => AddDiagnosticReportAsync(context.Document, declaration, c), "Report the diagnostic to the SyntaxNodeAnalysisContext"), diagnostic);
}
break;
case MetaCompilationAnalyzer.DiagnosticReportIncorrect:
IEnumerable<StatementSyntax> reportDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<StatementSyntax>();
if (reportDeclarations.Any())
{
StatementSyntax declaration = reportDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Report the diagnostic to the SyntaxNodeAnalysisContext", c => ReplaceDiagnosticReportAsync(context.Document, declaration, c), "Report the diagnostic to the SyntaxNodeAnalysisContext"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IncorrectSigSuppDiag:
IEnumerable<PropertyDeclarationSyntax> sigSuppDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<PropertyDeclarationSyntax>();
if (sigSuppDeclarations.Any())
{
PropertyDeclarationSyntax declaration = sigSuppDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Implement the correct signature for the SupportedDiagnostics property", c => IncorrectSigSuppDiagAsync(context.Document, declaration, c), "Implement the correct signature for the SupportedDiagnostics property"), diagnostic);
}
break;
case MetaCompilationAnalyzer.MissingAccessor:
IEnumerable<PropertyDeclarationSyntax> accessorDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<PropertyDeclarationSyntax>();
if (accessorDeclarations.Any())
{
PropertyDeclarationSyntax declaration = accessorDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Add a get-accessor to the SupportedDiagnostics property", c => MissingAccessorAsync(context.Document, declaration, c), "Add a get-accessor to the SupportedDiagnostics property"), diagnostic);
}
break;
case MetaCompilationAnalyzer.TooManyAccessors:
IEnumerable<PropertyDeclarationSyntax> manyAccessorsDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<PropertyDeclarationSyntax>();
if (manyAccessorsDeclarations.Any())
{
PropertyDeclarationSyntax declaration = manyAccessorsDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Remove unnecessary accessors from the SupportedDiagnostics property", c => TooManyAccessorsAsync(context.Document, declaration, c), "Remove unnecessary accessors from the SupportedDiagnostics property"), diagnostic);
}
break;
case MetaCompilationAnalyzer.SuppDiagReturnValue:
IEnumerable<PropertyDeclarationSyntax> incorrectAccessorDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<PropertyDeclarationSyntax>();
if (incorrectAccessorDeclarations.Any())
{
PropertyDeclarationSyntax declaration = incorrectAccessorDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Return an ImmutableArray of DiagnosticDescriptors from SupportedDiagnostics", c => AccessorReturnValueAsync(context.Document, declaration, c), "Return an ImmutableArray of DiagnosticDescriptors from SupportedDiagnostics"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IncorrectAccessorReturn:
IEnumerable<ClassDeclarationSyntax> accessorReturnDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>();
if (accessorReturnDeclarations.Any())
{
ClassDeclarationSyntax declaration = accessorReturnDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Return an ImmutableArray of all DiagnosticDescriptors that can be reported", c => AccessorWithRulesAsync(context.Document, declaration, c), "Return an ImmutableArray of all DiagnosticDescriptors that can be reported"), diagnostic);
}
break;
case MetaCompilationAnalyzer.SupportedRules:
IEnumerable<ClassDeclarationSyntax> rulesDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>();
if (rulesDeclarations.Any())
{
ClassDeclarationSyntax declaration = rulesDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Return a list of all diagnostics that can be reported by this analyzer", c => AccessorWithRulesAsync(context.Document, declaration, c), "Return a list of all diagnostics that can be reported by this analyzer"), diagnostic);
}
break;
case MetaCompilationAnalyzer.MissingSuppDiag:
IEnumerable<ClassDeclarationSyntax> suppDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>();
if (suppDeclarations.Any())
{
ClassDeclarationSyntax declaration = suppDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Add the required SupportedDiagnostics property", c => AddSuppDiagAsync(context.Document, declaration, c), "Add the required SupportedDiagnostics property"), diagnostic);
}
break;
case MetaCompilationAnalyzer.MissingRule:
IEnumerable<ClassDeclarationSyntax> missingRuleDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ClassDeclarationSyntax>();
if (missingRuleDeclarations.Any())
{
ClassDeclarationSyntax declaration = missingRuleDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Add a DiagnosticDescriptor rule to create the diagnostic", c => AddRuleAsync(context.Document, declaration, c), "Add a DiagnosticDescriptor rule to create the diagnostic"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IncorrectKind:
IEnumerable<ArgumentListSyntax> incorrectKindDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ArgumentListSyntax>();
if (incorrectKindDeclarations.Any())
{
ArgumentListSyntax declaration = incorrectKindDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Analyze the correct SyntaxKind", c => CorrectKindAsync(context.Document, declaration, c), "Analyze the correct SyntaxKind"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IncorrectRegister:
IEnumerable<IdentifierNameSyntax> incorrectRegisterDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<IdentifierNameSyntax>();
if (incorrectRegisterDeclarations.Any())
{
IdentifierNameSyntax declaration = incorrectRegisterDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Register an action of kind SyntaxNodeAnalysis", c => CorrectRegisterAsync(context.Document, declaration, c), "Register an action of kind SyntaxNodeAnalysis"), diagnostic);
}
break;
case MetaCompilationAnalyzer.IncorrectArguments:
IEnumerable<InvocationExpressionSyntax> argsDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<InvocationExpressionSyntax>();
if (argsDeclarations.Any())
{
InvocationExpressionSyntax declaration = argsDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Add the correct arguments to the Initialize method", c => CorrectArgumentsAsync(context.Document, declaration, c), "Add the correct arguments to the Initialize method"), diagnostic);
}
break;
case MetaCompilationAnalyzer.TrailingTriviaCountMissing:
IEnumerable<MethodDeclarationSyntax> countMissingDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (countMissingDeclarations.Any())
{
MethodDeclarationSyntax declaration = countMissingDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Check the amount of trailing trivia", c => TriviaCountMissingAsync(context.Document, declaration, c), "Check the amount of trailing trivia"), diagnostic);
}
break;
case MetaCompilationAnalyzer.TrailingTriviaCountIncorrect:
IEnumerable<MethodDeclarationSyntax> countIncorrectDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<MethodDeclarationSyntax>();
if (countIncorrectDeclarations.Any())
{
MethodDeclarationSyntax declaration = countIncorrectDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Check the amount of trailing trivia", c => TriviaCountIncorrectAsync(context.Document, declaration, c), "Check the amount of trailing trivia"), diagnostic);
}
break;
case MetaCompilationAnalyzer.Title:
IEnumerable<LiteralExpressionSyntax> titleDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<LiteralExpressionSyntax>();
if (titleDeclarations.Any())
{
LiteralExpressionSyntax declaration = titleDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Replace the title", c => ReplaceTitle(context.Document, declaration, c), "Replace the title"), diagnostic);
}
break;
case MetaCompilationAnalyzer.Message:
IEnumerable<LiteralExpressionSyntax> messageDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<LiteralExpressionSyntax>();
if (messageDeclarations.Any())
{
LiteralExpressionSyntax declaration = messageDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Replace the title", c => ReplaceMessage(context.Document, declaration, c), "Replace the title"), diagnostic);
}
break;
case MetaCompilationAnalyzer.Category:
IEnumerable<LiteralExpressionSyntax> categoryDeclarations = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<LiteralExpressionSyntax>();
if (categoryDeclarations.Any())
{
LiteralExpressionSyntax declaration = categoryDeclarations.First();
context.RegisterCodeFix(CodeAction.Create(MessagePrefix + "Replace the title", c => ReplaceCategory(context.Document, declaration, c), "Replace the title"), diagnostic);
}
break;
}
}
}
// replace the category string
private async Task<Document> ReplaceCategory(Document document, LiteralExpressionSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
SyntaxNode newString = generator.LiteralExpression("Formatting");
return await ReplaceNode(declaration, newString, document, cancellationToken).ConfigureAwait(false);
}
// replaces the messageFormat string
private async Task<Document> ReplaceMessage(Document document, LiteralExpressionSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
SyntaxNode newString = generator.LiteralExpression("The trivia between 'if' and '(' should be a single space");
return await ReplaceNode(declaration, newString, document, cancellationToken).ConfigureAwait(false);
}
// replaces the title string
private async Task<Document> ReplaceTitle(Document document, LiteralExpressionSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
SyntaxNode newString = generator.LiteralExpression("If-statement spacing");
return await ReplaceNode(declaration, newString, document, cancellationToken).ConfigureAwait(false);
}
// replaces a node in the document
private async Task<Document> ReplaceNode(SyntaxNode oldNode, SyntaxNode newNode, Document document, CancellationToken cancellationToken)
{
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
SyntaxNode newRoot = root.ReplaceNode(oldNode, newNode);
Document newDocument = document.WithSyntaxRoot(newRoot);
return newDocument;
}
#region analysis method fixes
// sets the analysis method to take a parameter of type SyntaxNodeAnalysisContext
private async Task<Document> IncorrectAnalysisParameterAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
SyntaxNode newDeclaration = CodeFixHelper.CreateMethodWithContextParameter(generator, declaration);
return await ReplaceNode(declaration, newDeclaration, document, cancellationToken).ConfigureAwait(false);
}
// sets the return type of the method to void
private async Task<Document> IncorrectAnalysisReturnTypeAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxNode newDeclaration = CodeFixHelper.MethodReturnType(declaration, "void");
return await ReplaceNode(declaration, newDeclaration, document, cancellationToken).ConfigureAwait(false);
}
// sets the method accessibility to private
private async Task<Document> IncorrectAnalysisAccessibilityAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
SyntaxNode newDeclaration = CodeFixHelper.MethodAccessibility(generator, declaration, Accessibility.Private);
return await ReplaceNode(declaration, newDeclaration, document, cancellationToken).ConfigureAwait(false);
}
// adds an analysis method to the enclosing class
private async Task<Document> MissingAnalysisMethodAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
string analysisMethodName = CodeFixHelper.AnalysisMethodName(declaration);
SemanticModel semanticModel = await document.GetSemanticModelAsync().ConfigureAwait(false);
var newAnalysisMethod = CodeFixHelper.CreateAnalysisMethod(generator, analysisMethodName, semanticModel) as MethodDeclarationSyntax;
ClassDeclarationSyntax classDeclaration = declaration.Ancestors().OfType<ClassDeclarationSyntax>().First();
return await ReplaceNode(classDeclaration, classDeclaration.AddMembers(newAnalysisMethod), document, cancellationToken).ConfigureAwait(false);
}
// replaces the if statement variable
private async Task<Document> IncorrectIfAsync(Document document, StatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
MethodDeclarationSyntax methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string name = CodeFixHelper.GetContextParameter(methodDeclaration);
SyntaxNode ifStatement = CodeFixHelper.IfHelper(generator, name);
return await ReplaceNode(declaration, ifStatement.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// The SyntaxNode found by the Initialize method should be cast to the expected type. Here, this type is IfStatementSyntax").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document, cancellationToken).ConfigureAwait(false);
}
// adds the if statement variable
private async Task<Document> MissingIfAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
string name = CodeFixHelper.GetContextParameter(declaration);
StatementSyntax ifStatement = CodeFixHelper.IfHelper(generator, name) as StatementSyntax;
var oldBlock = declaration.Body as BlockSyntax;
BlockSyntax newBlock = oldBlock.AddStatements(ifStatement.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// The SyntaxNode found by the Initialize method should be cast to the expected type. Here, this type is IfStatementSyntax").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))));
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// replaces the incorrect statement with the keyword statement
private async Task<Document> IncorrectKeywordAsync(Document document, StatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var block = declaration.Parent as BlockSyntax;
SyntaxNode ifKeyword = CodeFixHelper.KeywordHelper(generator, block);
return await ReplaceNode(declaration, ifKeyword.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// This statement navigates down the syntax tree one level to extract the 'if' keyword").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document, cancellationToken).ConfigureAwait(false);
}
// adds the keyword statement
private async Task<Document> MissingKeywordAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var methodBlock = declaration.Body as BlockSyntax;
var ifKeyword = CodeFixHelper.KeywordHelper(generator, methodBlock) as StatementSyntax;
BlockSyntax newBlock = methodBlock.AddStatements(ifKeyword.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// This statement navigates down the syntax tree one level to extract the 'if' keyword").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))));
return await ReplaceNode(methodBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// replaces the trailing trivia check
private async Task<Document> TrailingCheckIncorrectAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var ifBlockStatements = new SyntaxList<StatementSyntax>();
if (declaration.Body.Statements[2].Kind() == SyntaxKind.IfStatement)
{
var ifDeclaration = declaration.Body.Statements[2] as IfStatementSyntax;
if (ifDeclaration.Statement is BlockSyntax ifBlock)
{
ifBlockStatements = ifBlock.Statements;
}
}
StatementSyntax ifStatement = CodeFixHelper.TriviaCheckHelper(generator, declaration.Body, ifBlockStatements) as StatementSyntax;
BlockSyntax oldBlock = declaration.Body;
BlockSyntax newBlock = declaration.Body.WithStatements(declaration.Body.Statements.Replace(declaration.Body.Statements[2], ifStatement));
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// adds the trailing trivia check
private async Task<Document> TrailingCheckMissingAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var ifBlockStatements = new SyntaxList<StatementSyntax>();
SyntaxTriviaList leadingTrivia = SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// Checks if there is any trailing trivia (eg spaces or comments) associated with the if-keyword").ElementAt(0), SyntaxFactory.CarriageReturnLineFeed);
StatementSyntax ifStatement = (CodeFixHelper.TriviaCheckHelper(generator, declaration.Body, ifBlockStatements) as StatementSyntax).WithLeadingTrivia(leadingTrivia);
BlockSyntax oldBlock = declaration.Body;
BlockSyntax newBlock = oldBlock.WithStatements(declaration.Body.Statements.Add(ifStatement));
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// adds the trivia count check
private async Task<Document> TriviaCountMissingAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
string name = CodeFixHelper.GetIfKeywordName(declaration.Body);
var ifBlockStatements = new SyntaxList<StatementSyntax>();
var ifStatement = declaration.Body.Statements[2] as IfStatementSyntax;
SyntaxTriviaList leadingTrivia = SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// Checks that there is only one piece of trailing trivia").ElementAt(0), SyntaxFactory.CarriageReturnLineFeed);
SyntaxList<SyntaxNode> localDeclaration = new SyntaxList<SyntaxNode>().Add((CodeFixHelper.TriviaCountHelper(generator, name, ifBlockStatements)).WithLeadingTrivia(leadingTrivia));
var oldBlock = ifStatement.Statement as BlockSyntax;
BlockSyntax newBlock = oldBlock.WithStatements(localDeclaration);
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// replaces the trivia count check
private async Task<Document> TriviaCountIncorrectAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
string name = CodeFixHelper.GetIfKeywordName(declaration.Body);
var ifStatement = declaration.Body.Statements[2] as IfStatementSyntax;
var ifBlockStatements = new SyntaxList<StatementSyntax>();
if (ifStatement != null)
{
var ifDeclaration = ifStatement.Statement as BlockSyntax;
if (ifDeclaration.Statements[0] is IfStatementSyntax ifBlockStatement)
{
if (ifBlockStatement.Statement is BlockSyntax ifBlock)
{
ifBlockStatements = ifBlock.Statements;
}
}
}
var localDeclaration = CodeFixHelper.TriviaCountHelper(generator, name, ifBlockStatements) as StatementSyntax;
var oldBlock = ifStatement.Statement as BlockSyntax;
StatementSyntax oldStatement = oldBlock.Statements[0];
SyntaxList<StatementSyntax> newStatements = oldBlock.Statements.Replace(oldStatement, localDeclaration);
BlockSyntax newBlock = oldBlock.WithStatements(newStatements);
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// adds the trailing trivia variable
private async Task<Document> TrailingVarMissingAsync(Document document, IfStatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var ifStatement = declaration.Parent.Parent as IfStatementSyntax;
SyntaxList<SyntaxNode> localDeclaration = new SyntaxList<SyntaxNode>().Add(CodeFixHelper.TriviaVarMissingHelper(generator, ifStatement));
var oldBlock = declaration.Statement as BlockSyntax;
BlockSyntax newBlock = oldBlock.WithStatements(localDeclaration);
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// replaces the trailing trivia variable
private async Task<Document> TrailingVarIncorrectAsync(Document document, IfStatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
IfStatementSyntax ifStatement;
if (declaration.Parent.Parent.Parent.Parent.Kind() == SyntaxKind.MethodDeclaration)
{
ifStatement = declaration as IfStatementSyntax;
}
else
{
ifStatement = declaration.Parent.Parent as IfStatementSyntax;
}
var localDeclaration = CodeFixHelper.TriviaVarMissingHelper(generator, ifStatement) as LocalDeclarationStatementSyntax;
var oldBlock = ifStatement.Statement as BlockSyntax;
StatementSyntax oldStatement = oldBlock.Statements[0];
SyntaxList<StatementSyntax> newStatements = oldBlock.Statements.Replace(oldStatement, localDeclaration);
BlockSyntax newBlock = oldBlock.WithStatements(newStatements);
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// replaces the trailing trivia kind check
private async Task<Document> TrailingKindCheckIncorrectAsync(Document document, IfStatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
IfStatementSyntax ifStatement;
var ifBlockStatements = new SyntaxList<SyntaxNode>();
if (declaration.Parent.Parent.Parent.Parent.Kind() == SyntaxKind.MethodDeclaration)
{
ifStatement = declaration as IfStatementSyntax;
}
else
{
ifStatement = declaration.Parent.Parent as IfStatementSyntax;
if (declaration.Statement is BlockSyntax ifBlock)
{
ifBlockStatements = ifBlock.Statements;
}
}
var newIfStatement = CodeFixHelper.TriviaKindCheckHelper(generator, ifStatement, ifBlockStatements) as StatementSyntax;
var oldBlock = ifStatement.Statement as BlockSyntax;
StatementSyntax oldStatement = oldBlock.Statements[1];
SyntaxList<StatementSyntax> newStatements = oldBlock.Statements.Replace(oldStatement, newIfStatement);
BlockSyntax newBlock = oldBlock.WithStatements(newStatements);
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// adds the trailing trivia kind check
private async Task<Document> TrailingKindCheckMissingAsync(Document document, IfStatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var ifBlockStatements = new SyntaxList<SyntaxNode>();
SyntaxTriviaList leadingTrivia = SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// Checks that the single trailing trivia is of kind whitespace (as opposed to a comment for example)").ElementAt(0), SyntaxFactory.CarriageReturnLineFeed);
StatementSyntax newIfStatement = (CodeFixHelper.TriviaKindCheckHelper(generator, declaration, ifBlockStatements) as StatementSyntax).WithLeadingTrivia(leadingTrivia);
var oldBlock = declaration.Statement as BlockSyntax;
BlockSyntax newBlock = oldBlock.AddStatements(newIfStatement);
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// replaces the whitespace check
private async Task<Document> WhitespaceCheckIncorrectAsync(Document document, IfStatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
IfStatementSyntax ifStatement;
var ifBlockStatements = new SyntaxList<SyntaxNode>();
if (declaration.Parent.Parent.Parent.Parent.Parent.Parent.Kind() == SyntaxKind.MethodDeclaration)
{
ifStatement = declaration as IfStatementSyntax;
}
else
{
ifStatement = declaration.Parent.Parent as IfStatementSyntax;
if (declaration.Statement is BlockSyntax ifBlock)
{
ifBlockStatements = ifBlock.Statements;
}
}
var newIfStatement = CodeFixHelper.WhitespaceCheckHelper(generator, ifStatement, ifBlockStatements) as StatementSyntax;
var oldBlock = ifStatement.Statement as BlockSyntax;
StatementSyntax oldStatement = oldBlock.Statements[0];
SyntaxList<StatementSyntax> newStatements = oldBlock.Statements.Replace(oldStatement, newIfStatement);
BlockSyntax newBlock = oldBlock.WithStatements(newStatements);
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// adds the whitespace check
private async Task<Document> WhitespaceCheckMissingAsync(Document document, IfStatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
var ifBlockStatements = new SyntaxList<SyntaxNode>();
SyntaxTriviaList leadingTrivia = SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// Finally, this statement checks that the trailing trivia is one single space").ElementAt(0), SyntaxFactory.CarriageReturnLineFeed);
SyntaxList<SyntaxNode> newIfStatement = new SyntaxList<SyntaxNode>().Add((CodeFixHelper.WhitespaceCheckHelper(generator, declaration, ifBlockStatements) as StatementSyntax).WithLeadingTrivia(leadingTrivia));
var oldBlock = declaration.Statement as BlockSyntax;
BlockSyntax newBlock = oldBlock.WithStatements(newIfStatement);
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// replaces the return statement
private async Task<Document> ReturnIncorrectAsync(Document document, IfStatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
IfStatementSyntax ifStatement;
if (declaration.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Kind() != SyntaxKind.MethodDeclaration)
{
ifStatement = declaration.Parent.Parent as IfStatementSyntax;
}
else
{
ifStatement = declaration as IfStatementSyntax;
}
var returnStatement = generator.ReturnStatement() as ReturnStatementSyntax;
var oldBlock = ifStatement.Statement as BlockSyntax;
SyntaxList<StatementSyntax> newStatements = oldBlock.Statements.Replace(oldBlock.Statements[0], returnStatement.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// If the analyzer is satisfied that there is only a single whitespace between 'if' and '(', it will return from this method without reporting a diagnostic").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))));
BlockSyntax newBlock = oldBlock.WithStatements(newStatements);
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// adds the return statement
private async Task<Document> ReturnMissingAsync(Document document, IfStatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
SyntaxList<SyntaxNode> returnStatements = new SyntaxList<SyntaxNode>().Add(generator.ReturnStatement().WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.ParseLeadingTrivia("// If the analyzer is satisfied that there is only a single whitespace between 'if' and '(', it will return from this method without reporting a diagnostic").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))));
var oldBlock = declaration.Statement as BlockSyntax;
BlockSyntax newBlock = oldBlock.WithStatements(returnStatements);
return await ReplaceNode(oldBlock, newBlock, document, cancellationToken).ConfigureAwait(false);
}
// replaces the open paren statement
private async Task<Document> ReplaceOpenParenAsync(Document document, StatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
MethodDeclarationSyntax methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string expressionString = CodeFixHelper.GetIfStatementName(methodDeclaration.Body);
SyntaxNode openParen = CodeFixHelper.CreateOpenParen(generator, expressionString);
return await ReplaceNode(declaration, openParen.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// Extracts the opening parenthesis of the if-statement condition").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document, cancellationToken).ConfigureAwait(false);
}
// adds the open paren statement
private async Task<Document> AddOpenParenAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
string expressionString = CodeFixHelper.GetIfStatementName(declaration.Body);
SyntaxNode openParen = CodeFixHelper.CreateOpenParen(generator, expressionString);
SyntaxNode newMethod = CodeFixHelper.AddStatementToMethod(generator, declaration, openParen.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// Extracts the opening parenthesis of the if-statement condition").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))));
return await ReplaceNode(declaration, newMethod, document, cancellationToken).ConfigureAwait(false);
}
// replaces the start span statement
private async Task<Document> ReplaceStartSpanAsync(Document document, StatementSyntax declaration, CancellationToken cancellationToken)
{
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);
MethodDeclarationSyntax methodDeclaration = declaration.Ancestors().OfType<MethodDeclarationSyntax>().First();
string identifierString = CodeFixHelper.GetIfKeywordName(methodDeclaration.Body);
SyntaxNode startSpan = CodeFixHelper.CreateEndOrStartSpan(generator, identifierString, "startDiagnosticSpan");
return await ReplaceNode(declaration, startSpan.WithLeadingTrivia(SyntaxFactory.TriviaList(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.ParseLeadingTrivia("// Determines the start of the span of the diagnostic that will be reported, ie the start of the squiggle").ElementAt(0), SyntaxFactory.EndOfLine("\r\n"))), document, cancellationToken).ConfigureAwait(false);
}