Schenkerx has uploaded a new change for review.
https://gerrit.wikimedia.org/r/276466
Change subject: Improve invalid whitespaces in comment detection.
......................................................................
Improve invalid whitespaces in comment detection.
- The original version does not detect the situation where tabs and
spaces mixed up.
- Now it will add a single space after the comment delimeter which there is no
space
between delimeter and comment.
- The fix procedures are more robust now.
Bug: T129474
Change-Id: Ib74e7aeeb779de32e6e73c382cf03cb3bc0fc7c0
---
M MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
M MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php
2 files changed, 58 insertions(+), 45 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer
refs/changes/66/276466/1
diff --git a/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
b/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
index 0a4a7a4..c2ff45c 100644
--- a/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
+++ b/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
@@ -36,49 +36,60 @@
$stackPtr,
'EmptyComment'
);
- // Checking whether there is a space between the
comment delimiter
- // and the comment
- } elseif ( substr( $currToken['content'], 0, 2 ) ===
'//'
- && $currToken['content'][2] !== ' '
- ) {
- $error = 'Single space expected between "//"
and comment';
- $fix = $phpcsFile->addFixableWarning( $error,
$stackPtr,
- 'SingleSpaceBeforeSingleLineComment'
- );
- if ( $fix === true ) {
- $content = $currToken['content'];
- $newContent = preg_replace(
'/^\/\/\t?/', '// ', $content );
- $phpcsFile->fixer->replaceToken(
$stackPtr, $newContent );
- }
- // Finding what the comment delimiter is and checking
whether there is a space
- // between the comment delimiter and the comment.
- } elseif ( $currToken['content'][0] === '#' ) {
- // Find number of `#` used.
- $startComment = 0;
- while ( $currToken['content'][$startComment]
=== '#' ) {
- $startComment += 1;
- }
- if ( $currToken['content'][$startComment] !== '
' ) {
- $error = 'Single space expected between
"#" and comment';
- $fix = $phpcsFile->addFixableWarning(
$error, $stackPtr,
-
'SingleSpaceBeforeSingleLineComment'
- );
- if ( $fix === true ) {
- $content =
$currToken['content'];
- $delimiter = substr(
$currToken['content'], 0, $startComment );
- if ( $content[$startComment+1]
=== '\t' ) {
- $newContent =
preg_replace(
- '/^' .
$delimiter . '\t/', $delimiter . ' ', $content
- );
- } else {
- $newContent =
preg_replace(
- '/^' .
$delimiter . '/', $delimiter . ' ', $content
- );
- }
-
$phpcsFile->fixer->replaceToken( $stackPtr, $newContent );
- }
- }
- }
+ // If the comment delimeter is "//"
+ } elseif ( substr( $currToken['content'], 0, 2 ) ===
'//' ) {
+ $error = 'Single space expected between "//" and comment';
+ // No space between comment delimeter and the comment
+ if ( $currToken['content'][2] !== ' ' ) {
+ $fix = $phpcsFile->addFixableWarning( $error, $stackPtr,
+ 'SingleSpaceBeforeSingleLineComment'
+ );
+ if ( $fix === true ) {
+ $content = $currToken['content'];
+ $newContent = substr_replace( $content, ' ', 2, 0 );
+ $phpcsFile->fixer->replaceToken( $stackPtr,
$newContent );
+ }
+ // More than one spaces or tabs between comment delimeter
+ // and the comment
+ } elseif ( preg_match( '/^\/\/((\s{2,})|(\t{1,}))/',
+ rtrim( $currToken['content'] ) )
+ ) {
+ $fix = $phpcsFile->addFixableWarning( $error, $stackPtr,
+ 'SingleSpaceBeforeSingleLineComment'
+ );
+ if ( $fix === true ) {
+ $content = $currToken['content'];
+ $newContent = preg_replace( '/^\/\/\s+/', '// ',
$content );
+ $phpcsFile->fixer->replaceToken( $stackPtr,
$newContent );
+ }
+ }
+ // If the comment delimeter is "#"
+ } elseif ( $currToken['content'][0] === '#' ) {
+ $error = 'Single space expected between "#" and comment';
+ // No space between comment delimeter and the comment
+ if ( $currToken['content'][1] !== ' ' ) {
+ $fix = $phpcsFile->addFixableWarning( $error, $stackPtr,
+ 'SingleSpaceBeforeSingleLineComment'
+ );
+ if ( $fix === true ) {
+ $content = $currToken['content'];
+ $newContent = substr_replace( $content, ' ', 1, 0 );
+ $phpcsFile->fixer->replaceToken( $stackPtr,
$newContent );
+ }
+ // More than one spaces or tabs between comment delimeter
+ // and the comment
+ } elseif ( preg_match( '/^#((\s{2,})|(\t{1,}))/',
+ rtrim( $currToken['content'] ) ) ) {
+ $fix = $phpcsFile->addFixableWarning( $error, $stackPtr,
+ 'SingleSpaceBeforeSingleLineComment'
+ );
+ if ( $fix === true ) {
+ $content = $currToken['content'];
+ $newContent = preg_replace( '/^#\s+/', '# ', $content
);
+ $phpcsFile->fixer->replaceToken( $stackPtr,
$newContent );
+ }
+ }
+ }
}
}
}
diff --git
a/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php
b/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php
index c5016dc..ccc4d3e 100644
---
a/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php
+++
b/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php
@@ -1,8 +1,10 @@
<?php
+// a comment with multiple spaces
// a comment with tabs instead of a space
-//
+//
+# yet another comment with multiple spaces
# yet another comment with tabs instead of a space.
-#
+#
//A comment without a space
#Yup, no spaces.
--
To view, visit https://gerrit.wikimedia.org/r/276466
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib74e7aeeb779de32e6e73c382cf03cb3bc0fc7c0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Schenkerx <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits