-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExtensions.cs
176 lines (154 loc) · 5.94 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Immutable;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
namespace CSharpEssentials
{
internal static class Extensions
{
public static ImmutableArray<IParameterSymbol> GetParameters(this ISymbol symbol)
{
switch (symbol?.Kind)
{
case SymbolKind.Method:
return ((IMethodSymbol)symbol).Parameters;
case SymbolKind.Property:
return ((IPropertySymbol)symbol).Parameters;
default:
return ImmutableArray<IParameterSymbol>.Empty;
}
}
public static BaseParameterListSyntax GetParameterList(this SyntaxNode node)
{
switch (node?.Kind())
{
case SyntaxKind.MethodDeclaration:
return ((MethodDeclarationSyntax)node).ParameterList;
case SyntaxKind.ConstructorDeclaration:
return ((ConstructorDeclarationSyntax)node).ParameterList;
case SyntaxKind.IndexerDeclaration:
return ((IndexerDeclarationSyntax)node).ParameterList;
case SyntaxKind.ParenthesizedLambdaExpression:
return ((ParenthesizedLambdaExpressionSyntax)node).ParameterList;
case SyntaxKind.AnonymousMethodExpression:
return ((AnonymousMethodExpressionSyntax)node).ParameterList;
default:
return null;
}
}
internal struct ArgumentInfo
{
public readonly ISymbol MethodOrProperty;
public readonly IParameterSymbol Parameter;
public ArgumentInfo(ISymbol methodOrProperty, IParameterSymbol parameter)
{
this.MethodOrProperty = methodOrProperty;
this.Parameter = parameter;
}
}
public static ArgumentInfo GetArgumentInfo(this SemanticModel semanticModel, ArgumentSyntax argument)
{
if (semanticModel == null)
{
throw new ArgumentNullException("semanticModel");
}
if (argument == null)
{
throw new ArgumentNullException("argument");
}
var argumentList = argument.Parent as ArgumentListSyntax;
if (argumentList == null)
{
return default(ArgumentInfo);
}
var expression = argumentList.Parent as ExpressionSyntax;
if (expression == null)
{
return default(ArgumentInfo);
}
var methodOrProperty = semanticModel.GetSymbolInfo(expression).Symbol;
if (methodOrProperty == null)
{
return default(ArgumentInfo);
}
var parameters = methodOrProperty.GetParameters();
if (parameters.Length == 0)
{
return default(ArgumentInfo);
}
if (argument.NameColon != null)
{
if (argument.NameColon.Name == null)
{
return default(ArgumentInfo);
}
// We've got a named argument...
var nameText = argument.NameColon.Name.Identifier.ValueText;
if (nameText == null)
{
return default(ArgumentInfo);
}
foreach (var parameter in parameters)
{
if (string.Equals(parameter.Name, nameText, StringComparison.Ordinal))
{
return new ArgumentInfo(methodOrProperty, parameter);
}
}
}
else
{
// Positional argument...
var index = argumentList.Arguments.IndexOf(argument);
if (index < 0)
{
return default(ArgumentInfo);
}
if (index < parameters.Length)
{
return new ArgumentInfo(methodOrProperty, parameters[index]);
}
if (index >= parameters.Length &&
parameters[parameters.Length - 1].IsParams)
{
return new ArgumentInfo(methodOrProperty, parameters[parameters.Length - 1]);
}
}
return default(ArgumentInfo);
}
private static bool IsGeneratedCode(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return false;
}
var fileName = Path.GetFileName(filePath);
if (fileName.StartsWith("TemporaryGeneratedFile_", StringComparison.OrdinalIgnoreCase))
{
return true;
}
var extension = Path.GetExtension(fileName);
if (string.IsNullOrEmpty(extension))
{
return false;
}
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
if (fileNameWithoutExtension.EndsWith("AssemblyInfo", StringComparison.OrdinalIgnoreCase) ||
fileNameWithoutExtension.EndsWith(".designer", StringComparison.OrdinalIgnoreCase) ||
fileNameWithoutExtension.EndsWith(".g", StringComparison.OrdinalIgnoreCase) ||
fileNameWithoutExtension.EndsWith(".g.i", StringComparison.OrdinalIgnoreCase) ||
fileNameWithoutExtension.EndsWith(".AssemblyAttributes", StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
}
public static bool IsGeneratedCode(this SyntaxTree tree)
{
return IsGeneratedCode(tree.FilePath);
}
}
}