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

FED-1718 Callback ref null safety hint suggestor #275

Merged
merged 13 commits into from
Mar 21, 2024

Conversation

sydneyjodon-wk
Copy link
Contributor

@sydneyjodon-wk sydneyjodon-wk commented Mar 6, 2024

Motivation

In React, callback refs are called with the component instance upon render, and can be called with null when the component unmounts or when the component providing the ref rerenders.

For example instance:

// @Dart=2.11
..ref = (FooComponent ref) {
// ref could be null!
_myRefVariable = ref;
}
As a result, callback refs should be typed as if the ref passed to them could be null.

The null safety migrator tool, however, seems to like to make these refs non-nullable.

To help with nullability inference during the null safety migration of over_react components, and to help prevent null errors afterwards, we need to develop codemods that adjust the typings of over_react callback refs.

Changes

  • Add a suggestor that changes the following cases:
  1. For ref prop param types:
- (ButtonToolbar()..ref = (ButtonElement r) => ref = r)();
+ (ButtonToolbar()..ref = (ButtonElement /*?*/ r) => ref = r)();
  1. For ref variable declarations:
- ButtonElement ref;
+ ButtonElement /*?*/ ref;
(ButtonToolbar()..ref = (r) => ref = r)();
  1. For ref prop type casts:
- (ButtonToolbar()..ref = (r) => ref = r as ButtonElement)();
+ (ButtonToolbar()..ref = (r) => ref = r as ButtonElement /*?*/)();

Release Notes

Review

See CONTRIBUTING.md for more details on review types (+1 / QA +1 / +10) and code review process.

Please review:

QA Checklist

  • Tests were updated and provide good coverage of the changeset and other affected code
  • Manual testing was performed if needed
dart pub global activate https://github.com/Workiva/over_react_codemod.git --source=git --git-ref=callback-ref-null-safety-hint
dart pub global run over_react_codemod:null_safety_prep --yes-to-all
- [ ] Anything falling under manual testing criteria [outlined in CONTRIBUTING.md][contributing-manual-testing]

Merge Checklist

While we perform many automated checks before auto-merging, some manual checks are needed:

  • A Frontend Frameworks Design member has reviewed these changes
  • There are no unaddressed comments - this check can be automated if reviewers use the "Request Changes" feature
  • For release PRs - Version metadata in Rosie comment is correct

@aviary-wf
Copy link

Security Insights

No security relevant content was detected by automated scans.

Action Items

  • Review PR for security impact; comment "security review required" if needed or unsure
  • Verify aviary.yaml coverage of security relevant code

Questions or Comments? Reach out on Slack: #support-infosec.

@sydneyjodon-wk sydneyjodon-wk marked this pull request as ready for review March 20, 2024 23:54
Copy link
Contributor

@aaronlademann-wf aaronlademann-wf left a comment

Choose a reason for hiding this comment

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

When I ran this in admin_client, the _hintAlreadyExists() fn didn't seem to work in all cases - or at least that is what seems like is happening, because I see this in some changed files:

Screenshot 2024-03-21 at 9 14 26 AM Screenshot 2024-03-21 at 9 16 27 AM


@override
Future<void> generatePatches() async {
_log.info('Resolving ${context.relativePath}...');
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make this something that doesn't produce stdout by default? Pretty noisy...

@sydneyjodon-wk
Copy link
Contributor Author

@aaronlademann-wf yeahhh I was talking to Greg about that edge case because if the ref is referenced multiple times it will keep adding the nullability hint and we decided that it wouldn't be worth the effort to make the codemod be internally aware of the changes it was making to reduce duplication since it's an easy manual fix and I think the migrator tool should work regardless of comment duplication

Copy link
Contributor

@aaronlademann-wf aaronlademann-wf left a comment

Choose a reason for hiding this comment

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

+1 / +10

Ran the codemod locally in admin_client, xbrl-module, w_table, graph_ui and doc_plat_client. All expected changes look good, no unexpected changes or rtes while executing the codemod.

@sydneyjodon-wk
Copy link
Contributor Author

@Workiva/release-management-p

Copy link

@rmconsole-wf rmconsole-wf left a comment

Choose a reason for hiding this comment

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

+1 from RM

@rmconsole2-wf rmconsole2-wf merged commit 2931202 into master Mar 21, 2024
8 checks passed
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.

5 participants