jenkins-bot has submitted this change and it was merged. Change subject: Fix single space expected on single line comment. ......................................................................
Fix single space expected on single line comment. * The original version ignored multiple spaces before content. * The original version ignored the mixture of whitespaces and tabs. 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(-) Approvals: Legoktm: Looks good to me, approved jenkins-bot: Verified diff --git a/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php b/MediaWiki/Sniffs/WhiteSpace/SpaceBeforeSingleLineCommentSniff.php index 0a4a7a4..65d92b7 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 int $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: merged Gerrit-Change-Id: I942b93ca08da61a5231a42559910b84faa378463 Gerrit-PatchSet: 4 Gerrit-Project: mediawiki/tools/codesniffer Gerrit-Branch: master Gerrit-Owner: Lethexie <xietaoe...@gmail.com> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits