Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PossibleIncorrectComparisonWithNull rule return a SuggestCorrection for auto-fixes in VS-Code or via the -Fix switch #1115

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Rules/PossibleIncorrectComparisonWithNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
{
if (IncorrectComparisonWithNull(binExpressionAst, ast))
{
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binExpressionAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName,
null, suggestedCorrections: GetCorrectionExtent(binExpressionAst));
}
}
}
Expand All @@ -60,7 +61,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) {
{
if (IncorrectComparisonWithNull(binAst, funcAst))
{
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName);
yield return new DiagnosticRecord(Strings.PossibleIncorrectComparisonWithNullError, binAst.Extent, GetName(), DiagnosticSeverity.Warning, fileName,
null, suggestedCorrections: GetCorrectionExtent(binAst));
}
}
}
Expand Down Expand Up @@ -101,6 +103,21 @@ private bool IncorrectComparisonWithNull(BinaryExpressionAst binExpressionAst, A
return false;
}

private IEnumerable<CorrectionExtent> GetCorrectionExtent(BinaryExpressionAst binaryExpressionAst)
{
var correction = new CorrectionExtent(
binaryExpressionAst.Extent.StartLineNumber,
binaryExpressionAst.Extent.EndLineNumber,
binaryExpressionAst.Extent.StartColumnNumber,
binaryExpressionAst.Extent.EndColumnNumber,
$"{binaryExpressionAst.Right.Extent.Text} {binaryExpressionAst.ErrorPosition.Text} {binaryExpressionAst.Left.Extent.Text}",
binaryExpressionAst.Extent.File,
Strings.PossibleIncorrectComparisonWithNullSuggesteCorrectionDescription
);

yield return correction;
}

/// <summary>
/// GetName: Retrieves the name of this rule.
/// </summary>
Expand Down
13 changes: 11 additions & 2 deletions Rules/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Rules/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -990,4 +990,7 @@
<data name="PossibleIncorrectUsageOfRedirectionOperatorName" xml:space="preserve">
<value>PossibleIncorrectUsageOfRedirectionOperator</value>
</data>
<data name="PossibleIncorrectComparisonWithNullSuggesteCorrectionDescription" xml:space="preserve">
<value>Use $null on the left hand side for safe comparison with $null.</value>
</data>
</root>
4 changes: 4 additions & 0 deletions Tests/Rules/PossibleIncorrectComparisonWithNull.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Describe "PossibleIncorrectComparisonWithNull" {
It "has the correct description message" {
$violations.Message | Should -Match $violationMessage
}

It "has the correct description message" {
$violations[0].SuggestedCorrections[0].Text | Should -Be '$null -eq @("dfd", "eee")'
}
}

Context "When there are no violations" {
Expand Down