forked from StefanMaron/BusinessCentral.LinterCop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
47 lines (38 loc) · 2.34 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Microsoft.Dynamics.Nav.CodeAnalysis;
using Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics;
using Microsoft.Dynamics.Nav.CodeAnalysis.Syntax;
using System.Text.RegularExpressions;
using System.Threading;
namespace BusinessCentral.LinterCop
{
internal static class Extensions
{
private static readonly Regex CamelCaseRegex = new Regex("^[a-z][a-zA-Z0-9]*$", RegexOptions.Compiled);
internal static bool IsTestCodeunit(this IApplicationObjectTypeSymbol symbol) => symbol is ICodeunitTypeSymbol codeunitTypeSymbol && codeunitTypeSymbol.Subtype == CodeunitSubtypeKind.Test;
internal static bool IsUpgradeCodeunit(this IApplicationObjectTypeSymbol symbol) => symbol is ICodeunitTypeSymbol codeunitTypeSymbol && codeunitTypeSymbol.Subtype == CodeunitSubtypeKind.Upgrade;
internal static bool IsAllowedLowerPermissionObject(this IApplicationObjectTypeSymbol symbol) => symbol.Kind == SymbolKind.Codeunit && (symbol.Id == 132218 && SemanticFacts.IsSameName(symbol.Name, "Permission Test Catalog") || symbol.Id == 132230 && SemanticFacts.IsSameName(symbol.Name, "Library - E2E Role Permissions"));
internal static int GetTokenLine(this SyntaxToken token) => token.GetLocation().GetMappedLineSpan().StartLinePosition.Line;
internal static bool IsValidCamelCase(this string str) => !string.IsNullOrEmpty(str) && Extensions.CamelCaseRegex.IsMatch(str);
internal static IdentifierNameSyntax GetIdentifierNameSyntax(
this SyntaxNodeAnalysisContext context)
{
if (context.Node.IsKind(SyntaxKind.IdentifierName))
return (IdentifierNameSyntax) context.Node;
return !context.Node.IsKind(SyntaxKind.IdentifierNameOrEmpty) ? (IdentifierNameSyntax) null : ((IdentifierNameOrEmptySyntax) context.Node).IdentifierName;
}
internal static bool TryGetSymbolFromIdentifier(
SyntaxNodeAnalysisContext syntaxNodeAnalysisContext,
IdentifierNameSyntax identifierName,
SymbolKind symbolKind,
out ISymbol symbol)
{
symbol = (ISymbol) null;
SymbolInfo symbolInfo = syntaxNodeAnalysisContext.SemanticModel.GetSymbolInfo((ExpressionSyntax) identifierName, new CancellationToken());
ISymbol symbol1 = symbolInfo.Symbol;
if ((symbol1 != null ? (symbol1.Kind != symbolKind ? 1 : 0) : 1) != 0)
return false;
symbol = symbolInfo.Symbol;
return true;
}
}
}