-
-
Notifications
You must be signed in to change notification settings - Fork 519
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
d130e94
258b3a5
c33ae93
2fab295
409f1dd
b215081
7dc4bd4
b8b4fc5
0f6a099
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 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p | |||||
| $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_whitespace( $matched_content, $param_name, $param_info ); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -804,6 +805,42 @@ private function check_string_has_no_html_wrapper( $matched_content, $param_name | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Check if a translatable string has leading or trailing whitespace. | ||||||
| * | ||||||
| * @since 3.4.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_whitespace( $matched_content, $param_name, $param_info ) { | ||||||
|
Contributor
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. Nitpick: maybe |
||||||
| $content_without_quotes = TextStrings::stripQuotes( $param_info['clean'] ); | ||||||
| $first_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, $param_info['start'], ( $param_info['end'] + 1 ), true ); | ||||||
|
|
||||||
| if ( preg_match( '/^\s+/u', $content_without_quotes ) ) { | ||||||
|
Contributor
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. Nitpick: I believe the
Suggested change
Contributor
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. I wonder about the use of the If it is important to match non-ASCII whitespace, I'd suggest adding tests to cover the cases the sniff should catch (presumably the ones you mentioned here). Currently, the tests pass if the |
||||||
| $this->phpcsFile->addError( | ||||||
| 'Translatable string should not have leading whitespace. Found: %s', | ||||||
| $first_non_empty, | ||||||
| 'LeadingWhiteSpace', | ||||||
| array( $param_info['clean'] ) | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| if ( preg_match( '/\s+$/u', $content_without_quotes ) ) { | ||||||
| $this->phpcsFile->addError( | ||||||
| 'Translatable string should not have trailing whitespace. Found: %s', | ||||||
| $first_non_empty, | ||||||
| 'TrailingWhiteSpace', | ||||||
| array( $param_info['clean'] ) | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Check for inconsistencies in the placeholders between single and plural form of the translatable text string. | ||||||
| * | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -328,4 +328,56 @@ MyNamespace\__( 'foo', 'my-slug' ); // Ok. | |
| namespace\esc_html_e( 'foo', '' ); // The sniff should start flagging this once it can resolve relative namespaces. | ||
| namespace\Sub\translate( 'foo', 'my-slug' ); // Ok. | ||
|
|
||
| __( ' string', 'my-slug' ); // Bad: leading space. | ||
|
Contributor
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. Looking at the new test cases for this check, I wonder if there is a reason to have duplicate tests that differ only in function name? They seem redundant to me because the method's logic doesn't vary based on the function name. To me, the tests should cover different whitespace positions, whitespace types, and potentially parameter names, plus a few variations in whitespace quantity to guard against future regressions. What do you think? |
||
| _e( 'string ', 'my-slug' ); // Bad: trailing space. | ||
| _x( ' string ', 'context', 'my-slug' ); // Bad: leading and trailing spaces. | ||
| _ex( ' string', 'context', 'my-slug' ); // Bad: leading spaces. | ||
| _n( ' There is %1$d monkey in the %2$s', 'In the %2$s there are %1$d monkeys', $number, 'my-slug' ); // Bad: leading space. | ||
| _n( 'There is %1$d monkey in the %2$s', 'In the %2$s there are %1$d monkeys ', $number, 'my-slug' ); // Bad: trailing space. | ||
| _n( ' There is %1$d monkey in the %2$s', 'In the %2$s there are %1$d monkeys ', $number, 'my-slug' ); // Bad: leading and trailing spaces. | ||
| _nx( ' I have %d cat.', 'I have %d cats.', $number, 'Not really.', 'my-slug' ); // Bad: leading spaces. | ||
| _nx( 'I have %d cat.', 'I have %d cats. ', $number, 'Not really.', 'my-slug' ); // Bad: trailing spaces. | ||
| _nx( ' I have %d cat.', 'I have %d cats. ', $number, 'Not really.', 'my-slug' ); // Bad: leading and trailing spaces. | ||
| _n_noop( ' I have %d cat.', 'I have %d cats.', 'my-slug' ); // Bad: leading space. | ||
| _n_noop( 'I have %d cat.', 'I have %d cats. ', 'my-slug' ); // Bad: trailing space. | ||
| _n_noop( ' I have %d cat.', 'I have %d cats. ', 'my-slug' ); // Bad: leading and trailing spaces. | ||
| _nx_noop( ' I have %d cat.', 'I have %d cats.', 'Not really.', 'my-slug' ); // Bad: leading spaces. | ||
| _nx_noop( 'I have %d cat.', 'I have %d cats. ', 'Not really.', 'my-slug' ); // Bad: trailing spaces. | ||
| _nx_noop( ' I have %d cat.', 'I have %d cats. ', 'Not really.', 'my-slug' ); // Bad: leading and trailing spaces. | ||
| esc_html__( ' string', 'my-slug' ); // Bad: leading space. | ||
| esc_html_e( 'string ', 'my-slug' ); // Bad: trailing space. | ||
| esc_html_x( ' string ', 'context', 'my-slug' ); // Bad: leading and trailing spaces. | ||
| esc_attr__( ' string', 'my-slug' ); // Bad: leading space. | ||
| esc_attr_e( 'string ', 'my-slug' ); // Bad: trailing space. | ||
| esc_attr_x( ' string ', 'context', 'my-slug' ); // Bad: leading and trailing spaces. | ||
| __( ' string', 'my-slug' ); // Bad: leading tab. | ||
| __( 'string ', 'my-slug' ); // Bad: trailing tab. | ||
| __( ' string ', 'my-slug' ); // Bad: leading and trailing tabs. | ||
| __( ' string', 'my-slug' ); // Bad: two leading tabs. | ||
| __( 'string ', 'my-slug' ); // Bad: two trailing tabs. | ||
| __( "String with leading vertical tab", 'my-slug' ); // Bad: leading vertical tab. | ||
| __( "String with trailing vertical tab", 'my-slug' ); // Bad: trailing vertical tab. | ||
| __( "String with leading and trailing vertical tabs", 'my-slug' ); // Bad: leading and trailing vertical tabs. | ||
| __( "String with 2 leading vertical tabs", 'my-slug' ); // Bad: 2 leading vertical tabs. | ||
| __( "String with 2 trailing vertical tabs", 'my-slug' ); // Bad: 2 trailing vertical tabs. | ||
| __( ' | ||
| string', 'my-slug' ); // Bad: leading new line. | ||
| __( 'string | ||
| ', 'my-slug' ); // Bad: trailing new line. | ||
| __( ' | ||
| string | ||
| ', 'my-slug' ); // Bad: leading and trailing new line. | ||
| __( ' | ||
|
|
||
| string', 'my-slug' ); // Bad: two leading new lines. | ||
| __( 'string | ||
|
|
||
| ', 'my-slug' ); // Bad: two trailing new lines. | ||
| __( ' string', 'my-slug' ); // Bad: leading tab + space (1 leading whitespace error). | ||
| __( 'string ', 'my-slug' ); // Bad: trailing space + tab (1 trailing whitespace error). | ||
| __( ' string ', 'my-slug' ); // Bad: mixed leading and trailing whitespace (2 errors). | ||
| __( ' | ||
| string | ||
| ', 'my-slug' ); // Bad: leading newline+tab and trailing tab+newline (2 errors). | ||
|
|
||
| // phpcs:enable WordPress.WP.I18n.MissingTranslatorsComment | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
This method accepts
$matched_contentand$param_namebut doesn't use either. The siblingcheck_string_has_no_html_wrapper()and other related methods (introduced in c9e8dbd) have the same pattern.@jrfnl, is there a reason for the uniform signature with unused parameters that I'm missing?
If not, I lean toward dropping the unused parameters for
check_string_has_no_leading_trailing_whitespace().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.
IIRC, in the original sniff (before extensive refactoring), most, if not all, the "sub-functions" were called via
call_user_func(), which meant the expected parameters of these "sub-functions" needed to be the same.You may very well be correct in that the various refactoring done since that time have made that requirement redundant. This would need to be verified and confirmed and if so, any method signatures for
privatemethods can be cleaned up.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.
Thanks for the additional details, @jrfnl.
@amieiro, considering the above, I think it is better to remove the unused parameters from
check_string_has_no_leading_trailing_whitespace(). No need to change the other methods.