Exemplo n.º 1
0
        private static void Analyze(SymbolAnalysisContext symbolAnalysisContext)
        {
            var namedTypeSymbol = (INamedTypeSymbol)symbolAnalysisContext.Symbol;

            if (namedTypeSymbol == null ||
                namedTypeSymbol.IsAbstract ||
                namedTypeSymbol.IsNested())
            {
                return;
            }

            // Symbol is defined in multiple locations if it is a partial class -- all locations need to be checked
            var symbolFilePaths    = namedTypeSymbol.Locations.Select(location => location?.SourceTree?.FilePath).ToArray();
            var isInWebPartsFolder = symbolFilePaths.Any(SolutionFolders.FileIsInWebPartsFolder);

            if (!isInWebPartsFolder)
            {
                return;
            }

            var isUiWebPart          = symbolFilePaths.Any(SolutionFolders.IsUIWebPart);
            var allowedBaseTypeNames = isUiWebPart ? UiWebPartBases : WebPartBases;
            var ruleToBeUsed         = isUiWebPart ? UiWebPartRule : WebPartRule;

            var extendsOnlyObject = namedTypeSymbol.ExtendsOnlyObject();
            var inheritsFromOneOfRequiredTypes = InheritsFromOneOfRequiredTypes(allowedBaseTypeNames, namedTypeSymbol);

            if (extendsOnlyObject || inheritsFromOneOfRequiredTypes)
            {
                return;
            }

            var diagnostics = DiagnosticFormatter.CreateDiagnostics(ruleToBeUsed, namedTypeSymbol);

            symbolAnalysisContext.ReportDiagnostics(diagnostics);
        }