|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | | -using System.Collections.Generic; |
5 | 4 | using System.Collections.Immutable; |
6 | 5 | using System.Linq; |
7 | 6 | using Microsoft.CodeAnalysis; |
8 | | -using Microsoft.CodeAnalysis.CSharp; |
9 | 7 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
10 | 8 | using Microsoft.Gen.ContextualOptions.Model; |
11 | 9 |
|
12 | 10 | namespace Microsoft.Gen.ContextualOptions; |
13 | 11 |
|
14 | 12 | internal static class Parser |
15 | 13 | { |
16 | | - public static IEnumerable<OptionsContextType> GetContextualOptionTypes(Dictionary<INamedTypeSymbol, List<TypeDeclarationSyntax>> types) => |
17 | | - types |
18 | | - .Select(type => new OptionsContextType(type.Key, type.Value.ToImmutableArray(), GetContextProperties(type.Key))) |
19 | | - .Select(CheckInstantiable) |
20 | | - .Select(CheckPartial) |
21 | | - .Select(CheckRefLikeType) |
22 | | - .Select(CheckHasProperties); |
| 14 | + public static OptionsContextType GetContextualOptionTypes(INamedTypeSymbol symbol, TypeDeclarationSyntax typeDeclarationSyntax) |
| 15 | + => new OptionsContextType(symbol, typeDeclarationSyntax, GetContextProperties(symbol)); |
23 | 16 |
|
24 | | - private static OptionsContextType CheckInstantiable(OptionsContextType type) |
25 | | - { |
26 | | - if (type.Symbol.IsStatic) |
27 | | - { |
28 | | - type.Diagnostics.AddRange( |
29 | | - type.Definitions |
30 | | - .SelectMany(def => def.Modifiers) |
31 | | - .Where(modifier => modifier.IsKind(SyntaxKind.StaticKeyword)) |
32 | | - .Select(modifier => Diagnostic.Create(DiagDescriptors.ContextCannotBeStatic, modifier.GetLocation(), type.Name))); |
33 | | - } |
| 17 | + // TODO: Separate analyzer for diagnostics. |
| 18 | + //private static OptionsContextType CheckInstantiable(OptionsContextType type) |
| 19 | + //{ |
| 20 | + // if (type.Symbol.IsStatic) |
| 21 | + // { |
| 22 | + // type.Diagnostics.AddRange( |
| 23 | + // type.Definitions |
| 24 | + // .SelectMany(def => def.Modifiers) |
| 25 | + // .Where(modifier => modifier.IsKind(SyntaxKind.StaticKeyword)) |
| 26 | + // .Select(modifier => Diagnostic.Create(DiagDescriptors.ContextCannotBeStatic, modifier.GetLocation(), type.Name))); |
| 27 | + // } |
34 | 28 |
|
35 | | - return type; |
36 | | - } |
| 29 | + // return type; |
| 30 | + //} |
37 | 31 |
|
38 | | - private static OptionsContextType CheckRefLikeType(OptionsContextType type) |
39 | | - { |
40 | | - if (type.Symbol.IsRefLikeType) |
41 | | - { |
42 | | - type.Diagnostics.AddRange( |
43 | | - type.Definitions |
44 | | - .SelectMany(def => def.Modifiers) |
45 | | - .Where(modifier => modifier.IsKind(SyntaxKind.RefKeyword)) |
46 | | - .Select(modifier => Diagnostic.Create(DiagDescriptors.ContextCannotBeRefLike, modifier.GetLocation(), type.Name))); |
47 | | - } |
| 32 | + // TODO: Separate analyzer for diagnostics. |
| 33 | + //private static OptionsContextType CheckRefLikeType(OptionsContextType type) |
| 34 | + //{ |
| 35 | + // if (type.Symbol.IsRefLikeType) |
| 36 | + // { |
| 37 | + // type.Diagnostics.AddRange( |
| 38 | + // type.Definitions |
| 39 | + // .SelectMany(def => def.Modifiers) |
| 40 | + // .Where(modifier => modifier.IsKind(SyntaxKind.RefKeyword)) |
| 41 | + // .Select(modifier => Diagnostic.Create(DiagDescriptors.ContextCannotBeRefLike, modifier.GetLocation(), type.Name))); |
| 42 | + // } |
48 | 43 |
|
49 | | - return type; |
50 | | - } |
| 44 | + // return type; |
| 45 | + //} |
51 | 46 |
|
52 | | - private static OptionsContextType CheckPartial(OptionsContextType type) |
53 | | - { |
54 | | - if (!type.Definitions.Any(def => def.Modifiers.Any(static token => token.IsKind(SyntaxKind.PartialKeyword)))) |
55 | | - { |
56 | | - type.Diagnostics.AddRange( |
57 | | - type.Definitions.Select(def => Diagnostic.Create(DiagDescriptors.ContextMustBePartial, def.Identifier.GetLocation(), type.Name))); |
58 | | - } |
| 47 | + // TODO: Separate analyzer for diagnostics. |
| 48 | + //private static OptionsContextType CheckPartial(OptionsContextType type) |
| 49 | + //{ |
| 50 | + // if (!type.Definitions.Any(def => def.Modifiers.Any(static token => token.IsKind(SyntaxKind.PartialKeyword)))) |
| 51 | + // { |
| 52 | + // type.Diagnostics.AddRange( |
| 53 | + // type.Definitions.Select(def => Diagnostic.Create(DiagDescriptors.ContextMustBePartial, def.Identifier.GetLocation(), type.Name))); |
| 54 | + // } |
59 | 55 |
|
60 | | - return type; |
61 | | - } |
| 56 | + // return type; |
| 57 | + //} |
62 | 58 |
|
63 | | - private static OptionsContextType CheckHasProperties(OptionsContextType type) |
64 | | - { |
65 | | - if (type.OptionsContextProperties.IsEmpty) |
66 | | - { |
67 | | - type.Diagnostics.AddRange( |
68 | | - type.Definitions.Select(def => Diagnostic.Create(DiagDescriptors.ContextDoesNotHaveValidProperties, def.Identifier.GetLocation(), type.Name))); |
69 | | - } |
| 59 | + // TODO: Separate analyzer for diagnostics. |
| 60 | + //private static OptionsContextType CheckHasProperties(OptionsContextType type) |
| 61 | + //{ |
| 62 | + // if (type.OptionsContextProperties.IsEmpty) |
| 63 | + // { |
| 64 | + // type.Diagnostics.AddRange( |
| 65 | + // type.Definitions.Select(def => Diagnostic.Create(DiagDescriptors.ContextDoesNotHaveValidProperties, def.Identifier.GetLocation(), type.Name))); |
| 66 | + // } |
70 | 67 |
|
71 | | - return type; |
72 | | - } |
| 68 | + // return type; |
| 69 | + //} |
73 | 70 |
|
74 | 71 | private static ImmutableArray<string> GetContextProperties(INamedTypeSymbol symbol) |
75 | 72 | { |
|
0 commit comments