|
3 | 3 |
|
4 | 4 | using System.Collections.Generic; |
5 | 5 | using System.Collections.Immutable; |
| 6 | +using System.Globalization; |
6 | 7 | using System.Threading; |
7 | 8 | using Microsoft.CodeAnalysis; |
8 | 9 | using Microsoft.CodeAnalysis.CSharp; |
@@ -43,45 +44,58 @@ public void Initialize(GeneratorInitializationContext context) |
43 | 44 | /// <param name="executionContext"></param> |
44 | 45 | public void Execute(GeneratorExecutionContext executionContext) |
45 | 46 | { |
46 | | - if (executionContext.SyntaxContextReceiver is not SyntaxContextReceiver receiver || receiver.ContextClassDeclarations == null) |
| 47 | + // Ensure the source generator parses and emits using invariant culture. |
| 48 | + // This prevents issues such as locale-specific negative signs (e.g., U+2212 in fi-FI) |
| 49 | + // from being written to generated source files. |
| 50 | + // Note: RS1035 is already disabled at the file level for this Roslyn version. |
| 51 | + CultureInfo originalCulture = CultureInfo.CurrentCulture; |
| 52 | + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; |
| 53 | + try |
47 | 54 | { |
48 | | - // nothing to do yet |
49 | | - return; |
50 | | - } |
| 55 | + if (executionContext.SyntaxContextReceiver is not SyntaxContextReceiver receiver || receiver.ContextClassDeclarations == null) |
| 56 | + { |
| 57 | + // nothing to do yet |
| 58 | + return; |
| 59 | + } |
51 | 60 |
|
52 | | - // Stage 1. Parse the identified JsonSerializerContext classes and store the model types. |
53 | | - KnownTypeSymbols knownSymbols = new(executionContext.Compilation); |
54 | | - Parser parser = new(knownSymbols); |
| 61 | + // Stage 1. Parse the identified JsonSerializerContext classes and store the model types. |
| 62 | + KnownTypeSymbols knownSymbols = new(executionContext.Compilation); |
| 63 | + Parser parser = new(knownSymbols); |
55 | 64 |
|
56 | | - List<ContextGenerationSpec>? contextGenerationSpecs = null; |
57 | | - foreach ((ClassDeclarationSyntax? contextClassDeclaration, SemanticModel semanticModel) in receiver.ContextClassDeclarations) |
58 | | - { |
59 | | - ContextGenerationSpec? contextGenerationSpec = parser.ParseContextGenerationSpec(contextClassDeclaration, semanticModel, executionContext.CancellationToken); |
60 | | - if (contextGenerationSpec is null) |
| 65 | + List<ContextGenerationSpec>? contextGenerationSpecs = null; |
| 66 | + foreach ((ClassDeclarationSyntax? contextClassDeclaration, SemanticModel semanticModel) in receiver.ContextClassDeclarations) |
61 | 67 | { |
62 | | - continue; |
| 68 | + ContextGenerationSpec? contextGenerationSpec = parser.ParseContextGenerationSpec(contextClassDeclaration, semanticModel, executionContext.CancellationToken); |
| 69 | + if (contextGenerationSpec is null) |
| 70 | + { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + (contextGenerationSpecs ??= new()).Add(contextGenerationSpec); |
63 | 75 | } |
64 | 76 |
|
65 | | - (contextGenerationSpecs ??= new()).Add(contextGenerationSpec); |
66 | | - } |
| 77 | + // Stage 2. Report any diagnostics gathered by the parser. |
| 78 | + foreach (DiagnosticInfo diagnosticInfo in parser.Diagnostics) |
| 79 | + { |
| 80 | + executionContext.ReportDiagnostic(diagnosticInfo.CreateDiagnostic()); |
| 81 | + } |
67 | 82 |
|
68 | | - // Stage 2. Report any diagnostics gathered by the parser. |
69 | | - foreach (DiagnosticInfo diagnosticInfo in parser.Diagnostics) |
70 | | - { |
71 | | - executionContext.ReportDiagnostic(diagnosticInfo.CreateDiagnostic()); |
72 | | - } |
| 83 | + if (contextGenerationSpecs is null) |
| 84 | + { |
| 85 | + return; |
| 86 | + } |
73 | 87 |
|
74 | | - if (contextGenerationSpecs is null) |
75 | | - { |
76 | | - return; |
| 88 | + // Stage 3. Emit source code from the spec models. |
| 89 | + OnSourceEmitting?.Invoke(contextGenerationSpecs.ToImmutableArray()); |
| 90 | + Emitter emitter = new(executionContext); |
| 91 | + foreach (ContextGenerationSpec contextGenerationSpec in contextGenerationSpecs) |
| 92 | + { |
| 93 | + emitter.Emit(contextGenerationSpec); |
| 94 | + } |
77 | 95 | } |
78 | | - |
79 | | - // Stage 3. Emit source code from the spec models. |
80 | | - OnSourceEmitting?.Invoke(contextGenerationSpecs.ToImmutableArray()); |
81 | | - Emitter emitter = new(executionContext); |
82 | | - foreach (ContextGenerationSpec contextGenerationSpec in contextGenerationSpecs) |
| 96 | + finally |
83 | 97 | { |
84 | | - emitter.Emit(contextGenerationSpec); |
| 98 | + CultureInfo.CurrentCulture = originalCulture; |
85 | 99 | } |
86 | 100 | } |
87 | 101 |
|
|
0 commit comments