当前位置: 首页>>代码示例>>C#>>正文


C# Ast.Find方法代码示例

本文整理汇总了C#中Ast.Find方法的典型用法代码示例。如果您正苦于以下问题:C# Ast.Find方法的具体用法?C# Ast.Find怎么用?C# Ast.Find使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Ast的用法示例。


在下文中一共展示了Ast.Find方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AnalyzeScript

        /// <summary>
        /// AnalyzeScript: Run Test Module Manifest to check that none of the required fields are missing. From the ILintScriptRule interface.
        /// </summary>
        /// <param name="ast">The script's ast</param>
        /// <param name="fileName">The script's file name</param>
        /// <returns>A List of diagnostic results of this rule</returns>
        public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
        {
            if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);

            if (String.Equals(System.IO.Path.GetExtension(fileName), ".psd1", StringComparison.OrdinalIgnoreCase))
            {
                IEnumerable<ErrorRecord> errorRecords;
                var psModuleInfo = Helper.Instance.GetModuleManifest(fileName, out errorRecords);
                if (errorRecords != null)
                {
                    foreach (var errorRecord in errorRecords)
                    {
                        if (Helper.IsMissingManifestMemberException(errorRecord))
                        {
                            System.Diagnostics.Debug.Assert(
                                errorRecord.Exception != null && !String.IsNullOrWhiteSpace(errorRecord.Exception.Message),
                                Strings.NullErrorMessage);
                            var hashTableAst = ast.Find(x => x is HashtableAst, false);
                            if (hashTableAst == null)
                            {
                                yield break;
                            }
                            yield return new DiagnosticRecord(
                                errorRecord.Exception.Message,
                                hashTableAst.Extent,
                                GetName(),
                                DiagnosticSeverity.Warning,
                                fileName,
                                suggestedCorrections:GetCorrectionExtent(hashTableAst as HashtableAst));
                        }

                    }
                }
            }
        }
开发者ID:kilasuit,项目名称:PSScriptAnalyzer,代码行数:41,代码来源:MissingModuleManifestField.cs

示例2: AnalyzeScript

        /// <summary>
        /// AnalyzeScript: Analyzes the AST to check if AliasToExport, CmdletsToExport, FunctionsToExport 
        /// and VariablesToExport fields do not use wildcards and $null in their entries. 
        /// </summary>
        /// <param name="ast">The script's ast</param>
        /// <param name="fileName">The script's file name</param>
        /// <returns>A List of diagnostic results of this rule</returns>
        public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
        {
            if (ast == null)
            {
                throw new ArgumentNullException(Strings.NullAstErrorMessage);
            }

            if (fileName == null || !fileName.EndsWith(".psd1", StringComparison.OrdinalIgnoreCase))
            {
                yield break;
            }

            if (!IsValidManifest(ast, fileName))
            {
                yield break;
            }

            String[] manifestFields = {"FunctionsToExport", "CmdletsToExport", "VariablesToExport", "AliasesToExport"};
            var hashtableAst = ast.Find(x => x is HashtableAst, false) as HashtableAst;

            if (hashtableAst == null)
            {
                yield break;
            }

            foreach(String field in manifestFields)
            {
                IScriptExtent extent;
                if (!HasAcceptableExportField(field, hashtableAst, ast.Extent.Text, out extent) && extent != null)
                {
                    yield return new DiagnosticRecord(GetError(field), extent, GetName(), DiagnosticSeverity.Warning, fileName);
                }
            }
        }
开发者ID:rkeithhill,项目名称:PSScriptAnalyzer,代码行数:41,代码来源:UseToExportFieldsInManifest.cs

示例3: CheckSuspiciousContent

        // Quick check for script blocks that may have suspicious content. If this
        // is true, we log them to the event log despite event log settings.
        //
        // Performance notes:
        // 
        // For the current number of search terms, the this approach is about as high
        // performance as we can get. It adds about 1ms to the invocation of a script
        // block (we don't do this at parse time).
        // The manual tokenization is much faster than either Regex.Split
        // or a series of String.Split calls. Lookups in the HashSet are much faster
        // than a ton of calls to String.IndexOf (which .NET implements in native code).
        //
        // If we were to expand this set of keywords much farther, it would make sense
        // to look into implementing the Aho-Corasick algorithm (the one used by antimalware
        // engines), but Aho-Corasick is slower than the current approach for relatively
        // small match sets.
        internal static string CheckSuspiciousContent(Ast scriptBlockAst)
        {
            // Split the script block text into an array of elements that have
            // a-Z A-Z dash.
            string scriptBlockText = scriptBlockAst.Extent.Text;
            IEnumerable<string> elements = TokenizeWordElements(scriptBlockText);

            // First check for plain-text signatures
            ParallelOptions parallelOptions = new ParallelOptions();
            string foundSignature = null;

            Parallel.ForEach(elements, parallelOptions, (element, loopState) =>
            {
                if (foundSignature == null)
                {
                    if (s_signatures.Contains(element))
                    {
                        foundSignature = element;
                        loopState.Break();
                    }
                }
            });

            if (!String.IsNullOrEmpty(foundSignature))
            {
                return foundSignature;
            }

            if (scriptBlockAst.HasSuspiciousContent)
            {
                Ast foundAst = scriptBlockAst.Find(ast =>
                {
                    // Try to find the lowest AST that was not considered suspicious, but its parent
                    // was.
                    return (!ast.HasSuspiciousContent) && (ast.Parent.HasSuspiciousContent);
                }, true);

                if (foundAst != null)
                {
                    return foundAst.Parent.Extent.Text;
                }
                else
                {
                    return scriptBlockAst.Extent.Text;
                }
            }

            return null;
        }
开发者ID:40a,项目名称:PowerShell,代码行数:65,代码来源:CompiledScriptBlock.cs


注:本文中的Ast.Find方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。