Пример #1
0
        public LightMockSyntaxReceiver()
        {
            var multicastDelegateType = typeof(MulticastDelegate);

            multicastDelegateNameSpaceAndName = multicastDelegateType.Namespace + "." + multicastDelegateType.Name;
            syntaxHelpers = new SyntaxHelpers();
        }
        public LightMockGenerator()
        {
            var multicastDelegateType = typeof(MulticastDelegate);

            multicastDelegateNameSpaceAndName = multicastDelegateType.Namespace + "." + multicastDelegateType.Name;
            compilationContexts = new ConditionalWeakTable <Compilation, CompilationContext>();
            syntaxHelpers       = new SyntaxHelpers();
        }
        INamedTypeSymbol?GetMockedType(GeneratorSyntaxContext context)
        {
            var candidateGeneric = SyntaxHelpers.GetMockSymbol(context.Node);
            var semanticModel    = context.SemanticModel;

            return(candidateGeneric != null
                ? syntaxHelpers.GetMockedType(candidateGeneric, semanticModel)
                : null);
        }
        (GenericNameSyntax mock, INamedTypeSymbol mockedType)? ConvertToAbstractClass(GeneratorSyntaxContext context)
        {
            var candidateGeneric = SyntaxHelpers.GetMockSymbol(context.Node);

            if (candidateGeneric != null)
            {
                var mockedType = syntaxHelpers.GetMockedType(candidateGeneric, context.SemanticModel);
                if (mockedType != null)
                {
                    var mtbt = mockedType.BaseType;
                    if (mtbt != null)
                    {
                        if (mtbt.ToDisplayString(SymbolDisplayFormats.Namespace) != multicastDelegateNameSpaceAndName)
                        {
                            return(candidateGeneric, mockedType);
                        }
                    }
                }
            }
            return(null);
        }
        public void Initialize(IncrementalGeneratorInitializationContext context)
        {
            var disableCodegenerationAttributes = context.SyntaxProvider.CreateSyntaxProvider(
                (sn, ct) => sn is AttributeSyntax @as && SyntaxHelpers.IsDisableCodeGenerationAttribute(@as),
                (ctx, ct) => syntaxHelpers.IsDisableCodeGenerationAttribute(ctx.SemanticModel, (AttributeSyntax)ctx.Node));

            var interfaces = context.SyntaxProvider.CreateSyntaxProvider(
                (sn, ct) => SyntaxHelpers.IsMock(sn),
                (ctx, ct) => ConvertToInterface(ctx));

            context.RegisterSourceOutput(interfaces
                                         .Combine(context.CompilationProvider)
                                         .Combine(context.AnalyzerConfigOptionsProvider)
                                         .Combine(disableCodegenerationAttributes.Collect())
                                         .Combine(context.ParseOptionsProvider)
                                         .Select((comb, ct) => (
                                                     candidate:                          comb.Left.Left.Left.Left,
                                                     compilation:                        comb.Left.Left.Left.Right,
                                                     options:                            comb.Left.Left.Right,
                                                     disableCodegenerationAttributes:    comb.Left.Right,
                                                     parseOptions:                       comb.Right))
                                         .Where(t
                                                => IsCodeGenerationDisabledByAttributes(t.disableCodegenerationAttributes) &&
                                                IsGenerationDisabledByOptions(t.options) == false &&
                                                t.candidate != null &&
                                                t.compilation is CSharpCompilation &&
                                                t.parseOptions is CSharpParseOptions),
                                         (sp, sr) => DoGenerateCode(
                                             new CodeGenerationContext(sp,
                                                                       (CSharpCompilation)sr.compilation,
                                                                       (CSharpParseOptions)sr.parseOptions,
                                                                       GetCompilationContext(sr.compilation)),
                                             new InterfaceProcessor(sr.compilation, sr.candidate !),
                                             sp.CancellationToken));

            var delegates = context.SyntaxProvider.CreateSyntaxProvider(
                (sn, ct) => SyntaxHelpers.IsMock(sn),
                (ctx, ct) => ConvertToDelegate(ctx));

            context.RegisterSourceOutput(delegates
                                         .Combine(context.CompilationProvider)
                                         .Combine(context.AnalyzerConfigOptionsProvider)
                                         .Combine(disableCodegenerationAttributes.Collect())
                                         .Combine(context.ParseOptionsProvider)
                                         .Select((comb, ct) => (
                                                     candidate:                          comb.Left.Left.Left.Left,
                                                     compilation:                        comb.Left.Left.Left.Right,
                                                     options:                            comb.Left.Left.Right,
                                                     disableCodegenerationAttributes:    comb.Left.Right,
                                                     parseOptions:                       comb.Right))
                                         .Where(t
                                                => IsCodeGenerationDisabledByAttributes(t.disableCodegenerationAttributes) &&
                                                IsGenerationDisabledByOptions(t.options) == false &&
                                                t.candidate != null &&
                                                t.compilation is CSharpCompilation &&
                                                t.parseOptions is CSharpParseOptions),
                                         (sp, sr) => DoGenerateCode(
                                             new CodeGenerationContext(sp,
                                                                       (CSharpCompilation)sr.compilation,
                                                                       (CSharpParseOptions)sr.parseOptions,
                                                                       GetCompilationContext(sr.compilation)),
                                             new DelegateProcessor(sr.candidate !),
                                             sp.CancellationToken));

            var classes = context.SyntaxProvider.CreateSyntaxProvider(
                (sn, ct) => SyntaxHelpers.IsMock(sn),
                (ctx, ct) => ConvertToAbstractClass(ctx));
            var dontOverrideTypes = context.SyntaxProvider.CreateSyntaxProvider(
                (sn, ct) => sn is AttributeSyntax @as && SyntaxHelpers.IsDontOverrideAttribute(@as),
                (ctx, ct) => syntaxHelpers.CovertToDontOverride(ctx.SemanticModel, (AttributeSyntax)ctx.Node));

            context.RegisterSourceOutput(classes
                                         .Combine(context.CompilationProvider)
                                         .Combine(context.AnalyzerConfigOptionsProvider)
                                         .Combine(disableCodegenerationAttributes.Collect())
                                         .Combine(context.ParseOptionsProvider)
                                         .Combine(dontOverrideTypes.Collect())
                                         .Select((comb, ct) => (
                                                     candidate:                          comb.Left.Left.Left.Left.Left,
                                                     compilation:                        comb.Left.Left.Left.Left.Right,
                                                     options:                            comb.Left.Left.Left.Right,
                                                     disableCodegenerationAttributes:    comb.Left.Left.Right,
                                                     parseOptions:                       comb.Left.Right,
                                                     dontOverrideTypes:                  comb.Right))
                                         .Where(t
                                                => IsCodeGenerationDisabledByAttributes(t.disableCodegenerationAttributes) &&
                                                IsGenerationDisabledByOptions(t.options) == false &&
                                                t.candidate != null &&
                                                t.compilation is CSharpCompilation &&
                                                t.parseOptions is CSharpParseOptions),
                                         (sp, sr) => DoGenerateCode(
                                             new CodeGenerationContext(sp,
                                                                       (CSharpCompilation)sr.compilation,
                                                                       (CSharpParseOptions)sr.parseOptions,
                                                                       GetCompilationContext(sr.compilation)),
                                             new AbstractClassProcessor(sr.compilation, sr.candidate !.Value.mock, sr.candidate !.Value.mockedType, sr.dontOverrideTypes),
                                             sp.CancellationToken));

            var invocations = context.SyntaxProvider.CreateSyntaxProvider(
                (sn, ct) => sn is InvocationExpressionSyntax ies && SyntaxHelpers.IsArrangeInvocation(ies),
                (ctx, ct) => syntaxHelpers.ConvertToInvocation(ctx.Node, ctx.SemanticModel, ct));

            context.RegisterSourceOutput(invocations
                                         .Combine(context.CompilationProvider)
                                         .Combine(context.AnalyzerConfigOptionsProvider)
                                         .Combine(disableCodegenerationAttributes.Collect())
                                         .Combine(context.ParseOptionsProvider)
                                         .Select((comb, ct) => (
                                                     candidate: comb.Left.Left.Left.Left,
                                                     compilation: comb.Left.Left.Left.Right,
                                                     options: comb.Left.Left.Right,
                                                     disableCodegenerationAttributes: comb.Left.Right,
                                                     parseOptions: comb.Right))
                                         .Where(t
                                                => IsCodeGenerationDisabledByAttributes(t.disableCodegenerationAttributes) &&
                                                IsGenerationDisabledByOptions(t.options) == false &&
                                                t.compilation is CSharpCompilation &&
                                                t.parseOptions is CSharpParseOptions),
                                         (sp, sr) => DoGenerateInvocation(
                                             new CodeGenerationContext(sp,
                                                                       (CSharpCompilation)sr.compilation,
                                                                       (CSharpParseOptions)sr.parseOptions,
                                                                       GetCompilationContext(sr.compilation)),
                                             sr.candidate,
                                             sp.CancellationToken));
        }