forked from SonarSource/sonar-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegexContext.cs
119 lines (105 loc) · 4.97 KB
/
RegexContext.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2024 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System.Text.RegularExpressions;
namespace SonarAnalyzer.RegularExpressions;
[DebuggerDisplay("Pattern = {Pattern}, Options = {Options}")]
public sealed class RegexContext
{
private static readonly RegexOptions ValidationMask = (RegexOptions)int.MaxValue ^ RegexOptions.Compiled;
private static readonly string[] MatchMethods =
[
nameof(Regex.IsMatch),
nameof(Regex.Match),
nameof(Regex.Matches),
nameof(Regex.Replace),
nameof(Regex.Split),
];
public SyntaxNode PatternNode { get; }
public string Pattern { get; }
public SyntaxNode OptionsNode { get; }
public RegexOptions? Options { get; }
public Regex Regex { get; }
public Exception ParseError { get; }
public RegexContext(SyntaxNode patternNode, string pattern, SyntaxNode optionsNode, RegexOptions? options)
{
PatternNode = patternNode;
Pattern = pattern;
OptionsNode = optionsNode;
Options = options;
if (!string.IsNullOrEmpty(pattern))
{
try
{
Regex = new(Pattern, options.GetValueOrDefault() & ValidationMask, RegexConstants.DefaultTimeout);
}
catch (Exception ex)
{
ParseError = ex;
}
}
}
public static RegexContext FromAttribute<TSyntaxKind>(ILanguageFacade<TSyntaxKind> language, SemanticModel model, SyntaxNode node) where TSyntaxKind : struct
{
if (model.GetSymbolInfo(node).Symbol is IMethodSymbol method
&& method.IsInType(KnownType.System_ComponentModel_DataAnnotations_RegularExpressionAttribute))
{
var parameters = language.MethodParameterLookup(node, method);
var pattern = TryGetNonParamsSyntax(method, parameters, "pattern");
return new RegexContext(pattern, language.FindConstantValue(model, pattern) as string, null, null);
}
else
{
return null;
}
}
public static RegexContext FromCtor<TSyntaxKind>(ILanguageFacade<TSyntaxKind> language, SemanticModel model, SyntaxNode node) where TSyntaxKind : struct =>
model.GetSymbolInfo(node).Symbol is IMethodSymbol method
&& method.IsConstructor()
&& method.ContainingType.Is(KnownType.System_Text_RegularExpressions_Regex)
? FromMethod(language, model, node, method)
: null;
public static RegexContext FromMethod<TSyntaxKind>(ILanguageFacade<TSyntaxKind> language, SemanticModel model, SyntaxNode node) where TSyntaxKind : struct =>
language.Syntax.NodeIdentifier(node).GetValueOrDefault().Text is { } name
&& MatchMethods.Any(x => name.Equals(x, language.NameComparison))
&& model.GetSymbolInfo(node).Symbol is IMethodSymbol { IsStatic: true } method
&& method.ContainingType.Is(KnownType.System_Text_RegularExpressions_Regex)
? FromMethod(language, model, node, method)
: null;
private static RegexContext FromMethod<TSyntaxKind>(ILanguageFacade<TSyntaxKind> language, SemanticModel model, SyntaxNode node, IMethodSymbol method) where TSyntaxKind : struct
{
var parameters = language.MethodParameterLookup(node, method);
var pattern = TryGetNonParamsSyntax(method, parameters, "pattern");
var options = TryGetNonParamsSyntax(method, parameters, "options");
return new RegexContext(
pattern,
language.FindConstantValue(model, pattern) as string,
options,
FindRegexOptions(language, model, options));
}
private static RegexOptions? FindRegexOptions<TSyntaxKind>(ILanguageFacade<TSyntaxKind> language, SemanticModel model, SyntaxNode options) where TSyntaxKind : struct =>
language.FindConstantValue(model, options) is int constant
? (RegexOptions)constant
: null;
private static SyntaxNode TryGetNonParamsSyntax(IMethodSymbol method, IMethodParameterLookup parameters, string paramName) =>
method.Parameters.SingleOrDefault(x => x.Name == paramName) is { } param
&& parameters.TryGetNonParamsSyntax(param, out var node)
? node
: null;
}