babel-plugin-replace-textdomain: detect aliased imports from the i18n ESM module#48355
babel-plugin-replace-textdomain: detect aliased imports from the i18n ESM module#48355
Conversation
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
Code Coverage SummaryCoverage changed in 1 file.
|
anomiex
left a comment
There was a problem hiding this comment.
I was concerned that this might negatively affect build time, since it's checking the scope of every non-i18n-name call, but some quick tests showed no noticable difference. 👍
| let funcName = calleeName; | ||
| if ( ! Object.hasOwn( functions, funcName ) ) { | ||
| return; | ||
| funcName = resolveI18nAlias( path, calleeName ); |
There was a problem hiding this comment.
Will this do the right thing for the code path via the true case on line 76? e.g. code like
import { __ as __alias } from '@wordpress/i18n';
someObj.__alias( '???' );There was a problem hiding this comment.
Good catch, this case was indeed buggy. We need an additional check that the callee is not a member expression. Fixed, together with two new unit tests.
10fa5cc to
87b5331
Compare
Thanks for verifying this. Analyzing the scopes and building the graph is probably already requested by other plugins that are a part of the Babel config, it's a commonly used feature. So there's no extra new work. And the lookups should be fast and cheap. |
anomiex
left a comment
There was a problem hiding this comment.
I was going to suggest adding something to the readme about support for aliased imports, and when I checked to see what the readme currently says I note that the module isn't really WordPress-specific. It advertises itself as simply handling "gettext-style function calls".
That makes me wonder whether we should make the @wordpress/i18n import name configurable (still defaulting to @wordpress/i18n, of course) in case someone has reason to use a different import. That should be fairly straightforward: check something like opts.i18nmodule, and then pass it through to resolveI18nAlias instead of using a module-global.
OTOH, if you really don't want to, that could be done in a followup PR.
Proposed changes
Enhance the
babel-plugin-replace-textdomainplugin so that for every function call, it checks if the identifier is coming from the@wordpress/i18nimport, even with aliased name:This "real" name is then checked agains the
functionsobject instead of the alias. Thanks to this, we can reliably detect also the aliased__45calls, under any name, and can avoid the much less elegantgenerateI18nVariantshelper.Related product discussion/links
p1774956551856089-slack-C05Q5HSS013
Does this pull request change what data or activity we track or use?
No.
Testing instructions
How I tested this:
cd projects/packages/newsletter(many other packages will do, but I used this one)pnpm build-jsbuild/newsletter.jsand search forSort ascending. That's a translated string from thedataviewspackage. Verify that it has domain appended: the call should be__( 'Sort ascending', 'jetpack-newsletter' ).__( 'Sort ascending' ), that would mean that the Babel plugin is failing to do its job, no matter the method.