示例#1
0
        public static SyntaxToken GetReplacementToken(SyntaxTrivia comment)
        {
            SyntaxDebug.Assert(CSharpFacts.IsCommentTrivia(comment.Kind()), comment);

            SyntaxToken token = comment.Token;

            int index = token.LeadingTrivia.IndexOf(comment);

            if (index != -1)
            {
                if (comment.IsKind(SyntaxKind.SingleLineCommentTrivia))
                {
                    return(token.WithLeadingTrivia(RemoveTrivia(token.LeadingTrivia, index)));
                }

                return(token.WithLeadingTrivia(token.LeadingTrivia.RemoveAt(index)));
            }

            index = token.TrailingTrivia.IndexOf(comment);

            if (index != -1)
            {
                if (comment.IsKind(SyntaxKind.SingleLineCommentTrivia))
                {
                    return(token.WithTrailingTrivia(RemoveTrivia(token.TrailingTrivia, index)));
                }

                return(token.WithTrailingTrivia(token.TrailingTrivia.RemoveAt(index)));
            }

            Debug.Fail("comment trivia not found");

            return(token);
        }
            public override Accessibility GetDefaultAccessibility(AccessorDeclarationSyntax declaration)
            {
                if (declaration == null)
                {
                    throw new ArgumentNullException(nameof(declaration));
                }

                SyntaxNode containingDeclaration = declaration.Parent?.Parent;

                switch (containingDeclaration?.Kind())
                {
                case SyntaxKind.PropertyDeclaration:
                    return(SyntaxAccessibility <PropertyDeclarationSyntax> .Instance.GetDefaultAccessibility((PropertyDeclarationSyntax)containingDeclaration));

                case SyntaxKind.IndexerDeclaration:
                    return(SyntaxAccessibility <IndexerDeclarationSyntax> .Instance.GetDefaultAccessibility((IndexerDeclarationSyntax)containingDeclaration));

                case SyntaxKind.EventDeclaration:
                    return(SyntaxAccessibility <EventDeclarationSyntax> .Instance.GetDefaultAccessibility((EventDeclarationSyntax)containingDeclaration));
                }

                SyntaxDebug.Assert(containingDeclaration == null, containingDeclaration);

                return(Accessibility.NotApplicable);
            }
        public void Append(InterpolatedStringExpressionSyntax interpolatedString)
        {
            if (interpolatedString == null)
            {
                return;
            }

            if (!IsInterpolated)
            {
                throw new ArgumentException("", nameof(interpolatedString));
            }

            bool isVerbatim = interpolatedString.IsVerbatim();

            foreach (InterpolatedStringContentSyntax content in interpolatedString.Contents)
            {
                SyntaxDebug.Assert(content.IsKind(SyntaxKind.Interpolation, SyntaxKind.InterpolatedStringText), content);

                switch (content.Kind())
                {
                case SyntaxKind.Interpolation:
                {
                    StringBuilder.Append(content.ToFullString());
                    break;
                }

                case SyntaxKind.InterpolatedStringText:
                {
                    var interpolatedText = (InterpolatedStringTextSyntax)content;

                    if (IsVerbatim == isVerbatim)
                    {
                        StringBuilder.Append(interpolatedText.TextToken.Text);
                    }
                    else
                    {
                        Append(interpolatedText.TextToken.ValueText);
                    }

                    break;
                }
                }
            }
        }
示例#4
0
        /// <summary>
        /// Return true if the specified declaration is publicly visible.
        /// </summary>
        /// <param name="declaration"></param>
        public static bool IsPubliclyVisible(MemberDeclarationSyntax declaration)
        {
            if (declaration == null)
            {
                throw new ArgumentNullException(nameof(declaration));
            }

            do
            {
                if (declaration.IsKind(SyntaxKind.NamespaceDeclaration))
                {
                    return(true);
                }

                if (!GetAccessibility(declaration).Is(
                        Accessibility.Public,
                        Accessibility.Protected,
                        Accessibility.ProtectedOrInternal))
                {
                    return(false);
                }

                SyntaxNode parent = declaration.Parent;

                if (parent == null)
                {
                    return(true);
                }

                if (parent is ICompilationUnitSyntax)
                {
                    return(true);
                }

                SyntaxDebug.Assert(parent is MemberDeclarationSyntax, parent);

                declaration = parent as MemberDeclarationSyntax;
            }while (declaration != null);

            return(false);
        }
示例#5
0
        /// <summary>
        /// Returns true if the node can have specified accessibility.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="accessibility"></param>
        /// <param name="ignoreOverride">Ignore "override" modifier.</param>
        public static bool IsValidAccessibility(SyntaxNode node, Accessibility accessibility, bool ignoreOverride = false)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            switch (node.Parent?.Kind())
            {
            case SyntaxKind.NamespaceDeclaration:
            case SyntaxKind.CompilationUnit:
            {
                return(accessibility.Is(Accessibility.Public, Accessibility.Internal));
            }

            case SyntaxKind.StructDeclaration:
            case SyntaxKind.RecordStructDeclaration:
            {
                if (accessibility.ContainsProtected())
                {
                    return(false);
                }

                break;
            }
            }

            switch (node.Kind())
            {
            case SyntaxKind.ClassDeclaration:
            case SyntaxKind.InterfaceDeclaration:
            case SyntaxKind.RecordDeclaration:
            case SyntaxKind.StructDeclaration:
            case SyntaxKind.RecordStructDeclaration:
            case SyntaxKind.EnumDeclaration:
            {
                return(true);
            }

            case SyntaxKind.EventDeclaration:
            {
                var eventDeclaration = (EventDeclarationSyntax)node;

                ModifierFilter filter = SyntaxInfo.ModifierListInfo(eventDeclaration).GetFilter();

                return((ignoreOverride || !filter.HasAnyFlag(ModifierFilter.Override)) &&
                       (accessibility != Accessibility.Private || !filter.HasAnyFlag(ModifierFilter.AbstractVirtualOverride)) &&
                       CheckProtectedInStaticOrSealedClass(node) &&
                       CheckAccessorAccessibility(eventDeclaration.AccessorList));
            }

            case SyntaxKind.IndexerDeclaration:
            {
                var indexerDeclaration = (IndexerDeclarationSyntax)node;

                ModifierFilter filter = SyntaxInfo.ModifierListInfo(indexerDeclaration).GetFilter();

                return((ignoreOverride || !filter.HasAnyFlag(ModifierFilter.Override)) &&
                       (accessibility != Accessibility.Private || !filter.HasAnyFlag(ModifierFilter.AbstractVirtualOverride)) &&
                       CheckProtectedInStaticOrSealedClass(node) &&
                       CheckAccessorAccessibility(indexerDeclaration.AccessorList));
            }

            case SyntaxKind.PropertyDeclaration:
            {
                var propertyDeclaration = (PropertyDeclarationSyntax)node;

                ModifierFilter filter = SyntaxInfo.ModifierListInfo(propertyDeclaration).GetFilter();

                return((ignoreOverride || !filter.HasAnyFlag(ModifierFilter.Override)) &&
                       (accessibility != Accessibility.Private || !filter.HasAnyFlag(ModifierFilter.AbstractVirtualOverride)) &&
                       CheckProtectedInStaticOrSealedClass(node) &&
                       CheckAccessorAccessibility(propertyDeclaration.AccessorList));
            }

            case SyntaxKind.MethodDeclaration:
            {
                var methodDeclaration = (MethodDeclarationSyntax)node;

                ModifierFilter filter = SyntaxInfo.ModifierListInfo(methodDeclaration).GetFilter();

                return((ignoreOverride || !filter.HasAnyFlag(ModifierFilter.Override)) &&
                       (accessibility != Accessibility.Private || !filter.HasAnyFlag(ModifierFilter.AbstractVirtualOverride)) &&
                       CheckProtectedInStaticOrSealedClass(node));
            }

            case SyntaxKind.EventFieldDeclaration:
            {
                var eventFieldDeclaration = (EventFieldDeclarationSyntax)node;

                ModifierFilter filter = SyntaxInfo.ModifierListInfo(eventFieldDeclaration).GetFilter();

                return((ignoreOverride || !filter.HasAnyFlag(ModifierFilter.Override)) &&
                       (accessibility != Accessibility.Private || !filter.HasAnyFlag(ModifierFilter.AbstractVirtualOverride)) &&
                       CheckProtectedInStaticOrSealedClass(node));
            }

            case SyntaxKind.ConstructorDeclaration:
            case SyntaxKind.DelegateDeclaration:
            case SyntaxKind.FieldDeclaration:
            case SyntaxKind.IncompleteMember:
            {
                return(CheckProtectedInStaticOrSealedClass(node));
            }

            case SyntaxKind.OperatorDeclaration:
            case SyntaxKind.ConversionOperatorDeclaration:
            {
                return(accessibility == Accessibility.Public);
            }

            case SyntaxKind.GetAccessorDeclaration:
            case SyntaxKind.SetAccessorDeclaration:
            case SyntaxKind.AddAccessorDeclaration:
            case SyntaxKind.RemoveAccessorDeclaration:
            case SyntaxKind.InitAccessorDeclaration:
            case SyntaxKind.UnknownAccessorDeclaration:
            {
                var memberDeclaration = node.Parent?.Parent as MemberDeclarationSyntax;

                SyntaxDebug.Assert(memberDeclaration != null, node);

                if (memberDeclaration != null)
                {
                    if (!CheckProtectedInStaticOrSealedClass(memberDeclaration))
                    {
                        return(false);
                    }

                    return(accessibility.IsMoreRestrictiveThan(GetAccessibility(memberDeclaration)));
                }

                return(false);
            }

            case SyntaxKind.LocalFunctionStatement:
            {
                return(false);
            }

            default:
            {
                SyntaxDebug.Fail(node);
                return(false);
            }
            }

            bool CheckProtectedInStaticOrSealedClass(SyntaxNode declaration)
            {
                return(!accessibility.ContainsProtected() ||
                       (declaration.Parent as ClassDeclarationSyntax)?
                       .Modifiers
                       .ContainsAny(SyntaxKind.StaticKeyword, SyntaxKind.SealedKeyword) != true);
            }

            bool CheckAccessorAccessibility(AccessorListSyntax accessorList)
            {
                if (accessorList != null)
                {
                    foreach (AccessorDeclarationSyntax accessor in accessorList.Accessors)
                    {
                        Accessibility accessorAccessibility = GetExplicitAccessibility(accessor.Modifiers);

                        if (accessorAccessibility != Accessibility.NotApplicable)
                        {
                            return(accessorAccessibility.IsMoreRestrictiveThan(accessibility));
                        }
                    }
                }

                return(true);
            }
        }
        /// <summary>
        /// Create a new document with the specified member declaration removed.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="member"></param>
        /// <param name="cancellationToken"></param>
        internal static Task <Document> RemoveMemberAsync(
            this Document document,
            MemberDeclarationSyntax member,
            CancellationToken cancellationToken = default)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            if (member == null)
            {
                throw new ArgumentNullException(nameof(member));
            }

            SyntaxNode parent = member.Parent;

            switch (parent?.Kind())
            {
            case SyntaxKind.CompilationUnit:
            {
                var compilationUnit = (CompilationUnitSyntax)parent;

                return(document.ReplaceNodeAsync(compilationUnit, SyntaxRefactorings.RemoveMember(compilationUnit, member), cancellationToken));
            }

            case SyntaxKind.NamespaceDeclaration:
            case SyntaxKind.FileScopedNamespaceDeclaration:
            {
                var namespaceDeclaration = (BaseNamespaceDeclarationSyntax)parent;

                return(document.ReplaceNodeAsync(namespaceDeclaration, SyntaxRefactorings.RemoveMember(namespaceDeclaration, member), cancellationToken));
            }

            case SyntaxKind.ClassDeclaration:
            {
                var classDeclaration = (ClassDeclarationSyntax)parent;

                return(document.ReplaceNodeAsync(classDeclaration, SyntaxRefactorings.RemoveMember(classDeclaration, member), cancellationToken));
            }

            case SyntaxKind.StructDeclaration:
            {
                var structDeclaration = (StructDeclarationSyntax)parent;

                return(document.ReplaceNodeAsync(structDeclaration, SyntaxRefactorings.RemoveMember(structDeclaration, member), cancellationToken));
            }

            case SyntaxKind.InterfaceDeclaration:
            {
                var interfaceDeclaration = (InterfaceDeclarationSyntax)parent;

                return(document.ReplaceNodeAsync(interfaceDeclaration, SyntaxRefactorings.RemoveMember(interfaceDeclaration, member), cancellationToken));
            }

            case SyntaxKind.RecordDeclaration:
            case SyntaxKind.RecordStructDeclaration:
            {
                var recordDeclaration = (RecordDeclarationSyntax)parent;

                return(document.ReplaceNodeAsync(recordDeclaration, SyntaxRefactorings.RemoveMember(recordDeclaration, member), cancellationToken));
            }

            default:
            {
                SyntaxDebug.Assert(parent == null, parent);

                return(document.RemoveNodeAsync(member, SyntaxRefactorings.DefaultRemoveOptions, cancellationToken));
            }
            }
        }