• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xaviersolau / BlazorJsonLocalization / 18022654327

25 Sep 2025 10:45PM UTC coverage: 79.916% (+2.0%) from 77.941%
18022654327

Pull #86

github

web-flow
Merge f80b9ce4f into d1acab631
Pull Request #86: Improve localizationgen tool to generate code in intermediate binary …

147 of 184 new or added lines in 5 files covered. (79.89%)

1 existing line in 1 file now uncovered.

1146 of 1434 relevant lines covered (79.92%)

34.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

56.76
/src/tools/SoloX.BlazorJsonLocalization.Generator/GeneratorHandler.cs
1
// ----------------------------------------------------------------------
2
// <copyright file="GeneratorHandler.cs" company="Xavier Solau">
3
// Copyright © 2021 Xavier Solau.
4
// Licensed under the MIT license.
5
// See LICENSE file in the project root for full license information.
6
// </copyright>
7
// ----------------------------------------------------------------------
8

9
using System;
10
using System.Collections.Immutable;
11
using System.Linq;
12
using Microsoft.CodeAnalysis;
13
using Microsoft.CodeAnalysis.CSharp.Syntax;
14
using Microsoft.CodeAnalysis.Diagnostics;
15
using SoloX.BlazorJsonLocalization.Tools.Core;
16
using SoloX.BlazorJsonLocalization.Tools.Core.Impl;
17
using SoloX.GeneratorTools.Core.CSharp.Workspace.Impl;
18
using SoloX.GeneratorTools.Core.Utils;
19

20
#if DEBUG_WITH_DIAG
21
using System.Diagnostics;
22
#endif
23

24
namespace SoloX.GeneratorTools.Test
25
{
26
    /// <summary>
27
    /// C# code analyzer generator implementation.
28
    /// </summary>
29
    [Generator(LanguageNames.CSharp)]
30
    public class GeneratorHandler : IIncrementalGenerator
31
    {
32
        /// <summary>
33
        /// Setup the generator.
34
        /// </summary>
35
        /// <param name="context">Code analyzer context.</param>
36
        public void Initialize(IncrementalGeneratorInitializationContext context)
37
        {
38
            var classDeclarations = context.SyntaxProvider
1✔
39
                .CreateSyntaxProvider(
1✔
40
                    static (s, _) => IsSyntaxTargetForGeneration(s),
73✔
41
                    static (ctx, _) => GetSemanticTargetForGeneration(ctx))
1✔
42
                .Where(static m => m is not null);
2✔
43

44
            var buildOptions = context.AnalyzerConfigOptionsProvider.Select((c, _) => c.GlobalOptions);
2✔
45

46
            IncrementalValueProvider<(
1✔
47
                    (Compilation Compilation, ImmutableArray<InterfaceDeclarationSyntax> InterfaceDeclarations) Declarations,
1✔
48
                    AnalyzerConfigOptions Options)> compilationAndClasses =
1✔
49
                context.CompilationProvider.Combine(classDeclarations.Collect()).Combine(buildOptions);
1✔
50

51
            context.RegisterSourceOutput(
1✔
52
                compilationAndClasses,
1✔
53
                static (spc, source) => Execute(source.Declarations.Compilation, source.Declarations.InterfaceDeclarations, source.Options, spc));
2✔
54

55
        }
1✔
56

57
        internal const string RootNamespaceParameterKey = "build_property.rootnamespace";
58
        internal const string TargetFrameworkParameterKey = "build_property.targetframework";
59
        internal const string ProjectDirParameterKey = "build_property.projectdir";
60

61
        /// <summary>
62
        /// Execute the ToolsGenerator.
63
        /// </summary>
64
        /// <param name="compilation"></param>
65
        /// <param name="classes"></param>
66
        /// <param name="options"></param>
67
        /// <param name="context"></param>
68
        public static void Execute(
69
            Compilation compilation,
70
            ImmutableArray<InterfaceDeclarationSyntax> classes,
71
            AnalyzerConfigOptions options,
72
            SourceProductionContext context)
73
        {
74
            if (options == null)
1✔
75
            {
NEW
76
                throw new ArgumentNullException(nameof(options));
×
77
            }
78
#if DEBUG_WITH_DIAG
79
            if (!Debugger.IsAttached)
80
            {
81
                Debugger.Launch();
82
            }
83
            else
84
            {
85
                Debugger.Break();
86
            }
87
#endif
88
            var logFactory = new LogFactory(context, classes.First());
1✔
89

90
            if (!options.TryGetValue(RootNamespaceParameterKey, out var rootNamespace))
1✔
91
            {
NEW
92
                var message = "Unable to get root namespace";
×
NEW
93
                context.ReportDiagnostic(
×
NEW
94
                        Diagnostic.Create(new DiagnosticDescriptor("XS9999", "BlazorJsonLocalization", message, "Generator", DiagnosticSeverity.Error, true),
×
NEW
95
                        null));
×
NEW
96
                return;
×
97
            }
98

99
            if (!options.TryGetValue(TargetFrameworkParameterKey, out var targetFramework))
1✔
100
            {
NEW
101
                var message = "Unable to get target framework";
×
NEW
102
                context.ReportDiagnostic(
×
NEW
103
                        Diagnostic.Create(new DiagnosticDescriptor("XS9999", "BlazorJsonLocalization", message, "Generator", DiagnosticSeverity.Error, true),
×
NEW
104
                        null));
×
NEW
105
                return;
×
106
            }
107

108
            if (!options.TryGetValue(ProjectDirParameterKey, out var projectDir))
1✔
109
            {
NEW
110
                var message = "Unable to get project directory";
×
NEW
111
                context.ReportDiagnostic(
×
NEW
112
                        Diagnostic.Create(new DiagnosticDescriptor("XS9999", "BlazorJsonLocalization", message, "Generator", DiagnosticSeverity.Error, true),
×
NEW
113
                        null));
×
NEW
114
                return;
×
115
            }
116

117
            var projectParameters = new ProjectParameters(projectDir, rootNamespace);
1✔
118

119
            var csharpWorkspaceFactory = new CSharpWorkspaceFactory(logFactory);
1✔
120

121
            var generator = new LocalizationGenerator(logFactory.CreateLogger<LocalizationGenerator>(), csharpWorkspaceFactory);
1✔
122

123
            generator.Generate(projectParameters, compilation, classes, context);
1✔
124
        }
1✔
125

126
        internal sealed class LogFactory : IGeneratorLoggerFactory
127
        {
128
            private readonly SourceProductionContext context;
129
            private readonly InterfaceDeclarationSyntax interfaceDeclarationSyntax;
130

131
            public LogFactory(SourceProductionContext context, InterfaceDeclarationSyntax interfaceDeclarationSyntax)
132
            {
133
                this.context = context;
1✔
134
                this.interfaceDeclarationSyntax = interfaceDeclarationSyntax;
1✔
135
            }
1✔
136

137
            public IGeneratorLogger<TType> CreateLogger<TType>()
138
            {
139
                return new Logger<TType>(this.context, this.interfaceDeclarationSyntax);
21✔
140
            }
141

142
            public sealed class Logger<TType> : IGeneratorLogger<TType>
143
            {
144
                private readonly SourceProductionContext context;
145
                private readonly InterfaceDeclarationSyntax interfaceDeclarationSyntax;
146

147
                public Logger(SourceProductionContext context, InterfaceDeclarationSyntax interfaceDeclarationSyntax)
148
                {
149
                    this.context = context;
21✔
150
                    this.interfaceDeclarationSyntax = interfaceDeclarationSyntax;
21✔
151
                }
21✔
152

153
                public void LogDebug(string message)
154
                {
155
                    Log(DiagnosticSeverity.Hidden, message);
1,240✔
156
                }
1,240✔
157

158
                public void LogDebug(Exception exception, string message)
159
                {
160
                    Log(DiagnosticSeverity.Hidden, exception, message);
×
161
                }
×
162

163
                public void LogError(string message)
164
                {
165
                    Log(DiagnosticSeverity.Error, message);
×
166
                }
×
167

168
                public void LogError(Exception exception, string message)
169
                {
170
                    Log(DiagnosticSeverity.Error, exception, message);
×
171
                }
×
172

173
                public void LogInformation(string message)
174
                {
175
                    Log(DiagnosticSeverity.Info, message);
8✔
176
                }
8✔
177

178
                public void LogInformation(Exception exception, string message)
179
                {
180
                    Log(DiagnosticSeverity.Info, exception, message);
×
181
                }
×
182

183
                public void LogWarning(string message)
184
                {
185
                    Log(DiagnosticSeverity.Warning, message);
×
186
                }
×
187

188
                public void LogWarning(Exception exception, string message)
189
                {
190
                    Log(DiagnosticSeverity.Warning, exception, message);
×
191
                }
×
192

193
                private void Log(DiagnosticSeverity diagnosticSeverity, string message)
194
                {
195
                    this.context.ReportDiagnostic(
1,248✔
196
                        Diagnostic.Create(new DiagnosticDescriptor("XS9999", "BlazorJsonLocalization", message, "Generator", diagnosticSeverity, true),
1,248✔
197
                        this.interfaceDeclarationSyntax.GetLocation()));
1,248✔
198
                }
1,248✔
199

200
                private void Log(DiagnosticSeverity diagnosticSeverity, Exception exception, string message)
201
                {
202
                    this.context.ReportDiagnostic(
×
203
                        Diagnostic.Create(new DiagnosticDescriptor("XS9999", "BlazorJsonLocalization", message + ": " + exception.Message, "Generator", diagnosticSeverity, true),
×
204
                        this.interfaceDeclarationSyntax.GetLocation()));
×
205
                }
×
206
            }
207
        }
208

209
        internal static bool IsSyntaxTargetForGeneration(SyntaxNode node) => node is InterfaceDeclarationSyntax;
73✔
210

211
        internal static InterfaceDeclarationSyntax GetSemanticTargetForGeneration(GeneratorSyntaxContext context)
212
        {
213
            var methodDeclarationSyntax = (InterfaceDeclarationSyntax)context.Node;
1✔
214

215
            return methodDeclarationSyntax;
1✔
216
        }
217
    }
218
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc