コード例 #1
0
        private static IReadOnlyList <IntermediateToken> GetAttributeContent(IntermediateNode node)
        {
            var nodes    = node.FindDescendantNodes <TemplateIntermediateNode>();
            var template = nodes.Count > 0 ? nodes[0] : default;

            if (template != null)
            {
                // See comments in TemplateDiagnosticPass
                node.Diagnostics.Add(ComponentDiagnosticFactory.Create_TemplateInvalidLocation(template.Source));
                return(new[] { new IntermediateToken()
                               {
                                   Kind = TokenKind.CSharp, Content = string.Empty,
                               }, });
            }

            if (node.Children.Count == 1 && node.Children[0] is HtmlContentIntermediateNode htmlContentNode)
            {
                // This case can be hit for a 'string' attribute. We want to turn it into
                // an expression.
                var tokens = htmlContentNode.FindDescendantNodes <IntermediateToken>();

                var content = "\"" + string.Join(string.Empty, tokens.Select(t => t.Content.Replace("\"", "\\\""))) + "\"";
                return(new[] { new IntermediateToken()
                               {
                                   Content = content, Kind = TokenKind.CSharp,
                               } });
            }
            else
            {
                return(node.FindDescendantNodes <IntermediateToken>());
            }
        }
コード例 #2
0
        private bool ShouldGenerateField(IntermediateNode parent)
        {
            var parameters = parent.FindDescendantNodes <TagHelperDirectiveAttributeParameterIntermediateNode>();

            for (var i = 0; i < parameters.Count; i++)
            {
                var parameter = parameters[i];
                if (parameter.TagHelper.IsRefTagHelper() && parameter.BoundAttributeParameter.Name == "suppressField")
                {
                    if (parameter.HasDiagnostics)
                    {
                        parent.Diagnostics.AddRange(parameter.GetAllDiagnostics());
                    }

                    parent.Children.Remove(parameter);

                    if (parameter.AttributeStructure == AttributeStructure.Minimized)
                    {
                        return(false);
                    }

                    // We do not support non-minimized attributes here because we can't allow the value to be dynamic.
                    // As a design/experience decision, we don't let you write @ref:suppressField="false" even though
                    // we could parse it. The rationale is that it's misleading, you type something that looks like code,
                    // but it's not really.
                    parent.Diagnostics.Add(ComponentDiagnosticFactory.Create_RefSuppressFieldNotMinimized(parameter.Source));
                }
            }

            if (parent is ComponentIntermediateNode component && component.Component.IsGenericTypedComponent())
            {
                // We cannot automatically generate a 'ref' field for generic components because we don't know
                // how to write the type.
                parent.Diagnostics.Add(ComponentDiagnosticFactory.Create_RefSuppressFieldRequiredForGeneric(parent.Source));
                return(false);
            }

            return(true);
        }
コード例 #3
0
        public static RazorDiagnostic Create_UnsupportedComplexContent(IntermediateNode node, string attributeName)
        {
            var content = string.Join("", node.FindDescendantNodes <IntermediateToken>().Select(t => t.Content));

            return(RazorDiagnostic.Create(UnsupportedComplexContent, node.Source ?? SourceSpan.Undefined, attributeName, content));
        }