本文整理汇总了C#中Ast.FindAll方法的典型用法代码示例。如果您正苦于以下问题:C# Ast.FindAll方法的具体用法?C# Ast.FindAll怎么用?C# Ast.FindAll使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ast
的用法示例。
在下文中一共展示了Ast.FindAll方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AnalyzeScript
/// <summary>
/// AnalyzeScript: Analyzes the ast to check that $null is on the left side of any equality comparisons.
/// <param name="ast">The script's ast</param>
/// <param name="fileName">The script's file name</param>
/// <returns>The diagnostic results of this rule</returns>
/// </summary>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
IEnumerable<Ast> binExpressionAsts = ast.FindAll(testAst => testAst is BinaryExpressionAst, false);
foreach (BinaryExpressionAst binExpressionAst in binExpressionAsts) {
if ((binExpressionAst.Operator.Equals(TokenKind.Equals) || binExpressionAst.Operator.Equals(TokenKind.Ceq)
|| binExpressionAst.Operator.Equals(TokenKind.Cne) || binExpressionAst.Operator.Equals(TokenKind.Ine) || binExpressionAst.Operator.Equals(TokenKind.Ieq))
&& binExpressionAst.Right.Extent.Text.Equals("$null", StringComparison.OrdinalIgnoreCase))
{
if (IncorrectComparisonWithNull(binExpressionAst, ast))
{
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
IEnumerable<Ast> funcAsts = ast.FindAll(item => item is FunctionDefinitionAst, true).Union(ast.FindAll(item => item is FunctionMemberAst, true));
foreach (Ast funcAst in funcAsts)
{
IEnumerable<Ast> binAsts = funcAst.FindAll(item => item is BinaryExpressionAst, true);
foreach (BinaryExpressionAst binAst in binAsts)
{
if (IncorrectComparisonWithNull(binAst, funcAst))
{
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}
示例2: AnalyzeScript
/// <summary>
/// AnalyzeScript: Analyzes the ast to check that cmdlets that have a Credential parameter accept PSCredential.
/// </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);
IEnumerable<Ast> funcDefAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);
IEnumerable<Ast> scriptBlockAsts = ast.FindAll(testAst => testAst is ScriptBlockAst, true);
string funcName;
foreach (FunctionDefinitionAst funcDefAst in funcDefAsts)
{
funcName = funcDefAst.Name;
if (funcDefAst.Parameters != null)
{
foreach (ParameterAst parameter in funcDefAst.Parameters)
{
if (WrongCredentialUsage(parameter))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeError, funcName), funcDefAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
if (funcDefAst.Body.ParamBlock != null)
{
foreach (ParameterAst parameter in funcDefAst.Body.ParamBlock.Parameters)
{
if (WrongCredentialUsage(parameter))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeError, funcName), funcDefAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}
foreach (ScriptBlockAst scriptBlockAst in scriptBlockAsts)
{
// check for the case where it's parent is function, in that case we already processed above
if (scriptBlockAst.Parent != null && scriptBlockAst.Parent is FunctionDefinitionAst)
{
continue;
}
if (scriptBlockAst.ParamBlock != null && scriptBlockAst.ParamBlock.Parameters != null)
{
foreach (ParameterAst parameter in scriptBlockAst.ParamBlock.Parameters)
{
if (WrongCredentialUsage(parameter))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeErrorSB), scriptBlockAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}
}
示例3: AnalyzeScript
/// <summary>
/// AnalyzeScript: Analyzes the ast to check that cmdlets that have a Credential parameter accept PSCredential.
/// </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);
IEnumerable<Ast> funcDefAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);
IEnumerable<Ast> scriptBlockAsts = ast.FindAll(testAst => testAst is ScriptBlockAst, true);
string funcName;
foreach (FunctionDefinitionAst funcDefAst in funcDefAsts)
{
funcName = funcDefAst.Name;
if (funcDefAst.Parameters != null)
{
foreach (ParameterAst parameter in funcDefAst.Parameters)
{
if (parameter.Name.VariablePath.UserPath.Equals("Credential", StringComparison.OrdinalIgnoreCase) && parameter.StaticType != typeof(PSCredential))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeError, funcName), funcDefAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
if (funcDefAst.Body.ParamBlock != null)
{
foreach (ParameterAst parameter in funcDefAst.Body.ParamBlock.Parameters)
{
if (parameter.Name.VariablePath.UserPath.Equals("Credential", StringComparison.OrdinalIgnoreCase) && parameter.StaticType != typeof(PSCredential))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeError, funcName), funcDefAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}
foreach (ScriptBlockAst scriptBlockAst in scriptBlockAsts)
{
if (scriptBlockAst.ParamBlock != null && scriptBlockAst.ParamBlock.Parameters != null)
{
foreach (ParameterAst parameter in scriptBlockAst.ParamBlock.Parameters)
{
if (parameter.Name.VariablePath.UserPath.Equals("Credential", StringComparison.OrdinalIgnoreCase) && parameter.StaticType != typeof(PSCredential))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UsePSCredentialTypeErrorSB), scriptBlockAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}
}
示例4: AnalyzeScript
/// <summary>
/// AnalyzeScript: Analyzes the script to check if any non-constant members have been invoked.
/// </summary>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
IEnumerable<Ast> memberExpression = ast.FindAll(testAst => testAst is MemberExpressionAst, true);
foreach (MemberExpressionAst member in memberExpression)
{
string context = member.Member.Extent.ToString();
if (context.Contains("("))
{
//check if parenthesis and have non-constant members
IEnumerable<Ast> binaryExpression = member.FindAll(
binaryAst => binaryAst is BinaryExpressionAst, true);
if (binaryExpression.Any())
{
foreach (BinaryExpressionAst bin in binaryExpression)
{
if (!bin.Operator.Equals(null))
{
yield return
new DiagnosticRecord(
string.Format(CultureInfo.CurrentCulture,
Strings.AvoidInvokingEmptyMembersError,
context),
member.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}
}
}
示例5: AnalyzeScript
/// <summary>
/// AnalyzeScript: Check that cmdlets are invoked with the correct mandatory parameter
/// </summary>
/// <param name="ast"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
// Finds all CommandAsts.
IEnumerable<Ast> foundAsts = ast.FindAll(testAst => testAst is CommandAst, true);
// Iterates all CommandAsts and check the command name.
foreach (Ast foundAst in foundAsts)
{
CommandAst cmdAst = (CommandAst)foundAst;
// Handles the exception caused by commands like, {& $PLINK $args 2> $TempErrorFile}.
// You can also review the remark section in following document,
// MSDN: CommandAst.GetCommandName Method
if (cmdAst.GetCommandName() == null) continue;
// Checks mandatory parameters.
if (!IsMandatoryParameterExisted(cmdAst))
{
yield return new DiagnosticRecord(String.Format(CultureInfo.CurrentCulture, Strings.UseCmdletCorrectlyError, cmdAst.GetCommandName()),
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
示例6: AnalyzeScript
/// <summary>
/// Checks that all defined cmdlet use singular noun
/// </summary>
/// <param name="ast"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
if (ast == null) throw new ArgumentNullException(Strings.NullCommandInfoError);
IEnumerable<Ast> funcAsts = ast.FindAll(item => item is FunctionDefinitionAst, true);
char[] funcSeperator = { '-' };
string[] funcNamePieces = new string[2];
foreach (FunctionDefinitionAst funcAst in funcAsts)
{
if (funcAst.Name != null && funcAst.Name.Contains('-'))
{
funcNamePieces = funcAst.Name.Split(funcSeperator);
String noun = funcNamePieces[1];
var ps = System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us"));
if (!ps.IsSingular(noun) && ps.IsPlural(noun))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseSingularNounsError, funcAst.Name),
funcAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}
示例7: AnalyzeDSCResource
/// <summary>
/// AnalyzeDSCResource: Analyzes given DSC Resource
/// </summary>
/// <param name="ast">The script's ast</param>
/// <param name="fileName">The name of the script file being analyzed</param>
/// <returns>The results of the analysis</returns>
public IEnumerable<DiagnosticRecord> AnalyzeDSCResource(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
// Expected TargetResource functions in the DSC Resource module
List<string> expectedTargetResourceFunctionNames = new List<string>(new string[] { "Get-TargetResource", "Set-TargetResource", "Test-TargetResource" });
// Retrieve a list of Asts where the function name contains TargetResource
IEnumerable<Ast> functionDefinitionAsts = (ast.FindAll(dscAst => dscAst is FunctionDefinitionAst && ((dscAst as FunctionDefinitionAst).Name.IndexOf("targetResource", StringComparison.CurrentCultureIgnoreCase) != -1), true));
List<string> targetResourceFunctionNamesInAst = new List<string>();
foreach (FunctionDefinitionAst functionDefinitionAst in functionDefinitionAsts)
{
targetResourceFunctionNamesInAst.Add(functionDefinitionAst.Name);
}
foreach (string expectedTargetResourceFunctionName in expectedTargetResourceFunctionNames)
{
// If the Ast does not contain the expected functions, provide a Rule violation message
if (!targetResourceFunctionNamesInAst.Contains(expectedTargetResourceFunctionName, StringComparer.CurrentCultureIgnoreCase))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseStandardDSCFunctionsInResourceError, expectedTargetResourceFunctionName),
ast.Extent, GetName(), DiagnosticSeverity.Error, fileName);
}
}
}
示例8: AnalyzeDSCClass
/// <summary>
/// AnalyzeDSCClass: Analyzes dsc classes and the file and check that they have get, set and test
/// </summary>
/// <param name="ast"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public IEnumerable<DiagnosticRecord> AnalyzeDSCClass(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
List<string> resourceFunctionNames = new List<string>(new string[] {"Test", "Get", "Set"});
IEnumerable<Ast> dscClasses = ast.FindAll(item =>
item is TypeDefinitionAst
&& ((item as TypeDefinitionAst).IsClass)
&& (item as TypeDefinitionAst).Attributes.Any(attr => String.Equals("DSCResource", attr.TypeName.FullName, StringComparison.OrdinalIgnoreCase)), true);
foreach (TypeDefinitionAst dscClass in dscClasses)
{
IEnumerable<Ast> functions = dscClass.Members.Where(member => member is FunctionMemberAst);
foreach (string resourceFunctionName in resourceFunctionNames)
{
if (!functions.Any(function => String.Equals(resourceFunctionName, (function as FunctionMemberAst).Name)))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.UseStandardDSCFunctionsInClassError, resourceFunctionName),
dscClass.Extent, GetName(), DiagnosticSeverity.Error, fileName);
}
}
}
}
示例9: AnalyzeScript
/// <summary>
/// Analyze ast to check that all the cmdlet does not use reserved char
/// </summary>
/// <param name="ast"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
IEnumerable<Ast> funcAsts = ast.FindAll(item => item is FunctionDefinitionAst, true);
string reservedChars = Strings.ReserverCmdletChars;
HashSet<string> exportedFunction = Helper.Instance.GetExportedFunction(ast);
foreach (FunctionDefinitionAst funcAst in funcAsts)
{
if (funcAst.Body != null && funcAst.Body.ParamBlock != null && funcAst.Body.ParamBlock.Attributes != null)
{
// only raise this rule for function with cmdletbinding
if (!funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute)))
{
continue;
}
string funcName = Helper.Instance.FunctionNameWithoutScope(funcAst.Name);
// only raise if the function is exported
if (funcName != null && funcName.Intersect(reservedChars).Count() > 0 && (exportedFunction.Contains(funcAst.Name) || exportedFunction.Contains(funcName)))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ReservedCmdletCharError, funcAst.Name),
funcAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}
示例10: GetNodeLevelRequiredModules
private static List<string> GetNodeLevelRequiredModules(Ast ast)
{
IEnumerable<CommandAst> importAsts = ast.FindAll(IsCommandImportDscResource, true).OfType<CommandAst>();
List<string> modules = new List<string>();
foreach (CommandAst importAst in importAsts)
{
// TODO: refactor code to avoid calling a script, just use StaticBindingResult directly,
// once System.Management.Automation.dll version will be updated from 3.0.0.0.
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.AddScript(
@"function BindArguments($ast, $out)
{
$dic = ([System.Management.Automation.Language.StaticParameterBinder]::BindCommand($ast)).BoundParameters
foreach ($binding in $dic.GetEnumerator())
{
if ($binding.Key -like ""[N]*"") { $out.Add( (Get-DscResource $binding.Value.Value.Value).Module.Name ) }
else {if ($binding.Key -like ""[M]*"") { $out.Add( $binding.Value.Value.Value ) }}
}
}");
powerShell.Invoke();
powerShell.Commands.Clear();
powerShell.AddCommand("BindArguments").AddParameter("ast", importAst).AddParameter("out", modules);
powerShell.Invoke();
}
}
return modules;
}
示例11: AnalyzeScript
/// <summary>
/// AnalyzeScript: Analyzes the ast to check for reserved parameters in function definitions.
/// </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);
IEnumerable<Ast> funcAsts = ast.FindAll(item => item is FunctionDefinitionAst, true);
List<string> commonParamNames = typeof(CommonParameters).GetProperties().Select(param => param.Name).ToList();
foreach (FunctionDefinitionAst funcAst in funcAsts)
{
// this rule only applies to function with param block
if (funcAst.Body != null && funcAst.Body.ParamBlock != null
&& funcAst.Body.ParamBlock.Attributes != null && funcAst.Body.ParamBlock.Parameters != null)
{
// no cmdlet binding
if (!funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute)))
{
continue;
}
foreach (ParameterAst paramAst in funcAst.Body.ParamBlock.Parameters)
{
string paramName = paramAst.Name.VariablePath.UserPath;
if (commonParamNames.Contains(paramName, StringComparer.OrdinalIgnoreCase))
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.ReservedParamsError, funcAst.Name, paramName),
paramAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}
}
示例12: AnalyzeScript
/// <summary>
/// AnalyzeScript: Analyze the script to check if cmdlet alias is used.
/// </summary>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
// Finds all CommandAsts.
IEnumerable<Ast> foundAsts = ast.FindAll(testAst => testAst is CommandAst, true);
// Iterates all CommandAsts and check the command name.
foreach (Ast foundAst in foundAsts)
{
CommandAst cmdAst = (CommandAst)foundAst;
string aliasName = cmdAst.GetCommandName();
// Handles the exception caused by commands like, {& $PLINK $args 2> $TempErrorFile}.
// You can also review the remark section in following document,
// MSDN: CommandAst.GetCommandName Method
if (aliasName == null)
{
continue;
}
string cmdletName = Helper.Instance.GetCmdletNameFromAlias(aliasName);
if (!String.IsNullOrEmpty(cmdletName))
{
yield return new DiagnosticRecord(
string.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingCmdletAliasesError, aliasName, cmdletName),
cmdAst.Extent,
GetName(),
DiagnosticSeverity.Warning,
fileName,
aliasName,
suggestedCorrections: GetCorrectionExtent(cmdAst, cmdletName));
}
}
}
示例13: AnalyzeScript
/// <summary>
/// AnalyzeScript: Check if any uninitialized variable is used.
/// </summary>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
// Finds all functionAst
IEnumerable<Ast> functionAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);
foreach (FunctionDefinitionAst funcAst in functionAsts)
{
if (funcAst.Body != null && funcAst.Body.ParamBlock != null
&& funcAst.Body.ParamBlock.Attributes != null && funcAst.Body.ParamBlock.Parameters != null)
{
// only raise this rule for function with cmdletbinding
if (!funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute)))
{
continue;
}
foreach (var paramAst in funcAst.Body.ParamBlock.Parameters)
{
bool mandatory = false;
// check that param is mandatory
foreach (var paramAstAttribute in paramAst.Attributes)
{
if (paramAstAttribute is AttributeAst)
{
var namedArguments = (paramAstAttribute as AttributeAst).NamedArguments;
if (namedArguments != null)
{
foreach (NamedAttributeArgumentAst namedArgument in namedArguments)
{
if (String.Equals(namedArgument.ArgumentName, "mandatory", StringComparison.OrdinalIgnoreCase))
{
// 2 cases: [Parameter(Mandatory)] and [Parameter(Mandatory=$true)]
if (namedArgument.ExpressionOmitted || (!namedArgument.ExpressionOmitted && String.Equals(namedArgument.Argument.Extent.Text, "$true", StringComparison.OrdinalIgnoreCase)))
{
mandatory = true;
break;
}
}
}
}
}
}
if (!mandatory)
{
break;
}
if (paramAst.DefaultValue != null)
{
yield return new DiagnosticRecord(string.Format(CultureInfo.CurrentCulture, Strings.AvoidDefaultValueForMandatoryParameterError, paramAst.Name.VariablePath.UserPath),
paramAst.Name.Extent, GetName(), DiagnosticSeverity.Warning, fileName, paramAst.Name.VariablePath.UserPath);
}
}
}
}
}
示例14: AnalyzeScript
/// <summary>
/// AnalyzeScript: Avoid Using Get-WMIObject, Remove-WMIObject, Invoke-WmiMethod, Register-WmiEvent, Set-WmiInstance
/// </summary>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
// Rule is applicable only when PowerShell Version is < 3.0, since CIM cmdlet was introduced in 3.0
int majorPSVersion = GetPSMajorVersion(ast);
if (!(3 > majorPSVersion && 0 < majorPSVersion))
{
// Finds all CommandAsts.
IEnumerable<Ast> commandAsts = ast.FindAll(testAst => testAst is CommandAst, true);
// Iterate all CommandAsts and check the command name
foreach (CommandAst cmdAst in commandAsts)
{
if (cmdAst.GetCommandName() != null &&
(String.Equals(cmdAst.GetCommandName(), "get-wmiobject", StringComparison.OrdinalIgnoreCase)
|| String.Equals(cmdAst.GetCommandName(), "remove-wmiobject", StringComparison.OrdinalIgnoreCase)
|| String.Equals(cmdAst.GetCommandName(), "invoke-wmimethod", StringComparison.OrdinalIgnoreCase)
|| String.Equals(cmdAst.GetCommandName(), "register-wmievent", StringComparison.OrdinalIgnoreCase)
|| String.Equals(cmdAst.GetCommandName(), "set-wmiinstance", StringComparison.OrdinalIgnoreCase))
)
{
yield return new DiagnosticRecord(String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingWMICmdletError, System.IO.Path.GetFileName(fileName)),
cmdAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}
}
示例15: AnalyzeScript
/// <summary>
/// AvoidUsingPlainTextForPassword: Check that parameter "password", "passphrase" and do not use plaintext.
/// </summary>
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
// Finds all ParamAsts.
IEnumerable<Ast> paramAsts = ast.FindAll(testAst => testAst is ParameterAst, true);
List<String> passwords = new List<String>() {"Password", "Passphrase", "Auth", "Cred", "Credential"};
// Iterrates all ParamAsts and check if their names are on the list.
foreach (ParameterAst paramAst in paramAsts)
{
TypeInfo paramType = (TypeInfo) paramAst.StaticType;
bool hasPwd = false;
String paramName = paramAst.Name.VariablePath.ToString();
foreach (String password in passwords)
{
if (paramName.IndexOf(password, StringComparison.OrdinalIgnoreCase) != -1)
{
hasPwd = true;
break;
}
}
if (hasPwd && ((!paramType.IsArray && (paramType == typeof(String) || paramType == typeof(object)))
|| (paramType.IsArray && (paramType.GetElementType() == typeof(String) || paramType.GetElementType() == typeof(object)))))
{
yield return new DiagnosticRecord(
String.Format(CultureInfo.CurrentCulture, Strings.AvoidUsingPlainTextForPasswordError, paramAst.Name),
paramAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
}
}
}