Skip to content

Commit

Permalink
Merge pull request #320 from StefanMaron/Rule0029CompareDateTimeThrou…
Browse files Browse the repository at this point in the history
…ghCodeunit

Rule0029 Use CompareDateTime method in Type Helper codeunit for DateTime variable comparisons.
  • Loading branch information
Arthurvdv authored Nov 4, 2023
2 parents ce5929a + 45df520 commit d0146fb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Design/Rule0029CompareDateTimeThroughCodeunit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Dynamics.Nav.Analyzers.Common.AppSourceCopConfiguration;
using Microsoft.Dynamics.Nav.CodeAnalysis;
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics;
using Microsoft.Dynamics.Nav.CodeAnalysis.Syntax;
using System.Collections.Immutable;

namespace BusinessCentral.LinterCop.Design
{
[DiagnosticAnalyzer]
public class Rule0029CompareDateTimeThroughCodeunit : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0029CompareDateTimeThroughCodeunit);

public override void Initialize(AnalysisContext context) => context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.CompareDateTimeWithTypeHelper), OperationKind.BinaryOperatorExpression);

private void CompareDateTimeWithTypeHelper(OperationAnalysisContext context)
{
if (context.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || context.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return;
if (context.ContainingSymbol.IsObsoletePending || context.ContainingSymbol.IsObsoleteRemoved) return;

IBinaryOperatorExpression operation = (IBinaryOperatorExpression)context.Operation;

if (operation.LeftOperand.Type.NavTypeKind == NavTypeKind.DateTime && operation.RightOperand.Type.NavTypeKind == NavTypeKind.DateTime &&
(operation.Syntax.IsKind(SyntaxKind.EqualsExpression) ||
operation.Syntax.IsKind(SyntaxKind.NotEqualsExpression) ||
operation.Syntax.IsKind(SyntaxKind.GreaterThanExpression) ||
operation.Syntax.IsKind(SyntaxKind.GreaterThanOrEqualExpression) ||
operation.Syntax.IsKind(SyntaxKind.LessThanExpression) ||
operation.Syntax.IsKind(SyntaxKind.LessThanOrEqualExpression)))
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0029CompareDateTimeThroughCodeunit, context.Operation.Syntax.GetLocation()));
}
}
}
5 changes: 5 additions & 0 deletions LinterCop.ruleset.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
"id": "LC0028",
"action": "Info",
"justification": "Event subscriber arguments now use identifier syntax instead of string literals. Convert the argument literal to an identifier."
},
{
"id": "LC0029",
"action": "Info",
"justification": "Use CompareDateTime method in Type Helper codeunit for DateTime variable comparisons."
}
]
}
1 change: 1 addition & 0 deletions LinterCopAnalyzers.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public static class DiagnosticDescriptors
public static readonly DiagnosticDescriptor Rule0022GlobalLanguageImplementTranslationHelper = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0022", (LocalizableString)new LocalizableResourceString("Rule0022GlobalLanguageImplementTranslationHelperTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0022GlobalLanguageImplementTranslationHelperFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0022GlobalLanguageImplementTranslationHelperDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0022");
public static readonly DiagnosticDescriptor Rule0023AlwaysSpecifyFieldgroups = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0023", (LocalizableString)new LocalizableResourceString("Rule0023AlwaysSpecifyFieldgroups", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0023AlwaysSpecifyFieldgroups", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0023AlwaysSpecifyFieldgroups", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0023");
public static readonly DiagnosticDescriptor Rule0028CodeNavigabilityOnEventSubscribers = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0028", (LocalizableString)new LocalizableResourceString("Rule0028CodeNavigabilityOnEventSubscribersTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0028CodeNavigabilityOnEventSubscribersFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0028CodeNavigabilityOnEventSubscribersDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0028");
public static readonly DiagnosticDescriptor Rule0029CompareDateTimeThroughCodeunit = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0029", (LocalizableString)new LocalizableResourceString("Rule0029CompareDateTimeThroughCodeunitTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0029CompareDateTimeThroughCodeunitFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0029CompareDateTimeThroughCodeunitDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0029");
}
}
12 changes: 12 additions & 0 deletions LinterCopAnalyzers.resx
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,16 @@
<value>Event subscriber arguments now use identifier syntax instead of string literals. Convert the argument literal to an identifier.</value>
<comment/>
</data>
<data name="Rule0029CompareDateTimeThroughCodeunitTitle" xml:space="preserve">
<value>Use CompareDateTime method in Type Helper codeunit for DateTime variable comparisons.</value>
<comment/>
</data>
<data name="Rule0029CompareDateTimeThroughCodeunitFormat" xml:space="preserve">
<value>Use CompareDateTime method in Type Helper codeunit for DateTime variable comparisons.</value>
<comment/>
</data>
<data name="Rule0029CompareDateTimeThroughCodeunitDescription" xml:space="preserve">
<value>Use CompareDateTime method in Type Helper codeunit for DateTime variable comparisons.</value>
<comment/>
</data>
</root>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Further note that you should have BcContainerHelper version 2.0.16 (or newer) in
|[LC0022](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0022)|`GlobalLanguage()` must be implemented through the `Translation Helper` codeunit from the Base Application.|Info|
|[LC0023](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0023)|Always provide fieldsgroups `DropDown` and `Brick` on tables.|Info|
|[LC0028](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0028)|Event subscriber arguments now use identifier syntax instead of string literals. Convert the argument literal to an identifier.|Info|
|[LC0029](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0029)|Use `CompareDateTime` method in `Type Helper` codeunit for DateTime variable comparisons.|Info|


## Configuration

Expand Down

0 comments on commit d0146fb

Please sign in to comment.