-
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.
New Rule0074 FlowFilter Assignment (#820)
* New Rule0074 FlowFilter Assignment
- Loading branch information
Showing
8 changed files
with
172 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,36 @@ | ||
namespace BusinessCentral.LinterCop.Test; | ||
|
||
public class Rule0074 | ||
{ | ||
private string _testCaseDir = ""; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
_testCaseDir = Path.Combine(Directory.GetParent(Environment.CurrentDirectory)!.Parent!.Parent!.FullName, | ||
"TestCases", "Rule0074"); | ||
} | ||
|
||
[Test] | ||
[TestCase("AssignmentStatement")] | ||
[TestCase("CompoundAssignmentStatement")] | ||
public async Task HasDiagnostic(string testCase) | ||
{ | ||
var code = await File.ReadAllTextAsync(Path.Combine(_testCaseDir, "HasDiagnostic", $"{testCase}.al")) | ||
.ConfigureAwait(false); | ||
|
||
var fixture = RoslynFixtureFactory.Create<Rule0074FlowFilterAssignment>(); | ||
fixture.HasDiagnostic(code, Rule0074FlowFilterAssignment.DiagnosticDescriptors.Rule0074FlowFilterAssignment.Id); | ||
} | ||
|
||
[Test] | ||
[TestCase("SetRange")] | ||
public async Task NoDiagnostic(string testCase) | ||
{ | ||
var code = await File.ReadAllTextAsync(Path.Combine(_testCaseDir, "NoDiagnostic", $"{testCase}.al")) | ||
.ConfigureAwait(false); | ||
|
||
var fixture = RoslynFixtureFactory.Create<Rule0074FlowFilterAssignment>(); | ||
fixture.NoDiagnosticAtMarker(code, Rule0074FlowFilterAssignment.DiagnosticDescriptors.Rule0074FlowFilterAssignment.Id); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
BusinessCentral.LinterCop.Test/TestCases/Rule0074/HasDiagnostic/AssignmentStatement.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,21 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
MyTable.[|"My Filter"|] := '1'; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
field(2; "My Filter"; Code[10]) | ||
{ | ||
FieldClass = FlowFilter; | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...essCentral.LinterCop.Test/TestCases/Rule0074/HasDiagnostic/CompoundAssignmentStatement.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,21 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
MyTable.[|"My Filter"|] += 1; | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
field(2; "My Filter"; Integer) | ||
{ | ||
FieldClass = FlowFilter; | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
BusinessCentral.LinterCop.Test/TestCases/Rule0074/NoDiagnostic/SetRange.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,21 @@ | ||
codeunit 50100 MyCodeunit | ||
{ | ||
procedure MyProcedure() | ||
var | ||
MyTable: Record MyTable; | ||
begin | ||
MyTable.SetRange([|"My Filter"|], '1'); | ||
end; | ||
} | ||
|
||
table 50100 MyTable | ||
{ | ||
fields | ||
{ | ||
field(1; MyField; Integer) { } | ||
field(2; "My Filter"; Code[10]) | ||
{ | ||
FieldClass = FlowFilter; | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
BusinessCentral.LinterCop/Design/Rule0074FlowFilterAssignment.cs
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,57 @@ | ||
using BusinessCentral.LinterCop.AnalysisContextExtension; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Syntax; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Utilities; | ||
using System.Collections.Immutable; | ||
|
||
namespace BusinessCentral.LinterCop.Design; | ||
|
||
[DiagnosticAnalyzer] | ||
public class Rule0074FlowFilterAssignment : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0074FlowFilterAssignment); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.RegisterSyntaxNodeAction(AnalyzeAssignmentStatement, SyntaxKind.AssignmentStatement, SyntaxKind.CompoundAssignmentStatement); | ||
} | ||
|
||
private void AnalyzeAssignmentStatement(SyntaxNodeAnalysisContext ctx) | ||
{ | ||
if (ctx.CancellationToken.IsCancellationRequested || ctx.IsObsoletePendingOrRemoved()) | ||
return; | ||
|
||
var target = ctx.Node switch | ||
{ | ||
AssignmentStatementSyntax assignment => assignment.Target, | ||
CompoundAssignmentStatementSyntax compoundAssignment => compoundAssignment.Target, | ||
_ => null | ||
}; | ||
|
||
if (target is not { Kind: SyntaxKind.MemberAccessExpression }) | ||
return; | ||
|
||
if (ctx.SemanticModel.GetSymbolInfo(target, ctx.CancellationToken).Symbol is not IFieldSymbol fieldSymbol) | ||
return; | ||
|
||
if (fieldSymbol.FieldClass == FieldClassKind.FlowFilter) | ||
{ | ||
ctx.ReportDiagnostic(Diagnostic.Create( | ||
DiagnosticDescriptors.Rule0074FlowFilterAssignment, | ||
target.GetIdentifierNameSyntax().GetLocation(), new object[] { fieldSymbol.Name.QuoteIdentifierIfNeeded() })); | ||
} | ||
} | ||
|
||
public static class DiagnosticDescriptors | ||
{ | ||
public static readonly DiagnosticDescriptor Rule0074FlowFilterAssignment = new( | ||
id: LinterCopAnalyzers.AnalyzerPrefix + "0074", | ||
title: LinterCopAnalyzers.GetLocalizableString("Rule0074FlowFilterAssignmentTitle"), | ||
messageFormat: LinterCopAnalyzers.GetLocalizableString("Rule0074FlowFilterAssignmentFormat"), | ||
category: "Design", | ||
defaultSeverity: DiagnosticSeverity.Warning, isEnabledByDefault: true, | ||
description: LinterCopAnalyzers.GetLocalizableString("Rule0074FlowFilterAssignmentDescription"), | ||
helpLinkUri: "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0074"); | ||
} | ||
} |
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