Skip to content

Commit 5d50510

Browse files
Simplify code with extension properties (#235)
1 parent 72509cb commit 5d50510

File tree

1 file changed

+77
-142
lines changed

1 file changed

+77
-142
lines changed

src/Common/ITypeSymbolExtensions.cs

Lines changed: 77 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,15 @@ typeSymbol is
1212
{
1313
Arity: 1,
1414
Name: "ICollection",
15-
ContainingNamespace:
16-
{
17-
Name: "Generic",
18-
ContainingNamespace:
19-
{
20-
Name: "Collections",
21-
ContainingNamespace:
22-
{
23-
Name: "System",
24-
ContainingNamespace.IsGlobalNamespace: true,
25-
},
26-
},
27-
},
15+
ContainingNamespace.IsSystemCollectionsGeneric: true,
2816
};
2917

3018
public static bool IsIReadOnlyCollection1(this INamedTypeSymbol typeSymbol) =>
3119
typeSymbol is
3220
{
3321
Arity: 1,
3422
Name: "IReadOnlyCollection",
35-
ContainingNamespace:
36-
{
37-
Name: "Generic",
38-
ContainingNamespace:
39-
{
40-
Name: "Collections",
41-
ContainingNamespace:
42-
{
43-
Name: "System",
44-
ContainingNamespace.IsGlobalNamespace: true,
45-
},
46-
},
47-
},
23+
ContainingNamespace.IsSystemCollectionsGeneric: true,
4824
};
4925

5026
public static IEnumerable<ITypeSymbol> GetBaseTypesAndThis(this ITypeSymbol? type)
@@ -68,41 +44,19 @@ public static IEnumerable<ISymbol> GetAllMembers(this ITypeSymbol type) =>
6844
.SelectMany(t => t.GetMembers());
6945

7046
public static bool IsValidateAttribute([NotNullWhen(returnValue: true)] this INamedTypeSymbol? typeSymbol) =>
71-
typeSymbol is
47+
typeSymbol is INamedTypeSymbol
7248
{
49+
Arity: 0,
7350
Name: "ValidateAttribute",
74-
ContainingNamespace:
75-
{
76-
Name: "Shared",
77-
ContainingNamespace:
78-
{
79-
Name: "Validations",
80-
ContainingNamespace:
81-
{
82-
Name: "Immediate",
83-
ContainingNamespace.IsGlobalNamespace: true,
84-
},
85-
},
86-
},
51+
ContainingNamespace.IsImmediateValidationsShared: true,
8752
};
8853

8954
public static bool IsValidatorAttribute([NotNullWhen(returnValue: true)] this INamedTypeSymbol? typeSymbol) =>
90-
typeSymbol is
55+
typeSymbol is INamedTypeSymbol
9156
{
57+
Arity: 0,
9258
Name: "ValidatorAttribute",
93-
ContainingNamespace:
94-
{
95-
Name: "Shared",
96-
ContainingNamespace:
97-
{
98-
Name: "Validations",
99-
ContainingNamespace:
100-
{
101-
Name: "Immediate",
102-
ContainingNamespace.IsGlobalNamespace: true,
103-
},
104-
},
105-
},
59+
ContainingNamespace.IsImmediateValidationsShared: true,
10660
};
10761

10862
public static bool ImplementsValidatorAttribute([NotNullWhen(true)] this INamedTypeSymbol? typeSymbol) =>
@@ -112,79 +66,33 @@ public static bool ImplementsValidatorAttribute([NotNullWhen(true)] this INamedT
11266
public static bool IsTargetTypeAttribute([NotNullWhen(returnValue: true)] this INamedTypeSymbol? typeSymbol) =>
11367
typeSymbol is
11468
{
69+
Arity: 0,
11570
Name: "TargetTypeAttribute",
116-
ContainingNamespace:
117-
{
118-
Name: "Shared",
119-
ContainingNamespace:
120-
{
121-
Name: "Validations",
122-
ContainingNamespace:
123-
{
124-
Name: "Immediate",
125-
ContainingNamespace.IsGlobalNamespace: true,
126-
},
127-
},
128-
},
71+
ContainingNamespace.IsImmediateValidationsShared: true,
12972
};
13073

13174
public static bool IsNotNullAttribute([NotNullWhen(returnValue: true)] this ITypeSymbol? typeSymbol) =>
13275
typeSymbol is INamedTypeSymbol
13376
{
77+
Arity: 0,
13478
Name: "NotNullAttribute",
135-
ContainingNamespace:
136-
{
137-
Name: "Shared",
138-
ContainingNamespace:
139-
{
140-
Name: "Validations",
141-
ContainingNamespace:
142-
{
143-
Name: "Immediate",
144-
ContainingNamespace.IsGlobalNamespace: true,
145-
},
146-
},
147-
},
79+
ContainingNamespace.IsImmediateValidationsShared: true,
14880
};
14981

15082
public static bool IsValidationResult(this INamedTypeSymbol? typeSymbol) =>
15183
typeSymbol is
15284
{
15385
Arity: 0,
15486
Name: "ValidationResult",
155-
ContainingNamespace:
156-
{
157-
Name: "Shared",
158-
ContainingNamespace:
159-
{
160-
Name: "Validations",
161-
ContainingNamespace:
162-
{
163-
Name: "Immediate",
164-
ContainingNamespace.IsGlobalNamespace: true,
165-
},
166-
},
167-
},
87+
ContainingNamespace.IsImmediateValidationsShared: true,
16888
};
16989

17090
public static bool IsIValidationTarget(this INamedTypeSymbol? typeSymbol) =>
17191
typeSymbol is
17292
{
17393
Arity: 1,
17494
Name: "IValidationTarget",
175-
ContainingNamespace:
176-
{
177-
Name: "Shared",
178-
ContainingNamespace:
179-
{
180-
Name: "Validations",
181-
ContainingNamespace:
182-
{
183-
Name: "Immediate",
184-
ContainingNamespace.IsGlobalNamespace: true,
185-
},
186-
},
187-
},
95+
ContainingNamespace.IsImmediateValidationsShared: true,
18896
};
18997

19098
public static bool IsValidationTarget([NotNullWhen(returnValue: true)] this INamedTypeSymbol? typeSymbol) =>
@@ -217,6 +125,7 @@ public static bool HasAdditionalValidationsMethod(this INamedTypeSymbol typeSymb
217125
.Any(m =>
218126
m is
219127
{
128+
Arity: 0,
220129
Name: "AdditionalValidations",
221130
IsStatic: true,
222131
ReturnsVoid: true,
@@ -233,6 +142,7 @@ m is
233142
public static bool IsDescriptionAttribute([NotNullWhen(returnValue: true)] this INamedTypeSymbol? typeSymbol) =>
234143
typeSymbol is
235144
{
145+
Arity: 0,
236146
Name: "DescriptionAttribute",
237147
ContainingNamespace:
238148
{
@@ -250,38 +160,15 @@ typeSymbol is
250160
{
251161
Arity: 2,
252162
Name: "ValidationBehavior",
253-
ContainingNamespace:
254-
{
255-
Name: "Shared",
256-
ContainingNamespace:
257-
{
258-
Name: "Validations",
259-
ContainingNamespace:
260-
{
261-
Name: "Immediate",
262-
ContainingNamespace.IsGlobalNamespace: true,
263-
},
264-
},
265-
},
163+
ContainingNamespace.IsImmediateValidationsShared: true,
266164
};
267165

268166
public static bool IsBehaviorsAttribute(this ITypeSymbol? typeSymbol) =>
269-
typeSymbol is
167+
typeSymbol is INamedTypeSymbol
270168
{
169+
Arity: 2,
271170
Name: "BehaviorsAttribute",
272-
ContainingNamespace:
273-
{
274-
Name: "Shared",
275-
ContainingNamespace:
276-
{
277-
Name: "Handlers",
278-
ContainingNamespace:
279-
{
280-
Name: "Immediate",
281-
ContainingNamespace.IsGlobalNamespace: true,
282-
},
283-
},
284-
},
171+
ContainingNamespace.IsImmediateHandlersShared: true,
285172
};
286173

287174
public static bool IsAllowNullAttribute(this ITypeSymbol? typeSymbol) =>
@@ -352,15 +239,63 @@ SemanticModel semanticModel
352239
}
353240

354241
public static bool IsTargetTypeSymbol(this ISymbol symbol) =>
355-
symbol switch
356-
{
357-
IParameterSymbol { Type: var type } => type.IsValidTargetTypeType(),
358-
IPropertySymbol { Type: var type } => type.IsValidTargetTypeType(),
359-
_ => false,
360-
}
242+
symbol is IParameterSymbol { Type.IsValidTargetTypeType: true }
243+
or IPropertySymbol { Type.IsValidTargetTypeType: true }
361244
&& symbol.GetAttributes().Any(a => a.AttributeClass.IsTargetTypeAttribute());
362245

363-
private static bool IsValidTargetTypeType(this ITypeSymbol? typeSymbol) =>
364-
typeSymbol is { SpecialType: SpecialType.System_Object or SpecialType.System_String }
365-
or IArrayTypeSymbol { ElementType.SpecialType: SpecialType.System_Object or SpecialType.System_String };
246+
extension(ITypeSymbol typeSymbol)
247+
{
248+
[SuppressMessage("Style", "IDE0051:Remove unused private members", Justification = "Roslyn Bug (https://github.com/dotnet/roslyn/issues/81213)")]
249+
private bool IsValidTargetTypeType =>
250+
typeSymbol is { SpecialType: SpecialType.System_Object or SpecialType.System_String }
251+
or IArrayTypeSymbol { ElementType.SpecialType: SpecialType.System_Object or SpecialType.System_String };
252+
}
253+
254+
extension(INamespaceSymbol namespaceSymbol)
255+
{
256+
public bool IsImmediateValidationsShared =>
257+
namespaceSymbol is
258+
{
259+
Name: "Shared",
260+
ContainingNamespace:
261+
{
262+
Name: "Validations",
263+
ContainingNamespace:
264+
{
265+
Name: "Immediate",
266+
ContainingNamespace.IsGlobalNamespace: true,
267+
},
268+
},
269+
};
270+
271+
public bool IsImmediateHandlersShared =>
272+
namespaceSymbol is
273+
{
274+
Name: "Shared",
275+
ContainingNamespace:
276+
{
277+
Name: "Handlers",
278+
ContainingNamespace:
279+
{
280+
Name: "Immediate",
281+
ContainingNamespace.IsGlobalNamespace: true,
282+
},
283+
},
284+
};
285+
286+
public bool IsSystemCollectionsGeneric =>
287+
namespaceSymbol is
288+
{
289+
Name: "Generic",
290+
ContainingNamespace:
291+
{
292+
Name: "Collections",
293+
ContainingNamespace:
294+
{
295+
Name: "System",
296+
ContainingNamespace.IsGlobalNamespace: true,
297+
},
298+
},
299+
};
300+
}
366301
}

0 commit comments

Comments
 (0)