Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rule0058PageVariableMethodOnTemporaryTable #659

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions Design/Rule0039ArgumentDifferentTypeThenExpected.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ namespace BusinessCentral.LinterCop.Design
[DiagnosticAnalyzer]
public class Rule0039ArgumentDifferentTypeThenExpected : DiagnosticAnalyzer
{
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, DiagnosticDescriptors.Rule0049PageWithoutSourceTable);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, DiagnosticDescriptors.Rule0049PageWithoutSourceTable, DiagnosticDescriptors.Rule0058PageVariableMethodOnTemporaryTable);

private static readonly string[] pageProcedureNames = ["GetRecord", "SetRecord", "SetSelectionFilter", "SetTableView"];
private static readonly string[] pageRunProcedureNames = ["Run", "RunModal"];

private static readonly List<PropertyKind> referencePageProviders = new List<PropertyKind>
{
Expand All @@ -34,8 +37,7 @@ private void AnalyzeRunPageArguments(OperationAnalysisContext ctx)
if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return;

if (operation.TargetMethod.ContainingType.GetTypeSymbol().GetNavTypeKindSafe() != NavTypeKind.Page) return;
string[] procedureNames = { "Run", "RunModal" };
if (!procedureNames.Contains(operation.TargetMethod.Name)) return;
if (!pageRunProcedureNames.Contains(operation.TargetMethod.Name)) return;
if (operation.Arguments.Count() < 2) return;

if (operation.Arguments[0].Syntax.Kind != SyntaxKind.OptionAccessExpression) return;
Expand All @@ -50,7 +52,7 @@ private void AnalyzeRunPageArguments(OperationAnalysisContext ctx)
ITableTypeSymbol recordArgument = ((IRecordTypeSymbol)operand.GetSymbol().GetTypeSymbol()).BaseTable;

if (!AreTheSameNavObjects(recordArgument, pageSourceTable))
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, ctx.Operation.Syntax.GetLocation(), new object[] { 2, operand.GetSymbol().GetTypeSymbol().ToString(), pageSourceTable.GetNavTypeKindSafe() + " \"" + pageSourceTable.Name + "\"" }));
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, ctx.Operation.Syntax.GetLocation(), new object[] { 2, operand.GetSymbol().GetTypeSymbol().ToString(), pageSourceTable.GetNavTypeKindSafe() + pageSourceTable.Name.QuoteIdentifierIfNeeded() }));
}

private void AnalyzeSetRecordArgument(OperationAnalysisContext ctx)
Expand All @@ -61,8 +63,7 @@ private void AnalyzeSetRecordArgument(OperationAnalysisContext ctx)
if (operation.TargetMethod.MethodKind != MethodKind.BuiltInMethod) return;

if (operation.TargetMethod.ContainingType.GetTypeSymbol().GetNavTypeKindSafe() != NavTypeKind.Page) return;
string[] procedureNames = { "GetRecord", "SetRecord", "SetSelectionFilter", "SetTableView" };
if (!procedureNames.Contains(operation.TargetMethod.Name)) return;
if (!pageProcedureNames.Contains(operation.TargetMethod.Name)) return;
if (operation.Arguments.Count() != 1) return;

if (operation.Arguments[0].Syntax.Kind != SyntaxKind.IdentifierName || operation.Arguments[0].Value.Kind != OperationKind.ConversionExpression) return;
Expand All @@ -78,14 +79,16 @@ private void AnalyzeSetRecordArgument(OperationAnalysisContext ctx)
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0049PageWithoutSourceTable, ctx.Operation.Syntax.GetLocation(), new object[] { NavTypeKind.Page, GetFullyQualifiedObjectName(pageTypeSymbol) }));
return;
}
ITableTypeSymbol pageSourceTable = pageTypeSymbol.RelatedTable;

IOperation operand = ((IConversionExpression)operation.Arguments[0].Value).Operand;
ITypeSymbol typeSymbol = operand.GetSymbol().GetTypeSymbol();
if (typeSymbol.GetNavTypeKindSafe() != NavTypeKind.Record)
IRecordTypeSymbol recordTypeSymbol = operand.GetSymbol().GetTypeSymbol() as IRecordTypeSymbol;
if (recordTypeSymbol.Temporary && SemanticFacts.IsSameName(operation.TargetMethod.Name, "SetRecord"))
{
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0058PageVariableMethodOnTemporaryTable, ctx.Operation.Syntax.GetLocation(), new object[] { variableSymbol.ToString().QuoteIdentifierIfNeeded(), operation.TargetMethod.Name }));
return;

ITableTypeSymbol recordArgument = ((IRecordTypeSymbol)typeSymbol).BaseTable;
}
ITableTypeSymbol pageSourceTable = pageTypeSymbol.RelatedTable;
ITableTypeSymbol recordArgument = recordTypeSymbol.BaseTable;

if (!AreTheSameNavObjects(recordArgument, pageSourceTable))
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0039ArgumentDifferentTypeThenExpected, ctx.Operation.Syntax.GetLocation(), new object[] { 1, operand.GetSymbol().GetTypeSymbol().ToString(), pageSourceTable.GetNavTypeKindSafe().ToString() + ' ' + pageSourceTable.Name.QuoteIdentifierIfNeeded() }));
Expand Down
5 changes: 5 additions & 0 deletions LinterCop.ruleset.json
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@
"id": "LC0057",
"action": "Info",
"justification": "Enum values must have non-empty a Caption to be selectable in the client."
},
{
"id": "LC0058",
"action": "Warning",
"justification": "PageVariable.SetRecord(): You cannot use a temporary record for the Record parameter."
}
]
}
1 change: 1 addition & 0 deletions LinterCopAnalyzers.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ public static class DiagnosticDescriptors
public static readonly DiagnosticDescriptor Rule0055TokSuffixForTokenLabels = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0055", (LocalizableString)new LocalizableResourceString("Rule0055TokSuffixForTokenLabelsTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0055TokSuffixForTokenLabelsFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0055TokSuffixForTokenLabelsDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0055");
public static readonly DiagnosticDescriptor Rule0056EmptyEnumValueWithCaption = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0056", (LocalizableString)new LocalizableResourceString("Rule0056EmptyEnumValueWithCaptionTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0056EmptyEnumValueWithCaptionFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0056EmptyEnumValueWithCaptionDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0056");
public static readonly DiagnosticDescriptor Rule0057EnumValueWithEmptyCaption = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0057", (LocalizableString)new LocalizableResourceString("Rule0057EnumValueWithEmptyCaptionTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0057EnumValueWithEmptyCaptionFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Info, true, (LocalizableString)new LocalizableResourceString("Rule0057EnumValueWithEmptyCaptionDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0057");
public static readonly DiagnosticDescriptor Rule0058PageVariableMethodOnTemporaryTable = new DiagnosticDescriptor(LinterCopAnalyzers.AnalyzerPrefix + "0058", (LocalizableString)new LocalizableResourceString("Rule0058PageVariableMethodOnTemporaryTableTitle", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), (LocalizableString)new LocalizableResourceString("Rule0058PageVariableMethodOnTemporaryTableFormat", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "Design", DiagnosticSeverity.Warning, true, (LocalizableString)new LocalizableResourceString("Rule0058PageVariableMethodOnTemporaryTableDescription", LinterCopAnalyzers.ResourceManager, typeof(LinterCopAnalyzers)), "https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0058");
}
}
6 changes: 3 additions & 3 deletions LinterCopAnalyzers.resx
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,12 @@
<value>Enum values must have non-empty a Caption to be selectable in the client.</value>
</data>
<data name="Rule0058PageVariableMethodOnTemporaryTableTitle" xml:space="preserve">
<value>You cannot use a temporary record for the Record parameter on {0}.{1}."</value>
<value>{0}.{1}(): You cannot use a temporary record for the Record parameter.</value>
</data>
<data name="Rule0058PageVariableMethodOnTemporaryTableFormat" xml:space="preserve">
<value>You cannot use a temporary record for the Record parameter on {0}.{1}."</value>
<value>{0}.{1}(): You cannot use a temporary record for the Record parameter.</value>
</data>
<data name="Rule0058PageVariableMethodOnTemporaryTableDescription" xml:space="preserve">
<value>You cannot use a temporary record for the Record parameter on {0}.{1}."</value>
<value>{0}.{1}(): You cannot use a temporary record for the Record parameter.</value>
</data>
</root>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ For an example and the default values see: [LinterCop.ruleset.json](LinterCop.ru
|[LC0055](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0055)|The suffix `Tok` is meant to be used when the value of the label matches the name.|Info|
|[LC0056](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0056)|Empty Enum values should not have a specified `Caption` property.|Info|
|[LC0057](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0057)|Enum values must have non-empty a `Caption` to be selectable in the client|Info|
|[LC0058](https://github.com/StefanMaron/BusinessCentral.LinterCop/wiki/LC0058)|PageVariable.SetRecord(): You cannot use a temporary record for the Record parameter.|Warning|