本文整理汇总了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));
}
}
}
}
}
示例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);
}
}
}
示例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;
}