private static bool IsSystemConsoleInvocation( SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocationSyntax) { // Get the method member access (Console.WriteLine or Console.Error.WriteLine) if (invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax && context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol) { // Check if contained within System.Console if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType)) { return(true); } // In case with Console.Error.WriteLine that wouldn't work, we need to check parent member access too if (memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax && // Get the semantic model for the parent member context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol && // Check if contained within System.Console KnownSymbols.IsSystemConsole(propertySymbol.ContainingType)) { return(true); } } return(false); }
private static bool IsScalarType(ITypeSymbol typeSymbol) { return(KnownSymbols.IsSystemString(typeSymbol) || !typeSymbol.AllInterfaces.Select(i => i.ConstructedFrom).Any(KnownSymbols.IsSystemCollectionsGenericIEnumerable)); }
private static void CheckCommandParameterProperties( SymbolAnalysisContext context, IReadOnlyList <IPropertySymbol> properties) { var parameters = properties .Select(p => { var attribute = p .GetAttributes() .First(a => KnownSymbols.IsCommandParameterAttribute(a.AttributeClass)); var order = attribute .ConstructorArguments .Select(a => a.Value) .FirstOrDefault() as int?; var name = attribute .NamedArguments .Where(a => a.Key == "Name") .Select(a => a.Value.Value) .FirstOrDefault() as string; return(new { Property = p, Order = order, Name = name }); }) .ToArray(); // Duplicate order var duplicateOrderParameters = parameters .Where(p => p.Order != null) .GroupBy(p => p.Order) .Where(g => g.Count() > 1) .SelectMany(g => g.AsEnumerable()) .ToArray(); foreach (var parameter in duplicateOrderParameters) { context.ReportDiagnostic( Diagnostic.Create(DiagnosticDescriptors.Typin0021, parameter.Property.Locations.First())); } // Duplicate name var duplicateNameParameters = parameters .Where(p => !string.IsNullOrWhiteSpace(p.Name)) .GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase) .Where(g => g.Count() > 1) .SelectMany(g => g.AsEnumerable()) .ToArray(); foreach (var parameter in duplicateNameParameters) { context.ReportDiagnostic( Diagnostic.Create(DiagnosticDescriptors.Typin0022, parameter.Property.Locations.First())); } // Multiple non-scalar var nonScalarParameters = parameters .Where(p => !IsScalarType(p.Property.Type)) .ToArray(); if (nonScalarParameters.Length > 1) { foreach (var parameter in nonScalarParameters) { context.ReportDiagnostic( Diagnostic.Create(DiagnosticDescriptors.Typin0023, parameter.Property.Locations.First())); } } // Non-last non-scalar var nonLastNonScalarParameter = parameters .OrderByDescending(a => a.Order) .Skip(1) .LastOrDefault(p => !IsScalarType(p.Property.Type)); if (nonLastNonScalarParameter != null) { context.ReportDiagnostic( Diagnostic.Create(DiagnosticDescriptors.Typin0024, nonLastNonScalarParameter.Property.Locations.First())); } }
private static void CheckCommandOptionProperties( SymbolAnalysisContext context, IReadOnlyList <IPropertySymbol> properties) { var options = properties .Select(p => { var attribute = p .GetAttributes() .First(a => KnownSymbols.IsCommandOptionAttribute(a.AttributeClass)); var name = attribute .ConstructorArguments .Where(a => KnownSymbols.IsSystemString(a.Type)) .Select(a => a.Value) .FirstOrDefault() as string; var shortName = attribute .ConstructorArguments .Where(a => KnownSymbols.IsSystemChar(a.Type)) .Select(a => a.Value) .FirstOrDefault() as char?; return(new { Property = p, Name = name, ShortName = shortName }); }) .ToArray(); // No name var noNameOptions = options .Where(o => string.IsNullOrWhiteSpace(o.Name) && o.ShortName == null) .ToArray(); foreach (var option in noNameOptions) { context.ReportDiagnostic( Diagnostic.Create(DiagnosticDescriptors.Typin0041, option.Property.Locations.First())); } // Too short name var invalidNameLengthOptions = options .Where(o => !string.IsNullOrWhiteSpace(o.Name) && o.Name.Length <= 1) .ToArray(); foreach (var option in invalidNameLengthOptions) { context.ReportDiagnostic( Diagnostic.Create(DiagnosticDescriptors.Typin0042, option.Property.Locations.First())); } // Duplicate name var duplicateNameOptions = options .Where(p => !string.IsNullOrWhiteSpace(p.Name)) .GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase) .Where(g => g.Count() > 1) .SelectMany(g => g.AsEnumerable()) .ToArray(); foreach (var option in duplicateNameOptions) { context.ReportDiagnostic( Diagnostic.Create(DiagnosticDescriptors.Typin0043, option.Property.Locations.First())); } // Duplicate name var duplicateShortNameOptions = options .Where(p => p.ShortName != null) .GroupBy(p => p.ShortName) .Where(g => g.Count() > 1) .SelectMany(g => g.AsEnumerable()) .ToArray(); foreach (var option in duplicateShortNameOptions) { context.ReportDiagnostic( Diagnostic.Create(DiagnosticDescriptors.Typin0044, option.Property.Locations.First())); } }