Exemplo n.º 1
0
    public static TypeDeclarationSyntax ReparentDeclaration(
        this TypeDeclarationSyntax classToNest,
        SourceProductionContext context,
        TypeDeclarationSyntax source
        )
    {
        var parent = source.Parent;

        while (parent is TypeDeclarationSyntax parentSyntax)
        {
            classToNest = parentSyntax
                          .WithMembers(SyntaxFactory.List <MemberDeclarationSyntax>())
                          .WithAttributeLists(SyntaxFactory.List <AttributeListSyntax>())
                          .WithConstraintClauses(SyntaxFactory.List <TypeParameterConstraintClauseSyntax>())
                          .WithBaseList(null)
                          .AddMembers(classToNest);

            if (!parentSyntax.Modifiers.Any(z => z.IsKind(SyntaxKind.PartialKeyword)))
            {
                context.ReportDiagnostic(
                    Diagnostic.Create(GeneratorDiagnostics.MustBePartial, parentSyntax.Identifier.GetLocation(), parentSyntax.GetFullMetadataName())
                    );
            }

            parent = parentSyntax.Parent;
        }

        return(classToNest);
    }
    private static void GeneratePropertyTracking(
        SourceProductionContext context,
        TypeDeclarationSyntax declaration,
        INamedTypeSymbol symbol,
        INamedTypeSymbol targetSymbol
        )
    {
        if (!declaration.Modifiers.Any(z => z.IsKind(SyntaxKind.PartialKeyword)))
        {
            context.ReportDiagnostic(
                Diagnostic.Create(GeneratorDiagnostics.MustBePartial, declaration.Identifier.GetLocation(), declaration.GetFullMetadataName())
                );
            return;
        }

        var isRecord = declaration is RecordDeclarationSyntax;

        if (targetSymbol.IsRecord != isRecord)
        {
            context.ReportDiagnostic(
                Diagnostic.Create(
                    GeneratorDiagnostics.ParameterMustBeSameTypeOfObject, declaration.Keyword.GetLocation(), declaration.GetFullMetadataName(),
                    declaration.Keyword.IsKind(SyntaxKind.ClassKeyword) ? "record" : "class"
                    )
                );
            return;
        }

        var classToInherit = declaration
                             .WithMembers(List <MemberDeclarationSyntax>())
                             .WithAttributeLists(List <AttributeListSyntax>())
                             .WithConstraintClauses(List <TypeParameterConstraintClauseSyntax>())
                             .WithBaseList(null)
                             .WithAttributeLists(
            SingletonList(
                AttributeList(
                    SingletonSeparatedList(Attribute(ParseName("System.Runtime.CompilerServices.CompilerGenerated")))
                    )
                )
            );

        var writeableProperties =
            targetSymbol.GetMembers()
            .OfType <IPropertySymbol>()
            // only works for `set`able properties not init only
            .Where(z => !symbol.GetMembers(z.Name).Any())
            .Where(z => z is { IsStatic: false, IsIndexer: false, IsReadOnly: false });
Exemplo n.º 3
0
        private void GenerateEnum(SourceProductionContext context, GeneratorSyntaxContext syntaxContext)
        {
            var candidate = (StructDeclarationSyntax)syntaxContext.Node;
            var model     = syntaxContext.SemanticModel;
            var symbol    = model.GetDeclaredSymbol(syntaxContext.Node);

            if (symbol is null)
            {
                return;
            }

            if (!candidate.Modifiers.Any(z => z.IsKind(SyntaxKind.PartialKeyword)))
            {
                context.ReportDiagnostic(Diagnostic.Create(GeneratorDiagnostics.MustBePartial, candidate.Identifier.GetLocation(), candidate.Identifier.Text));
                return;
            }

            if (!candidate.Modifiers.Any(z => z.IsKind(SyntaxKind.ReadOnlyKeyword)))
            {
                context.ReportDiagnostic(Diagnostic.Create(GeneratorDiagnostics.MustBeReadOnly, candidate.Identifier.GetLocation(), candidate.Identifier.Text));
                return;
            }

            var cu = CompilationUnit(
                List <ExternAliasDirectiveSyntax>(), List <UsingDirectiveSyntax>(), List <AttributeListSyntax>(), SingletonList <MemberDeclarationSyntax>(
                    NamespaceDeclaration(ParseName(symbol.ContainingNamespace.ToDisplayString()))
                    .WithMembers(SingletonList <MemberDeclarationSyntax>(GetImplementation(candidate)))
                    )
                )
                     .AddUsings(
                UsingDirective(ParseName("System")), UsingDirective(ParseName("System.Collections.Generic")),
                UsingDirective(ParseName("System.Diagnostics")), UsingDirective(ParseName("System.Linq")),
                UsingDirective(ParseName("System.Reflection")), UsingDirective(ParseName("Newtonsoft.Json")),
                UsingDirective(ParseName("OmniSharp.Extensions.JsonRpc")),
                UsingDirective(ParseName("OmniSharp.Extensions.JsonRpc.Serialization.Converters"))
                )
                     .WithLeadingTrivia()
                     .WithTrailingTrivia()
                     .WithLeadingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.EnableKeyword), true)))
                     .WithTrailingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.RestoreKeyword), true)));

            context.AddSource(
                $"{Path.GetFileNameWithoutExtension(candidate.SyntaxTree.FilePath)}_{candidate.Identifier.Text}{( candidate.Arity > 0 ? candidate.Arity.ToString() : "" )}.cs",
                cu.NormalizeWhitespace().GetText(Encoding.UTF8)
                );
        }
Exemplo n.º 4
0
 public void ReportDiagnosticTo(SourceProductionContext context)
 {
     if (_diagnosticList == null)
     {
         return;
     }
     foreach (var d in _diagnosticList)
     {
         context.ReportDiagnostic(d);
     }
 }
 public static void ReportDiagnostics(
     this SourceProductionContext context,
     DiagnosticDescriptor descriptor,
     IEnumerable <Location> locations,
     params object?[]?messageArgs)
 {
     foreach (var location in locations)
     {
         context.ReportDiagnostic(Diagnostic.Create(descriptor, location, messageArgs));
     }
 }
Exemplo n.º 6
0
        private static void Execute(
            SourceProductionContext context,
            ImmutableArray <SyntaxNode> syntaxNodes,
            OutputKind outputKind,
            ApplicationConfig?applicationConfig,
            Diagnostic?applicationConfigDiagnostics)
        {
            if (syntaxNodes.IsEmpty)
            {
                return;
            }

            if (applicationConfig is null)
            {
                if (applicationConfigDiagnostics is not null)
                {
                    context.ReportDiagnostic(applicationConfigDiagnostics);
                }

                return;
            }

            if (outputKind != OutputKind.WindowsApplication &&
                // Starting in the 5.0.100 version of the .NET SDK, when OutputType is set to Exe, it is automatically changed to WinExe
                // for WPF and Windows Forms apps that target any framework version, including .NET Framework.
                // https://docs.microsoft.com/en-us/dotnet/core/compatibility/sdk/5.0/automatically-infer-winexe-output-type
                outputKind != OutputKind.ConsoleApplication)
            {
                context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.s_errorUnsupportedProjectType, Location.None, nameof(OutputKind.WindowsApplication)));
                return;
            }

            string?code = ApplicationConfigurationInitializeBuilder.GenerateInitialize(projectNamespace: GetUserProjectNamespace(syntaxNodes[0]), applicationConfig);

            if (code is not null)
            {
                context.AddSource("ApplicationConfiguration.g.cs", code);
            }
        }
 public IEnumerable <MemberDeclarationSyntax> Apply(SourceProductionContext context, GeneratorData item)
 {
     foreach (var diagnostic in item.JsonRpcAttributes.RequestProxyDiagnostics)
     {
         context.ReportDiagnostic(diagnostic);
     }
     return(item.JsonRpcAttributes.RequestProxies
            .Select(
                registry => new ExtensionMethodContext(
                    item.JsonRpcAttributes.GenerateRequestMethods !.Data, item.TypeDeclaration, item.TypeSymbol, registry, item.JsonRpcAttributes.RequestProxies
                    )
     {
         IsProxy = true
     }
    private static void GenerateInheritance(
        SourceProductionContext context,
        Compilation compilation,
        TypeDeclarationSyntax declaration,
        INamedTypeSymbol symbol,
        AttributeData[] attributes
        )
    {
        if (!declaration.Modifiers.Any(z => z.IsKind(SyntaxKind.PartialKeyword)))
        {
            context.ReportDiagnostic(
                Diagnostic.Create(GeneratorDiagnostics.MustBePartial, declaration.Identifier.GetLocation(), declaration.GetFullMetadataName())
                );
            return;
        }

        var classToInherit = declaration
                             .WithMembers(List <MemberDeclarationSyntax>())
                             .WithAttributeLists(List <AttributeListSyntax>())
                             .WithConstraintClauses(List <TypeParameterConstraintClauseSyntax>())
                             .WithBaseList(null)
                             .WithAttributeLists(
            SingletonList(
                AttributeList(
                    SingletonSeparatedList(Attribute(ParseName("System.Runtime.CompilerServices.CompilerGenerated")))
                    )
                )
            );

        foreach (var attribute in attributes)
        {
            if (attribute.ApplicationSyntaxReference?.GetSyntax() is not {
            } attributeSyntax)
            {
                continue;
            }
            if (attribute is { ConstructorArguments: { Length: 0 } } || attribute.ConstructorArguments[0] is { Kind: not TypedConstantKind.Type })
Exemplo n.º 9
0
 public void ReportDiagnostic(Diagnostic diagnostic)
 {
     _sourceProductionContext?.ReportDiagnostic(diagnostic);
     _compilationAnalysisContext?.ReportDiagnostic(diagnostic);
 }
 public void ReportDiagnostic(Diagnostic diagnostic)
 {
     _context.ReportDiagnostic(diagnostic);
 }
Exemplo n.º 11
0
 public static void Report(this SourceProductionContext context, DiagnosticDescriptor diagnosticDescriptor, ISymbol?symbol, params object?[]?args) =>
 context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, symbol?.Locations.FirstOrDefault(), args));
Exemplo n.º 12
0
 public static void Report(this SourceProductionContext context, DiagnosticDescriptor diagnosticDescriptor, SyntaxNode?syntax, params object?[]?args) =>
 context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, syntax?.GetLocation(), args));