Skip to content

Commit 0a764ab

Browse files
authored
Merge pull request #3973 from Evangelink/resource-whitespaces
Update resource analyzers to flag leading and trailing whitespaces
2 parents 2208884 + d9302b6 commit 0a764ab

16 files changed

+650
-60
lines changed

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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,22 @@ private static void AnalyzeTitle(
501501

502502
private static void AnalyzeTitleCore(string title, IArgumentOperation argumentOperation, Location fixLocation, Action<Diagnostic> reportDiagnostic)
503503
{
504+
var hasLeadingOrTrailingWhitespaces = HasLeadingOrTrailingWhitespaces(title);
505+
if (hasLeadingOrTrailingWhitespaces)
506+
{
507+
title = RemoveLeadingAndTrailingWhitespaces(title);
508+
}
509+
504510
var isMultiSentences = IsMultiSentences(title);
505511
var endsWithPeriod = EndsWithPeriod(title);
506512
var containsLineReturn = ContainsLineReturn(title);
507-
if (isMultiSentences || endsWithPeriod || containsLineReturn)
513+
514+
if (isMultiSentences || endsWithPeriod || containsLineReturn || hasLeadingOrTrailingWhitespaces)
508515
{
516+
// Leading and trailing spaces were already fixed
509517
var fixedTitle = endsWithPeriod ? RemoveTrailingPeriod(title) : title;
510518
fixedTitle = isMultiSentences ? FixMultiSentences(fixedTitle) : fixedTitle;
511519
fixedTitle = containsLineReturn ? FixLineReturns(fixedTitle, allowMultisentences: false) : fixedTitle;
512-
Debug.Assert(title != fixedTitle);
513520

514521
ReportDefineDiagnosticArgumentCorrectlyDiagnostic(DefineDiagnosticTitleCorrectlyRule,
515522
argumentOperation, fixedTitle, fixLocation, reportDiagnostic);
@@ -591,11 +598,19 @@ private static void AnalyzeMessage(
591598

592599
private static void AnalyzeMessageCore(string message, IArgumentOperation argumentOperation, Location fixLocation, Action<Diagnostic> reportDiagnostic)
593600
{
601+
var hasLeadingOrTrailingWhitespaces = HasLeadingOrTrailingWhitespaces(message);
602+
if (hasLeadingOrTrailingWhitespaces)
603+
{
604+
message = RemoveLeadingAndTrailingWhitespaces(message);
605+
}
606+
594607
var isMultiSentences = IsMultiSentences(message);
595608
var endsWithPeriod = EndsWithPeriod(message);
596609
var containsLineReturn = ContainsLineReturn(message);
597-
if (isMultiSentences ^ endsWithPeriod || containsLineReturn)
610+
611+
if (isMultiSentences ^ endsWithPeriod || containsLineReturn || hasLeadingOrTrailingWhitespaces)
598612
{
613+
// Leading and trailing spaces were already fixed
599614
var fixedMessage = containsLineReturn ? FixLineReturns(message, allowMultisentences: true) : message;
600615
isMultiSentences = IsMultiSentences(fixedMessage);
601616
endsWithPeriod = EndsWithPeriod(fixedMessage);
@@ -630,9 +645,18 @@ private static void AnalyzeDescription(
630645

631646
private static void AnalyzeDescriptionCore(string description, IArgumentOperation argumentOperation, Location fixLocation, Action<Diagnostic> reportDiagnostic)
632647
{
633-
if (!EndsWithPunctuation(description))
648+
var hasLeadingOrTrailingWhitespaces = HasLeadingOrTrailingWhitespaces(description);
649+
if (hasLeadingOrTrailingWhitespaces)
634650
{
635-
var fixedDescription = description + ".";
651+
description = RemoveLeadingAndTrailingWhitespaces(description);
652+
}
653+
654+
var endsWithPunctuation = EndsWithPunctuation(description);
655+
656+
if (!endsWithPunctuation || hasLeadingOrTrailingWhitespaces)
657+
{
658+
var fixedDescription = !endsWithPunctuation ? description + "." : description;
659+
636660
ReportDefineDiagnosticArgumentCorrectlyDiagnostic(DefineDiagnosticDescriptionCorrectlyRule,
637661
argumentOperation, fixedDescription, fixLocation, reportDiagnostic);
638662
}
@@ -859,6 +883,15 @@ private static string RemoveTrailingPunctuation(string s)
859883
return s[0..^1];
860884
}
861885

886+
private static bool HasLeadingOrTrailingWhitespaces(string s)
887+
=> s.Trim().Length != s.Length;
888+
889+
private static string RemoveLeadingAndTrailingWhitespaces(string s)
890+
{
891+
Debug.Assert(HasLeadingOrTrailingWhitespaces(s));
892+
return s.Trim();
893+
}
894+
862895
private static void AnalyzeHelpLinkUri(
863896
OperationAnalysisContext operationAnalysisContext,
864897
ImmutableArray<IArgumentOperation> creationArguments,

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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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)