Пример #1
0
        private void AnalyzeNode(SyntaxNodeAnalysisContext context)
        {
            if (!ServiceLocator.GetService <IOptions>().IsFeatureEnabled(FeatureIdentifiers.NamespaceNormalizationAnalyzer))
            {
                return;
            }

            if (generatedCodeRecognitionService.IsGeneratedCode(context))
            {
                return;
            }

            var documentPath = context.Node.SyntaxTree.FilePath;

            if (documentPath == null)
            {
                return;
            }

            var assemblyName = context.Compilation.AssemblyName;

            if (assemblyName == null)
            {
                return;
            }

            var namespaceNode = (NamespaceDeclarationSyntax)context.Node;

            if (!namespaceNode.IsTopLevel())
            {
                return;
            }

            if (!IsTheOnlyNamespace(namespaceNode, context.CancellationToken))
            {
                return;
            }

            var desiredName = ConstructDesiredName(documentPath, assemblyName);

            if (string.IsNullOrWhiteSpace(desiredName))
            {
                return;
            }

            if (!namespaceNode.Name.ToString().Equals(desiredName, StringComparison.OrdinalIgnoreCase))
            {
                var properties = ImmutableDictionary <string, string> .Empty.Add("DesiredName", desiredName);

                context.ReportDiagnostic(Diagnostic.Create(Rule, namespaceNode.Name.GetLocation(), properties, namespaceNode.Name.ToString()));
            }
        }
        private void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            if (!ServiceLocator.GetService <IOptions>().IsFeatureEnabled(FeatureIdentifiers.TypeAndDocumentNameAnalyzer))
            {
                return;
            }

            if (generatedCodeRecognitionService.IsGeneratedCode(context))
            {
                return;
            }

            var symbol = (INamedTypeSymbol)context.Symbol;

            if (symbol.DeclaringSyntaxReferences.Count() != 1)
            {
                return;
            }

            if (symbol.ContainingType != null)
            {
                return;
            }

            if (!IsTheOnlyType(symbol, context.CancellationToken))
            {
                return;
            }

            var documentPath = symbol.DeclaringSyntaxReferences[0].SyntaxTree.FilePath;

            if (documentPath == null)
            {
                return;
            }

            var documentName = NameHelper.RemoveExtension(Path.GetFileName(documentPath));

            if (documentName != null)
            {
                if (!documentName.Equals(symbol.Name, StringComparison.OrdinalIgnoreCase))
                {
                    context.ReportDiagnostic(Diagnostic.Create(Rule, symbol.Locations[0], symbol.Name));
                }
            }
        }