|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="NoSealedClassAnalyzer.cs" company="P.O.S Informatique"> |
| 3 | +// Copyright (c) P.O.S Informatique. All rights reserved. |
| 4 | +// </copyright> |
| 5 | +//----------------------------------------------------------------------- |
| 6 | + |
| 7 | +namespace PosInformatique.Moq.Analyzers |
| 8 | +{ |
| 9 | + using System.Collections.Immutable; |
| 10 | + using Microsoft.CodeAnalysis; |
| 11 | + using Microsoft.CodeAnalysis.CSharp; |
| 12 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 13 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 14 | + |
| 15 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 16 | + public class NoSealedClassAnalyzer : DiagnosticAnalyzer |
| 17 | + { |
| 18 | + private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( |
| 19 | + "PosInfoMoq2002", |
| 20 | + "Mock<T> class can be used only to mock non-sealed class", |
| 21 | + "Mock<T> class can be used only to mock non-sealed class", |
| 22 | + "Compilation", |
| 23 | + DiagnosticSeverity.Error, |
| 24 | + isEnabledByDefault: true, |
| 25 | + description: "Mock<T> class can be used only to mock non-sealed class."); |
| 26 | + |
| 27 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
| 28 | + |
| 29 | + public override void Initialize(AnalysisContext context) |
| 30 | + { |
| 31 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 32 | + context.EnableConcurrentExecution(); |
| 33 | + |
| 34 | + context.RegisterSyntaxNodeAction(Analyze, SyntaxKind.ObjectCreationExpression); |
| 35 | + } |
| 36 | + |
| 37 | + private static void Analyze(SyntaxNodeAnalysisContext context) |
| 38 | + { |
| 39 | + var objectCreationExpression = (ObjectCreationExpressionSyntax)context.Node; |
| 40 | + |
| 41 | + var moqSymbols = MoqSymbols.FromCompilation(context.Compilation); |
| 42 | + |
| 43 | + if (moqSymbols is null) |
| 44 | + { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + var moqExpressionAnalyzer = new MoqExpressionAnalyzer(context.SemanticModel); |
| 49 | + |
| 50 | + // Check the expression is a Mock<T> instance creation. |
| 51 | + var mockedType = moqExpressionAnalyzer.GetMockedType(moqSymbols, objectCreationExpression, out var typeExpression); |
| 52 | + |
| 53 | + if (mockedType is null) |
| 54 | + { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (mockedType.TypeKind == TypeKind.Interface) |
| 59 | + { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + if (mockedType.IsAbstract) |
| 64 | + { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (!mockedType.IsSealed) |
| 69 | + { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // No returns method has been specified with Strict mode. Report the diagnostic issue. |
| 74 | + var diagnostic = Diagnostic.Create(Rule, typeExpression!.GetLocation()); |
| 75 | + context.ReportDiagnostic(diagnostic); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments