-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat: Don't require defaultProps for function components #156
feat: Don't require defaultProps for function components #156
Conversation
Thanks for the pull request, @bradenmacdonald! What's next?Please work through the following steps to get your changes ready for engineering review: 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. 🔘 Let us know that your PR is ready for review:Who will review my changes?This repository is currently unmaintained. To get help with finding a technical reviewer, tag the community contributions project manager for this PR in a comment and let them know that your changes are ready for review:
Where can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:
When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
.eslintrc.js
Outdated
overrides: [ | ||
{ | ||
// Disable default-props for TypeScript files; it's fine to have undefined prop values if declared that way | ||
files: ['**/*.tsx'], |
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.
Definitely in favor of disabling/modifying this rule, especially as we adopt more TypeScript stuff. Though, I do wonder if it's worth disabling the rule for both .jsx
and .tsx
, given defaultProps
for functions are technically deprecated and are expected to be removed in React 19. Non-TypeScript files could also probably incrementally start migrating to ES6 default function parameters today as well.
If we were to disable the rule across all .jsx
and .tsx
files now, would this config be moved under the rules
property instead of overrides
?
[suggestion] Also, I wonder if it'd be worth considering whether to modify the rule versus disabling it altogether. For example, I imagine we might still want linting on missing defaultProps
for any class components that might still exist (where ES6 default function params aren't applicable?). Similarly, there is a rule option (i.e., functions
) for changing the expected strategy for defaulting props; by default, it is defaultProps
but could be changed to defaultArguments
instead (see docs). Changing the functions
option to defaultArguments
seems like it might be preferable versus disabling the rule altogether? 🤔
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.
I'd love to just disable the rule altogether.
As for changing to defaultArguments
, that's fine with me but I think it would cause way too many warnings/errors with existing code which is written using defaultProps
(unless that's what you want, to force a big migration).
I'd still prefer to disable it altogether in TypeScript files, because I often write code like this (real example), which I think is perfectly fine:
const SearchUI: React.FC<{ courseId?: string, closeSearchModal?: () => void }> = (props) => {
...
and yet with defaultArguments
on, eslint wants me to write it much more verbosely as:
const SearchUI: React.FC<{ courseId?: string, closeSearchModal?: () => void }> = ({
courseId = undefined,
closeSearchModal = undefined,
}) => {
...
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.
@adamstankiewicz Would you like me to update this to just disable the rule altogether? (for JSX and TSX)
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.
@adamstankiewicz I updated this to not required defaultProps
for function components in general, and added a comment explaining why it's deprecated.
I'd love to change the rule to also require defaultArguments
but I'm not sure how to roll that out without requiring major changes of existing code, so I think this is the best incremental improvement.
Could you please take a look and approve if it's good with you?
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.
@bradenmacdonald I will take a look later this evening!
81d21d0
to
89f2925
Compare
@bradenmacdonald 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future. |
🎉 This PR is included in version 4.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This was done in eslint-config in this PR: openedx/eslint-config#156
I think
react/require-default-props
is an annoying rule in TSX files, as often it's perfectly fine to haveundefined
as the default value of a prop, and TypeScript already forces you to handle it accordingly in your component implementation.But eslint currently wants us to add a default value for no reason. See example below, where eslint complains that three of the props lack defaults, even though 2 have a default and the third makes sense to have no default.
In this example, we don't need
onToggleChildren
to have a default value of an empty function, because TypeScript will make sure we call it asonToggleChildren?.(path);
to handle the case where it's undefined. Or the author may choose to add a default, but in any case there's no errors nor incorrect code resulting from a missing default, so I don't believe the eslint rule is providing any value at all.