Skip to content

Commit

Permalink
Update resource analyzers to flag leading and trailing whitespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink committed Aug 7, 2020
1 parent 4016ef2 commit 1d4da1a
Show file tree
Hide file tree
Showing 16 changed files with 591 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -497,19 +497,19 @@
<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>
</data>
<data name="DefineDiagnosticTitleCorrectlyMessage" xml:space="preserve">
<value>The diagnostic title should not contain a period or any line return character</value>
<value>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</value>
</data>
<data name="DefineDiagnosticTitleCorrectlyTitle" xml:space="preserve">
<value>Define diagnostic title correctly</value>
</data>
<data name="DefineDiagnosticMessageCorrectlyMessage" xml:space="preserve">
<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>
<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>
</data>
<data name="DefineDiagnosticMessageCorrectlyTitle" xml:space="preserve">
<value>Define diagnostic message correctly</value>
</data>
<data name="DefineDiagnosticDescriptionCorrectlyMessage" xml:space="preserve">
<value>The diagnostic description should be one or multiple sentences ending with a punctuation sign</value>
<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>
</data>
<data name="DefineDiagnosticDescriptionCorrectlyTitle" xml:space="preserve">
<value>Define diagnostic description correctly</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,14 @@ private static void AnalyzeTitleCore(string title, IArgumentOperation argumentOp
var isMultiSentences = IsMultiSentences(title);
var endsWithPeriod = EndsWithPeriod(title);
var containsLineReturn = ContainsLineReturn(title);
if (isMultiSentences || endsWithPeriod || containsLineReturn)
var hasLeadingOrTrailingWhitespaces = HasLeadingOrTrailingWhitespaces(title);

if (isMultiSentences || endsWithPeriod || containsLineReturn || hasLeadingOrTrailingWhitespaces)
{
var fixedTitle = endsWithPeriod ? RemoveTrailingPeriod(title) : title;
fixedTitle = isMultiSentences ? FixMultiSentences(fixedTitle) : fixedTitle;
fixedTitle = containsLineReturn ? FixLineReturns(fixedTitle, allowMultisentences: false) : fixedTitle;
fixedTitle = hasLeadingOrTrailingWhitespaces ? RemoveLeadingAndTrailingWhitespaces(fixedTitle) : fixedTitle;
Debug.Assert(title != fixedTitle);

ReportDefineDiagnosticArgumentCorrectlyDiagnostic(DefineDiagnosticTitleCorrectlyRule,
Expand Down Expand Up @@ -594,7 +597,9 @@ private static void AnalyzeMessageCore(string message, IArgumentOperation argume
var isMultiSentences = IsMultiSentences(message);
var endsWithPeriod = EndsWithPeriod(message);
var containsLineReturn = ContainsLineReturn(message);
if (isMultiSentences ^ endsWithPeriod || containsLineReturn)
var hasLeadingOrTrailingWhitespaces = HasLeadingOrTrailingWhitespaces(message);

if (isMultiSentences ^ endsWithPeriod || containsLineReturn || hasLeadingOrTrailingWhitespaces)
{
var fixedMessage = containsLineReturn ? FixLineReturns(message, allowMultisentences: true) : message;
isMultiSentences = IsMultiSentences(fixedMessage);
Expand All @@ -605,6 +610,8 @@ private static void AnalyzeMessageCore(string message, IArgumentOperation argume
fixedMessage = endsWithPeriod ? RemoveTrailingPeriod(fixedMessage) : fixedMessage + ".";
}

fixedMessage = hasLeadingOrTrailingWhitespaces ? RemoveLeadingAndTrailingWhitespaces(fixedMessage) : fixedMessage;

ReportDefineDiagnosticArgumentCorrectlyDiagnostic(DefineDiagnosticMessageCorrectlyRule,
argumentOperation, fixedMessage, fixLocation, reportDiagnostic);
}
Expand All @@ -630,9 +637,14 @@ private static void AnalyzeMessageCore(string message, IArgumentOperation argume

private static void AnalyzeDescriptionCore(string description, IArgumentOperation argumentOperation, Location fixLocation, Action<Diagnostic> reportDiagnostic)
{
if (!EndsWithPunctuation(description))
var hasLeadingOrTrailingWhitespaces = HasLeadingOrTrailingWhitespaces(description);
var endsWithPunctuation = EndsWithPunctuation(description);

if (!endsWithPunctuation || hasLeadingOrTrailingWhitespaces)
{
var fixedDescription = description + ".";
var fixedDescription = !endsWithPunctuation ? description + "." : description;
fixedDescription = hasLeadingOrTrailingWhitespaces ? RemoveLeadingAndTrailingWhitespaces(fixedDescription) : fixedDescription;

ReportDefineDiagnosticArgumentCorrectlyDiagnostic(DefineDiagnosticDescriptionCorrectlyRule,
argumentOperation, fixedDescription, fixLocation, reportDiagnostic);
}
Expand Down Expand Up @@ -859,6 +871,15 @@ private static string RemoveTrailingPunctuation(string s)
return s[0..^1];
}

private static bool HasLeadingOrTrailingWhitespaces(string s)
=> s.Trim().Length != s.Length;

private static string RemoveLeadingAndTrailingWhitespaces(string s)
{
Debug.Assert(HasLeadingOrTrailingWhitespaces(s));
return s.Trim();
}

private static void AnalyzeHelpLinkUri(
OperationAnalysisContext operationAnalysisContext,
ImmutableArray<IArgumentOperation> creationArguments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
<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>
<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>
<note />
</trans-unit>
Expand All @@ -78,7 +78,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
<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>
<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>
<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>
<note />
</trans-unit>
Expand All @@ -88,8 +88,8 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
<source>The diagnostic title should not contain a period or any line return character</source>
<target state="translated">Nadpis diagnostiky by neměl obsahovat tečku ani žádný znak konce řádku.</target>
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
<target state="needs-review-translation">Nadpis diagnostiky by neměl obsahovat tečku ani žádný znak konce řádku.</target>
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
<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>
<target state="needs-review-translation">Die Diagnosebeschreibung sollte mindestens einen Satz umfassen und auf ein Satzzeichen enden.</target>
<note />
</trans-unit>
Expand All @@ -78,7 +78,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
<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>
<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>
<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>
<note />
</trans-unit>
Expand All @@ -88,8 +88,8 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
<source>The diagnostic title should not contain a period or any line return character</source>
<target state="translated">Der Titel der Diagnosemeldung darf keinen Satzendepunkt oder ein Zeilenvorschubzeichen enthalten.</target>
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
<target state="needs-review-translation">Der Titel der Diagnosemeldung darf keinen Satzendepunkt oder ein Zeilenvorschubzeichen enthalten.</target>
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
<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>
<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>
<note />
</trans-unit>
Expand All @@ -78,7 +78,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
<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>
<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>
<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>
<note />
</trans-unit>
Expand All @@ -88,8 +88,8 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
<source>The diagnostic title should not contain a period or any line return character</source>
<target state="translated">El título del diagnóstico no debe contener un punto ni un carácter de retorno de línea.</target>
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
<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>
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
<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>
<target state="needs-review-translation">La description de diagnostic doit être une ou plusieurs phrases se terminant par un signe de ponctuation.</target>
<note />
</trans-unit>
Expand All @@ -78,7 +78,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
<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>
<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>
<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>
<note />
</trans-unit>
Expand All @@ -88,8 +88,8 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
<source>The diagnostic title should not contain a period or any line return character</source>
<target state="translated">Le titre de diagnostic ne doit pas avoir de point ou de caractère de retour de ligne</target>
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
<target state="needs-review-translation">Le titre de diagnostic ne doit pas avoir de point ou de caractère de retour de ligne</target>
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
<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>
<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>
<note />
</trans-unit>
Expand All @@ -78,7 +78,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
<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>
<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>
<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>
<note />
</trans-unit>
Expand All @@ -88,8 +88,8 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
<source>The diagnostic title should not contain a period or any line return character</source>
<target state="translated">Il titolo della diagnostica non deve contenere un punto o un carattere di ritorno a capo</target>
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
<target state="needs-review-translation">Il titolo della diagnostica non deve contenere un punto o un carattere di ritorno a capo</target>
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticDescriptionCorrectlyMessage">
<source>The diagnostic description should be one or multiple sentences ending with a punctuation sign</source>
<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>
<target state="needs-review-translation">診断の説明は、句読点で終わる 1 つまたは複数の文にする必要があります。</target>
<note />
</trans-unit>
Expand All @@ -78,7 +78,7 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticMessageCorrectlyMessage">
<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>
<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>
<target state="needs-review-translation">診断メッセージには改行文字を含めないでください。また、末尾にピリオドが付いていない 1 つの文か、末尾にピリオドが付いた複数の文にする必要があります。</target>
<note />
</trans-unit>
Expand All @@ -88,8 +88,8 @@
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyMessage">
<source>The diagnostic title should not contain a period or any line return character</source>
<target state="translated">診断タイトルには、ピリオドも改行文字も含めないでください</target>
<source>The diagnostic title should not contain a period, nor any line return character, nor any leading or trailing whitespaces</source>
<target state="needs-review-translation">診断タイトルには、ピリオドも改行文字も含めないでください</target>
<note />
</trans-unit>
<trans-unit id="DefineDiagnosticTitleCorrectlyTitle">
Expand Down
Loading

0 comments on commit 1d4da1a

Please sign in to comment.