Skip to content

Commit 25ef2b1

Browse files
committed
优化(CodeInjectIncrementalSourceGenerator):增强代码健壮性
新增对空值和异常情况的检查,避免潜在运行时错误 - 在流操作中新增对 null 的检查 - 在属性解析中新增对名称和参数的验证 - 确保文件路径和区域名称非空 - 提取占位符时过滤空值 - 在属性列表为空时直接返回 null
1 parent 8caa7bc commit 25ef2b1

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/CodeInjectSourceGenerator/CodeInjectIncrementalSourceGenerator.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ private static string GetEmbeddedResourceContent(string resourceName = null)
8787
using (var stream = assembly.GetManifestResourceStream(targetResource))
8888
{
8989
if (stream == null)
90+
{
9091
return null;
92+
}
9193

9294
using (var reader = new StreamReader(stream, Encoding.UTF8))
9395
{
@@ -135,16 +137,22 @@ private static ClassToGenerate GetSemanticTargetForGeneration(GeneratorSyntaxCon
135137
foreach (var attributeData in classSymbol.GetAttributes())
136138
{
137139
if (attributeData.AttributeClass?.Name != "RegionInjectAttribute")
140+
{
138141
continue;
142+
}
139143

140144
if (attributeData.ConstructorArguments.Length < 2)
145+
{
141146
continue;
147+
}
142148

143149
var filePath = attributeData.ConstructorArguments[0].Value?.ToString();
144150
var regionName = attributeData.ConstructorArguments[1].Value?.ToString();
145151

146152
if (string.IsNullOrEmpty(filePath) || string.IsNullOrEmpty(regionName))
153+
{
147154
continue;
155+
}
148156

149157
var placeholders = new List<string>();
150158

@@ -155,7 +163,9 @@ private static ClassToGenerate GetSemanticTargetForGeneration(GeneratorSyntaxCon
155163
{
156164
var value = attributeData.ConstructorArguments[i].Value?.ToString();
157165
if (!string.IsNullOrEmpty(value))
166+
{
158167
placeholders.Add(value);
168+
}
159169
}
160170
}
161171

@@ -172,7 +182,9 @@ private static ClassToGenerate GetSemanticTargetForGeneration(GeneratorSyntaxCon
172182
{
173183
var stringValue = item.Value?.ToString();
174184
if (!string.IsNullOrEmpty(stringValue))
185+
{
175186
placeholders.Add(stringValue);
187+
}
176188
}
177189
}
178190
}
@@ -185,7 +197,9 @@ private static ClassToGenerate GetSemanticTargetForGeneration(GeneratorSyntaxCon
185197
}
186198

187199
if (attributes.Count == 0)
200+
{
188201
return null;
202+
}
189203

190204
return new ClassToGenerate(
191205
classSymbol.Name,

0 commit comments

Comments
 (0)