Skip to content

Commit d03a56f

Browse files
committed
♻️ refactor(I18n): consolidate leading/trailing whitespace check into two error codes
- replace eight per-character error codes (Leading/Trailing x Spaces/Tabs/VTabs/NewLines) with two generic codes: LeadingWhiteSpace and TrailingWhiteSpace - reduce the eight hex-code regexes to a single `\s` match on each side (covers space, tab, vertical tab, newline, CR, form feed in one pass) - reword error messages to use the generic term "whitespace" - bump @SInCE annotation from 3.2.0 to 3.4.0 - add four combination test cases (leading tab+space, trailing space+tab, mixed leading/trailing, multi-line with newline+tab on each side) to verify strings with multiple whitespace types produce at most one leading and one trailing error
1 parent df86492 commit d03a56f

4 files changed

Lines changed: 26 additions & 83 deletions

File tree

WordPress/Sniffs/WP/I18nSniff.php

Lines changed: 10 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
380380
$has_content = $this->check_string_has_translatable_content( $matched_content, $param_name, $param_info );
381381
if ( true === $has_content ) {
382382
$this->check_string_has_no_html_wrapper( $matched_content, $param_name, $param_info );
383-
$this->check_string_has_no_leading_trailing_spaces( $matched_content, $param_name, $param_info );
383+
$this->check_string_has_no_leading_trailing_whitespace( $matched_content, $param_name, $param_info );
384384
}
385385
}
386386
}
@@ -806,9 +806,9 @@ private function check_string_has_no_html_wrapper( $matched_content, $param_name
806806
}
807807

808808
/**
809-
* Check if a translatable string has leading or trailing spaces.
809+
* Check if a translatable string has leading or trailing whitespace.
810810
*
811-
* @since 3.2.0
811+
* @since 3.4.0
812812
*
813813
* @param string $matched_content The token content (function name) which was matched
814814
* in lowercase.
@@ -818,97 +818,24 @@ private function check_string_has_no_html_wrapper( $matched_content, $param_name
818818
*
819819
* @return void
820820
*/
821-
private function check_string_has_no_leading_trailing_spaces( $matched_content, $param_name, $param_info ) {
822-
// Strip surrounding quotes.
821+
private function check_string_has_no_leading_trailing_whitespace( $matched_content, $param_name, $param_info ) {
823822
$content_without_quotes = TextStrings::stripQuotes( $param_info['clean'] );
824823
$first_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, $param_info['start'], ( $param_info['end'] + 1 ), true );
825824

826-
// Define regex patterns.
827-
$pattern_leading_spaces = '/^[\x20]+/u';
828-
$pattern_trailing_spaces = '/[\x20]+$/u';
829-
$pattern_leading_tabs = '/^\x09+/u';
830-
$pattern_trailing_tabs = '/\x09+$/u';
831-
$pattern_leading_vtabs = '/^\x0B+/u';
832-
$pattern_trailing_vtabs = '/\x0B+$/u';
833-
$pattern_leading_newlines = '/^\x0A+/u';
834-
$pattern_trailing_newlines = '/\x0A+$/u';
835-
836-
// Check for leading spaces.
837-
if ( preg_match( $pattern_leading_spaces, $content_without_quotes ) ) {
838-
$this->phpcsFile->addError(
839-
'Translatable string should not have leading spaces. Found: %s',
840-
$first_non_empty,
841-
'LeadingSpaces',
842-
array( $param_info['clean'] )
843-
);
844-
}
845-
846-
// Check for trailing spaces.
847-
if ( preg_match( $pattern_trailing_spaces, $content_without_quotes ) ) {
848-
$this->phpcsFile->addError(
849-
'Translatable string should not have trailing spaces. Found: %s',
850-
$first_non_empty,
851-
'TrailingSpaces',
852-
array( $param_info['clean'] )
853-
);
854-
}
855-
856-
// Check for leading tabs.
857-
if ( preg_match( $pattern_leading_tabs, $content_without_quotes ) ) {
858-
$this->phpcsFile->addError(
859-
'Translatable string should not have leading tabs. Found: %s',
860-
$first_non_empty,
861-
'LeadingTabs',
862-
array( $param_info['clean'] )
863-
);
864-
}
865-
866-
// Check for trailing tabs.
867-
if ( preg_match( $pattern_trailing_tabs, $content_without_quotes ) ) {
825+
if ( preg_match( '/^\s+/u', $content_without_quotes ) ) {
868826
$this->phpcsFile->addError(
869-
'Translatable string should not have trailing tabs. Found: %s',
827+
'Translatable string should not have leading whitespace. Found: %s',
870828
$first_non_empty,
871-
'TrailingTabs',
872-
array( $param_info['clean'] )
873-
);
874-
}
875-
876-
// Check for leading vertical tabs.
877-
if ( preg_match( $pattern_leading_vtabs, $content_without_quotes ) ) {
878-
$this->phpcsFile->addError(
879-
'Translatable string should not have leading vertical tabs. Found: %s',
880-
$first_non_empty,
881-
'LeadingVTabs',
882-
array( $param_info['clean'] )
883-
);
884-
}
885-
886-
// Check for trailing vertical tabs.
887-
if ( preg_match( $pattern_trailing_vtabs, $content_without_quotes ) ) {
888-
$this->phpcsFile->addError(
889-
'Translatable string should not have trailing vertical tabs. Found: %s',
890-
$first_non_empty,
891-
'TrailingVTabs',
829+
'LeadingWhiteSpace',
892830
array( $param_info['clean'] )
893831
);
894832
}
895833

896-
// Check for leading new lines.
897-
if ( preg_match( $pattern_leading_newlines, $content_without_quotes ) ) {
898-
$this->phpcsFile->addError(
899-
'Translatable string should not have leading new lines. Found: %s',
900-
$first_non_empty,
901-
'LeadingNewLines',
902-
array( $param_info['clean'] )
903-
);
904-
}
905-
906-
// Check for trailing new lines.
907-
if ( preg_match( $pattern_trailing_newlines, $content_without_quotes ) ) {
834+
if ( preg_match( '/\s+$/u', $content_without_quotes ) ) {
908835
$this->phpcsFile->addError(
909-
'Translatable string should not have trailing new lines. Found: %s',
836+
'Translatable string should not have trailing whitespace. Found: %s',
910837
$first_non_empty,
911-
'TrailingNewLines',
838+
'TrailingWhiteSpace',
912839
array( $param_info['clean'] )
913840
);
914841
}

WordPress/Tests/WP/I18nUnitTest.1.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,5 +362,11 @@ string', 'my-slug' ); // Bad: two leading new lines.
362362
__( 'string
363363
364364
', 'my-slug' ); // Bad: two trailing new lines.
365+
__( ' string', 'my-slug' ); // Bad: leading tab + space (1 leading whitespace error).
366+
__( 'string ', 'my-slug' ); // Bad: trailing space + tab (1 trailing whitespace error).
367+
__( ' string ', 'my-slug' ); // Bad: mixed leading and trailing whitespace (2 errors).
368+
__( '
369+
string
370+
', 'my-slug' ); // Bad: leading newline+tab and trailing tab+newline (2 errors).
365371

366372
// phpcs:enable WordPress.WP.I18n.MissingTranslatorsComment

WordPress/Tests/WP/I18nUnitTest.1.inc.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,5 +362,11 @@ string', 'my-slug' ); // Bad: two leading new lines.
362362
__( 'string
363363

364364
', 'my-slug' ); // Bad: two trailing new lines.
365+
__( ' string', 'my-slug' ); // Bad: leading tab + space (1 leading whitespace error).
366+
__( 'string ', 'my-slug' ); // Bad: trailing space + tab (1 trailing whitespace error).
367+
__( ' string ', 'my-slug' ); // Bad: mixed leading and trailing whitespace (2 errors).
368+
__( '
369+
string
370+
', 'my-slug' ); // Bad: leading newline+tab and trailing tab+newline (2 errors).
365371

366372
// phpcs:enable WordPress.WP.I18n.MissingTranslatorsComment

WordPress/Tests/WP/I18nUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ public function getErrorList( $testFile = '' ) {
185185
356 => 2,
186186
359 => 1,
187187
362 => 1,
188+
365 => 1,
189+
366 => 1,
190+
367 => 2,
191+
368 => 2,
188192
);
189193

190194
case 'I18nUnitTest.2.inc':

0 commit comments

Comments
 (0)