-
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.
Merge pull request #423 from StefanMaron/prerelease
Merge from 'Prerelease' into master
- Loading branch information
Showing
9 changed files
with
313 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Symbols; | ||
|
||
namespace BusinessCentral.LinterCop.Design | ||
{ | ||
[DiagnosticAnalyzer] | ||
public class Rule0040ExplicitlySetRunTrigger : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0040ExplicitlySetRunTrigger); | ||
|
||
private static readonly List<string> buildInMethodNames = new List<string> | ||
{ | ||
"insert", | ||
"modify", | ||
"modifyall", | ||
"delete", | ||
"deleteall" | ||
}; | ||
|
||
public override void Initialize(AnalysisContext context) => context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeRunTriggerParameters), OperationKind.InvocationExpression); | ||
|
||
private void AnalyzeRunTriggerParameters(OperationAnalysisContext ctx) | ||
{ | ||
if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return; | ||
if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return; | ||
|
||
IInvocationExpression operation = (IInvocationExpression)ctx.Operation; | ||
if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return; | ||
if (!(operation.Instance?.GetSymbol().GetTypeSymbol().GetNavTypeKindSafe() == NavTypeKind.Record || operation.Instance?.GetSymbol().GetTypeSymbol().GetNavTypeKindSafe() == NavTypeKind.RecordRef)) return; | ||
if (!buildInMethodNames.Contains(operation.TargetMethod.Name.ToLowerInvariant())) return; | ||
|
||
if (operation.Arguments.Where(args => SemanticFacts.IsSameName(args.Parameter.Name, "RunTrigger")).SingleOrDefault() == null) | ||
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0040ExplicitlySetRunTrigger, ctx.Operation.Syntax.GetLocation())); | ||
} | ||
} | ||
} |
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,62 @@ | ||
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 Rule0041EmptyCaptionLocked : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(DiagnosticDescriptors.Rule0041EmptyCaptionLocked); | ||
|
||
// List based on https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/properties/devenv-caption-property | ||
public override void Initialize(AnalysisContext context) | ||
=> context.RegisterSyntaxNodeAction(new Action<SyntaxNodeAnalysisContext>(AnalyzeCaptionProperty), new SyntaxKind[] { | ||
SyntaxKind.TableObject, | ||
SyntaxKind.Field, // TableField | ||
SyntaxKind.PageField, | ||
SyntaxKind.PageGroup, | ||
SyntaxKind.PageObject, | ||
SyntaxKind.RequestPage, | ||
SyntaxKind.PageLabel, | ||
SyntaxKind.PageGroup, | ||
SyntaxKind.PagePart, | ||
SyntaxKind.PageSystemPart, | ||
SyntaxKind.PageAction, | ||
SyntaxKind.PageActionSeparator, | ||
SyntaxKind.PageActionGroup, | ||
SyntaxKind.XmlPortObject, | ||
SyntaxKind.ReportObject, | ||
SyntaxKind.QueryObject, | ||
SyntaxKind.QueryColumn, | ||
SyntaxKind.QueryFilter, | ||
SyntaxKind.ReportColumn, | ||
SyntaxKind.EnumValue, | ||
SyntaxKind.PageCustomAction, | ||
SyntaxKind.PageSystemAction, | ||
SyntaxKind.PageView, | ||
SyntaxKind.ReportLayout, | ||
SyntaxKind.ProfileObject, | ||
SyntaxKind.EnumType, | ||
SyntaxKind.PermissionSet, | ||
SyntaxKind.TableExtensionObject, | ||
SyntaxKind.PageExtensionObject | ||
}); | ||
|
||
private void AnalyzeCaptionProperty(SyntaxNodeAnalysisContext ctx) | ||
{ | ||
if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return; | ||
if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return; | ||
|
||
if (ctx.Node.IsKind(SyntaxKind.EnumValue) && ctx.ContainingSymbol.Kind == SymbolKind.Enum) return; // Prevent double raising the rule on EnumValue in a EnumObject | ||
|
||
LabelPropertyValueSyntax captionProperty = ctx.Node?.GetProperty("Caption")?.Value as LabelPropertyValueSyntax; | ||
if (captionProperty?.Value.LabelText.GetLiteralValue() == null || captionProperty.Value.LabelText.GetLiteralValue().ToString().Trim() != "") return; | ||
|
||
if (captionProperty.Value.Properties?.Values.Where(prop => prop.Identifier.Text.ToLowerInvariant() == "locked").FirstOrDefault() != null) return; | ||
|
||
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0041EmptyCaptionLocked, captionProperty.GetLocation())); | ||
} | ||
} | ||
} |
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,29 @@ | ||
using Microsoft.Dynamics.Nav.CodeAnalysis; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics; | ||
using System.Collections.Immutable; | ||
|
||
namespace BusinessCentral.LinterCop.Design | ||
{ | ||
[DiagnosticAnalyzer] | ||
public class Rule0042AutoCalcFieldsOnNormalFields : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(DiagnosticDescriptors.Rule0042AutoCalcFieldsOnNormalFields); | ||
|
||
public override void Initialize(AnalysisContext context) => context.RegisterSyntaxNodeAction(syntaxContext => | ||
{ | ||
if (!syntaxContext.Node.ToString().ToLowerInvariant().Contains("setautocalcfields")) | ||
return; | ||
|
||
IInvocationExpression operation = (IInvocationExpression)syntaxContext.SemanticModel.GetOperation(syntaxContext.Node); | ||
IMethodSymbol targetMethod = operation.TargetMethod; | ||
if (targetMethod == null || !SemanticFacts.IsSameName(targetMethod.Name, "setautocalcfields") || targetMethod.MethodKind != MethodKind.BuiltInMethod) | ||
return; | ||
|
||
foreach (IArgument obj in operation.Arguments) | ||
{ | ||
if ((obj.Value is IConversionExpression conversionExpression2 ? conversionExpression2.Operand : (IOperation)null) is IFieldAccess fieldAccess2 && fieldAccess2.FieldSymbol.FieldClass != FieldClassKind.FlowField && fieldAccess2.Type.NavTypeKind != NavTypeKind.Blob) | ||
syntaxContext.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0042AutoCalcFieldsOnNormalFields, fieldAccess2.Syntax.GetLocation(), (object)fieldAccess2.FieldSymbol.Name)); | ||
} | ||
}, SyntaxKind.InvocationExpression); | ||
} | ||
} |
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,74 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics; | ||
using Microsoft.Dynamics.Nav.CodeAnalysis.Symbols; | ||
|
||
namespace BusinessCentral.LinterCop.Design | ||
{ | ||
[DiagnosticAnalyzer] | ||
public class Rule0043SecretText : DiagnosticAnalyzer | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0043SecretText); | ||
|
||
private static readonly string authorization = "Authorization"; | ||
private static readonly List<string> buildInMethodNames = new List<string> | ||
{ | ||
"add", | ||
"getvalues", | ||
"tryaddwithoutvalidation" | ||
}; | ||
|
||
public override void Initialize(AnalysisContext context) => context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeHttpObjects), OperationKind.InvocationExpression); | ||
|
||
private void AnalyzeHttpObjects(OperationAnalysisContext ctx) | ||
{ | ||
if (!VersionChecker.IsSupported(ctx.ContainingSymbol, VersionCompatibility.Fall2023OrGreater)) return; | ||
|
||
if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return; | ||
if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return; | ||
|
||
IInvocationExpression operation = (IInvocationExpression)ctx.Operation; | ||
|
||
// We need at least two arguments | ||
if (operation.Arguments.Count() < 2) return; | ||
|
||
switch (operation.TargetMethod.MethodKind) | ||
{ | ||
case MethodKind.BuiltInMethod: | ||
if (!buildInMethodNames.Contains(operation.TargetMethod.Name.ToLowerInvariant())) return; | ||
if (!(operation.Instance?.GetSymbol().GetTypeSymbol().GetNavTypeKindSafe() == NavTypeKind.HttpHeaders || operation.Instance?.GetSymbol().GetTypeSymbol().GetNavTypeKindSafe() == NavTypeKind.HttpClient)) return; | ||
break; | ||
case MethodKind.Method: | ||
if (operation.TargetMethod.ContainingType.GetNavTypeKindSafe() != NavTypeKind.Codeunit) return; | ||
ICodeunitTypeSymbol codeunitTypeSymbol = (ICodeunitTypeSymbol)operation.TargetMethod.GetContainingObjectTypeSymbol(); | ||
if (!SemanticFacts.IsSameName(((INamespaceSymbol)codeunitTypeSymbol.ContainingSymbol).QualifiedName, "System.RestClient")) return; | ||
if (!SemanticFacts.IsSameName(codeunitTypeSymbol.Name, "Rest Client")) return; | ||
if (!SemanticFacts.IsSameName(operation.TargetMethod.Name, "SetDefaultRequestHeader")) return; | ||
break; | ||
default: | ||
return; | ||
} | ||
|
||
if (!IsAuthorizationArgument(operation.Arguments[0])) return; | ||
|
||
if (operation.Arguments[1].Parameter.OriginalDefinition.GetTypeSymbol().GetNavTypeKindSafe() != NavTypeKind.SecretText) | ||
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0043SecretText, ctx.Operation.Syntax.GetLocation())); | ||
} | ||
|
||
private static bool IsAuthorizationArgument(IArgument argument) | ||
{ | ||
switch (argument.Syntax.Kind) | ||
{ | ||
case SyntaxKind.LiteralExpression: | ||
return SemanticFacts.IsSameName(argument.Value.ConstantValue.Value.ToString(), authorization); | ||
case SyntaxKind.IdentifierName: | ||
IOperation operand = ((IConversionExpression)argument.Value).Operand; | ||
if (operand.GetSymbol().OriginalDefinition.GetTypeSymbol().GetNavTypeKindSafe() != NavTypeKind.Label) return false; | ||
ILabelTypeSymbol label = (ILabelTypeSymbol)operand.GetSymbol().OriginalDefinition.GetTypeSymbol(); | ||
return SemanticFacts.IsSameName(label.GetLabelText(), authorization); | ||
default: | ||
return false; | ||
} | ||
} | ||
} | ||
} |
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.