protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
        {
            if (!IsComponentDocument(documentNode))
            {
                return;
            }

            var @namespace = documentNode.FindPrimaryNamespace();
            var @class     = documentNode.FindPrimaryClass();

            if (@namespace == null || @class == null)
            {
                // Nothing to do, bail. We can't function without the standard structure.
                return;
            }

            // For each @bind *usage* we need to rewrite the tag helper node to map to basic constructs.
            var references          = documentNode.FindDescendantReferences <TagHelperDirectiveAttributeIntermediateNode>();
            var parameterReferences = documentNode.FindDescendantReferences <TagHelperDirectiveAttributeParameterIntermediateNode>();

            var parents = new HashSet <IntermediateNode>();

            for (var i = 0; i < references.Count; i++)
            {
                parents.Add(references[i].Parent);
            }
            for (var i = 0; i < parameterReferences.Count; i++)
            {
                parents.Add(parameterReferences[i].Parent);
            }

            foreach (var parent in parents)
            {
                ProcessDuplicates(parent);
            }

            // First, collect all the non-parameterized @bind or @bind-* attributes.
            // The dict key is a tuple of (parent, attributeName) to differentiate attributes with the same name in two different elements.
            // We don't have to worry about duplicate bound attributes in the same element
            // like, <Foo @bind="bar" @bind="bar" />, because IR lowering takes care of that.
            var bindEntries = new Dictionary <(IntermediateNode, string), BindEntry>();

            for (var i = 0; i < references.Count; i++)
            {
                var reference = references[i];
                var parent    = reference.Parent;
                var node      = (TagHelperDirectiveAttributeIntermediateNode)reference.Node;

                if (!parent.Children.Contains(node))
                {
                    // This node was removed as a duplicate, skip it.
                    continue;
                }

                if (node.TagHelper.IsBindTagHelper())
                {
                    bindEntries[(parent, node.AttributeName)] = new BindEntry(reference);
        protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
        {
            if (!IsComponentDocument(documentNode))
            {
                return;
            }

            var @namespace = documentNode.FindPrimaryNamespace();
            var @class     = documentNode.FindPrimaryClass();

            if (@namespace == null || @class == null)
            {
                // Nothing to do, bail. We can't function without the standard structure.
                return;
            }

            var references = documentNode.FindDescendantReferences <TagHelperDirectiveAttributeIntermediateNode>();

            for (var i = 0; i < references.Count; i++)
            {
                var reference = references[i];
                var node      = (TagHelperDirectiveAttributeIntermediateNode)reference.Node;

                if (node.TagHelper.IsKeyTagHelper())
                {
                    reference.Replace(RewriteUsage(reference.Parent, node));
                }
            }
        }
Exemplo n.º 3
0
        protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
        {
            if (!IsComponentDocument(documentNode))
            {
                return;
            }

            var references = documentNode.FindDescendantReferences <TagHelperDirectiveAttributeIntermediateNode>();
            var parents    = new HashSet <IntermediateNode>();

            for (var i = 0; i < references.Count; i++)
            {
                parents.Add(references[i].Parent);
            }

            for (var i = 0; i < references.Count; i++)
            {
                var reference = references[i];
                var node      = (TagHelperDirectiveAttributeIntermediateNode)reference.Node;
                if (node.TagHelper.IsSplatTagHelper())
                {
                    reference.Replace(RewriteUsage(reference.Parent, node));
                }
            }
        }
        protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
        {
            if (!IsComponentDocument(documentNode))
            {
                return;
            }

            var @namespace = documentNode.FindPrimaryNamespace();
            var @class     = documentNode.FindPrimaryClass();

            if (@namespace == null || @class == null)
            {
                // Nothing to do, bail. We can't function without the standard structure.
                return;
            }

            var references = documentNode.FindDescendantReferences <TagHelperDirectiveAttributeIntermediateNode>();

            for (var i = 0; i < references.Count; i++)
            {
                var reference = references[i];
                var node      = (TagHelperDirectiveAttributeIntermediateNode)reference.Node;

                if (node.TagHelper.IsRefTagHelper())
                {
                    // We've found an @ref directive attribute.
                    //
                    // If we can't get a nonempty identifier, do nothing because there will
                    // already be a diagnostic for empty values
                    var identifier = DetermineIdentifierToken(node);
                    if (identifier == null)
                    {
                        continue;
                    }

                    var rewritten = RewriteUsage(reference.Parent, identifier);
                    reference.Replace(rewritten);

                    // Now we need to check if the field generation has been suppressed.
                    //
                    // You have to suppress field generation for generic types because we don't know the
                    // type name to create the field.
                    var generateField = ShouldGenerateField(reference.Parent);

                    // Insert the field with other fields, near the top of the class.
                    if (generateField)
                    {
                        var position = 0;
                        while (position < @class.Children.Count && @class.Children[i] is FieldDeclarationIntermediateNode)
                        {
                            position++;
                        }

                        @class.Children.Insert(position, CreateField(rewritten.FieldTypeName, identifier));
                    }
                }
            }
        }
Exemplo n.º 5
0
        protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
        {
            if (!IsComponentDocument(documentNode))
            {
                return;
            }

            var @namespace = documentNode.FindPrimaryNamespace();
            var @class     = documentNode.FindPrimaryClass();

            if (@namespace == null || @class == null)
            {
                // Nothing to do, bail. We can't function without the standard structure.
                return;
            }

            // For each component *usage* we need to rewrite the tag helper node to map to the relevant component
            // APIs.
            var references = documentNode.FindDescendantReferences <TagHelperIntermediateNode>();

            for (var i = 0; i < references.Count; i++)
            {
                var reference = references[i];
                var node      = (TagHelperIntermediateNode)reference.Node;
                if (node.TagHelpers.Any(t => t.IsChildContentTagHelper()))
                {
                    // This is a child content tag helper. This will be rewritten when we visit its parent.
                    continue;
                }

                // The element didn't match any child content descriptors. Look for any matching component descriptors.
                var count = 0;
                for (var j = 0; j < node.TagHelpers.Count; j++)
                {
                    if (node.TagHelpers[j].IsComponentTagHelper())
                    {
                        // Only allow a single component tag helper per element. If there are multiple, we'll just consider
                        // the first one and ignore the others.
                        if (++count > 1)
                        {
                            node.Diagnostics.Add(ComponentDiagnosticFactory.Create_MultipleComponents(node.Source, node.TagName, node.TagHelpers));
                            break;
                        }
                    }
                }

                if (count >= 1)
                {
                    reference.Replace(RewriteAsComponent(node, node.TagHelpers.First(t => t.IsComponentTagHelper())));
                }
                else
                {
                    reference.Replace(RewriteAsElement(node));
                }
            }
        }
Exemplo n.º 6
0
        protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
        {
            if (!IsComponentDocument(documentNode))
            {
                return;
            }

            var @namespace = documentNode.FindPrimaryNamespace();
            var @class     = documentNode.FindPrimaryClass();

            if (@namespace == null || @class == null)
            {
                // Nothing to do, bail. We can't function without the standard structure.
                return;
            }

            // For each bind *usage* we need to rewrite the tag helper node to map to basic constructs.
            var references = documentNode.FindDescendantReferences <TagHelperPropertyIntermediateNode>();

            var parents = new HashSet <IntermediateNode>();

            for (var i = 0; i < references.Count; i++)
            {
                parents.Add(references[i].Parent);
            }

            foreach (var parent in parents)
            {
                ProcessDuplicates(parent);
            }

            for (var i = 0; i < references.Count; i++)
            {
                var reference = references[i];
                var node      = (TagHelperPropertyIntermediateNode)reference.Node;

                if (!reference.Parent.Children.Contains(node))
                {
                    // This node was removed as a duplicate, skip it.
                    continue;
                }

                if (node.TagHelper.IsBindTagHelper() && node.AttributeName.StartsWith("bind"))
                {
                    // Workaround for https://github.com/aspnet/Blazor/issues/703
                    var rewritten = RewriteUsage(reference.Parent, node);
                    reference.Remove();

                    for (var j = 0; j < rewritten.Length; j++)
                    {
                        reference.Parent.Children.Add(rewritten[j]);
                    }
                }
            }
        }
        protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
        {
            if (!IsComponentDocument(documentNode))
            {
                return;
            }

            var @namespace = documentNode.FindPrimaryNamespace();
            var @class     = documentNode.FindPrimaryClass();

            if (@namespace == null || @class == null)
            {
                // Nothing to do, bail. We can't function without the standard structure.
                return;
            }

            // For each event handler *usage* we need to rewrite the tag helper node to map to basic constructs.
            // Each usage will be represented by a tag helper property that is a descendant of either
            // a component or element.
            var references = documentNode.FindDescendantReferences <TagHelperPropertyIntermediateNode>();

            var parents = new HashSet <IntermediateNode>();

            for (var i = 0; i < references.Count; i++)
            {
                parents.Add(references[i].Parent);
            }

            foreach (var parent in parents)
            {
                ProcessDuplicates(parent);
            }

            for (var i = 0; i < references.Count; i++)
            {
                var reference = references[i];
                var node      = (TagHelperPropertyIntermediateNode)reference.Node;

                if (!reference.Parent.Children.Contains(node))
                {
                    // This node was removed as a duplicate, skip it.
                    continue;
                }

                if (node.TagHelper.IsEventHandlerTagHelper())
                {
                    reference.Replace(RewriteUsage(reference.Parent, node));
                }
            }
        }