5
5
using System . Collections . Generic ;
6
6
using System . Globalization ;
7
7
using System . Linq ;
8
+ using Microsoft . AspNetCore . Razor ;
8
9
using Microsoft . AspNetCore . Razor . Language ;
9
10
using Microsoft . AspNetCore . Razor . Language . Components ;
10
11
using Microsoft . AspNetCore . Razor . PooledObjects ;
11
12
using static Microsoft . AspNetCore . Razor . Language . CommonMetadata ;
12
13
13
14
namespace Microsoft . CodeAnalysis . Razor ;
14
15
15
- internal class BindTagHelperDescriptorProvider : ITagHelperDescriptorProvider
16
+ // Run after the component tag helper provider, because we need to see the results.
17
+ internal sealed class BindTagHelperDescriptorProvider ( ) : TagHelperDescriptorProviderBase ( order : 1000 )
16
18
{
17
- private static TagHelperDescriptor ? s_fallbackBindTagHelper ;
19
+ private static readonly Lazy < TagHelperDescriptor > s_fallbackBindTagHelper = new ( CreateFallbackBindTagHelper ) ;
18
20
19
- // Run after the component tag helper provider, because we need to see the results.
20
- public int Order { get ; set ; } = 1000 ;
21
-
22
- public RazorEngine ? Engine { get ; set ; }
23
-
24
- public void Execute ( TagHelperDescriptorProviderContext context )
21
+ public override void Execute ( TagHelperDescriptorProviderContext context )
25
22
{
26
- if ( context == null )
27
- {
28
- throw new ArgumentNullException ( nameof ( context ) ) ;
29
- }
23
+ ArgHelper . ThrowIfNull ( context ) ;
30
24
31
25
// This provider returns tag helper information for 'bind' which doesn't necessarily
32
26
// map to any real component. Bind behaviors more like a macro, which can map a single LValue to
@@ -91,11 +85,7 @@ public void Execute(TagHelperDescriptorProviderContext context)
91
85
// we have. Case #4 is data-driven based on component definitions.
92
86
//
93
87
// We provide a good set of attributes that map to the HTML dom. This set is user extensible.
94
- var compilation = context . GetCompilation ( ) ;
95
- if ( compilation == null )
96
- {
97
- return ;
98
- }
88
+ var compilation = context . Compilation ;
99
89
100
90
var bindMethods = compilation . GetTypeByMetadataName ( ComponentsApi . BindConverter . FullTypeName ) ;
101
91
if ( bindMethods == null )
@@ -105,14 +95,13 @@ public void Execute(TagHelperDescriptorProviderContext context)
105
95
return ;
106
96
}
107
97
108
- var targetSymbol = context . Items . GetTargetSymbol ( ) ;
109
- if ( targetSymbol is not null && ! SymbolEqualityComparer . Default . Equals ( targetSymbol , bindMethods . ContainingAssembly ) )
98
+ if ( context . TargetSymbol is { } targetSymbol && ! SymbolEqualityComparer . Default . Equals ( targetSymbol , bindMethods . ContainingAssembly ) )
110
99
{
111
100
return ;
112
101
}
113
102
114
103
// Tag Helper definition for case #1. This is the most general case.
115
- context . Results . Add ( GetOrCreateFallbackBindTagHelper ( ) ) ;
104
+ context . Results . Add ( s_fallbackBindTagHelper . Value ) ;
116
105
117
106
var bindElementAttribute = compilation . GetTypeByMetadataName ( ComponentsApi . BindElementAttribute . FullTypeName ) ;
118
107
var bindInputElementAttribute = compilation . GetTypeByMetadataName ( ComponentsApi . BindInputElementAttribute . FullTypeName ) ;
@@ -129,112 +118,107 @@ public void Execute(TagHelperDescriptorProviderContext context)
129
118
collector . Collect ( context ) ;
130
119
}
131
120
132
- private static TagHelperDescriptor GetOrCreateFallbackBindTagHelper ( )
121
+ private static TagHelperDescriptor CreateFallbackBindTagHelper ( )
133
122
{
134
- return s_fallbackBindTagHelper ??= CreateFallbackBindTagHelper ( ) ;
135
-
136
- static TagHelperDescriptor CreateFallbackBindTagHelper ( )
123
+ using var _ = TagHelperDescriptorBuilder . GetPooledInstance (
124
+ ComponentMetadata . Bind . TagHelperKind , "Bind" , ComponentsApi . AssemblyName ,
125
+ out var builder ) ;
126
+
127
+ builder . CaseSensitive = true ;
128
+ builder . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Fallback ) ;
129
+
130
+ builder . SetMetadata (
131
+ SpecialKind ( ComponentMetadata . Bind . TagHelperKind ) ,
132
+ MakeTrue ( TagHelperMetadata . Common . ClassifyAttributesOnly ) ,
133
+ RuntimeName ( ComponentMetadata . Bind . RuntimeName ) ,
134
+ MakeTrue ( ComponentMetadata . Bind . FallbackKey ) ,
135
+ TypeName ( "Microsoft.AspNetCore.Components.Bind" ) ,
136
+ TypeNamespace ( "Microsoft.AspNetCore.Components" ) ,
137
+ TypeNameIdentifier ( "Bind" ) ) ;
138
+
139
+ builder . TagMatchingRule ( rule =>
137
140
{
138
- using var _ = TagHelperDescriptorBuilder . GetPooledInstance (
139
- ComponentMetadata . Bind . TagHelperKind , "Bind" , ComponentsApi . AssemblyName ,
140
- out var builder ) ;
141
-
142
- builder . CaseSensitive = true ;
143
- builder . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Fallback ) ;
144
-
145
- builder . SetMetadata (
146
- SpecialKind ( ComponentMetadata . Bind . TagHelperKind ) ,
147
- MakeTrue ( TagHelperMetadata . Common . ClassifyAttributesOnly ) ,
148
- RuntimeName ( ComponentMetadata . Bind . RuntimeName ) ,
149
- MakeTrue ( ComponentMetadata . Bind . FallbackKey ) ,
150
- TypeName ( "Microsoft.AspNetCore.Components.Bind" ) ,
151
- TypeNamespace ( "Microsoft.AspNetCore.Components" ) ,
152
- TypeNameIdentifier ( "Bind" ) ) ;
153
-
154
- builder . TagMatchingRule ( rule =>
141
+ rule . TagName = "*" ;
142
+ rule . Attribute ( attribute =>
155
143
{
156
- rule . TagName = "*" ;
157
- rule . Attribute ( attribute =>
158
- {
159
- attribute . Name = "@bind-" ;
160
- attribute . NameComparisonMode = RequiredAttributeDescriptor . NameComparisonMode . PrefixMatch ;
161
- attribute . SetMetadata ( Attributes . IsDirectiveAttribute ) ;
162
- } ) ;
144
+ attribute . Name = "@bind-" ;
145
+ attribute . NameComparisonMode = RequiredAttributeDescriptor . NameComparisonMode . PrefixMatch ;
146
+ attribute . SetMetadata ( Attributes . IsDirectiveAttribute ) ;
163
147
} ) ;
148
+ } ) ;
164
149
165
- builder . BindAttribute ( attribute =>
166
- {
167
- attribute . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Fallback ) ;
150
+ builder . BindAttribute ( attribute =>
151
+ {
152
+ attribute . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Fallback ) ;
168
153
169
- var attributeName = "@bind-..." ;
170
- attribute . Name = attributeName ;
171
- attribute . AsDictionary ( "@bind-" , typeof ( object ) . FullName ) ;
154
+ var attributeName = "@bind-..." ;
155
+ attribute . Name = attributeName ;
156
+ attribute . AsDictionary ( "@bind-" , typeof ( object ) . FullName ) ;
172
157
173
- attribute . SetMetadata (
174
- PropertyName ( "Bind" ) ,
175
- IsDirectiveAttribute ) ;
158
+ attribute . SetMetadata (
159
+ PropertyName ( "Bind" ) ,
160
+ IsDirectiveAttribute ) ;
176
161
177
- attribute . TypeName = "System.Collections.Generic.Dictionary<string, object>" ;
162
+ attribute . TypeName = "System.Collections.Generic.Dictionary<string, object>" ;
178
163
179
- attribute . BindAttributeParameter ( parameter =>
180
- {
181
- parameter . Name = "format" ;
182
- parameter . TypeName = typeof ( string ) . FullName ;
183
- parameter . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Fallback_Format ) ;
164
+ attribute . BindAttributeParameter ( parameter =>
165
+ {
166
+ parameter . Name = "format" ;
167
+ parameter . TypeName = typeof ( string ) . FullName ;
168
+ parameter . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Fallback_Format ) ;
184
169
185
- parameter . SetMetadata ( Parameters . Format ) ;
186
- } ) ;
170
+ parameter . SetMetadata ( Parameters . Format ) ;
171
+ } ) ;
187
172
188
- attribute . BindAttributeParameter ( parameter =>
189
- {
190
- parameter . Name = "event" ;
191
- parameter . TypeName = typeof ( string ) . FullName ;
192
- parameter . SetDocumentation (
193
- DocumentationDescriptor . From (
194
- DocumentationId . BindTagHelper_Fallback_Event , attributeName ) ) ;
173
+ attribute . BindAttributeParameter ( parameter =>
174
+ {
175
+ parameter . Name = "event" ;
176
+ parameter . TypeName = typeof ( string ) . FullName ;
177
+ parameter . SetDocumentation (
178
+ DocumentationDescriptor . From (
179
+ DocumentationId . BindTagHelper_Fallback_Event , attributeName ) ) ;
195
180
196
- parameter . SetMetadata ( Parameters . Event ) ;
197
- } ) ;
181
+ parameter . SetMetadata ( Parameters . Event ) ;
182
+ } ) ;
198
183
199
- attribute . BindAttributeParameter ( parameter =>
200
- {
201
- parameter . Name = "culture" ;
202
- parameter . TypeName = typeof ( CultureInfo ) . FullName ;
203
- parameter . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Element_Culture ) ;
184
+ attribute . BindAttributeParameter ( parameter =>
185
+ {
186
+ parameter . Name = "culture" ;
187
+ parameter . TypeName = typeof ( CultureInfo ) . FullName ;
188
+ parameter . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Element_Culture ) ;
204
189
205
- parameter . SetMetadata ( Parameters . Culture ) ;
206
- } ) ;
190
+ parameter . SetMetadata ( Parameters . Culture ) ;
191
+ } ) ;
207
192
208
- attribute . BindAttributeParameter ( parameter =>
209
- {
210
- parameter . Name = "get" ;
211
- parameter . TypeName = typeof ( object ) . FullName ;
212
- parameter . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Element_Get ) ;
193
+ attribute . BindAttributeParameter ( parameter =>
194
+ {
195
+ parameter . Name = "get" ;
196
+ parameter . TypeName = typeof ( object ) . FullName ;
197
+ parameter . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Element_Get ) ;
213
198
214
- parameter . SetMetadata ( Parameters . Get ) ;
215
- } ) ;
199
+ parameter . SetMetadata ( Parameters . Get ) ;
200
+ } ) ;
216
201
217
- attribute . BindAttributeParameter ( parameter =>
218
- {
219
- parameter . Name = "set" ;
220
- parameter . TypeName = typeof ( Delegate ) . FullName ;
221
- parameter . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Element_Set ) ;
202
+ attribute . BindAttributeParameter ( parameter =>
203
+ {
204
+ parameter . Name = "set" ;
205
+ parameter . TypeName = typeof ( Delegate ) . FullName ;
206
+ parameter . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Element_Set ) ;
222
207
223
- parameter . SetMetadata ( Parameters . Set ) ;
224
- } ) ;
208
+ parameter . SetMetadata ( Parameters . Set ) ;
209
+ } ) ;
225
210
226
- attribute . BindAttributeParameter ( parameter =>
227
- {
228
- parameter . Name = "after" ;
229
- parameter . TypeName = typeof ( Delegate ) . FullName ;
230
- parameter . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Element_After ) ;
211
+ attribute . BindAttributeParameter ( parameter =>
212
+ {
213
+ parameter . Name = "after" ;
214
+ parameter . TypeName = typeof ( Delegate ) . FullName ;
215
+ parameter . SetDocumentation ( DocumentationDescriptor . BindTagHelper_Element_After ) ;
231
216
232
- parameter . SetMetadata ( Parameters . After ) ;
233
- } ) ;
217
+ parameter . SetMetadata ( Parameters . After ) ;
234
218
} ) ;
219
+ } ) ;
235
220
236
- return builder . Build ( ) ;
237
- }
221
+ return builder . Build ( ) ;
238
222
}
239
223
240
224
private class Collector (
0 commit comments