-
-
Notifications
You must be signed in to change notification settings - Fork 510
Add a check for the leading and trailing spaces #2504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
27715e8
436cd25
7f311cb
b18f6d4
70dfe78
50e118e
8d8b411
8be3ea1
fe679bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -380,6 +380,7 @@ | |||||
$has_content = $this->check_string_has_translatable_content( $matched_content, $param_name, $param_info ); | ||||||
if ( true === $has_content ) { | ||||||
$this->check_string_has_no_html_wrapper( $matched_content, $param_name, $param_info ); | ||||||
$this->check_string_has_no_leading_trailing_spaces( $matched_content, $param_name, $param_info ); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
@@ -804,6 +805,115 @@ | |||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Check if a translatable string has leading or trailing spaces. | ||||||
* | ||||||
* @since 3.2.0 | ||||||
* | ||||||
* @param string $matched_content The token content (function name) which was matched | ||||||
* in lowercase. | ||||||
* @param string $param_name The name of the parameter being examined. | ||||||
* @param array|false $param_info Parameter info array for an individual parameter, | ||||||
* as received from the PassedParameters class. | ||||||
* | ||||||
* @return void | ||||||
*/ | ||||||
private function check_string_has_no_leading_trailing_spaces( $matched_content, $param_name, $param_info ) { | ||||||
// Strip surrounding quotes. | ||||||
$content_without_quotes = TextStrings::stripQuotes( $param_info['clean'] ); | ||||||
$first_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, $param_info['start'], ( $param_info['end'] + 1 ), true ); | ||||||
|
||||||
// Define regex patterns. | ||||||
$pattern_leading_spaces = '/^[\x20]+/u'; | ||||||
$pattern_trailing_spaces = '/[\x20]+$/u'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
$pattern_leading_tabs = '/^\x09+/u'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
$pattern_trailing_tabs = '/\x09+$/u'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
$pattern_leading_vtabs = '/^\x0B+/u'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
$pattern_trailing_vtabs = '/\x0B+$/u'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
$pattern_leading_newlines = '/^\x0A+/u'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
$pattern_trailing_newlines = '/\x0A+$/u'; | ||||||
|
||||||
// Check for leading spaces. | ||||||
if ( preg_match( $pattern_leading_spaces, $content_without_quotes ) ) { | ||||||
$this->phpcsFile->addError( | ||||||
'Translatable string should not have leading spaces. Found: %s', | ||||||
$first_non_empty, | ||||||
'LeadingSpaces', | ||||||
array( $param_info['clean'] ) | ||||||
); | ||||||
} | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Check for trailing spaces. | ||||||
if ( preg_match( $pattern_trailing_spaces, $content_without_quotes ) ) { | ||||||
$this->phpcsFile->addError( | ||||||
'Translatable string should not have trailing spaces. Found: %s', | ||||||
$first_non_empty, | ||||||
'TrailingSpaces', | ||||||
array( $param_info['clean'] ) | ||||||
); | ||||||
} | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Check for leading tabs. | ||||||
if ( preg_match( $pattern_leading_tabs, $content_without_quotes ) ) { | ||||||
$this->phpcsFile->addError( | ||||||
'Translatable string should not have leading tabs. Found: %s', | ||||||
$first_non_empty, | ||||||
'LeadingTabs', | ||||||
array( $param_info['clean'] ) | ||||||
); | ||||||
} | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Check for trailing tabs. | ||||||
if ( preg_match( $pattern_trailing_tabs, $content_without_quotes ) ) { | ||||||
$this->phpcsFile->addError( | ||||||
'Translatable string should not have trailing tabs. Found: %s', | ||||||
$first_non_empty, | ||||||
'TrailingTabs', | ||||||
array( $param_info['clean'] ) | ||||||
); | ||||||
} | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Check for leading vertical tabs. | ||||||
if ( preg_match( $pattern_leading_vtabs, $content_without_quotes ) ) { | ||||||
$this->phpcsFile->addError( | ||||||
'Translatable string should not have leading vertical tabs. Found: %s', | ||||||
$first_non_empty, | ||||||
'LeadingVTabs', | ||||||
array( $param_info['clean'] ) | ||||||
); | ||||||
} | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Check for trailing vertical tabs. | ||||||
if ( preg_match( $pattern_trailing_vtabs, $content_without_quotes ) ) { | ||||||
$this->phpcsFile->addError( | ||||||
'Translatable string should not have trailing vertical tabs. Found: %s', | ||||||
$first_non_empty, | ||||||
'TrailingVTabs', | ||||||
array( $param_info['clean'] ) | ||||||
); | ||||||
} | ||||||
|
||||||
// Check for leading new lines. | ||||||
if ( preg_match( $pattern_leading_newlines, $content_without_quotes ) ) { | ||||||
$this->phpcsFile->addError( | ||||||
'Translatable string should not have leading new lines. Found: %s', | ||||||
$first_non_empty, | ||||||
'LeadingNewLines', | ||||||
array( $param_info['clean'] ) | ||||||
); | ||||||
} | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Check for trailing new lines. | ||||||
if ( preg_match( $pattern_trailing_newlines, $content_without_quotes ) ) { | ||||||
$this->phpcsFile->addError( | ||||||
'Translatable string should not have trailing new lines. Found: %s', | ||||||
$first_non_empty, | ||||||
'TrailingNewLines', | ||||||
array( $param_info['clean'] ) | ||||||
); | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Check for inconsistencies in the placeholders between single and plural form of the translatable text string. | ||||||
* | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.