/
DiagnosticAnalyzer.cs
3109 lines (2631 loc) · 154 KB
/
DiagnosticAnalyzer.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.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
#pragma warning disable CA1820 // Test for empty strings using string length
namespace MetaCompilation.Analyzers
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MetaCompilationAnalyzer : DiagnosticAnalyzer
{
private const string s_messagePrefix = "T: ";
//default values for the DiagnosticDescriptors
private const string s_ruleCategory = "Tutorial";
private const DiagnosticSeverity _ruleDefaultSeverity = DiagnosticSeverity.Error;
private const bool _ruleEnabledByDefault = true;
//creates a DiagnosticDescriptor with the above defaults
public static DiagnosticDescriptor CreateRule(string id, string title, string messageFormat, string description = "")
{
DiagnosticDescriptor rule = new DiagnosticDescriptor(
id: id,
title: title,
messageFormat: messageFormat,
defaultSeverity: _ruleDefaultSeverity,
isEnabledByDefault: _ruleEnabledByDefault,
category: s_ruleCategory,
description: description
);
return rule;
}
#region id rules
public const string MissingId = "MetaAnalyzer001";
internal static readonly DiagnosticDescriptor MissingIdRule = CreateRule(MissingId, "Missing diagnostic id", s_messagePrefix + "'{0}' should have a diagnostic id (a public, constant string uniquely identifying each diagnostic)", "The diagnostic id identifies a particular diagnostic so that the diagnotic can be fixed in CodeFixProvider.cs");
#endregion
#region Initialize rules
public const string MissingInit = "MetaAnalyzer002";
internal static readonly DiagnosticDescriptor MissingInitRule = CreateRule(MissingInit, "Missing Initialize method", s_messagePrefix + "'{0}' is missing the required inherited Initialize method, needed to register analysis actions", "An analyzer requires the Initialize method to register code analysis actions. Actions are registered to call an analysis method when something specific changes in the syntax tree or semantic model. For example, context.RegisterSyntaxNodeAction(AnalyzeMethod, SyntaxKind.IfStatement) will call AnalyzeMethod every time an if-statement changes in the syntax tree.");
public const string MissingRegisterStatement = "MetaAnalyzer003";
internal static readonly DiagnosticDescriptor MissingRegisterRule = CreateRule(MissingRegisterStatement, "Missing register statement", s_messagePrefix + "A syntax node action should be registered within the '{0}' method", "The Initialize method must register for at least one action so that some analysis can be performed. Otherwise, analysis will not be performed and no diagnostics will be reported. Registering a syntax node action is useful for analyzing the syntax of a piece of code.");
public const string TooManyInitStatements = "MetaAnalyzer004";
internal static readonly DiagnosticDescriptor TooManyInitStatementsRule = CreateRule(TooManyInitStatements, "Multiple registered actions", s_messagePrefix + "For this tutorial, the '{0}' method should only register one action", "For this tutorial only, the Initialize method should only register one action. More complicated analyzers may need to register multiple actions.");
public const string IncorrectInitSig = "MetaAnalyzer005";
internal static readonly DiagnosticDescriptor IncorrectInitSigRule = CreateRule(IncorrectInitSig, "Incorrect method signature", s_messagePrefix + "The '{0}' method should return void, have the 'override' modifier, and have a single parameter of type 'AnalysisContext'", "The Initialize method should override the abstract Initialize class member from the DiagnosticAnalyzer class. It therefore needs to be public, overriden, and return void. It needs to have a single input parameter of type 'AnalysisContext.'");
public const string InvalidStatement = "MetaAnalyzer006";
internal static readonly DiagnosticDescriptor InvalidStatementRule = CreateRule(InvalidStatement, "Incorrect statement", s_messagePrefix + "The Initialize method only registers actions, therefore any other statement placed in Initialize is incorrect", "By definition, the purpose of the Initialize method is to register actions for analysis. Therefore, all other statements placed in Initialize are incorrect.");
public const string IncorrectKind = "MetaAnalyzer051";
internal static readonly DiagnosticDescriptor IncorrectKindRule = CreateRule(IncorrectKind, "Incorrect kind", s_messagePrefix + "This tutorial only allows registering for SyntaxKind.IfStatement", "For the purposes of this tutorial, the only analysis will occur on an if-statement, so it is only necessary to register for syntax of kind IfStatement");
public const string IncorrectRegister = "MetaAnalyzer052";
internal static readonly DiagnosticDescriptor IncorrectRegisterRule = CreateRule(IncorrectRegister, "Incorrect register", s_messagePrefix + "This tutorial only registers SyntaxNode actions", "For the purposes of this tutorial, analysis will occur on SyntaxNodes, so only actions on SyntaxNodes should be registered");
public const string IncorrectArguments = "MetaAnalyzer053";
internal static readonly DiagnosticDescriptor IncorrectArgumentsRule = CreateRule(IncorrectArguments, "Incorrect arguments", s_messagePrefix + "The method RegisterSyntaxNodeAction requires 2 arguments: a method and a SyntaxKind", "The RegisterSyntaxNodeAction method takes two arguments. The first argument is a method that will be called to perform the analysis. The second argument is a SyntaxKind, which is the kind of syntax that the method will be triggered on");
#endregion
#region SupportedDiagnostics rules
public const string MissingSuppDiag = "MetaAnalyzer007";
internal static readonly DiagnosticDescriptor MissingSuppDiagRule = CreateRule(MissingSuppDiag, "Missing SupportedDiagnostics property", s_messagePrefix + "You are missing the required inherited SupportedDiagnostics property", "The SupportedDiagnostics property tells the analyzer which diagnostic ids the analyzer supports, in other words, which DiagnosticDescriptors might be reported by the analyzer. Generally, any DiagnosticDescriptor should be returned by SupportedDiagnostics.");
public const string IncorrectSigSuppDiag = "MetaAnalyzer008";
internal static readonly DiagnosticDescriptor IncorrectSigSuppDiagRule = CreateRule(IncorrectSigSuppDiag, "Incorrect SupportedDiagnostics property", s_messagePrefix + "The overriden SupportedDiagnostics property should return an Immutable Array of Diagnostic Descriptors");
public const string MissingAccessor = "MetaAnalyzer009";
internal static readonly DiagnosticDescriptor MissingAccessorRule = CreateRule(MissingAccessor, "Missing get-accessor", s_messagePrefix + "The '{0}' property is missing a get-accessor to return a list of supported diagnostics", "The SupportedDiagnostics property needs to have a get-accessor to make the ImmutableArray of DiagnosticDescriptors accessible");
public const string TooManyAccessors = "MetaAnalyzer010";
internal static readonly DiagnosticDescriptor TooManyAccessorsRule = CreateRule(TooManyAccessors, "Too many accessors", s_messagePrefix + "The '{0}' property needs only a single get-accessor", "The purpose of the SupportedDiagnostics property is to return a list of all diagnostics that can be reported by a particular analyzer, so it doesn't have a need for any other accessors");
public const string IncorrectAccessorReturn = "MetaAnalyzer011";
internal static readonly DiagnosticDescriptor IncorrectAccessorReturnRule = CreateRule(IncorrectAccessorReturn, "Get accessor missing return value", s_messagePrefix + "The get-accessor should return an ImmutableArray containing all of the DiagnosticDescriptor rules", "The purpose of the SupportedDiagnostics property's get-accessor is to return a list of all diagnostics that can be reported by a particular analyzer");
public const string SuppDiagReturnValue = "MetaAnalyzer012";
internal static readonly DiagnosticDescriptor SuppDiagReturnValueRule = CreateRule(SuppDiagReturnValue, "SupportedDiagnostics return value incorrect", s_messagePrefix + "The '{0}' property's get-accessor should return an ImmutableArray containing all DiagnosticDescriptor rules", "The purpose of the SupportedDiagnostics property's get-accessor is to return a list of all diagnostics that can be reported by a particular analyzer");
public const string SupportedRules = "MetaAnalyzer013";
internal static readonly DiagnosticDescriptor SupportedRulesRule = CreateRule(SupportedRules, "ImmutableArray incorrect", s_messagePrefix + "The ImmutableArray should contain every DiagnosticDescriptor rule that was created", "The purpose of the SupportedDiagnostics property is to return a list of all diagnostics that can be reported by a particular analyzer, so it should include every DiagnosticDescriptor rule that is created");
#endregion
#region rule rules
public const string IdDeclTypeError = "MetaAnalyzer014";
internal static readonly DiagnosticDescriptor IdDeclTypeErrorRule = CreateRule(IdDeclTypeError, "Incorrect DiagnosticDescriptor id", s_messagePrefix + "The diagnostic id should be the constant string declared above", "The id parameter of a DiagnosticDescriptor should be a string constant previously declared. This ensures that the diagnostic id is accessible from the CodeFixProvider.cs file.");
public const string MissingIdDeclaration = "MetaAnalyzer015";
internal static readonly DiagnosticDescriptor MissingIdDeclarationRule = CreateRule(MissingIdDeclaration, "Missing Diagnostic id declaration", s_messagePrefix + "This diagnostic id should be the constant string declared above", "The id parameter of a DiagnosticDescriptor should be a string constant previously declared. This ensures that the diagnostic id is accessible from the CodeFixProvider.cs file.");
public const string DefaultSeverityError = "MetaAnalyzer016";
internal static readonly DiagnosticDescriptor DefaultSeverityErrorRule = CreateRule(DefaultSeverityError, "Incorrect defaultSeverity", s_messagePrefix + "The 'defaultSeverity' should be either DiagnosticSeverity.Error or DiagnosticSeverity.Warning", "There are four option for the severity of the diagnostic: error, warning, hidden, and info. An error is completely not allowed and causes build errors. A warning is something that might be a problem, but is not a build error. An info diagnostic is simply information and is not actually a problem. A hidden diagnostic is raised as an issue, but it is not accessible through normal means. In simple analyzers, the most common severities are error and warning.");
public const string EnabledByDefaultError = "MetaAnalyzer017";
internal static readonly DiagnosticDescriptor EnabledByDefaultErrorRule = CreateRule(EnabledByDefaultError, "Incorrect isEnabledByDefault", s_messagePrefix + "The 'isEnabledByDefault' field should be set to true", "The 'isEnabledByDefault' field determines whether the diagnostic is enabled by default or the user of the analyzer has to manually enable the diagnostic. Generally, it will be set to true.");
public const string InternalAndStaticError = "MetaAnalyzer018";
internal static readonly DiagnosticDescriptor InternalAndStaticErrorRule = CreateRule(InternalAndStaticError, "Incorrect DiagnosticDescriptor modifiers", s_messagePrefix + "The '{0}' field should be internal and static", "The DiagnosticDescriptor rules should all be internal because they only need to be accessed by pieces of this project and nothing outside. They should be static because their lifetime will extend throughout the entire running of this program");
public const string MissingRule = "MetaAnalyzer019";
internal static readonly DiagnosticDescriptor MissingRuleRule = CreateRule(MissingRule, "Missing DiagnosticDescriptor", s_messagePrefix + "The analyzer should have at least one DiagnosticDescriptor rule", "The DiagnosticDescriptor rule is what is reported by the analyzer when it finds a problem, so there must be at least one. It should have an id to differentiate the diagnostic, a default severity level, a boolean describing if it's enabled by default, a title, a message, and a category.");
public const string IdStringLiteral = "MetaAnalyzer059";
internal static readonly DiagnosticDescriptor IdStringLiteralRule = CreateRule(IdStringLiteral, "ID string literal", s_messagePrefix + "The ID should not be a string literal, because the ID must be accessible from the code fix provider");
public const string Title = "MetaAnalyzer060";
internal static readonly DiagnosticDescriptor TitleRule = CreateRule(Title, "Change default title", s_messagePrefix + "Please change the title to a string of your choosing");
public const string Message = "MetaAnalyzer061";
internal static readonly DiagnosticDescriptor MessageRule = CreateRule(Message, "Change default message", s_messagePrefix + "Please change the default message to a string of your choosing");
public const string Category = "MetaAnalyzer062";
internal static readonly DiagnosticDescriptor CategoryRule = CreateRule(Category, "Change default category", s_messagePrefix + "Please change the category to a string of your choosing");
#endregion
#region analysis for IfStatement rules
public const string IfStatementMissing = "MetaAnalyzer020";
internal static readonly DiagnosticDescriptor IfStatementMissingRule = CreateRule(IfStatementMissing, "Missing if-statement extraction", s_messagePrefix + "The first step of the SyntaxNode analysis is to extract the if-statement from '{0}' by casting {0}.Node to IfStatementSyntax", "The context parameter has a Node member. This Node is what the register statement from Initialize triggered on, and so should be cast to the expected syntax or symbol type");
public const string IfStatementIncorrect = "MetaAnalyzer021";
internal static readonly DiagnosticDescriptor IfStatementIncorrectRule = CreateRule(IfStatementIncorrect, "If-statement extraction incorrect", s_messagePrefix + "This statement should extract the if-statement being analyzed by casting {0}.Node to IfStatementSyntax", "The context parameter has a Node member. This Node is what the register statement from Initialize triggered on, so it should be cast to the expected syntax or symbol type");
public const string IfKeywordMissing = "MetaAnalyzer022";
internal static readonly DiagnosticDescriptor IfKeywordMissingRule = CreateRule(IfKeywordMissing, "Missing if-keyword extraction", s_messagePrefix + "Next, extract the if-keyword SyntaxToken from '{0}'", "In the syntax tree, a node of type IfStatementSyntax has an IfKeyword attached to it. On the syntax tree diagram, this is represented by the green 'if' SyntaxToken");
public const string IfKeywordIncorrect = "MetaAnalyzer023";
internal static readonly DiagnosticDescriptor IfKeywordIncorrectRule = CreateRule(IfKeywordIncorrect, "Incorrect if-keyword extraction", s_messagePrefix + "This statement should extract the if-keyword SyntaxToken from '{0}'", "In the syntax tree, a node of type IfStatementSyntax has an IfKeyword attached to it. On the syntax tree diagram, this is represented by the green 'if' SyntaxToken");
public const string TrailingTriviaCheckMissing = "MetaAnalyzer024";
internal static readonly DiagnosticDescriptor TrailingTriviaCheckMissingRule = CreateRule(TrailingTriviaCheckMissing, "Missing trailing trivia check", s_messagePrefix + "Next, begin looking for the space between 'if' and '(' by checking if '{0}' has trailing trivia", "Syntax trivia are all the things that aren't actually code (i.e. comments, whitespace, end of line tokens, etc). The first step in checking for a single space between the if-keyword and '(' is to check if the if-keyword SyntaxToken has any trailing trivia");
public const string TrailingTriviaCheckIncorrect = "MetaAnalyzer025";
internal static readonly DiagnosticDescriptor TrailingTriviaCheckIncorrectRule = CreateRule(TrailingTriviaCheckIncorrect, "Incorrect trailing trivia check", s_messagePrefix + "This statement should be an if-statement that checks to see if '{0}' has trailing trivia", "Syntax trivia are all the things that aren't actually code (i.e. comments, whitespace, end of line tokens, etc). The first step in checking for a single space between the if-keyword and '(' is to check if the if-keyword SyntaxToken has any trailing trivia");
public const string TrailingTriviaVarMissing = "MetaAnalyzer026";
internal static readonly DiagnosticDescriptor TrailingTriviaVarMissingRule = CreateRule(TrailingTriviaVarMissing, "Missing trailing trivia extraction", s_messagePrefix + "Next, extract the first trailing trivia of '{0}' into a variable", "The first trailing trivia of the if-keyword should be a single whitespace");
public const string TrailingTriviaVarIncorrect = "MetaAnalyzer027";
internal static readonly DiagnosticDescriptor TrailingTriviaVarIncorrectRule = CreateRule(TrailingTriviaVarIncorrect, "Incorrect trailing trivia extraction", s_messagePrefix + "This statement should extract the first trailing trivia of '{0}' into a variable", "The first trailing trivia of the if-keyword should be a single whitespace");
public const string TrailingTriviaKindCheckMissing = "MetaAnalyzer028";
internal static readonly DiagnosticDescriptor TrailingTriviaKindCheckMissingRule = CreateRule(TrailingTriviaKindCheckMissing, "Missing SyntaxKind check", s_messagePrefix + "Next, check if the kind of '{0}' is whitespace trivia");
public const string TrailingTriviaKindCheckIncorrect = "MetaAnalyzer029";
internal static readonly DiagnosticDescriptor TrailingTriviaKindCheckIncorrectRule = CreateRule(TrailingTriviaKindCheckIncorrect, "Incorrect SyntaxKind check", s_messagePrefix + "This statement should check to see if the kind of '{0}' is whitespace trivia");
public const string WhitespaceCheckMissing = "MetaAnalyzer030";
internal static readonly DiagnosticDescriptor WhitespaceCheckMissingRule = CreateRule(WhitespaceCheckMissing, "Missing whitespace check", s_messagePrefix + "Next, check if '{0}' is a single whitespace, which is the desired formatting");
public const string WhitespaceCheckIncorrect = "MetaAnalyzer031";
internal static readonly DiagnosticDescriptor WhitespaceCheckIncorrectRule = CreateRule(WhitespaceCheckIncorrect, "Incorrect whitespace check", s_messagePrefix + "This statement should check to see if '{0}' is a single whitespace, which is the desired formatting");
public const string ReturnStatementMissing = "MetaAnalyzer032";
internal static readonly DiagnosticDescriptor ReturnStatementMissingRule = CreateRule(ReturnStatementMissing, "Missing return", s_messagePrefix + "Next, since if the code reaches this point the formatting must be correct, return from '{0}'", "If the analyzer determines that there are no issues with the code it is analyzing, it can simply return from the analysis method without reporting any diagnostics");
public const string ReturnStatementIncorrect = "MetaAnalyzer033";
internal static readonly DiagnosticDescriptor ReturnStatementIncorrectRule = CreateRule(ReturnStatementIncorrect, "Incorrect return", s_messagePrefix + "This statement should return from '{0}', because reaching this point in the code means that the if-statement being analyzed has the correct spacing", "If the analyzer determines that there are no issues with the code it is analyzing, it can simply return from the analysis method without reporting any diagnostics");
public const string OpenParenMissing = "MetaAnalyzer034";
internal static readonly DiagnosticDescriptor OpenParenMissingRule = CreateRule(OpenParenMissing, "Missing open parenthesis variable", s_messagePrefix + "Moving on to the creation and reporting of the diagnostic, extract the open parenthesis of '{0}' into a variable to use as the end of the diagnostic span", "The open parenthesis of the condition is going to be the end point of the diagnostic squiggle that is created");
public const string OpenParenIncorrect = "MetaAnalyzer035";
internal static readonly DiagnosticDescriptor OpenParenIncorrectRule = CreateRule(OpenParenIncorrect, "Open parenthesis variable incorrect", s_messagePrefix + "This statement should extract the open parenthesis of '{0}' to use as the end of the diagnostic span", "The open parenthesis of the condition is going to be the end point of the diagnostic squiggle that is created");
public const string StartSpanMissing = "MetaAnalyzer036";
internal static readonly DiagnosticDescriptor StartSpanMissingRule = CreateRule(StartSpanMissing, "Start span variable missing", s_messagePrefix + "Next, extract the start of the span of '{0}' into a variable, to be used as the start of the diagnostic span", "Each node in the syntax tree has a span. This span represents the number of character spaces that the node takes up");
public const string StartSpanIncorrect = "MetaAnalyzer037";
internal static readonly DiagnosticDescriptor StartSpanIncorrectRule = CreateRule(StartSpanIncorrect, "Start span variable incorrect", s_messagePrefix + "This statement should extract the start of the span of '{0}' into a variable, to be used as the start of the diagnostic span", "Each node in the syntax tree has a span. This span represents the number of character spaces that the node takes up");
public const string EndSpanMissing = "MetaAnalyzer038";
internal static readonly DiagnosticDescriptor EndSpanMissingRule = CreateRule(EndSpanMissing, "End span variable missing", s_messagePrefix + "Next, determine the end of the span of the diagnostic that is going to be reported", "The open parenthesis of the condition is going to be the end point of the diagnostic squiggle that is created");
public const string EndSpanIncorrect = "MetaAnalyzer039";
internal static readonly DiagnosticDescriptor EndSpanIncorrectRule = CreateRule(EndSpanIncorrect, "End span variable incorrect", s_messagePrefix + "This statement should extract the start of the span of '{0}' into a variable, to be used as the end of the diagnostic span", "Each node in the syntax tree has a span. This span represents the number of character spaces that the node takes up");
public const string SpanMissing = "MetaAnalyzer040";
internal static readonly DiagnosticDescriptor SpanMissingRule = CreateRule(SpanMissing, "Diagnostic span variable missing", s_messagePrefix + "Next, using TextSpan.FromBounds, create a variable that is the span of the diagnostic that will be reported", "Each node in the syntax tree has a span. This span represents the number of character spaces that the node takes up");
public const string SpanIncorrect = "MetaAnalyzer041";
internal static readonly DiagnosticDescriptor SpanIncorrectRule = CreateRule(SpanIncorrect, "Diagnostic span variable incorrect", s_messagePrefix + "This statement should use TextSpan.FromBounds, '{0}', and '{1}' to create the span of the diagnostic that will be reported", "Each node in the syntax tree has a span. This span represents the number of character spaces that the node takes up. TextSpan.FromBounds(start, end) can be used to create a span to use for a diagnostic");
public const string LocationMissing = "MetaAnalyzer042";
internal static readonly DiagnosticDescriptor LocationMissingRule = CreateRule(LocationMissing, "Diagnostic location variable missing", s_messagePrefix + "Next, using Location.Create, create a location for the diagnostic", "A location can be created by combining a span with a syntax tree. The span is applied to the given syntax tree so that the location within the syntax tree is determined");
public const string LocationIncorrect = "MetaAnalyzer043";
internal static readonly DiagnosticDescriptor LocationIncorrectRule = CreateRule(LocationIncorrect, "Diagnostic location variable incorrect", s_messagePrefix + "This statement should use Location.Create, '{0}', and '{1}' to create the location of the diagnostic", "A location can be created by combining a span with a syntax tree. The span is applied to the given syntax tree so that the location within the syntax tree is determined");
public const string TrailingTriviaCountMissing = "MetaAnalyzer057";
internal static readonly DiagnosticDescriptor TriviaCountMissingRule = CreateRule(TrailingTriviaCountMissing, "Trailing trivia count missing", s_messagePrefix + "Next, check that '{0}' only has one trailing trivia element");
public const string TrailingTriviaCountIncorrect = "MetaAnalyzer058";
internal static readonly DiagnosticDescriptor TriviaCountIncorrectRule = CreateRule(TrailingTriviaCountIncorrect, "Trailing trivia count incorrect", s_messagePrefix + "This statement should check that '{0}' only has one trailing trivia element");
#endregion
#region analysis rules
public const string MissingAnalysisMethod = "MetaAnalyzer044";
internal static readonly DiagnosticDescriptor MissingAnalysisMethodRule = CreateRule(MissingAnalysisMethod, "Missing analysis method", s_messagePrefix + "The method '{0}' that was registered to perform the analysis is missing", "In Initialize, the register statement denotes an analysis method to be called when an action is triggered. This method needs to be created");
public const string IncorrectAnalysisAccessibility = "MetaAnalyzer054";
internal static readonly DiagnosticDescriptor IncorrectAnalysisAccessibilityRule = CreateRule(IncorrectAnalysisAccessibility, "Incorrect analysis method accessibility", s_messagePrefix + "The '{0}' method should be private");
public const string IncorrectAnalysisReturnType = "MetaAnalyzer055";
internal static readonly DiagnosticDescriptor IncorrectAnalysisReturnTypeRule = CreateRule(IncorrectAnalysisReturnType, "Incorrect analysis method return type", s_messagePrefix + "The '{0}' method should have a void return type");
public const string IncorrectAnalysisParameter = "MetaAnalyzer056";
internal static readonly DiagnosticDescriptor IncorrectAnalysisParameterRule = CreateRule(IncorrectAnalysisParameter, "Incorrect parameter to analysis method", s_messagePrefix + "The '{0}' method should take one parameter of type SyntaxNodeAnalysisContext");
public const string TooManyStatements = "MetaAnalyzer045";
internal static readonly DiagnosticDescriptor TooManyStatementsRule = CreateRule(TooManyStatements, "Too many statements", s_messagePrefix + "This {0} should only have {1} statement(s), which should {2}", "For the purpose of this tutorial there are too many statements here, use the code fixes to guide you through the creation of this section");
public const string DiagnosticMissing = "MetaAnalyzer046";
internal static readonly DiagnosticDescriptor DiagnosticMissingRule = CreateRule(DiagnosticMissing, "Diagnostic variable missing", s_messagePrefix + "Next, use Diagnostic.Create to create the diagnostic", "This is the diagnostic that will be reported to the user as an error squiggle");
public const string DiagnosticIncorrect = "MetaAnalyzer047";
internal static readonly DiagnosticDescriptor DiagnosticIncorrectRule = CreateRule(DiagnosticIncorrect, "Diagnostic variable incorrect", s_messagePrefix + "This statement should use Diagnostic.Create, '{0}', and '{1}' to create the diagnostic that will be reported", "The diagnostic is created with a DiagnosticDescriptor, a Location, and message arguments. The message arguments are the inputs to the DiagnosticDescriptor MessageFormat format string");
public const string DiagnosticReportMissing = "MetaAnalyzer048";
internal static readonly DiagnosticDescriptor DiagnosticReportMissingRule = CreateRule(DiagnosticReportMissing, "Diagnostic report missing", s_messagePrefix + "Next, use '{0}'.ReportDiagnostic to report the diagnostic that has been created", "A diagnostic is reported to a context so that the diagnostic will appear as a squiggle and in the eroor list");
public const string DiagnosticReportIncorrect = "MetaAnalyzer049";
internal static readonly DiagnosticDescriptor DiagnosticReportIncorrectRule = CreateRule(DiagnosticReportIncorrect, "Diagnostic report incorrect", s_messagePrefix + "This statement should use {0}.ReportDiagnostic to report '{1}'", "A diagnostic is reported to a context so that the diagnostic will appear as a squiggle and in the eroor list");
#endregion
public const string GoToCodeFix = "MetaAnalyzer050";
internal static readonly DiagnosticDescriptor GoToCodeFixRule = new DiagnosticDescriptor(
id: GoToCodeFix,
title: "Analyzer tutorial complete",
messageFormat: s_messagePrefix + "Congratulations! You have written an analyzer! If you would like to explore a code fix for your diagnostic, open up CodeFixProvider.cs and take a look! To see your analyzer in action, press F5. A new instance of Visual Studio will open up, in which you can open a new C# console app and write test if-statements.",
category: s_ruleCategory,
defaultSeverity: DiagnosticSeverity.Info,
isEnabledByDefault: true);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(
MissingIdRule,
MissingInitRule,
MissingRegisterRule,
TooManyInitStatementsRule,
IncorrectInitSigRule,
InvalidStatementRule,
MissingSuppDiagRule,
IncorrectSigSuppDiagRule,
MissingAccessorRule,
TooManyAccessorsRule,
IncorrectAccessorReturnRule,
SuppDiagReturnValueRule,
SupportedRulesRule,
IdDeclTypeErrorRule,
MissingIdDeclarationRule,
DefaultSeverityErrorRule,
EnabledByDefaultErrorRule,
InternalAndStaticErrorRule,
MissingRuleRule,
IfStatementMissingRule,
IfStatementIncorrectRule,
IfKeywordMissingRule,
IfKeywordIncorrectRule,
TrailingTriviaCheckMissingRule,
TrailingTriviaCheckIncorrectRule,
TrailingTriviaVarMissingRule,
TrailingTriviaVarIncorrectRule,
TrailingTriviaKindCheckIncorrectRule,
TrailingTriviaKindCheckMissingRule,
WhitespaceCheckMissingRule,
WhitespaceCheckIncorrectRule,
ReturnStatementMissingRule,
ReturnStatementIncorrectRule,
OpenParenIncorrectRule,
OpenParenMissingRule,
StartSpanIncorrectRule,
StartSpanMissingRule,
EndSpanIncorrectRule,
EndSpanMissingRule,
SpanIncorrectRule,
SpanMissingRule,
LocationIncorrectRule,
LocationMissingRule,
MissingAnalysisMethodRule,
IncorrectAnalysisAccessibilityRule,
IncorrectAnalysisReturnTypeRule,
IncorrectAnalysisParameterRule,
TooManyStatementsRule,
DiagnosticMissingRule,
DiagnosticIncorrectRule,
DiagnosticReportIncorrectRule,
DiagnosticReportMissingRule,
GoToCodeFixRule,
IncorrectKindRule,
IncorrectRegisterRule,
IncorrectArgumentsRule,
TriviaCountMissingRule,
TriviaCountIncorrectRule,
IdStringLiteralRule,
TitleRule,
MessageRule,
CategoryRule);
#pragma warning disable RS1025 // Configure generated code analysis
#pragma warning disable RS1026 // Enable concurrent execution
public override void Initialize(AnalysisContext context)
#pragma warning restore RS1026 // Enable concurrent execution
#pragma warning restore RS1025 // Configure generated code analysis
{
context.RegisterCompilationStartAction(SetupAnalysis);
}
//creates an instance of a class to perform the analysis statefully, and registers for various actions
private void SetupAnalysis(CompilationStartAnalysisContext context)
{
//state collector
CompilationAnalyzer compilationAnalyzer = new CompilationAnalyzer();
//collects all class, method, field, and property symbols as state
context.RegisterSymbolAction(compilationAnalyzer.AddClass, SymbolKind.NamedType);
context.RegisterSymbolAction(compilationAnalyzer.AddMethod, SymbolKind.Method);
context.RegisterSymbolAction(compilationAnalyzer.AddField, SymbolKind.Field);
context.RegisterSymbolAction(compilationAnalyzer.AddProperty, SymbolKind.Property);
//analyzes the state that has been collected
context.RegisterCompilationEndAction(compilationAnalyzer.ReportCompilationEndDiagnostics);
}
// Performs stateful analysis
private class CompilationAnalyzer
{
private readonly List<IMethodSymbol> _analyzerMethodSymbols = new List<IMethodSymbol>();
private readonly List<IPropertySymbol> _analyzerPropertySymbols = new List<IPropertySymbol>();
private readonly List<IFieldSymbol> _analyzerFieldSymbols = new List<IFieldSymbol>();
private readonly List<INamedTypeSymbol> _otherAnalyzerClassSymbols = new List<INamedTypeSymbol>();
private IMethodSymbol _initializeSymbol;
private IPropertySymbol _propertySymbol;
private INamedTypeSymbol _analyzerClassSymbol;
private readonly Dictionary<string, string> _branchesDict = new Dictionary<string, string>();
private readonly List<IMethodSymbol> _codeFixMethodSymbols = new List<IMethodSymbol>();
//"main" method, performs the analysis once state has been collected
internal protected void ReportCompilationEndDiagnostics(CompilationAnalysisContext context)
{
//supported main branches for tutorial
_branchesDict.Add("RegisterSyntaxNodeAction", "SyntaxNode");
//supported sub-branches for tutorial
List<string> allowedKinds = new List<string>
{
"IfStatement"
};
if (_analyzerClassSymbol == null)
{
return;
}
//look for and interpret id fields
List<string> idNames = CheckIds();
if (idNames.Count > 0)
{
//look for and interpret rule fields
List<string> ruleNames = CheckRules(idNames, context);
if (ruleNames.Count > 0)
{
//look for and interpret SupportedDiagnostics property
bool supportedDiagnosticsCorrect = CheckSupportedDiagnostics(ruleNames, context);
if (supportedDiagnosticsCorrect)
{
//gather initialize info
CheckInitializeInfo registerInfo = CheckInitialize(context);
if (registerInfo == null)
{
return;
}
IMethodSymbol registerSymbol = registerInfo.RegisterMethod;
if (registerSymbol == null)
{
return;
}
IList<ISymbol> registerArgs = registerInfo.RegisterArgs;
if (registerArgs == null)
{
return;
}
if (registerArgs.Count == 0)
{
return;
}
IMethodSymbol analysisMethodSymbol = null;
if (registerArgs.Count > 0)
{
analysisMethodSymbol = (IMethodSymbol)registerArgs[0];
}
IFieldSymbol kind;
if (registerArgs.Count > 1)
{
kind = (IFieldSymbol)registerArgs[1];
}
else
{
return;
}
InvocationExpressionSyntax invocationExpression = registerInfo.InvocationExpr;
if (invocationExpression == null)
{
return;
}
//interpret initialize info
if (_branchesDict.ContainsKey(registerSymbol.Name))
{
string kindName = null;
if (kind != null)
{
kindName = kind.Name;
}
if (kindName == null || allowedKinds.Contains(kindName))
{
//look for and interpret analysis methods
bool analysisMethodFound = CheckMethods(invocationExpression, context);
if (analysisMethodFound)
{
//check the SyntaxNode, Symbol, Compilation, CodeBlock, etc analysis method(s)
bool analysisCorrect = CheckAnalysis(_branchesDict[registerSymbol.Name], kindName, ruleNames, context, analysisMethodSymbol);
if (analysisCorrect)
{
ReportDiagnostic(context, GoToCodeFixRule, _analyzerClassSymbol.Locations[0]);
}
else
{
return;
}
}
else
{
return;
}
}
else
{
Location loc = invocationExpression.ArgumentList.Arguments[1].GetLocation();
ReportDiagnostic(context, IncorrectKindRule, loc);
}
}
else
{
return;
}
}
else
{
return;
}
}
else
{
return;
}
}
else
{
ReportDiagnostic(context, MissingIdRule, _analyzerClassSymbol.Locations[0], _analyzerClassSymbol.Name);
}
}
// Checks the syntax tree analysis part of the user analyzer, returns a bool representing whether the check was successful or not
private bool CheckAnalysis(string branch, string kind, List<string> ruleNames, CompilationAnalysisContext context, IMethodSymbol analysisMethodSymbol)
{
if (branch == "SyntaxNode")
{
if (kind == "IfStatement")
{
return CheckIfStatementAnalysis(ruleNames, context, analysisMethodSymbol);
}
}
return false;
}
#region CheckAnalysis for IfStatement
// Checks the AnalyzeIfStatement of the user's analyzer, returns a bool representing whether the check was successful or not
private bool CheckIfStatementAnalysis(List<string> ruleNames, CompilationAnalysisContext context, IMethodSymbol analysisMethodSymbol)
{
var methodDeclaration = AnalysisGetStatements(analysisMethodSymbol) as MethodDeclarationSyntax;
var body = methodDeclaration.Body as BlockSyntax;
if (body == null)
{
return false;
}
SyntaxList<StatementSyntax> statements = body.Statements;
if (statements == null)
{
return false;
}
var contextParameter = methodDeclaration.ParameterList.Parameters[0] as ParameterSyntax;
if (contextParameter == null)
{
return false;
}
int statementCount = statements.Count;
if (statementCount > 0)
{
SyntaxToken statementIdentifierToken = IfStatementAnalysis1(statements, contextParameter);
if (statementIdentifierToken.Text == "")
{
IfDiagnostic(context, statements[0], IfStatementIncorrectRule, contextParameter.Identifier.Text);
return false;
}
if (statementCount > 1)
{
SyntaxToken keywordIdentifierToken = IfStatementAnalysis2(statements, statementIdentifierToken);
if (keywordIdentifierToken.Text == "")
{
IfDiagnostic(context, statements[1], IfKeywordIncorrectRule, statementIdentifierToken.Text);
return false;
}
// HasTrailingTrivia if-statement in user analyzer
if (statementCount > 2)
{
var triviaBlock = IfStatementAnalysis3(statements, keywordIdentifierToken) as BlockSyntax;
if (triviaBlock == null)
{
IfDiagnostic(context, statements[2], TrailingTriviaCheckIncorrectRule, keywordIdentifierToken.Text);
return false;
}
SyntaxList<StatementSyntax> triviaBlockStatements = triviaBlock.Statements;
if (triviaBlockStatements == null)
{
IfDiagnostic(context, triviaBlock.Parent as StatementSyntax, TriviaCountMissingRule, keywordIdentifierToken.Text);
return false;
}
if (triviaBlockStatements.Count > 0)
{
BlockSyntax triviaCountBlock = IfStatementAnalysis8(triviaBlockStatements, keywordIdentifierToken);
if (triviaCountBlock == null)
{
IfDiagnostic(context, triviaBlockStatements[0], TriviaCountIncorrectRule, keywordIdentifierToken.Text);
return false;
}
SyntaxList<StatementSyntax> triviaCountBlockStatements = triviaCountBlock.Statements;
if (triviaCountBlockStatements.Count > 0)
{
SyntaxToken triviaIdentifierToken = IfStatementAnalysis4(triviaCountBlockStatements, keywordIdentifierToken);
if (triviaIdentifierToken.Text == "")
{
IfDiagnostic(context, triviaCountBlockStatements[0], TrailingTriviaVarIncorrectRule, keywordIdentifierToken.Text);
return false;
}
// Kind if-statement in user analyzer
if (triviaCountBlockStatements.Count > 1)
{
BlockSyntax triviaKindCheckBlock = IfStatementAnalysis5(triviaCountBlockStatements, triviaIdentifierToken);
if (triviaKindCheckBlock == null)
{
IfDiagnostic(context, triviaCountBlockStatements[1], TrailingTriviaKindCheckIncorrectRule, triviaIdentifierToken.Text);
return false;
}
SyntaxList<StatementSyntax> triviaKindCheckBlockStatements = triviaKindCheckBlock.Statements;
if (triviaKindCheckBlockStatements == null)
{
IfDiagnostic(context, triviaCountBlockStatements[1], TrailingTriviaKindCheckIncorrectRule, triviaIdentifierToken.Text);
return false;
}
// Whitespace if-statement in user analyzer
if (triviaKindCheckBlockStatements.Count > 0)
{
BlockSyntax triviaCheckBlock = IfStatementAnalysis6(triviaKindCheckBlock.Statements, triviaIdentifierToken);
if (triviaCheckBlock == null)
{
IfDiagnostic(context, triviaKindCheckBlockStatements[0], WhitespaceCheckIncorrectRule, triviaIdentifierToken.Text);
return false;
}
SyntaxList<StatementSyntax> triviaCheckBlockStatements = triviaCheckBlock.Statements;
if (triviaCheckBlockStatements == null)
{
IfDiagnostic(context, triviaKindCheckBlockStatements[0], WhitespaceCheckIncorrectRule, triviaIdentifierToken.Text);
return false;
}
if (triviaCheckBlockStatements.Count > 0)
{
if (!IfStatementAnalysis7(triviaCheckBlockStatements))
{
IfDiagnostic(context, triviaCheckBlockStatements[0], ReturnStatementIncorrectRule, methodDeclaration.Identifier.Text);
return false;
}
if (triviaCheckBlockStatements.Count > 1)
{
IfDiagnostic(context, triviaCheckBlock.Parent as StatementSyntax, TooManyStatementsRule, "if-block", "1", "return from the method");
return false;
}
//successfully through if-statement checks
}
else
{
IfDiagnostic(context, triviaCheckBlock.Parent as StatementSyntax, ReturnStatementMissingRule, methodDeclaration.Identifier.Text);
return false;
}
if (triviaKindCheckBlockStatements.Count > 1)
{
IfDiagnostic(context, triviaKindCheckBlock.Parent as StatementSyntax, TooManyStatementsRule, "if-block", "1", "check if the trivia is a single space");
return false;
}
}
else
{
IfDiagnostic(context, triviaKindCheckBlock.Parent as StatementSyntax, WhitespaceCheckMissingRule, triviaIdentifierToken.Text);
return false;
}
if (triviaCountBlockStatements.Count > 2)
{
IfDiagnostic(context, triviaCountBlock.Parent as StatementSyntax, TooManyStatementsRule, "if-block", "2", "extract the first trivia of the if-keyword and check its kind");
return false;
}
}
else
{
ReportDiagnostic(context, TrailingTriviaKindCheckMissingRule, triviaCountBlockStatements[0].GetLocation(), triviaIdentifierToken.Text);
return false;
}
}
else
{
IfDiagnostic(context, triviaCountBlock.Parent as StatementSyntax, TrailingTriviaVarMissingRule, keywordIdentifierToken.Text);
return false;
}
if (triviaBlockStatements.Count > 1)
{
IfDiagnostic(context, triviaBlock.Parent as StatementSyntax, TooManyStatementsRule, "if-block", "1", "check the number of trailing trivia on the if-keyword");
return false;
}
}
else
{
IfDiagnostic(context, triviaBlock.Parent as StatementSyntax, TriviaCountMissingRule, keywordIdentifierToken.Text);
return false;
}
//check diagnostic reporting statements
if (statementCount > 3)
{
bool diagnosticReportingCorrect = CheckDiagnosticCreation(context, statementIdentifierToken, keywordIdentifierToken, ruleNames, statements, contextParameter);
if (!diagnosticReportingCorrect)
{
return false;
}
if (statementCount > 10)
{
ReportDiagnostic(context, TooManyStatementsRule, methodDeclaration.Identifier.GetLocation(), "method", "10", "walk through the Syntax Tree and check the spacing of the if-statement");
return false;
}
}
else
{
IfDiagnostic(context, statements[2], OpenParenMissingRule, statementIdentifierToken.Text);
return false;
}
}
else
{
ReportDiagnostic(context, TrailingTriviaCheckMissingRule, statements[1].GetLocation(), keywordIdentifierToken.Text);
return false;
}
}
else
{
ReportDiagnostic(context, IfKeywordMissingRule, statements[0].GetLocation(), statementIdentifierToken.Text);
return false;
}
}
else
{
ReportDiagnostic(context, IfStatementMissingRule, methodDeclaration.Identifier.GetLocation(), contextParameter.Identifier.Text);
return false;
}
return true;
}
// Checks step one of the user's AnalyzerIfStatement method, returns a SyntaxToken of "" if analysis failed
private SyntaxToken IfStatementAnalysis1(SyntaxList<StatementSyntax> statements, ParameterSyntax contextParameter)
{
SyntaxToken emptyResult = SyntaxFactory.Identifier("");
var ifStatement = statements[0] as LocalDeclarationStatementSyntax;
if (ifStatement == null)
{
return emptyResult;
}
SyntaxToken statementName = GetIdentifierTokenFromLocalDecl(ifStatement);
if (statementName.Text == "")
{
return emptyResult;
}
EqualsValueClauseSyntax statementEqualsValueClause = GetEqualsValueClauseFromLocalDecl(ifStatement);
if (statementEqualsValueClause == null)
{
return emptyResult;
}
var statementCastExpression = statementEqualsValueClause.Value as CastExpressionSyntax;
if (statementCastExpression == null)
{
var statementAsExpression = statementEqualsValueClause.Value as BinaryExpressionSyntax;
if (statementAsExpression == null)
{
return emptyResult;
}
var left = statementAsExpression.Left as MemberAccessExpressionSyntax;
if (left == null)
{
return emptyResult;
}
var leftName = left.Name as IdentifierNameSyntax;
if (leftName == null || leftName.Identifier.Text != "Node")
{
return emptyResult;
}
var leftMember = left.Expression as IdentifierNameSyntax;
if (leftMember == null || leftMember.Identifier.Text != contextParameter.Identifier.Text)
{
return emptyResult;
}
var right = statementAsExpression.Right as IdentifierNameSyntax;
if (right == null || right.Identifier.Text != "IfStatementSyntax")
{
return emptyResult;
}
return statementName;
}
var statementIdentifier = statementCastExpression.Type as IdentifierNameSyntax;
if (statementIdentifier == null || statementIdentifier.Identifier.Text != "IfStatementSyntax")
{
return emptyResult;
}
var statementExpression = statementCastExpression.Expression as MemberAccessExpressionSyntax;
if (statementExpression == null)
{
return emptyResult;
}
var statementExpressionIdentifier = statementExpression.Expression as IdentifierNameSyntax;
if (statementExpressionIdentifier == null || statementExpressionIdentifier.Identifier.Text != contextParameter.Identifier.Text)
{
return emptyResult;
}
var statementExpressionNode = statementExpression.Name as IdentifierNameSyntax;
if (statementExpressionNode == null || statementExpressionNode.Identifier.Text != "Node")
{
return emptyResult;
}
return statementName;
}
// Checks step two of the user's AnalyzerIfStatement method, returns a SyntaxToken of "" if analysis failed
private SyntaxToken IfStatementAnalysis2(SyntaxList<StatementSyntax> statements, SyntaxToken statementIdentifierToken)
{
SyntaxToken emptyResult = SyntaxFactory.Identifier("");
var statement = statements[1] as LocalDeclarationStatementSyntax;
if (statement == null)
{
return emptyResult;
}
SyntaxToken keywordIdentifierToken = GetIdentifierTokenFromLocalDecl(statement);
if (keywordIdentifierToken.Text == "")
{
return emptyResult;
}
EqualsValueClauseSyntax equalsValueClause = GetEqualsValueClauseFromLocalDecl(statement);
if (equalsValueClause == null)
{
return emptyResult;
}
var memberExpr = equalsValueClause.Value as MemberAccessExpressionSyntax;
if (memberExpr == null)
{
return emptyResult;
}
var identifier = memberExpr.Expression as IdentifierNameSyntax;
if (identifier == null || identifier.Identifier.Text != statementIdentifierToken.Text)
{
return emptyResult;
}
var name = memberExpr.Name as IdentifierNameSyntax;
if (name == null || name.Identifier.Text != "IfKeyword")
{
return emptyResult;
}
return keywordIdentifierToken;
}
// Checks step three of the user's AnalyzerIfStatement method, returns null if analysis failed
private BlockSyntax IfStatementAnalysis3(SyntaxList<StatementSyntax> statements, SyntaxToken keywordIdentifierToken)
{
BlockSyntax emptyResult = null;
var statement = statements[2] as IfStatementSyntax;
if (statement == null)
{
return emptyResult;
}
var booleanExpression = statement.Condition as MemberAccessExpressionSyntax;
if (booleanExpression == null)
{
return emptyResult;
}
var identifier = booleanExpression.Expression as IdentifierNameSyntax;
if (identifier == null || identifier.Identifier.Text != keywordIdentifierToken.Text)
{
return emptyResult;
}
var name = booleanExpression.Name as IdentifierNameSyntax;
if (name == null || name.Identifier.Text != "HasTrailingTrivia")
{
return emptyResult;
}
var block = statement.Statement as BlockSyntax;
if (block == null)
{
return emptyResult;
}
return block;
}
// Checks step four of the user's AnalyzerIfStatement method, returns a SyntaxToken of "" if analysis failed
private SyntaxToken IfStatementAnalysis4(SyntaxList<StatementSyntax> statements, SyntaxToken keywordIdentifierToken)
{
SyntaxToken emptyResult = SyntaxFactory.Identifier("");
var statement = statements[0] as LocalDeclarationStatementSyntax;
if (statement == null)
{
return emptyResult;
}
SyntaxToken triviaIdentifierToken = GetIdentifierTokenFromLocalDecl(statement);
if (triviaIdentifierToken.Text == "")
{
return emptyResult;
}
EqualsValueClauseSyntax statementEqualsValueClause = GetEqualsValueClauseFromLocalDecl(statement);
if (statementEqualsValueClause == null)
{
return emptyResult;
}
var invocationExpression = statementEqualsValueClause.Value as InvocationExpressionSyntax;
if (invocationExpression == null)
{
return emptyResult;
}
var memberExpr = invocationExpression.Expression as MemberAccessExpressionSyntax;
if (memberExpr == null)
{
return emptyResult;
}
var memberExprInner = memberExpr.Expression as MemberAccessExpressionSyntax;
if (memberExprInner == null)
{
return emptyResult;
}
var innerIdentifier = memberExprInner.Expression as IdentifierNameSyntax;
if (innerIdentifier == null || innerIdentifier.Identifier.Text != keywordIdentifierToken.Text)
{
return emptyResult;
}
var innerName = memberExprInner.Name as IdentifierNameSyntax;
if (innerName == null || innerName.Identifier.Text != "TrailingTrivia")
{
return emptyResult;
}
var memberExprName = memberExpr.Name as IdentifierNameSyntax;
if (memberExprName == null || memberExprName.Identifier.Text != "First")
{
return emptyResult;
}
return triviaIdentifierToken;
}
// Checks step five of the user's AnalyzerIfStatement method, returns null if analysis failed
private BlockSyntax IfStatementAnalysis5(SyntaxList<StatementSyntax> statements, SyntaxToken triviaIdentifierToken)
{
BlockSyntax emptyResult = null;
var statement = statements[1] as IfStatementSyntax;
if (statement == null)
{
return emptyResult;
}
var booleanExpression = statement.Condition as BinaryExpressionSyntax;
if (booleanExpression == null)
{
var blockResult = WhitespaceKindCheckAlternate(statement, triviaIdentifierToken) as BlockSyntax;
if (blockResult == null)
{
return emptyResult;
}
return blockResult;
}
var left = booleanExpression.Left as InvocationExpressionSyntax;
if (left == null)
{
return emptyResult;
}
var leftMemberExpr = left.Expression as MemberAccessExpressionSyntax;
if (leftMemberExpr == null)
{
return emptyResult;
}
var leftIdentifier = leftMemberExpr.Expression as IdentifierNameSyntax;
if (leftIdentifier == null || leftIdentifier.Identifier.Text != triviaIdentifierToken.Text)
{
return emptyResult;
}
var leftName = leftMemberExpr.Name as IdentifierNameSyntax;
if (leftName == null || leftName.Identifier.Text != "Kind")
{
return emptyResult;
}
var leftArgumentList = left.ArgumentList as ArgumentListSyntax;
if (leftArgumentList == null)
{