<commit_before>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ExtractMethod;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.ExtractMethod
{
    internal static class Extensions
    {
        [return: NotNullIfNotNull("node")]
        public static ExpressionSyntax? GetUnparenthesizedExpression(this ExpressionSyntax? node)
        {
            if (!(node is ParenthesizedExpressionSyntax parenthesizedExpression))
            {
                return node;
            }

            return GetUnparenthesizedExpression(parenthesizedExpression.Expression);
        }

        public static StatementSyntax? GetStatementUnderContainer(this SyntaxNode node)
        {
            Contract.ThrowIfNull(node);

            for (var current = node; current is object; current = current.Parent)
            {
                if (current.Parent != null &&
                    current.Parent.IsStatementContainerNode())
                {
                    return current as StatementSyntax;
                }
            }

            return null;
        }

        public static StatementSyntax GetParentLabeledStatementIfPossible(this SyntaxNode node)
            => (StatementSyntax)((node.Parent is LabeledStatementSyntax) ? node.Parent : node);

        public static bool IsStatementContainerNode([NotNullWhen(returnValue: true)] this SyntaxNode? node)
            => node is BlockSyntax || node is SwitchSectionSyntax;

        public static BlockSyntax? GetBlockBody(this SyntaxNode? node)
        {
            switch (node)
            {
                case BaseMethodDeclarationSyntax m: return m.Body;
                case AccessorDeclarationSyntax a: return a.Body;
                case SimpleLambdaExpressionSyntax s: return s.Body as BlockSyntax;
                case ParenthesizedLambdaExpressionSyntax p: return p.Body as BlockSyntax;
                case AnonymousMethodExpressionSyntax a: return a.Block;
                default: return null;
            }
        }

        public static bool UnderValidContext(this SyntaxNode node)
        {
            Contract.ThrowIfNull(node);

            if (!node.GetAncestorsOrThis<SyntaxNode>().Any(predicate))
            {
                return false;
            }

            if (node.FromScript() || node.GetAncestor<TypeDeclarationSyntax>() != null)
            {
                return true;
            }

            return false;

            bool predicate(SyntaxNode n)
            {
                if (n is BaseMethodDeclarationSyntax or
                    AccessorDeclarationSyntax or
                    BlockSyntax or
                    GlobalStatementSyntax)
                {
                    return true;
                }

                if (n is ConstructorInitializerSyntax constructorInitializer)
                {
                    return constructorInitializer.ContainsInArgument(node.Span);
                }

                return false;
            }
        }

        public static bool ContainedInValidType(this SyntaxNode node)
        {
            Contract.ThrowIfNull(node);
            foreach (var ancestor in node.AncestorsAndSelf())
            {
                if (ancestor is TypeDeclarationSyntax)
                {
                    return true;
                }
                if (ancestor is NamespaceDeclarationSyntax)
                {
                    return false;
                }
            }

            return true;
        }

        public static bool UnderValidContext(this SyntaxToken token)
            => token.GetAncestors<SyntaxNode>().Any(n => n.CheckTopLevel(token.Span));<commit_msg>fixwarnign<commit_after>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ExtractMethod;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.CSharp.ExtractMethod
{
    internal static class Extensions
    {
        [return: NotNullIfNotNull("node")]
        public static ExpressionSyntax? GetUnparenthesizedExpression(this ExpressionSyntax? node)
        {
            if (!(node is ParenthesizedExpressionSyntax parenthesizedExpression))
            {
                return node;
            }

            return GetUnparenthesizedExpression(parenthesizedExpression.Expression);
        }

        public static StatementSyntax? GetStatementUnderContainer(this SyntaxNode node)
        {
            Contract.ThrowIfNull(node);

            for (var current = node; current is object; current = current.Parent)
            {
                if (current.Parent != null &&
                    current.Parent.IsStatementContainerNode())
                {
                    return current as StatementSyntax;
                }
            }

            return null;
        }

        public static StatementSyntax GetParentLabeledStatementIfPossible(this SyntaxNode node)
            => (StatementSyntax)((node.Parent is LabeledStatementSyntax) ? node.Parent : node);

        public static bool IsStatementContainerNode([NotNullWhen(returnValue: true)] this SyntaxNode? node)
            => node is BlockSyntax || node is SwitchSectionSyntax;

        public static BlockSyntax? GetBlockBody(this SyntaxNode? node)
        {
            switch (node)
            {
                case BaseMethodDeclarationSyntax m: return m.Body;
                case AccessorDeclarationSyntax a: return a.Body;
                case SimpleLambdaExpressionSyntax s: return s.Body as BlockSyntax;
                case ParenthesizedLambdaExpressionSyntax p: return p.Body as BlockSyntax;
                case AnonymousMethodExpressionSyntax a: return a.Block;
                default: return null;
            }
        }

        public static bool UnderValidContext(this SyntaxNode node)
        {
            Contract.ThrowIfNull(node);

            if (!node.GetAncestorsOrThis<SyntaxNode>().Any(predicate))
            {
                return false;
            }

            if (node.FromScript() || node.GetAncestor<TypeDeclarationSyntax>() != null)
            {
                return true;
            }

            return false;

            bool predicate(SyntaxNode n)
            {
                if (n is BaseMethodDeclarationSyntax or
                    AccessorDeclarationSyntax or
                    BlockSyntax or
                    GlobalStatementSyntax)
                {
                    return true;
                }

                if (n is ConstructorInitializerSyntax constructorInitializer)
                {
                    return constructorInitializer.ContainsInArgument(node.Span);
                }

                return false;
            }
        }

        public static bool ContainedInValidType(this SyntaxNode node)
        {
            Contract.ThrowIfNull(node);
            foreach (var ancestor in node.AncestorsAndSelf())
            {
                if (ancestor is TypeDeclarationSyntax)
                {
                    return true;
                }

                if (ancestor is NamespaceDeclarationSyntax)
                {
                    return false;
                }
            }

            return true;
        }

        public static bool UnderValidContext(this SyntaxToken token)
            => token.GetAncestors<SyntaxNode>().Any(n => n.CheckTopLevel(token.Span));
{'fraction_non_alphanumeric': 0.0645938780429019, 'fraction_numerical': 0.0, 'mean_word_length': 1.7867696440564138, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1084034', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1834, 'n_words': 493}