/// <summary> /// Analyzes command ast to check for new-object command and parse its arguments /// </summary> private void AnalyzeNewObjectCommand(CommandAst commandAst) { string typeName; List <string> argumentList; GetParametersFromCommandAst(commandAst, out typeName, out argumentList); if (typeName == null || !presetTypeNameSet.Contains(typeName)) { return; } if (argumentList != null && HasIgnoreCaseComparerArg(argumentList)) { return; } var dr = new DiagnosticRecord( Strings.UseLiteralInitilializerForHashtableDescription, commandAst.Extent, GetName(), GetDiagnosticSeverity(), fileName, ruleId: null, suggestedCorrections: GetSuggestedCorrections(commandAst, this.fileName)); diagnosticRecords.Add(dr); }
private void AddToDiagnosticRecords( DiagnosticRecord diagnosticRecord, ref List <DiagnosticRecord> diagnosticRecords) { if (diagnosticRecord != null) { diagnosticRecords.Add(diagnosticRecord); } }
public void HydrateArtifacts_Invalid_ScriptPath() { var artifacts = new HashSet <Artifact>(); var diagnosticRecord = new DiagnosticRecord() { ScriptPath = "This is invalid format" }; Assert.Throws <UriFormatException>(() => SARIFConverter.HydrateArtifacts(artifacts, diagnosticRecord)); }
public void HydrateArtifacts_Null_ScriptPath() { var artifacts = new HashSet <Artifact>(); var diagnosticRecord = new DiagnosticRecord() { ScriptPath = null }; Assert.Throws <ArgumentNullException>(() => SARIFConverter.HydrateArtifacts(artifacts, diagnosticRecord)); }
internal static ScriptFileMarker FromDiagnosticRecord( DiagnosticRecord diagnosticRecord) { Validate.IsNotNull("diagnosticRecord", diagnosticRecord); return(new ScriptFileMarker { Message = diagnosticRecord.Message, Level = GetMarkerLevelFromDiagnosticSeverity(diagnosticRecord.Severity), ScriptRegion = ScriptRegion.Create(diagnosticRecord.Extent) }); }
/// <summary> /// Analyzes the given ast to find violations. /// </summary> /// <param name="ast">AST to be analyzed. This should be non-null</param> /// <param name="fileName">Name of file that corresponds to the input AST.</param> /// <returns>A an enumerable type containing the violations</returns> public override IEnumerable <DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) { if (ast == null) { throw new ArgumentNullException(nameof(ast)); } var diagnosticRecords = new List <DiagnosticRecord>(); string[] lines = ast.Extent.Text.Split(s_lineSeparators, StringSplitOptions.None); for (int lineNumber = 0; lineNumber < lines.Length; lineNumber++) { string line = lines[lineNumber]; if (line.Length <= MaximumLineLength) { continue; } int startLine = lineNumber + 1; int endLine = startLine; int startColumn = 1; int endColumn = line.Length; var violationExtent = new ScriptExtent( new ScriptPosition( ast.Extent.File, startLine, startColumn, line ), new ScriptPosition( ast.Extent.File, endLine, endColumn, line )); var record = new DiagnosticRecord( String.Format(CultureInfo.CurrentCulture, String.Format(Strings.AvoidLongLinesError, MaximumLineLength)), violationExtent, GetName(), GetDiagnosticSeverity(), ast.Extent.File, null ); diagnosticRecords.Add(record); } return(diagnosticRecords); }
/// <summary> /// Creates a suppressed record based on a diagnostic record and the rule suppression /// </summary> /// <param name="record"></param> /// <param name="Suppression"></param> public SuppressedRecord(DiagnosticRecord record, RuleSuppression suppression) { Suppression = suppression; if (record != null) { RuleName = record.RuleName; Message = record.Message; Extent = record.Extent; Severity = record.Severity; ScriptName = record.ScriptName; RuleSuppressionID = record.RuleSuppressionID; } }
private DiagnosticRecord CreateDiagnosticRecord(string FunctionName, string PSVer, IScriptExtent ViolationExtent) { var record = new DiagnosticRecord( string.Format(CultureInfo.CurrentCulture, string.Format(Strings.AvoidOverwritingBuiltInCmdletsError, FunctionName, PSVer)), ViolationExtent, GetName(), GetDiagnosticSeverity(), ViolationExtent.File, null ); return(record); }
public void HydrateArtifacts_Added() { var artifacts = new HashSet <Artifact>(); var diagnosticRecord = new DiagnosticRecord() { ScriptPath = Directory.GetCurrentDirectory() }; SARIFConverter.HydrateArtifacts(artifacts, diagnosticRecord); var expectedArtifact = new Artifact() { Location = new ArtifactLocation() { Uri = new Uri(diagnosticRecord.ScriptPath) } }; Assert.Contains(expectedArtifact, artifacts, Artifact.ValueComparer); }
/// <summary> /// Checks if a hashtable is created using [hashtable]::new() /// </summary> public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressionAst methodCallAst) { if (methodCallAst == null) { return(AstVisitAction.SkipChildren); } var typeExprAst = methodCallAst.Expression as TypeExpressionAst; if (typeExprAst == null || !presetTypeNameSet.Contains(typeExprAst.TypeName.FullName)) { return(AstVisitAction.Continue); } var memberStringConstantExprAst = methodCallAst.Member as StringConstantExpressionAst; if (memberStringConstantExprAst == null || !memberStringConstantExprAst.Value.Equals("new", StringComparison.OrdinalIgnoreCase)) { return(AstVisitAction.Continue); } // no arguments provided to new OR one of the argument ends with ignorecase // (heuristics find to something like [system.stringcomparer]::ordinalignorecase) if (methodCallAst.Arguments == null || !HasIgnoreCaseComparerArg(methodCallAst.Arguments)) { var dr = new DiagnosticRecord( Strings.UseLiteralInitilializerForHashtableDescription, methodCallAst.Extent, GetName(), GetDiagnosticSeverity(), fileName, ruleId: null, suggestedCorrections: GetSuggestedCorrections(methodCallAst, this.fileName)); diagnosticRecords.Add(dr); } return(AstVisitAction.Continue); }
public void HydrateRules_NullOrEmptyRuleName(string ruleName) { var rules = new HashSet <ReportingDescriptor>(); var diagnosticRecord = new DiagnosticRecord() { RuleName = ruleName }; SARIFConverter.HydrateRules(rules, diagnosticRecord); // It should procude nullable or empty id object var expectedRule = new ReportingDescriptor() { Id = diagnosticRecord?.RuleName, Name = diagnosticRecord?.RuleName, HelpUri = new Uri("https://github.com/PowerShell/Psscriptanalyzer") }; Assert.Contains(expectedRule, rules, ReportingDescriptor.ValueComparer); }
public void HydrateRules_Added() { var rules = new HashSet <ReportingDescriptor>(); var diagnosticRecord = new DiagnosticRecord() { RuleName = "Write-Host" }; SARIFConverter.HydrateRules(rules, diagnosticRecord); var expectedRule = new ReportingDescriptor() { Id = diagnosticRecord?.RuleName, Name = diagnosticRecord?.RuleName, HelpUri = new Uri("https://github.com/PowerShell/Psscriptanalyzer") }; Assert.Contains(expectedRule, rules, ReportingDescriptor.ValueComparer); }
public void HydrateResults_Added() { var diagnosticRecord = new DiagnosticRecord() { Line = 1, Message = "Dont use Write-Host", RuleName = "Write-Host", ScriptPath = "C:\\users\\johnsmith", Severity = "Warning", Suppressions = new List <Suppression>() { new Suppression() { Justification = "suppress write-host1" }, new Suppression() { Justification = "suppress write-host2" } } }; var expectedResult = new Result() { RuleId = diagnosticRecord.RuleName, Level = FailureLevel.Warning, Message = new Message() { Text = diagnosticRecord.Message }, Locations = new List <Location>() { new Location() { PhysicalLocation = new PhysicalLocation() { ArtifactLocation = new ArtifactLocation() { Uri = new Uri(diagnosticRecord.ScriptPath) }, Region = new Region() { StartLine = diagnosticRecord.Line, EndColumn = 0 } } } }, Suppressions = new List <Suppression>() { new Suppression() { Justification = "suppress write-host1" }, new Suppression() { Justification = "suppress write-host2" } } }; var results = new List <Result>(); SARIFConverterMock.Setup(x => x.ResolveSeverity(diagnosticRecord.Severity)).CallBase(); SARIFConverter.HydrateResults(results, diagnosticRecord); SARIFConverterMock.Verify(x => x.ResolveSeverity(diagnosticRecord.Severity), Times.Once); Assert.Contains(expectedResult, results, Result.ValueComparer); }
public void HydrateRules_Null_Rules() { var diagnosticRecord = new DiagnosticRecord(); Assert.Throws <ArgumentNullException>(() => SARIFConverter.HydrateRules(null, diagnosticRecord)); }
public void HydrateArtifacts_Null_Artifacts() { var diagnosticError = new DiagnosticRecord(); Assert.Throws <ArgumentNullException>(() => SARIFConverter.HydrateArtifacts(null, diagnosticError)); }