private void RegisterAnalysis(CompilationStartAnalysisContext context) { var attributeType = context.Compilation.GetTypeByMetadataName("D2L.LP.Web.Rest.Attributes.JsonParamBinder"); if (attributeType == null) { // Attribute is presumably not being used, so no need to register our analyzer return; } AllowedTypeList allowedTypeList = AllowedTypeList.CreateFromAnalyzerOptions( allowedListFileName: "LegacyJsonParamBinderAllowedList.txt", analyzerOptions: context.Options ); context.RegisterSyntaxNodeAction( ctx => AnalyzeAttribute(ctx, attributeType, allowedTypeList), SyntaxKind.Attribute ); context.RegisterSymbolAction( ctx => PreventUnnecessaryAllowedListing( ctx, attributeType, allowedTypeList ), SymbolKind.NamedType ); }
public void RegisterServiceLocatorAnalyzer(CompilationStartAnalysisContext context) { // Cache some important type lookups var locatorType = context.Compilation.GetTypeByMetadataName("D2L.LP.Extensibility.Activation.Domain.OldAndBrokenServiceLocator"); var factoryType = context.Compilation.GetTypeByMetadataName("D2L.LP.Extensibility.Activation.Domain.OldAndBrokenServiceLocatorFactory"); var activatorType = context.Compilation.GetTypeByMetadataName("D2L.LP.Extensibility.Activation.Domain.IObjectActivator"); var customActivatorType = context.Compilation.GetTypeByMetadataName("D2L.LP.Extensibility.Activation.Domain.ICustomObjectActivator"); var staticDILocatorType = context.Compilation.GetTypeByMetadataName("D2L.LP.Extensibility.Activation.Domain.Default.StaticDI.Current"); // If those type lookups failed then OldAndBrokenServiceLocator // cannot resolve and we don't need to register our analyzer. if (locatorType == null || locatorType.Kind == SymbolKind.ErrorType) { return; } if (factoryType == null || factoryType.Kind == SymbolKind.ErrorType) { return; } ImmutableArray <INamedTypeSymbol> disallowedTypes = ImmutableArray.Create( locatorType, factoryType, activatorType, customActivatorType, staticDILocatorType ); AllowedTypeList allowedTypeList = AllowedTypeList.CreateFromAnalyzerOptions( allowedListFileName: "OldAndBrokenServiceLocatorAllowedList.txt", analyzerOptions: context.Options ); //Prevent static usage of OldAndBrokenServiceLocator //For example, OldAndBrokenServiceLocator.Instance.Get<IFoo>() context.RegisterSyntaxNodeAction( ctx => PreventOldAndBrokenUsage( ctx, disallowedTypes, allowedTypeList ), SyntaxKind.IdentifierName ); context.RegisterSymbolAction( ctx => PreventUnnecessaryAllowedListing( ctx, disallowedTypes, allowedTypeList ), SymbolKind.NamedType ); }