private void RewriteUsage(Context context, TagHelperIntermediateNode node, TagHelperDescriptor tagHelper) { if (!tagHelper.IsDefaultKind()) { return; } context.Add(tagHelper); // First we need to insert a node for the creation of the tag helper, and the hook up to the execution // context. This should come after the body node and any existing create nodes. // // If we're dealing with something totally malformed, then we'll end up just inserting at the end, and that's not // so bad. var i = 0; // Find the body node. while (i < node.Children.Count && node.Children[i] is TagHelperBodyIntermediateNode) { i++; } while (i < node.Children.Count && node.Children[i] is DefaultTagHelperBodyIntermediateNode) { i++; } // Now find the last create node. while (i < node.Children.Count && node.Children[i] is DefaultTagHelperCreateIntermediateNode) { i++; } // Now i has the right insertion point. node.Children.Insert(i, new DefaultTagHelperCreateIntermediateNode() { FieldName = context.GetFieldName(tagHelper), TagHelper = tagHelper, TypeName = tagHelper.GetTypeName(), }); // Next we need to rewrite any property nodes to use the field and property name for this // tag helper. for (i = 0; i < node.Children.Count; i++) { if (node.Children[i] is TagHelperPropertyIntermediateNode propertyNode && propertyNode.TagHelper == tagHelper) { // This belongs to the current tag helper, replace it. node.Children[i] = new DefaultTagHelperPropertyIntermediateNode(propertyNode) { FieldName = context.GetFieldName(tagHelper), PropertyName = propertyNode.BoundAttribute.GetPropertyName(), }; } } }