Skip to content
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

ESLint Plugin: Add I18N ESLint rules #19777

Closed
9 of 16 tasks
swissspidy opened this issue Jan 21, 2020 · 8 comments
Closed
9 of 16 tasks

ESLint Plugin: Add I18N ESLint rules #19777

swissspidy opened this issue Jan 21, 2020 · 8 comments
Labels
Internationalization (i18n) Issues or PRs related to internationalization efforts [Status] In Progress Tracking issues with work in progress [Tool] ESLint plugin /packages/eslint-plugin [Type] Enhancement A suggestion for improvement.

Comments

@swissspidy
Copy link
Member

swissspidy commented Jan 21, 2020

Is your feature request related to a problem? Please describe.

Similar to the i18n sniffs in WPCS there could be some custom ESLint rules to catch some common i18n mistakes.

Describe the solution you'd like

Off the top of my head, there could be rules like follows:

Done

  • Missing text domain
  • Mismatched text domain (would require setting the expected text domain in the ESLint config)
  • Unnecessary text domain (e.g. when passing default instead of just omitting it)
  • Missing translator comments when using sprintf with placeholders
  • Not using numbered placeholders when using %s and sprintf -> tracked separately in sprintf ESLint rule does not check for numbered placeholders #20554.

Likely to-do

On the fence

  • Non-translatable text
    Needs some way to detect whether text is passed as a prop or child that should probably be translatable (e.g. aria-label prop). Helps catch places where __() is forgotten.
  • String concatenation within __() (e.g. __( 'Hello' + '' + 'World' )
  • Suggest using _x() instead of __() under certain conditions (needs list of ambiguous terms)
  • When using markup in strings but not __experimentalCreateInterpolateElement
  • String concatenation with markup instead of using __experimentalCreateInterpolateElement
  • Using translation functions as defaultProps (when using prop-types)

Actual implementation should be split up into multiple PRs.

Describe alternatives you've considered

Find other solutions out there that likely won't work with @wordpress/i18n or build something custom.

@swissspidy swissspidy added [Type] Enhancement A suggestion for improvement. [Package] i18n /packages/i18n [Tool] ESLint plugin /packages/eslint-plugin labels Jan 21, 2020
@swissspidy swissspidy changed the title Add I18N rel Add I18N ESLint rules Feb 23, 2020
@swissspidy
Copy link
Member Author

I have a working version of text domain validation ready, will push it soon.

Passing a variable to __() et al instead of a string literal

This is currently enforced using no-restricted-syntax:

'no-restricted-syntax': [
'error',
{
selector:
'CallExpression[callee.name=/^(__|_n|_nx|_x)$/]:not([arguments.0.type=/^Literal|BinaryExpression$/])',
message:
'Translate function arguments must be string literals.',
},
{
selector:
'CallExpression[callee.name=/^(_n|_nx|_x)$/]:not([arguments.1.type=/^Literal|BinaryExpression$/])',
message:
'Translate function arguments must be string literals.',
},
{
selector:
'CallExpression[callee.name=_nx]:not([arguments.3.type=/^Literal|BinaryExpression$/])',
message:
'Translate function arguments must be string literals.',
},
],

Should be fairly easy to port this to its own proper rule.

@swissspidy swissspidy added Internationalization (i18n) Issues or PRs related to internationalization efforts and removed [Package] i18n /packages/i18n labels Feb 29, 2020
@swissspidy
Copy link
Member Author

swissspidy commented Feb 29, 2020

Needs some way to detect whether text is passed as a prop or child that should probably be translatable

Some cases, off the top of my head:

  • aria-*,title, alt attributes
  • string between tags, e.g. <div>Hello World</div> or <div>{ 'Hello World' }</div>
  • additional attributes could be added via whitelist

Might even be fixable by wrapping the found strings in __()

@swissspidy swissspidy added the [Status] In Progress Tracking issues with work in progress label Mar 2, 2020
@skorasaurus skorasaurus removed the [Status] In Progress Tracking issues with work in progress label Feb 9, 2021
@gziolo gziolo added the [Status] In Progress Tracking issues with work in progress label May 7, 2022
@gziolo
Copy link
Member

gziolo commented May 7, 2022

It looks like a lot of tasks listed are done. Excellent work! There is only one left on the list that was initially marked as “likely to do”. @swissspidy, what would be your current recommendation on criteria to consider this issue finished?

@gziolo gziolo changed the title Add I18N ESLint rules ESLint Plugin: Add I18N ESLint rules May 7, 2022
@swissspidy
Copy link
Member Author

I was just thinking the pther day that a rule for catching trailing whitespace would be nice. See https://core.trac.wordpress.org/ticket/55669

This mistake wasn‘t caught in the Gutenberg code base.

@gziolo
Copy link
Member

gziolo commented May 7, 2022

I was just thinking the pther day that a rule for catching trailing whitespace would be nice. See https://core.trac.wordpress.org/ticket/55669

This mistake wasn‘t caught in the Gutenberg code base.

I agree that it would be great to cover as much as possible from the cases that slipped through WP 6.0 Beta phase from the Gutenberg repository ❤️

@swissspidy
Copy link
Member Author

swissspidy commented Jul 4, 2022

Looks like #38225 introduced such a rule (i18n-no-flanking-whitespace)

Edit: this begs the question why it didn't catch dd7db32

@swissspidy
Copy link
Member Author

Looks like the rule was only enabled for React Native code and not the whole code base 🙃

@jhnstn Do you recall why it wasn't considered to apply the i18n-no-flanking-whitespace rule for the rest of the code base? Seems like a missed opportunity.

@gziolo
Copy link
Member

gziolo commented Jul 6, 2022

@jhnstn Do you recall why it wasn't considered to apply the i18n-no-flanking-whitespace rule for the rest of the code base? Seems like a missed opportunity.

It would be great to find out and potentially enable for the rest of Gutenberg 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Internationalization (i18n) Issues or PRs related to internationalization efforts [Status] In Progress Tracking issues with work in progress [Tool] ESLint plugin /packages/eslint-plugin [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

No branches or pull requests

3 participants