-
Notifications
You must be signed in to change notification settings - Fork 889
Expand file tree
/
Copy pathComplianceReportEmitter.cs
More file actions
140 lines (126 loc) · 6.95 KB
/
Copy pathComplianceReportEmitter.cs
File metadata and controls
140 lines (126 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using Microsoft.Gen.Shared;
namespace Microsoft.Gen.ComplianceReports;
internal sealed class ComplianceReportEmitter : JsonEmitterBase
{
public ComplianceReportEmitter()
: base(false)
{
}
/// <summary>
/// Generates JSON object containing the <see cref="ClassifiedTypes"/> for compliance report.
/// </summary>
/// <param name="classifiedTypes">The classified types.</param>
/// <param name="assemblyName">The assembly name.</param>
/// <param name="includeName">Whether to include the assembly name in the report. Defaulted to true.</param>
/// <param name="indentationLevel">The number of indentations in case its nested in other reports like <see cref="MetadataReportsGenerator"/>.Defaulted to zero.</param>
/// <returns>string report as json or String.Empty.</returns>
[SuppressMessage("Performance", "LA0002:Use 'Microsoft.Extensions.Text.NumericExtensions.ToInvariantString' for improved performance", Justification = "Can't use that in a generator")]
public string Emit(IReadOnlyCollection<ClassifiedType> classifiedTypes, string assemblyName,
bool includeName = true, int indentationLevel = 0) // show or hide assemblyName in the report,defaulted to true.
{
Indent(indentationLevel);
OutObject(() =>
{
// this is only for not displaying a name as part of ComplianceReport properties,it should be at the root of the report, defaulted to true for backward compatibility
if (includeName)
{
OutNameValue("Name", assemblyName);
}
OutArray("Types", () =>
{
foreach (var classifiedType in classifiedTypes.OrderBy(ct => ct.TypeName))
{
OutObject(() =>
{
OutNameValue("Name", classifiedType.TypeName);
if (classifiedType.Members != null)
{
OutArray("Members", () =>
{
foreach (var member in classifiedType.Members.OrderBy(m => m.Name))
{
OutObject(() =>
{
OutNameValue("Name", member.Name);
OutNameValue("Type", member.TypeName);
OutNameValue("File", member.SourceFilePath);
OutNameValue("Line", member.SourceLine.ToString(CultureInfo.InvariantCulture));
if (member.Classifications.Count > 0)
{
OutArray("Classifications", () =>
{
foreach (var c in member.Classifications.OrderBy(c => c.Name))
{
OutObject(() =>
{
OutNameValue("Name", c.Name);
if (!string.IsNullOrEmpty(c.Notes))
{
OutNameValue("Notes", c.Notes!);
}
});
}
});
}
});
}
});
}
if (classifiedType.LogMethods != null)
{
OutArray("Logging Methods", () =>
{
foreach (var method in classifiedType.LogMethods.OrderBy(m => m.MethodName))
{
OutObject(() =>
{
OutNameValue("Name", method.MethodName);
OutArray("Parameters", () =>
{
foreach (var p in method.Parameters)
{
OutObject(() =>
{
OutNameValue("Name", p.Name);
OutNameValue("Type", p.TypeName);
OutNameValue("File", p.SourceFilePath);
OutNameValue("Line", p.SourceLine.ToString(CultureInfo.InvariantCulture));
if (p.Classifications.Count > 0)
{
OutArray("Classifications", () =>
{
foreach (var c in p.Classifications.OrderBy(c => c.Name))
{
OutObject(() =>
{
OutNameValue("Name", c.Name);
if (!string.IsNullOrEmpty(c.Notes))
{
OutNameValue("Notes", c.Notes!);
}
});
}
});
}
});
}
});
});
}
});
}
});
}
});
});
Unindent(indentationLevel);
return Capture();
}
}