/
NamingStyle.cs
525 lines (444 loc) · 20.1 KB
/
NamingStyle.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Xml.Linq;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Collections;
using Microsoft.CodeAnalysis.Shared.Utilities;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.NamingStyles
{
internal partial struct NamingStyle : IEquatable<NamingStyle>, IObjectWritable
{
public Guid ID { get; }
public string Name { get; }
public string Prefix { get; }
public string Suffix { get; }
public string WordSeparator { get; }
public Capitalization CapitalizationScheme { get; }
public NamingStyle(
Guid id, string name = null, string prefix = null, string suffix = null,
string wordSeparator = null, Capitalization capitalizationScheme = Capitalization.PascalCase) : this()
{
ID = id;
Name = name;
Prefix = prefix ?? "";
Suffix = suffix ?? "";
WordSeparator = wordSeparator ?? "";
CapitalizationScheme = capitalizationScheme;
}
public NamingStyle With(
Optional<string> name = default,
Optional<string> prefix = default,
Optional<string> suffix = default,
Optional<string> wordSeparator = default,
Optional<Capitalization> capitalizationScheme = default)
{
var newName = name.HasValue ? name.Value : this.Name;
var newPrefix = prefix.HasValue ? prefix.Value : this.Prefix;
var newSuffix = suffix.HasValue ? suffix.Value : this.Suffix;
var newWordSeparator = wordSeparator.HasValue ? wordSeparator.Value : this.WordSeparator;
var newCapitalizationScheme = capitalizationScheme.HasValue ? capitalizationScheme.Value : this.CapitalizationScheme;
if (newName == this.Name &&
newPrefix == this.Prefix &&
newSuffix == this.Suffix &&
newWordSeparator == this.WordSeparator &&
newCapitalizationScheme == this.CapitalizationScheme)
{
return this;
}
return new NamingStyle(this.ID,
newName, newPrefix, newSuffix, newWordSeparator, newCapitalizationScheme);
}
public override bool Equals(object obj)
{
return obj is NamingStyle other
&& Equals(other);
}
public bool Equals(NamingStyle other)
{
return ID == other.ID
&& Name == other.Name
&& Prefix == other.Prefix
&& Suffix == other.Suffix
&& WordSeparator == other.WordSeparator
&& CapitalizationScheme == other.CapitalizationScheme;
}
public override int GetHashCode()
{
return Hash.Combine(ID.GetHashCode(),
Hash.Combine(Name?.GetHashCode() ?? 0,
Hash.Combine(Prefix?.GetHashCode() ?? 0,
Hash.Combine(Suffix?.GetHashCode() ?? 0,
Hash.Combine(WordSeparator?.GetHashCode() ?? 0,
(int)CapitalizationScheme)))));
}
public string CreateName(ImmutableArray<string> words)
{
var wordsWithCasing = ApplyCapitalization(words);
var combinedWordsWithCasing = string.Join(WordSeparator, wordsWithCasing);
return Prefix + combinedWordsWithCasing + Suffix;
}
private IEnumerable<string> ApplyCapitalization(IEnumerable<string> words)
{
switch (CapitalizationScheme)
{
case Capitalization.PascalCase:
return words.Select(CapitalizeFirstLetter);
case Capitalization.CamelCase:
return words.Take(1).Select(DecapitalizeFirstLetter).Concat(words.Skip(1).Select(CapitalizeFirstLetter));
case Capitalization.FirstUpper:
return words.Take(1).Select(CapitalizeFirstLetter).Concat(words.Skip(1).Select(DecapitalizeFirstLetter));
case Capitalization.AllUpper:
return words.Select(w => w.ToUpper());
case Capitalization.AllLower:
return words.Select(w => w.ToLower());
default:
throw new InvalidOperationException();
}
}
private string CapitalizeFirstLetter(string word)
{
if (word.Length == 0)
{
return word;
}
if (char.IsUpper(word[0]))
{
return word;
}
var chars = word.ToCharArray();
chars[0] = char.ToUpper(chars[0]);
return new string(chars);
}
private string DecapitalizeFirstLetter(string word)
{
if (word.Length == 0)
{
return word;
}
if (char.IsLower(word[0]))
{
return word;
}
var chars = word.ToCharArray();
chars[0] = char.ToLower(chars[0]);
return new string(chars);
}
public bool IsNameCompliant(string name, out string failureReason)
{
if (!name.StartsWith(Prefix))
{
failureReason = string.Format(CompilerExtensionsResources.Missing_prefix_colon_0, Prefix);
return false;
}
if (!name.EndsWith(Suffix))
{
failureReason = string.Format(CompilerExtensionsResources.Missing_suffix_colon_0, Suffix);
return false;
}
if (name.Length <= Prefix.Length + Suffix.Length)
{
// name consists of Prefix and Suffix and no base name
// Prefix and Suffix can overlap
// Example: Prefix = "s_", Suffix = "_t", name "s_t"
failureReason = null;
return true;
}
// remove specified Prefix, then look for any other common prefixes
name = StripCommonPrefixes(name.Substring(Prefix.Length), out var prefix);
if (prefix != string.Empty)
{
// name started with specified prefix, but has at least one additional common prefix
// Example: specified prefix "test_", actual prefix "test_m_"
failureReason = Prefix == string.Empty ?
string.Format(CompilerExtensionsResources.Prefix_0_is_not_expected, prefix) :
string.Format(CompilerExtensionsResources.Prefix_0_does_not_match_expected_prefix_1, prefix, Prefix);
return false;
}
// specified and common prefixes have been removed. Now see that the base name has correct capitalization
var spanToCheck = TextSpan.FromBounds(0, name.Length - Suffix.Length);
Debug.Assert(spanToCheck.Length > 0);
switch (CapitalizationScheme)
{
case Capitalization.PascalCase: return CheckPascalCase(name, spanToCheck, out failureReason);
case Capitalization.CamelCase: return CheckCamelCase(name, spanToCheck, out failureReason);
case Capitalization.FirstUpper: return CheckFirstUpper(name, spanToCheck, out failureReason);
case Capitalization.AllUpper: return CheckAllUpper(name, spanToCheck, out failureReason);
case Capitalization.AllLower: return CheckAllLower(name, spanToCheck, out failureReason);
default: throw new InvalidOperationException();
}
}
private WordSpanEnumerable GetWordSpans(string name, TextSpan nameSpan)
=> new(name, nameSpan, WordSeparator);
private static string Substring(string name, TextSpan wordSpan)
=> name.Substring(wordSpan.Start, wordSpan.Length);
private static readonly Func<string, TextSpan, bool> s_firstCharIsLowerCase = (val, span) => !DoesCharacterHaveCasing(val[span.Start]) || char.IsLower(val[span.Start]);
private static readonly Func<string, TextSpan, bool> s_firstCharIsUpperCase = (val, span) => !DoesCharacterHaveCasing(val[span.Start]) || char.IsUpper(val[span.Start]);
private static readonly Func<string, TextSpan, bool> s_wordIsAllUpperCase = (val, span) =>
{
for (int i = span.Start, n = span.End; i < n; i++)
{
if (DoesCharacterHaveCasing(val[i]) && !char.IsUpper(val[i]))
{
return false;
}
}
return true;
};
private static readonly Func<string, TextSpan, bool> s_wordIsAllLowerCase = (val, span) =>
{
for (int i = span.Start, n = span.End; i < n; i++)
{
if (DoesCharacterHaveCasing(val[i]) && !char.IsLower(val[i]))
{
return false;
}
}
return true;
};
private bool CheckAllWords(
string name, TextSpan nameSpan, Func<string, TextSpan, bool> wordCheck,
string resourceId, out string reason)
{
reason = null;
using var _ = ArrayBuilder<string>.GetInstance(out var violations);
foreach (var wordSpan in GetWordSpans(name, nameSpan))
{
if (!wordCheck(name, wordSpan))
{
violations.Add(Substring(name, wordSpan));
}
}
if (violations.Count > 0)
{
reason = string.Format(resourceId, string.Join(", ", violations));
}
return reason == null;
}
private bool CheckPascalCase(string name, TextSpan nameSpan, out string reason)
=> CheckAllWords(
name, nameSpan, s_firstCharIsUpperCase,
CompilerExtensionsResources.These_words_must_begin_with_upper_case_characters_colon_0, out reason);
private bool CheckAllUpper(string name, TextSpan nameSpan, out string reason)
=> CheckAllWords(
name, nameSpan, s_wordIsAllUpperCase,
CompilerExtensionsResources.These_words_cannot_contain_lower_case_characters_colon_0, out reason);
private bool CheckAllLower(string name, TextSpan nameSpan, out string reason)
=> CheckAllWords(
name, nameSpan, s_wordIsAllLowerCase,
CompilerExtensionsResources.These_words_cannot_contain_upper_case_characters_colon_0, out reason);
private bool CheckFirstAndRestWords(
string name, TextSpan nameSpan,
Func<string, TextSpan, bool> firstWordCheck,
Func<string, TextSpan, bool> restWordCheck,
string firstResourceId,
string restResourceId,
out string reason)
{
reason = null;
using var _ = ArrayBuilder<string>.GetInstance(out var violations);
var first = true;
foreach (var wordSpan in GetWordSpans(name, nameSpan))
{
if (first)
{
if (!firstWordCheck(name, wordSpan))
{
reason = string.Format(firstResourceId, Substring(name, wordSpan));
}
}
else
{
if (!restWordCheck(name, wordSpan))
{
violations.Add(Substring(name, wordSpan));
}
}
first = false;
}
if (violations.Count > 0)
{
var restString = string.Format(restResourceId, string.Join(", ", violations));
reason = reason == null
? restString
: reason + Environment.NewLine + restString;
}
return reason == null;
}
private bool CheckCamelCase(string name, TextSpan nameSpan, out string reason)
=> CheckFirstAndRestWords(
name, nameSpan, s_firstCharIsLowerCase, s_firstCharIsUpperCase,
CompilerExtensionsResources.The_first_word_0_must_begin_with_a_lower_case_character,
CompilerExtensionsResources.These_non_leading_words_must_begin_with_an_upper_case_letter_colon_0,
out reason);
private bool CheckFirstUpper(string name, TextSpan nameSpan, out string reason)
=> CheckFirstAndRestWords(
name, nameSpan, s_firstCharIsUpperCase, s_firstCharIsLowerCase,
CompilerExtensionsResources.The_first_word_0_must_begin_with_an_upper_case_character,
CompilerExtensionsResources.These_non_leading_words_must_begin_with_a_lowercase_letter_colon_0,
out reason);
private static bool DoesCharacterHaveCasing(char c) => char.ToLower(c) != char.ToUpper(c);
private string CreateCompliantNameDirectly(string name)
{
// Example: for specified prefix = "Test_" and name = "Test_m_BaseName", we remove "Test_m_"
// "Test_" will be added back later in this method
name = StripCommonPrefixes(name.StartsWith(Prefix) ? name.Substring(Prefix.Length) : name, out _);
var addPrefix = !name.StartsWith(Prefix);
var addSuffix = !name.EndsWith(Suffix);
name = addPrefix ? (Prefix + name) : name;
name = addSuffix ? (name + Suffix) : name;
return FinishFixingName(name);
}
public IEnumerable<string> MakeCompliant(string name)
{
var name1 = CreateCompliantNameReusingPartialPrefixesAndSuffixes(name);
yield return name1;
var name2 = CreateCompliantNameDirectly(name);
if (name2 != name1)
{
yield return name2;
}
}
private string CreateCompliantNameReusingPartialPrefixesAndSuffixes(string name)
{
name = StripCommonPrefixes(name, out _);
name = EnsurePrefix(name);
name = EnsureSuffix(name);
return FinishFixingName(name);
}
public static string StripCommonPrefixes(string name, out string prefix)
{
var index = 0;
while (index + 1 < name.Length)
{
switch (char.ToLowerInvariant(name[index]))
{
case 'm':
case 's':
case 't':
if (index + 2 < name.Length && name[index + 1] == '_')
{
index += 2;
continue;
}
break;
case '_':
index++;
continue;
default:
break;
}
// If we reach this point, the current iteration did not strip any additional characters
break;
}
prefix = name.Substring(0, index);
return name.Substring(index);
}
private string FinishFixingName(string name)
{
// Edge case: prefix "as", suffix "sa", name "asa"
if (Suffix.Length + Prefix.Length >= name.Length)
{
return name;
}
name = name.Substring(Prefix.Length, name.Length - Suffix.Length - Prefix.Length);
IEnumerable<string> words = new[] { name };
if (!string.IsNullOrEmpty(WordSeparator))
{
words = name.Split(new[] { WordSeparator }, StringSplitOptions.RemoveEmptyEntries);
// Edge case: the only character(s) in the name is(are) the WordSeparator
if (words.Count() == 0)
{
return name;
}
if (words.Count() == 1) // Only Split if words have not been split before
{
var isWord = true;
using var parts = TemporaryArray<TextSpan>.Empty;
StringBreaker.AddParts(name, isWord, ref parts.AsRef());
var newWords = new string[parts.Count];
for (var i = 0; i < parts.Count; i++)
{
newWords[i] = name.Substring(parts[i].Start, parts[i].End - parts[i].Start);
}
words = newWords;
}
}
words = ApplyCapitalization(words);
return Prefix + string.Join(WordSeparator, words) + Suffix;
}
private string EnsureSuffix(string name)
{
// If the name already ends with any prefix of the Suffix, only append the suffix of
// the Suffix not contained in the longest such Suffix prefix. For example, if the
// required suffix is "_catdog" and the name is "test_cat", then only append "dog".
for (var i = Suffix.Length; i > 0; i--)
{
if (name.EndsWith(Suffix.Substring(0, i)))
{
return name + Suffix.Substring(i);
}
}
return name + Suffix;
}
private string EnsurePrefix(string name)
{
// If the name already starts with any suffix of the Prefix, only prepend the prefix of
// the Prefix not contained in the longest such Prefix suffix. For example, if the
// required prefix is "catdog_" and the name is "dog_test", then only prepend "cat".
for (var i = 0; i < Prefix.Length; i++)
{
if (name.StartsWith(Prefix.Substring(i)))
{
return Prefix.Substring(0, i) + name;
}
}
return Prefix + name;
}
internal XElement CreateXElement()
=> new(nameof(NamingStyle),
new XAttribute(nameof(ID), ID),
new XAttribute(nameof(Name), Name),
new XAttribute(nameof(Prefix), Prefix ?? string.Empty),
new XAttribute(nameof(Suffix), Suffix ?? string.Empty),
new XAttribute(nameof(WordSeparator), WordSeparator ?? string.Empty),
new XAttribute(nameof(CapitalizationScheme), CapitalizationScheme));
internal static NamingStyle FromXElement(XElement namingStyleElement)
=> new(
id: Guid.Parse(namingStyleElement.Attribute(nameof(ID)).Value),
name: namingStyleElement.Attribute(nameof(Name)).Value,
prefix: namingStyleElement.Attribute(nameof(Prefix)).Value,
suffix: namingStyleElement.Attribute(nameof(Suffix)).Value,
wordSeparator: namingStyleElement.Attribute(nameof(WordSeparator)).Value,
capitalizationScheme: (Capitalization)Enum.Parse(typeof(Capitalization), namingStyleElement.Attribute(nameof(CapitalizationScheme)).Value));
public bool ShouldReuseInSerialization => false;
public void WriteTo(ObjectWriter writer)
{
writer.WriteGuid(ID);
writer.WriteString(Name);
writer.WriteString(Prefix ?? string.Empty);
writer.WriteString(Suffix ?? string.Empty);
writer.WriteString(WordSeparator ?? string.Empty);
writer.WriteInt32((int)CapitalizationScheme);
}
public static NamingStyle ReadFrom(ObjectReader reader)
{
return new NamingStyle(
reader.ReadGuid(),
reader.ReadString(),
reader.ReadString(),
reader.ReadString(),
reader.ReadString(),
(Capitalization)reader.ReadInt32());
}
}
}