Skip to content

Commit 1d4da1a

Browse files
committed
Update resource analyzers to flag leading and trailing whitespaces
1 parent 4016ef2 commit 1d4da1a

16 files changed

+591
-59
lines changed

src/Microsoft.CodeAnalysis.Analyzers/Core/CodeAnalysisDiagnosticsResources.resx

+3-3
Original file line numberDiff line numberDiff line change
@@ -497,19 +497,19 @@
497497
<value>Enabling release tracking for analyzer packages helps in tracking and documenting the analyzer diagnostics that ship and/or change with each analyzer release. See details at https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md.</value>
498498
</data>
499499
<data name="DefineDiagnosticTitleCorrectlyMessage" xml:space="preserve">
500-
<value>The diagnostic title should not contain a period or any line return character</value>
500+
<value>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</value>
501501
</data>
502502
<data name="DefineDiagnosticTitleCorrectlyTitle" xml:space="preserve">
503503
<value>Define diagnostic title correctly</value>
504504
</data>
505505
<data name="DefineDiagnosticMessageCorrectlyMessage" xml:space="preserve">
506-
<value>The diagnostic message should not contain any line return character and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</value>
506+
<value>The diagnostic message should not contain any line return character nor any leading or trailing whitespaces and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</value>
507507
</data>
508508
<data name="DefineDiagnosticMessageCorrectlyTitle" xml:space="preserve">
509509
<value>Define diagnostic message correctly</value>
510510
</data>
511511
<data name="DefineDiagnosticDescriptionCorrectlyMessage" xml:space="preserve">
512-
<value>The diagnostic description should be one or multiple sentences ending with a punctuation sign</value>
512+
<value>The diagnostic description should be one or multiple sentences ending with a punctuation sign and should not have any leading or trailing whitespaces</value>
513513
</data>
514514
<data name="DefineDiagnosticDescriptionCorrectlyTitle" xml:space="preserve">
515515
<value>Define diagnostic description correctly</value>

src/Microsoft.CodeAnalysis.Analyzers/Core/MetaAnalyzers/DiagnosticDescriptorCreationAnalyzer.cs

+25-4
Original file line numberDiff line numberDiff line change
@@ -504,11 +504,14 @@ private static void AnalyzeTitleCore(string title, IArgumentOperation argumentOp
504504
var isMultiSentences = IsMultiSentences(title);
505505
var endsWithPeriod = EndsWithPeriod(title);
506506
var containsLineReturn = ContainsLineReturn(title);
507-
if (isMultiSentences || endsWithPeriod || containsLineReturn)
507+
var hasLeadingOrTrailingWhitespaces = HasLeadingOrTrailingWhitespaces(title);
508+
509+
if (isMultiSentences || endsWithPeriod || containsLineReturn || hasLeadingOrTrailingWhitespaces)
508510
{
509511
var fixedTitle = endsWithPeriod ? RemoveTrailingPeriod(title) : title;
510512
fixedTitle = isMultiSentences ? FixMultiSentences(fixedTitle) : fixedTitle;
511513
fixedTitle = containsLineReturn ? FixLineReturns(fixedTitle, allowMultisentences: false) : fixedTitle;
514+
fixedTitle = hasLeadingOrTrailingWhitespaces ? RemoveLeadingAndTrailingWhitespaces(fixedTitle) : fixedTitle;
512515
Debug.Assert(title != fixedTitle);
513516

514517
ReportDefineDiagnosticArgumentCorrectlyDiagnostic(DefineDiagnosticTitleCorrectlyRule,
@@ -594,7 +597,9 @@ private static void AnalyzeMessageCore(string message, IArgumentOperation argume
594597
var isMultiSentences = IsMultiSentences(message);
595598
var endsWithPeriod = EndsWithPeriod(message);
596599
var containsLineReturn = ContainsLineReturn(message);
597-
if (isMultiSentences ^ endsWithPeriod || containsLineReturn)
600+
var hasLeadingOrTrailingWhitespaces = HasLeadingOrTrailingWhitespaces(message);
601+
602+
if (isMultiSentences ^ endsWithPeriod || containsLineReturn || hasLeadingOrTrailingWhitespaces)
598603
{
599604
var fixedMessage = containsLineReturn ? FixLineReturns(message, allowMultisentences: true) : message;
600605
isMultiSentences = IsMultiSentences(fixedMessage);
@@ -605,6 +610,8 @@ private static void AnalyzeMessageCore(string message, IArgumentOperation argume
605610
fixedMessage = endsWithPeriod ? RemoveTrailingPeriod(fixedMessage) : fixedMessage + ".";
606611
}
607612

613+
fixedMessage = hasLeadingOrTrailingWhitespaces ? RemoveLeadingAndTrailingWhitespaces(fixedMessage) : fixedMessage;
614+
608615
ReportDefineDiagnosticArgumentCorrectlyDiagnostic(DefineDiagnosticMessageCorrectlyRule,
609616
argumentOperation, fixedMessage, fixLocation, reportDiagnostic);
610617
}
@@ -630,9 +637,14 @@ private static void AnalyzeDescription(
630637

631638
private static void AnalyzeDescriptionCore(string description, IArgumentOperation argumentOperation, Location fixLocation, Action<Diagnostic> reportDiagnostic)
632639
{
633-
if (!EndsWithPunctuation(description))
640+
var hasLeadingOrTrailingWhitespaces = HasLeadingOrTrailingWhitespaces(description);
641+
var endsWithPunctuation = EndsWithPunctuation(description);
642+
643+
if (!endsWithPunctuation || hasLeadingOrTrailingWhitespaces)
634644
{
635-
var fixedDescription = description + ".";
645+
var fixedDescription = !endsWithPunctuation ? description + "." : description;
646+
fixedDescription = hasLeadingOrTrailingWhitespaces ? RemoveLeadingAndTrailingWhitespaces(fixedDescription) : fixedDescription;
647+
636648
ReportDefineDiagnosticArgumentCorrectlyDiagnostic(DefineDiagnosticDescriptionCorrectlyRule,
637649
argumentOperation, fixedDescription, fixLocation, reportDiagnostic);
638650
}
@@ -859,6 +871,15 @@ private static string RemoveTrailingPunctuation(string s)
859871
return s[0..^1];
860872
}
861873

874+
private static bool HasLeadingOrTrailingWhitespaces(string s)
875+
=> s.Trim().Length != s.Length;
876+
877+
private static string RemoveLeadingAndTrailingWhitespaces(string s)
878+
{
879+
Debug.Assert(HasLeadingOrTrailingWhitespaces(s));
880+
return s.Trim();
881+
}
882+
862883
private static void AnalyzeHelpLinkUri(
863884
OperationAnalysisContext operationAnalysisContext,
864885
ImmutableArray<IArgumentOperation> creationArguments,

src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.cs.xlf

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<note />
6969
</trans-unit>
7070
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
71-
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
71+
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign and should not have any leading or trailing whitespaces</source>
7272
<target state="needs-review-translation">Diagnostický popis by měla být jedna věta nebo několik vět zakončených interpunkčním znaménkem.</target>
7373
<note />
7474
</trans-unit>
@@ -78,7 +78,7 @@
7878
<note />
7979
</trans-unit>
8080
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
81-
<source>The diagnostic message should not contain any line return character and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
81+
<source>The diagnostic message should not contain any line return character nor any leading or trailing whitespaces and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
8282
<target state="needs-review-translation">Diagnostická zpráva by neměla obsahovat žádný návratový znak (konec řádku) a měla by to být buď jedna věta bez tečky na konci nebo víc vět s tečkou na konci.</target>
8383
<note />
8484
</trans-unit>
@@ -88,8 +88,8 @@
8888
<note />
8989
</trans-unit>
9090
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
91-
<source>The diagnostic title should not contain a period or any line return character</source>
92-
<target state="translated">Nadpis diagnostiky by neměl obsahovat tečku ani žádný znak konce řádku.</target>
91+
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
92+
<target state="needs-review-translation">Nadpis diagnostiky by neměl obsahovat tečku ani žádný znak konce řádku.</target>
9393
<note />
9494
</trans-unit>
9595
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">

src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.de.xlf

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<note />
6969
</trans-unit>
7070
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
71-
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
71+
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign and should not have any leading or trailing whitespaces</source>
7272
<target state="needs-review-translation">Die Diagnosebeschreibung sollte mindestens einen Satz umfassen und auf ein Satzzeichen enden.</target>
7373
<note />
7474
</trans-unit>
@@ -78,7 +78,7 @@
7878
<note />
7979
</trans-unit>
8080
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
81-
<source>The diagnostic message should not contain any line return character and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
81+
<source>The diagnostic message should not contain any line return character nor any leading or trailing whitespaces and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
8282
<target state="needs-review-translation">Die Diagnosemeldung darf keine Zeilenvorschubzeichen enthalten und muss entweder einen einzelnen Satz ohne Satzendepunkt oder mehrere Sätze mit Satzendepunkt umfassen.</target>
8383
<note />
8484
</trans-unit>
@@ -88,8 +88,8 @@
8888
<note />
8989
</trans-unit>
9090
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
91-
<source>The diagnostic title should not contain a period or any line return character</source>
92-
<target state="translated">Der Titel der Diagnosemeldung darf keinen Satzendepunkt oder ein Zeilenvorschubzeichen enthalten.</target>
91+
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
92+
<target state="needs-review-translation">Der Titel der Diagnosemeldung darf keinen Satzendepunkt oder ein Zeilenvorschubzeichen enthalten.</target>
9393
<note />
9494
</trans-unit>
9595
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">

src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.es.xlf

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<note />
6969
</trans-unit>
7070
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
71-
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
71+
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign and should not have any leading or trailing whitespaces</source>
7272
<target state="needs-review-translation">La descripción del diagnóstico debe ser una o varias oraciones que terminen con un signo de puntuación.</target>
7373
<note />
7474
</trans-unit>
@@ -78,7 +78,7 @@
7878
<note />
7979
</trans-unit>
8080
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
81-
<source>The diagnostic message should not contain any line return character and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
81+
<source>The diagnostic message should not contain any line return character nor any leading or trailing whitespaces and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
8282
<target state="needs-review-translation">El mensaje de diagnóstico no debe contener ningún carácter de retorno de línea y debe ser una sola oración sin punto final o varias oraciones con punto final.</target>
8383
<note />
8484
</trans-unit>
@@ -88,8 +88,8 @@
8888
<note />
8989
</trans-unit>
9090
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
91-
<source>The diagnostic title should not contain a period or any line return character</source>
92-
<target state="translated">El título del diagnóstico no debe contener un punto ni un carácter de retorno de línea.</target>
91+
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
92+
<target state="needs-review-translation">El título del diagnóstico no debe contener un punto ni un carácter de retorno de línea.</target>
9393
<note />
9494
</trans-unit>
9595
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">

src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.fr.xlf

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<note />
6969
</trans-unit>
7070
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
71-
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
71+
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign and should not have any leading or trailing whitespaces</source>
7272
<target state="needs-review-translation">La description de diagnostic doit être une ou plusieurs phrases se terminant par un signe de ponctuation.</target>
7373
<note />
7474
</trans-unit>
@@ -78,7 +78,7 @@
7878
<note />
7979
</trans-unit>
8080
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
81-
<source>The diagnostic message should not contain any line return character and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
81+
<source>The diagnostic message should not contain any line return character nor any leading or trailing whitespaces and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
8282
<target state="needs-review-translation">Le message de diagnostic ne doit avoir aucun caractère de retour de ligne, et doit tenir en une seule phrase sans point final ou en plusieurs phrases avec un point final.</target>
8383
<note />
8484
</trans-unit>
@@ -88,8 +88,8 @@
8888
<note />
8989
</trans-unit>
9090
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
91-
<source>The diagnostic title should not contain a period or any line return character</source>
92-
<target state="translated">Le titre de diagnostic ne doit pas avoir de point ou de caractère de retour de ligne</target>
91+
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
92+
<target state="needs-review-translation">Le titre de diagnostic ne doit pas avoir de point ou de caractère de retour de ligne</target>
9393
<note />
9494
</trans-unit>
9595
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">

src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.it.xlf

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<note />
6969
</trans-unit>
7070
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
71-
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
71+
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign and should not have any leading or trailing whitespaces</source>
7272
<target state="needs-review-translation">La descrizione della diagnostica deve essere costituita da una o più frasi che terminano con un segno di punteggiatura.</target>
7373
<note />
7474
</trans-unit>
@@ -78,7 +78,7 @@
7878
<note />
7979
</trans-unit>
8080
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
81-
<source>The diagnostic message should not contain any line return character and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
81+
<source>The diagnostic message should not contain any line return character nor any leading or trailing whitespaces and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
8282
<target state="needs-review-translation">Il messaggio della diagnostica non deve contenere caratteri di ritorno a capo e deve essere costituito da una singola frase senza punto finale oppure da più frasi con un punto finale.</target>
8383
<note />
8484
</trans-unit>
@@ -88,8 +88,8 @@
8888
<note />
8989
</trans-unit>
9090
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
91-
<source>The diagnostic title should not contain a period or any line return character</source>
92-
<target state="translated">Il titolo della diagnostica non deve contenere un punto o un carattere di ritorno a capo</target>
91+
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
92+
<target state="needs-review-translation">Il titolo della diagnostica non deve contenere un punto o un carattere di ritorno a capo</target>
9393
<note />
9494
</trans-unit>
9595
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">

src/Microsoft.CodeAnalysis.Analyzers/Core/xlf/CodeAnalysisDiagnosticsResources.ja.xlf

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<note />
6969
</trans-unit>
7070
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
71-
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
71+
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign and should not have any leading or trailing whitespaces</source>
7272
<target state="needs-review-translation">診断の説明は、句読点で終わる 1 つまたは複数の文にする必要があります。</target>
7373
<note />
7474
</trans-unit>
@@ -78,7 +78,7 @@
7878
<note />
7979
</trans-unit>
8080
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
81-
<source>The diagnostic message should not contain any line return character and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
81+
<source>The diagnostic message should not contain any line return character nor any leading or trailing whitespaces and should either be a single sentence without a trailing period or a multi-sentences with a trailing period</source>
8282
<target state="needs-review-translation">診断メッセージには改行文字を含めないでください。また、末尾にピリオドが付いていない 1 つの文か、末尾にピリオドが付いた複数の文にする必要があります。</target>
8383
<note />
8484
</trans-unit>
@@ -88,8 +88,8 @@
8888
<note />
8989
</trans-unit>
9090
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
91-
<source>The diagnostic title should not contain a period or any line return character</source>
92-
<target state="translated">診断タイトルには、ピリオドも改行文字も含めないでください</target>
91+
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
92+
<target state="needs-review-translation">診断タイトルには、ピリオドも改行文字も含めないでください</target>
9393
<note />
9494
</trans-unit>
9595
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">

0 commit comments

Comments
 (0)