Lethexie has uploaded a new change for review.
https://gerrit.wikimedia.org/r/292313
Change subject: Fix single space expected on single line comment.
......................................................................
Fix single space expected on single line comment.
- The original version ingore the multiple space before content.
- The original version ingore the a mixture of whitespace and tab.
This change fixed the above situations.
Bug: T136791
Change-Id: I942b93ca08da61a5231a42559910b84faa378463
---
M MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
M MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php
M
MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php.expect
M
MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php.fixed
4 files changed, 33 insertions(+), 30 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer
refs/changes/13/292313/1
diff --git a/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
b/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
index 0a4a7a4..1c2b4a5 100644
--- a/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
+++ b/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php
@@ -6,12 +6,20 @@
class MediaWiki_Sniffs_WhiteSpace_SpaceBeforeSingleLineCommentSniff
implements PHP_CodeSniffer_Sniff {
// @codingStandardsIgnoreEnd
+ /**
+ * @return array
+ */
public function register() {
return [
T_COMMENT
];
}
+ /**
+ * @param PHP_CodeSniffer_File $phpcsFile PHP_CodeSniffer_File object.
+ * @param integer $stackPtr The current token index.
+ * @return void
+ */
public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) {
$tokens = $phpcsFile->getTokens();
$currToken = $tokens[$stackPtr];
@@ -27,10 +35,10 @@
}
// Checking whether the comment is an empty one
- if ( ( substr( $currToken['content'], 0, 2 ) === '//'
- && rtrim( $currToken['content'] ) ===
'//' )
- || ( $currToken['content'][0] === '#'
- && rtrim( $currToken['content'] ) ===
'#' )
+ if ( ( substr( $currToken['content'], 0, 2 ) === '//' &&
+ rtrim( $currToken['content'] ) === '//' ) ||
+ ( $currToken['content'][0] === '#' &&
+ rtrim( $currToken['content'] ) === '#' )
) {
$phpcsFile->addWarning( 'Unnecessary empty
comment found',
$stackPtr,
@@ -38,17 +46,21 @@
);
// Checking whether there is a space between the
comment delimiter
// and the comment
- } elseif ( substr( $currToken['content'], 0, 2 ) ===
'//'
- && $currToken['content'][2] !== ' '
- ) {
+ } elseif ( substr( $currToken['content'], 0, 2 ) ===
'//' ) {
+ $commentContent = substr(
$currToken['content'], 2 );
+ $commentTrim = ltrim( $commentContent );
+ if ( strlen( $commentContent ) !== ( strlen(
$commentTrim ) + 1 ) ||
+ $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 );
+ $newContent = '// ';
+ $newContent .= $commentTrim;
$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.
@@ -65,16 +77,9 @@
);
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
- );
- }
+ $newContent = '# ';
+ $tmpContent = substr( $content,
1 );
+ $newContent .= ltrim(
$tmpContent );
$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..e790e74 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,8 @@
<?php
-// a comment with tabs instead of a space
-//
-# yet another comment with tabs instead of a space.
-#
+// a comment with tabs instead of a space
+//
+# yet another comment with tabs instead of a space.
+#
//A comment without a space
#Yup, no spaces.
diff --git
a/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php.expect
b/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php.expect
index ae709f7..fb639a2 100644
---
a/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php.expect
+++
b/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php.expect
@@ -1,19 +1,17 @@
FILE: ...s/files/WhiteSpace/space_after_delim_singleline_comment_fail.php
----------------------------------------------------------------------
-FOUND 2 ERRORS AND 6 WARNINGS AFFECTING 6 LINES
+FOUND 0 ERRORS AND 6 WARNINGS AFFECTING 6 LINES
----------------------------------------------------------------------
2 | WARNING | [x] Single space expected between "//" and comment
3 | WARNING | [ ] Unnecessary empty comment found
- 3 | ERROR | [x] Whitespace found at end of line
4 | WARNING | [x] Single space expected between "#" and comment
5 | WARNING | [ ] Unnecessary empty comment found
- 5 | ERROR | [x] Whitespace found at end of line
6 | WARNING | [x] Single space expected between "//" and comment
7 | WARNING | [x] Single space expected between "#" and comment
----------------------------------------------------------------------
-PHPCBF CAN FIX THE 6 MARKED SNIFF VIOLATIONS AUTOMATICALLY
+PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
-Time: 11ms; Memory: 3.25Mb
+Time: 16ms; Memory: 3.5Mb
diff --git
a/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php.fixed
b/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php.fixed
index 193ba31..5562b76 100644
---
a/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php.fixed
+++
b/MediaWiki/Tests/files/WhiteSpace/space_after_delim_singleline_comment_fail.php.fixed
@@ -1,7 +1,7 @@
<?php
-// a comment with tabs instead of a space
+// a comment with tabs instead of a space
//
-# yet another comment with tabs instead of a space.
+# 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/292313
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I942b93ca08da61a5231a42559910b84faa378463
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/tools/codesniffer
Gerrit-Branch: master
Gerrit-Owner: Lethexie <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits