-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add ESLint rule to prevent usage of the verb 'toggle' in translatable strings #67741
Add ESLint rule to prevent usage of the verb 'toggle' in translatable strings #67741
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Hey @afercia, |
@swissspidy, I think you've got the most experience with custom i18n ESLint rules. Do you mind having a look at this? |
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.
IMHO such a rule is too niche/specific for this specific string. If anything, it should use a user-configurable blocklist. It's also very specific for WordPress/Gutenberg, so I don't think eslint-plugin
is a good fit for it.
I would just use no-restricted-syntax
for this. There's a lot of precedent for this in eslintrc.js
:
Lines 86 to 140 in 2ee383c
const restrictedSyntax = [ | |
// NOTE: We can't include the forward slash in our regex or | |
// we'll get a `SyntaxError` (Invalid regular expression: \ at end of pattern) | |
// here. That's why we use \\u002F in the regexes below. | |
{ | |
selector: | |
'ImportDeclaration[source.value=/^@wordpress\\u002F.+\\u002F/]', | |
message: 'Path access on WordPress dependencies is not allowed.', | |
}, | |
{ | |
selector: | |
'CallExpression[callee.name="deprecated"] Property[key.name="version"][value.value=/' + | |
majorMinorRegExp + | |
'/]', | |
message: | |
'Deprecated functions must be removed before releasing this version.', | |
}, | |
{ | |
selector: | |
'CallExpression[callee.object.name="page"][callee.property.name="waitFor"]', | |
message: | |
'This method is deprecated. You should use the more explicit API methods available.', | |
}, | |
{ | |
selector: | |
'CallExpression[callee.object.name="page"][callee.property.name="waitForTimeout"]', | |
message: 'Prefer page.waitForSelector instead.', | |
}, | |
{ | |
selector: 'JSXAttribute[name.name="id"][value.type="Literal"]', | |
message: | |
'Do not use string literals for IDs; use withInstanceId instead.', | |
}, | |
{ | |
// Discourage the usage of `Math.random()` as it's a code smell | |
// for UUID generation, for which we already have a higher-order | |
// component: `withInstanceId`. | |
selector: | |
'CallExpression[callee.object.name="Math"][callee.property.name="random"]', | |
message: | |
'Do not use Math.random() to generate unique IDs; use withInstanceId instead. (If you’re not generating unique IDs: ignore this message.)', | |
}, | |
{ | |
selector: | |
'CallExpression[callee.name="withDispatch"] > :function > BlockStatement > :not(VariableDeclaration,ReturnStatement)', | |
message: | |
'withDispatch must return an object with consistent keys. Avoid performing logic in `mapDispatchToProps`.', | |
}, | |
{ | |
selector: | |
'LogicalExpression[operator="&&"][left.property.name="length"][right.type="JSXElement"]', | |
message: | |
'Avoid truthy checks on length property rendering, as zero length is rendered verbatim.', | |
}, | |
]; |
The following rule works just fine:
{
selector: 'Literal[value=/^toggle/i]',
message: "Avoid using the verb 'Toggle' in translatable strings",
},
Hi @swissspidy, I used This approach helps avoid false positives in translatable strings. Please take a look at my PR and let me know your feedback. |
Looks much better! Now we just need to fix the 20 newly reported errors :) |
Upon reviewing the errors flagged by the current rule, it appears that the rule is raising errors for the word "toggle" whenever it appears in any string literal, including cases where it is not inside a translation function (such as in Examples 11, 12, 13, etc.). However, our initial goal was to only prevent the use of the word "toggle" in translation-ready functions like __() and similar. Could you please advise on how to adjust the rule to restrict the error only to those specific translation contexts, and prevent it from flagging usage of "toggle" in other non-translation-related string literals? Thank you in advance for your guidance!
|
So how about |
Thank you very much, @swissspidy. I really appreciate it. After adopting the new approach, the errors have been reduced to three, which are string literals in the __() functions. I believe these might have been missed previously.
Click to view codegutenberg/packages/block-editor/src/components/responsive-block-control/index.js Lines 59 to 61 in 287d653
gutenberg/packages/block-library/src/query-pagination/query-pagination-label-control.js Lines 12 to 14 in 287d653
gutenberg/packages/block-library/src/table-of-contents/edit.js Lines 119 to 127 in 84fc197
|
So can we fix them? :-) |
Could you please suggest which of the following revised strings would be the most appropriate?
|
I'd say:
|
@im3dabasia, the Query Pagination fix has been merged. Rebasing will resolve the failing Static Analysis check. |
Thank you for the update. I will go ahead and rebase my branch right away |
a4fb20a
to
d54337c
Compare
Closes : #66372
What?
As described in the above issue, the Gutenberg repository moved away from using the verb "toggle" because it was causing issues with translations. A new issue was proposed (#66372) to add an ESLint rule to prevent contributors from using this word in translation-ready functions like __().
Why?
The usage of the verb "toggle" was causing incorrect translations in the core, as it does not translate well in many languages.
How?
An ESLint rule was added to prevent the usage of the verb "toggle."
Screenshots or screencast
Screen.Recording.2024-12-11.at.11.38.52.AM.mov