-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge 'prerelease' into master (#885)
* Add support for Primary Key field other then of type Code (#875) * Allow Implicit conversion from Integer to Decimal (#876) * Resolve bug in Rule0027 (#877) * Resolve false positive on Rule0008 (#878) * Resolve InvalidCastException on Rule0053/0053 (#879) * New Rule0087: Use IsNullGuid (#882) * Change Guid to GUID on Documentation for Rule0087
- Loading branch information
Showing
20 changed files
with
255 additions
and
56 deletions.
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
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
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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
BusinessCentral.LinterCop.Test/TestCases/Rule0008/NoDiagnostic/2.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,20 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
Field: Record Field; | ||
begin | ||
[|Field.SetRange(FieldName, 'Document No.')|]; | ||
end; | ||
} | ||
|
||
table 50100 Field | ||
{ | ||
fields | ||
{ | ||
field(1; TableNo; Integer) { } | ||
field(2; "No."; Integer) { } | ||
field(3; TableName; Text[30]) { } | ||
field(4; FieldName; Text[30]) { } | ||
} | ||
} |
45 changes: 7 additions & 38 deletions
45
BusinessCentral.LinterCop.Test/TestCases/Rule0027/HasDiagnostic/1.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 |
---|---|---|
@@ -1,45 +1,14 @@ | ||
table 50100 MyTable | ||
{ | ||
LookupPageId = MyPage; | ||
DrillDownPageId = MyPage; | ||
|
||
fields | ||
{ | ||
field(1; MyField; Integer) | ||
{ | ||
|
||
} | ||
} | ||
|
||
keys | ||
{ | ||
key(Key1; MyField) { } | ||
} | ||
} | ||
|
||
page 50100 MyPage | ||
{ | ||
PageType = List; | ||
SourceTable = MyTable; | ||
|
||
layout | ||
{ | ||
area(Content) | ||
{ | ||
field(MyField; MyField) | ||
{ | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
codeunit 50100 MyCodeunit | ||
{ | ||
local procedure MyProcedure() | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
[|Page.Run(Page::MyPage, MyTable);|] | ||
[|Page.Run(0, MyTable)|]; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields { field(1; MyField; Integer) { } } | ||
} |
15 changes: 15 additions & 0 deletions
15
BusinessCentral.LinterCop.Test/TestCases/Rule0027/HasDiagnostic/2.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,15 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
SalesHeader: Record "Sales Header"; | ||
begin | ||
[|Page.Run(Page::MyPage, SalesHeader)|]; | ||
end; | ||
} | ||
|
||
page 50100 MyPage { } | ||
table 36 "Sales Header" | ||
{ | ||
fields { field(1; MyField; Integer) { } } | ||
} |
17 changes: 17 additions & 0 deletions
17
BusinessCentral.LinterCop.Test/TestCases/Rule0075/NoDiagnostic/PrimaryKeyAsInteger.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,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
[|MyTable.Get()|]; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; "Primary Key"; Integer) { } | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
BusinessCentral.LinterCop.Test/TestCases/Rule0075/NoDiagnostic/RecordGetDecimalToInteger.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,17 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure(MyInteger: Integer) | ||
var | ||
MyTabe: Record MyTabe; | ||
begin | ||
[|MyTabe.Get(MyInteger)|]; | ||
end; | ||
} | ||
|
||
table 50100 MyTabe | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Decimal) { } | ||
} | ||
} |
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
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
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
Oops, something went wrong.