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

Framework: Run i18n-mixin-to-localize codemod #18590

Merged
merged 7 commits into from
Oct 10, 2017
Merged

Framework: Run i18n-mixin-to-localize codemod #18590

merged 7 commits into from
Oct 10, 2017

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Oct 5, 2017

TODO:

@psealock
Copy link
Contributor

psealock commented Oct 6, 2017

Revert PostEditor changes

@ockham Do you know why these tests were failing by chance? It seemed a reference to the Component from ReactDOM.render() was working sometimes, but not others.

@ockham
Copy link
Contributor Author

ockham commented Oct 6, 2017

@ockham Do you know why these tests were failing by chance? It seemed a reference to the Component from ReactDOM.render() was working sometimes, but not others.

I think there are two errors in play here -- 6dc6a62 was just to fix tests; turns out the default export from that file was already being localize; the codemod wrapped the unlocalized component in a localize call, which broken tests

@ockham
Copy link
Contributor Author

ockham commented Oct 6, 2017

The other error I'm currently seeing with PostEditor is this:

image

The immediate reason for this is that sections.js (as generated by server/bundler/loader) tries to access the isRetry static method from LoadingError (in client/layout/error), but that component is now wrapped in a localize. To mitigate that, one option would be to have localize hoist statics, much like react-redux' connect() does. Or maybe we can rewrite server/bundler/loader and/or client/layout/error to decouple the isRetry logic from the component.

This still doesn't fix the underlying error with the editor, but maybe we'll get more meaningful error messages to track it down once we fix the LoadingError issue.

@ockham
Copy link
Contributor Author

ockham commented Oct 6, 2017

Fix in c17bc44. I think I'll spin up a separate PR for that.

@ockham
Copy link
Contributor Author

ockham commented Oct 6, 2017

So the actual error in PostEditor is

image

and I think the reason are once again static methods --

in this case. After the codemod is run, that component is wrapped in localize. So we might have to modify i18n-calypso to hoist static methods after all.

@ockham
Copy link
Contributor Author

ockham commented Oct 6, 2017

@psealock
Copy link
Contributor

psealock commented Oct 9, 2017

Oh, we might work around that using a similar pattern as https://github.com/Automattic/wp-calypso/blob/master/client/components/tinymce/plugins/wpcom-view/views/simple-payments/index.jsx

I like this pattern better because it signals intention better than independently exported functions. However, fixing the root cause of the issue by addressing the localize function itself is the best solution.

@sirreal sirreal self-requested a review October 9, 2017 07:53
@ockham ockham force-pushed the codemod/i18n branch 3 times, most recently from 2043b8b to 6afdc24 Compare October 9, 2017 10:11
@sirreal
Copy link
Member

sirreal commented Oct 9, 2017

Grep for files containing this.props.translate but not i18n-calypso. Any takers?

$ ack -L 'i18n-calypso' $(ack --js -l 'this\.props\.(translate|moment|numberFormat)' client)

client/my-sites/checkout/cart/cart-messages-mixin.jsx
client/my-sites/sharing/connections/services/eventbrite.js
client/my-sites/sharing/connections/services/google-photos.js
client/my-sites/sharing/connections/services/instagram.js

I'll have a look at these… cracks knuckles

@sirreal
Copy link
Member

sirreal commented Oct 9, 2017

After some grepping, all the usage seems fine. The cart-messages-mixin could have some extra protection, but that's probably not necessary. See #18651 for more details.

@sirreal
Copy link
Member

sirreal commented Oct 9, 2017

Check refs. Do we need localize to hoist them?

What's the concern here? Here are some findings that may be relevant:

refs in components that need codemodding:

$ ack -l '\Wref=' $(ack --js -l 'this\.(translate|moment|numberFormat)' client )

client/blocks/author-selector/index.jsx
client/components/plans/premium-popover/index.jsx
client/components/tinymce/plugins/contact-form/dialog/field-remove-button.jsx
client/components/tinymce/plugins/wplink/dialog.jsx
client/me/profile-links/add-buttons.jsx
client/me/security-2fa-backup-codes-list/index.jsx
client/me/security-2fa-sms-settings/index.jsx
client/me/security-account-recovery/edit-email.jsx
client/my-sites/domains/components/form/country-select.jsx
client/my-sites/exporter/export-card/post-type-options.jsx
client/my-sites/importer/uploading-pane.jsx
client/my-sites/media-library/header.jsx
client/my-sites/people/team-list/index.jsx
client/my-sites/people/viewers-list/index.jsx
client/my-sites/plugins/plugin-sections/index.jsx
client/my-sites/plugins/plugin-site-update-indicator/index.jsx
client/my-sites/post-selector/selector.jsx
client/my-sites/sharing/buttons/preview-buttons.jsx
client/my-sites/stats/post-trends/day.jsx
client/my-sites/stats/post-trends/index.jsx
client/post-editor/editor-status-label/index.jsx
client/post-editor/editor-sticky/index.jsx
client/post-editor/media-modal/gallery-help.jsx
client/post-editor/post-editor.jsx

There appear to be 84 uses of ref in components with localize. Here's an example:

<Card
className={ classNames( 'app-banner', 'is-compact', currentSection ) }
ref={ this.preventNotificationsClose }
>

Is verifying that that example works fine enough?

@sirreal
Copy link
Member

sirreal commented Oct 10, 2017

Good thinking!

EDIT: I improved the original to catch a few more cases:

$ ack -l --js '(?<!this)\.refs' client

client/components/drop-zone/test/index.jsx
client/components/root-child/test/index.jsx
client/components/segmented-control/index.jsx
client/components/select-dropdown/index.jsx
client/devdocs/test/doc.jsx
client/extensions/wp-super-cache/components/advanced/directly-cached-files.jsx
client/my-sites/domains/domain-management/list/test/index.js
client/my-sites/themes/themes-magic-search-card/index.jsx

Looks like a manageable list, I'll dig further…

@sirreal
Copy link
Member

sirreal commented Oct 10, 2017

After looking a bit, cleaning up all those refs looks like it might get a bit hairy. Note the especially fun ones like this.refs[ 'item-' + newIndex ].refs.itemLink.

@ockham Let's hear what you think, but it may be best to add the ref hoisting to localize. Sounds like it should really be there anyways (at least until string refs move from "leagacy" to "deprecated").

Here's the full output of non-this refs:

$ ack --js '(?<!this)\.refs' client

client/components/drop-zone/test/index.jsx
60:             expect( tree.refs.zone.parentNode.id ).to.equal( 'container' );
72:             expect( tree.refs.zone.parentNode.id ).to.not.equal( 'container' );
73:             expect( tree.refs.zone.parentNode.parentNode ).to.eql( document.body );

client/components/root-child/test/index.jsx
55:                     expect( tree.refs.parentChild.parentNode.className ).to.equal( 'parent' );
57:                     expect( tree.refs.rootChild.parentNode.parentNode ).to.eql( document.body );
68:                     expect( tree.refs.rootChild.parentNode.className ).to.equal( 'wrapper' );
70:                     expect( tree.refs.rootChild.parentNode.parentNode.parentNode ).to.eql( document.body );

client/components/segmented-control/index.jsx
221:            ReactDom.findDOMNode( this.refs[ 'item-' + newIndex ].refs.itemLink ).focus();

client/components/select-dropdown/index.jsx
171:                                                    self.refs.dropdownContainer.focus();
401:            ReactDom.findDOMNode( this.refs[ 'item-' + newIndex ].refs.itemLink ).focus();

client/devdocs/test/doc.jsx
36:                             const html = ReactDom.findDOMNode( renderedSingleDoc.refs.body ).innerHTML;

client/extensions/wp-super-cache/components/advanced/directly-cached-files.jsx
27:             const newDirectPage = this.refs.newDirectPage.refs.textField;

client/my-sites/domains/domain-management/list/test/index.js
122:                            assert( ReactDom.findDOMNode( component.refs.cancelChangePrimaryButton ) );
126:                            const button = ReactDom.findDOMNode( component.refs.cancelChangePrimaryButton );

client/my-sites/themes/themes-magic-search-card/index.jsx
102:            const { value, selectionStart } = this.refs[ 'url-search' ].refs.searchInput;
142:                            cursorPosition: val.slice( 0, this.refs[ 'url-search' ].refs.searchInput.selectionStart )

@ockham
Copy link
Contributor Author

ockham commented Oct 10, 2017

Oh wow, thanks a lot for the grepping, really helps to see the bigger picture here!

I took a random sample from that list: client/components/root-child/test/index.jsx really only uses the ref for testing, and the tested component was already localize()d before (might be the case for a few other ones, too). I might take some more samples, but I'm kinda adamant to cross fingers and discover the problematic ones in testing. I'm a bit hesitant to add ref hoisting to i18n-calypso when string refs are on their way out...

@ockham
Copy link
Contributor Author

ockham commented Oct 10, 2017

Other instances are textbook cases of ref function attributes, e.g. client/components/segmented-control/index.jsx...

Edit: Okay, maybe a bit trickier than the textbook example -- but on the bright side, it doesn't need to be localize()d!

@ockham
Copy link
Contributor Author

ockham commented Oct 10, 2017

Here's the full output of non-this refs: [...]

I had a look at the rest of the list. Tests should be fine by definition (they wouldn't pass otherwise), and the other ones seem to either use localize() already, or aren't using translate() at all.

@ockham
Copy link
Contributor Author

ockham commented Oct 10, 2017

I just vetted form-base, see note in PR desc. Seems good. Thus: 368d5c0

@ockham ockham added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. and removed [Status] In Progress labels Oct 10, 2017
@ockham
Copy link
Contributor Author

ockham commented Oct 10, 2017

Is it me or does this thing feel way faster now that the auto-injected mixin is gone? 😮

@rachelmcr
Copy link
Member

I ran through our full e2e tests on this branch and everything looks good. 👍

@ockham ockham merged commit dc99dd9 into master Oct 10, 2017
@ockham ockham deleted the codemod/i18n branch October 10, 2017 14:07
@matticbot matticbot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Oct 10, 2017
ockham added a commit that referenced this pull request Oct 10, 2017
Fixes a bug I introduced with #18590, as reported by @jorgefilipecosta in Slack:

> If open media and select an image and then press edit I get an error related with localize it may be related with the last changes

Turns out that `EditorMediaModalDetail` is [properly `localize()`d](https://github.com/Automattic/wp-calypso/blob/e7e4265d9b65eccb771c3977b54bc0d272bc3c7b/client/post-editor/media-modal/detail/index.jsx#L100), but `my-sites/media/main` was importing the unlocalized version of the component.
@samouri
Copy link
Contributor

samouri commented Oct 10, 2017

Nice work @ockham!!! This was a tricky one

@jsnmoon jsnmoon mentioned this pull request Oct 10, 2017

module.exports = React.createClass( {
module.exports = localize(React.createClass( {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related: #18710 will soon be ready for review (once a desc is written).
It aims to fix up instances similar to this ^

It currently only looks at export defaults but could be tweaked to consider module.exports = too :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants