Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* rspec scaffolding * Remove unnecessary usings * Create initial skeleton for logic * Implement the simplest case and some testcases, minor refactoring * Implement single-line documentation comments * Add Comment SyntaxTrivia Kinds on the Facades * Refactor CommentsShouldNotBeEmpty * Add a lot of testcases for CommentsShouldNotBeEmpty (C#) * Minor changes on S4663 C# Testcases * Minor refactoring on S4663 (C#) * Add testcases for S4663 VB.NET * Happy New Year for S4663 * Remove empty line * Refactor the logic of S4663 * Add a lot of testcases for S4663 * Update dotnet ITs expected for Ember * Apply PR Review Changes * Appy PR Review changes * Move some logic to the base for S4663 * Apply some more PR Review changes * Move some members of the facades in the correct alphabetical order * Refactor partitioner of S4663 and minor refactor on a helper * Inline a simple local function * Final PR Review changes * Add some whitespace that i removed * Move some members of the VB facade around * Move two SyntaxKinds in the correct alphabetical order in the C# Facade
- Loading branch information
1 parent
ae8b656
commit 81d0d19
Showing
23 changed files
with
1,008 additions
and
14 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
analyzers/its/expected/Ember-MM/EmberAPI-{208AA35E-C6AE-4D2D-A9DD-B6EFD19A4279}-S4663.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"issues": [ | ||
{ | ||
"id": "S4663", | ||
"message": "Remove this empty comment", | ||
"location": { | ||
"uri": "sources\Ember-MM\EmberAPI\clsAPICommon.vb", | ||
"region": { | ||
"startLine": 577, | ||
"startColumn": 19, | ||
"endLine": 577, | ||
"endColumn": 21 | ||
} | ||
} | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
...its/expected/Ember-MM/scraper.EmberCore-{EF6A550E-DD76-4F4D-8250-8598140F828B}-S4663.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"issues": [ | ||
{ | ||
"id": "S4663", | ||
"message": "Remove this empty comment", | ||
"location": { | ||
"uri": "sources\Ember-MM\Addons\scraper.EmberCore\scraperMovieNativeModule.vb", | ||
"region": { | ||
"startLine": 364, | ||
"startColumn": 104, | ||
"endLine": 364, | ||
"endColumn": 105 | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<p>An empty comment is likely to be a mistake and doesn’t help to improve the readability of the code. For these reasons, it should be removed.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
// | ||
|
||
/// | ||
|
||
/** | ||
|
||
*/ | ||
|
||
/* | ||
|
||
*/ | ||
</pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"title": "Comments should not be empty", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "1min" | ||
}, | ||
"tags": [], | ||
"defaultSeverity": "Minor", | ||
"ruleSpecification": "RSPEC-4663", | ||
"sqKey": "S4663", | ||
"scope": "Main", | ||
"quickfix": "unknown" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -241,6 +241,7 @@ | |
"S4583", | ||
"S4586", | ||
"S4635", | ||
"S4663", | ||
"S4790", | ||
"S4792", | ||
"S4830", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<p>An empty comment is likely to be a mistake and doesn’t help to improve the readability of the code. For these reasons, it should be removed.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
' | ||
|
||
''' | ||
</pre> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"title": "Comments should not be empty", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "1min" | ||
}, | ||
"tags": [], | ||
"defaultSeverity": "Minor", | ||
"ruleSpecification": "RSPEC-4663", | ||
"sqKey": "S4663", | ||
"scope": "Main", | ||
"quickfix": "unknown" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,7 @@ | |
"S4581", | ||
"S4583", | ||
"S4586", | ||
"S4663", | ||
"S4790", | ||
"S4792", | ||
"S4830", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
analyzers/src/SonarAnalyzer.CSharp/Rules/CommentsShouldNotBeEmpty.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2023 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System.Text; | ||
|
||
namespace SonarAnalyzer.Rules.CSharp; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class CommentsShouldNotBeEmpty : CommentsShouldNotBeEmptyBase<SyntaxKind> | ||
{ | ||
protected override ILanguageFacade<SyntaxKind> Language => CSharpFacade.Instance; | ||
|
||
protected override string GetCommentText(SyntaxTrivia trivia) => | ||
trivia.Kind() switch | ||
{ | ||
SyntaxKind.SingleLineCommentTrivia => GetSingleLineText(trivia), | ||
SyntaxKind.MultiLineCommentTrivia => GetMultiLineText(trivia), | ||
SyntaxKind.SingleLineDocumentationCommentTrivia => GetSingleLineDocumentationText(trivia), | ||
SyntaxKind.MultiLineDocumentationCommentTrivia => GetMultiLineDocumentationText(trivia), | ||
}; | ||
|
||
// // | ||
private static string GetSingleLineText(SyntaxTrivia trivia) => | ||
trivia.ToString().Trim().Substring(2); | ||
|
||
// /// | ||
private static string GetSingleLineDocumentationText(SyntaxTrivia trivia) | ||
{ | ||
var stringBuilder = new StringBuilder(); | ||
foreach (var line in trivia.ToFullString().Split(MetricsBase.LineTerminators, StringSplitOptions.None)) | ||
{ | ||
var trimmedLine = line.TrimStart(null); | ||
trimmedLine = trimmedLine.StartsWith("///") | ||
? trimmedLine.Substring(3).Trim() | ||
: trimmedLine.TrimEnd(null); | ||
stringBuilder.Append(trimmedLine); | ||
} | ||
return stringBuilder.ToString(); | ||
} | ||
|
||
// /* */ | ||
private static string GetMultiLineText(SyntaxTrivia trivia) => | ||
ParseMultiLine(trivia.ToString(), 2); // Length of "/*" | ||
|
||
// /** */ | ||
private static string GetMultiLineDocumentationText(SyntaxTrivia trivia) => | ||
ParseMultiLine(trivia.ToFullString(), 3); // Length of "/**" | ||
|
||
private static string ParseMultiLine(string commentText, int initialTrimSize) | ||
{ | ||
commentText = commentText.Trim().Substring(initialTrimSize); | ||
if (commentText.EndsWith("*/", StringComparison.Ordinal)) // Might be unclosed, still reported | ||
{ | ||
commentText = commentText.Substring(0, commentText.Length - 2); | ||
} | ||
|
||
var stringBuilder = new StringBuilder(); | ||
foreach (var line in commentText.Split(MetricsBase.LineTerminators, StringSplitOptions.None)) | ||
{ | ||
var trimmedLine = line.TrimStart(null); | ||
if (trimmedLine.StartsWith("*", StringComparison.Ordinal)) | ||
{ | ||
trimmedLine = trimmedLine.TrimStart('*'); | ||
} | ||
|
||
stringBuilder.Append(trimmedLine.Trim()); | ||
} | ||
return stringBuilder.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.