public override void VisitRazorDirective(RazorDirectiveSyntax node)
            {
                if (node != null && node.DirectiveDescriptor == NamespaceDirective.Directive)
                {
                    var directiveContent = node.Body?.GetContent();

                    // In practice, this should never be null and always start with 'namespace'. Just being defensive here.
                    if (directiveContent != null && directiveContent.StartsWith(NamespaceDirective.Directive.Directive, StringComparison.Ordinal))
                    {
                        LastNamespaceContent  = directiveContent.Substring(NamespaceDirective.Directive.Directive.Length).Trim();
                        LastNamespaceLocation = node.GetSourceSpan(_source);
                    }
                }

                base.VisitRazorDirective(node);
            }
示例#2
0
        public override void VisitRazorDirective(RazorDirectiveSyntax node)
        {
            var descendantLiterals = node.DescendantNodes();

            foreach (var child in descendantLiterals)
            {
                if (child is not CSharpStatementLiteralSyntax literal)
                {
                    continue;
                }

                var context = literal.GetSpanContext();
                if (context == null)
                {
                    // We can't find a chunk generator.
                    continue;
                }
                else if (context.ChunkGenerator is AddTagHelperChunkGenerator addTagHelper)
                {
                    // Make sure this node exists in the file we're parsing and not in its imports.
                    if (_filePath.Equals(_source.FilePath, StringComparison.Ordinal))
                    {
                        addTagHelper.Diagnostics.Add(
                            ComponentDiagnosticFactory.Create_UnsupportedTagHelperDirective(node.GetSourceSpan(_source)));
                    }
                }
                else if (context.ChunkGenerator is RemoveTagHelperChunkGenerator removeTagHelper)
                {
                    // Make sure this node exists in the file we're parsing and not in its imports.
                    if (_filePath.Equals(_source.FilePath, StringComparison.Ordinal))
                    {
                        removeTagHelper.Diagnostics.Add(
                            ComponentDiagnosticFactory.Create_UnsupportedTagHelperDirective(node.GetSourceSpan(_source)));
                    }
                }
                else if (context.ChunkGenerator is TagHelperPrefixDirectiveChunkGenerator tagHelperPrefix)
                {
                    // Make sure this node exists in the file we're parsing and not in its imports.
                    if (_filePath.Equals(_source.FilePath, StringComparison.Ordinal))
                    {
                        tagHelperPrefix.Diagnostics.Add(
                            ComponentDiagnosticFactory.Create_UnsupportedTagHelperDirective(node.GetSourceSpan(_source)));
                    }
                }
                else if (context.ChunkGenerator is AddImportChunkGenerator usingStatement && !usingStatement.IsStatic)
                {
                    // Get the namespace from the using statement.
                    var @namespace = usingStatement.ParsedNamespace;
                    if (@namespace.IndexOf('=') != -1)
                    {
                        // We don't support usings with alias.
                        continue;
                    }

                    for (var i = 0; _notFullyQualifiedComponents is not null && i < _notFullyQualifiedComponents.Count; i++)
                    {
                        var tagHelper = _notFullyQualifiedComponents[i];
                        Debug.Assert(!tagHelper.IsComponentFullyQualifiedNameMatch(), "We've already processed these.");

                        if (tagHelper.IsChildContentTagHelper())
                        {
                            // If this is a child content tag helper, we want to add it if it's original type is in scope of the given namespace.
                            // E.g, if the type name is `Test.MyComponent.ChildContent`, we want to add it if `Test.MyComponent` is in this namespace.
                            TrySplitNamespaceAndType(tagHelper, out var typeName);
                            if (!typeName.IsEmpty && IsTypeInNamespace(typeName, @namespace))
                            {
                                Matches.Add(tagHelper);
                            }
                        }
                        else if (IsTypeInNamespace(tagHelper, @namespace))
                        {
                            // If the type is at the top-level or if the type's namespace matches the using's namespace, add it.
                            Matches.Add(tagHelper);
                        }
                    }
                }
            }
        }