-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
155 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
namespace BusinessCentral.LinterCop.Test; | ||
|
||
public class Rule0087 | ||
{ | ||
private string _testCaseDir = ""; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_testCaseDir = Path.Combine(Directory.GetParent(Environment.CurrentDirectory)!.Parent!.Parent!.FullName, | ||
"TestCases", "Rule0087"); | ||
} | ||
|
||
[Test] | ||
[TestCase("EmptyStringLiteral")] | ||
public async Task HasDiagnostic(string testCase) | ||
{ | ||
var code = await File.ReadAllTextAsync(Path.Combine(_testCaseDir, "HasDiagnostic", $"{testCase}.al")) | ||
.ConfigureAwait(false); | ||
|
||
var fixture = RoslynFixtureFactory.Create<Rule0087UseIsNullGuid>(); | ||
fixture.HasDiagnostic(code, DiagnosticDescriptors.Rule0087UseIsNullGuid.Id); | ||
} | ||
|
||
[Test] | ||
[TestCase("EmptyStringLiteral")] | ||
public async Task NoDiagnostic(string testCase) | ||
{ | ||
var code = await File.ReadAllTextAsync(Path.Combine(_testCaseDir, "NoDiagnostic", $"{testCase}.al")) | ||
.ConfigureAwait(false); | ||
|
||
var fixture = RoslynFixtureFactory.Create<Rule0087UseIsNullGuid>(); | ||
fixture.NoDiagnosticAtMarker(code, DiagnosticDescriptors.Rule0087UseIsNullGuid.Id); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
BusinessCentral.LinterCop.Test/TestCases/Rule0087/HasDiagnostic/EmptyStringLiteral.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyGuid: Guid; | ||
begin | ||
if MyGuid = [|''|] then; | ||
end; | ||
} |
9 changes: 9 additions & 0 deletions
9
BusinessCentral.LinterCop.Test/TestCases/Rule0087/NoDiagnostic/EmptyStringLiteral.al
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyGuid: Guid; | ||
begin | ||
MyGuid := [|''|]; | ||
end; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using BusinessCentral.LinterCop.Helpers; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Symbols; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Utilities; | ||
using System.Collections.Immutable; | ||
|
||
namespace BusinessCentral.LinterCop.Design; | ||
|
||
[DiagnosticAnalyzer] | ||
public class Rule0087UseIsNullGuid : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = | ||
ImmutableArray.Create(DiagnosticDescriptors.Rule0087UseIsNullGuid); | ||
|
||
public override void Initialize(AnalysisContext context) => | ||
context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeEqualsStatement), OperationKind.BinaryOperatorExpression); | ||
|
||
private void AnalyzeEqualsStatement(OperationAnalysisContext ctx) | ||
{ | ||
if (ctx.IsObsoletePendingOrRemoved() || ctx.Operation is not IBinaryOperatorExpression operation) | ||
return; | ||
|
||
// Validate the left operand is a Guid type | ||
if (!IsNavTypeKindGuid(operation.LeftOperand)) | ||
return; | ||
|
||
// Validate the right operand is an empty string literal | ||
if (IsEmptyStringLiteral(operation.RightOperand)) | ||
{ | ||
ctx.ReportDiagnostic(Diagnostic.Create( | ||
DiagnosticDescriptors.Rule0087UseIsNullGuid, | ||
operation.RightOperand.Syntax.GetLocation(), | ||
GetVariableName(operation.LeftOperand), | ||
"''")); | ||
} | ||
} | ||
|
||
private static bool IsNavTypeKindGuid(IOperation operand) | ||
{ | ||
// Check for a conversion expression with a valid Guid type | ||
if (operand.Kind == OperationKind.ConversionExpression && | ||
operand is IConversionExpression conversion && | ||
conversion.Operand is IOperation innerOperation) | ||
{ | ||
return innerOperation.Type.GetNavTypeKindSafe() == NavTypeKind.Guid; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static bool IsEmptyStringLiteral(IOperation operand) | ||
{ | ||
// Ensure the operand is a literal expression of a string type | ||
if (operand.Kind == OperationKind.LiteralExpression && operand.Type.IsTextType()) | ||
{ | ||
var constantValue = operand.ConstantValue.Value?.ToString(); | ||
return string.IsNullOrEmpty(constantValue); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private static string GetVariableName(IOperation operand) | ||
{ | ||
// Extract the name of the Guid variable, or return an empty string if unavailable | ||
if (operand.Kind == OperationKind.ConversionExpression && | ||
operand is IConversionExpression conversion && | ||
conversion.Operand is IOperation innerOperation) | ||
{ | ||
return innerOperation.GetSymbol()?.Name.QuoteIdentifierIfNeeded() ?? string.Empty; | ||
} | ||
|
||
return string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters