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

[Feat] no-render-return-undefined: disallow components rendering undefined #3750

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

akulsr0
Copy link
Contributor

@akulsr0 akulsr0 commented May 8, 2024

Closes #3020

Copy link

codecov bot commented May 8, 2024

Codecov Report

Attention: Patch coverage is 85.83333% with 17 lines in your changes are missing coverage. Please review.

Project coverage is 51.86%. Comparing base (8e1a94b) to head (6d3cfec).
Report is 7 commits behind head on master.

Current head 6d3cfec differs from pull request most recent head d1814c1

Please upload reports for the commit d1814c1 to get more accurate results.

Files Patch % Lines
lib/util/eslint.js 47.82% 12 Missing ⚠️
lib/util/ast.js 55.55% 4 Missing ⚠️
lib/util/variable.js 80.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #3750       +/-   ##
===========================================
- Coverage   97.62%   51.86%   -45.77%     
===========================================
  Files         134        8      -126     
  Lines        9605      349     -9256     
  Branches     3486      128     -3358     
===========================================
- Hits         9377      181     -9196     
+ Misses        228      168       -60     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@akulsr0 akulsr0 marked this pull request as ready for review May 15, 2024 10:35
@ljharb ljharb marked this pull request as draft May 15, 2024 17:23
@akulsr0
Copy link
Contributor Author

akulsr0 commented May 16, 2024

@ljharb Let me know your thoughts on this!

@akulsr0 akulsr0 marked this pull request as ready for review May 18, 2024 14:50
@@ -0,0 +1,62 @@
# Disallow returning undefined from react components (`react/no-render-return-undefined`)

💼 This rule is enabled in the ☑️ `recommended` [config](https://github.com/jsx-eslint/eslint-plugin-react/#shareable-configs).
Copy link
Member

Choose a reason for hiding this comment

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

i think the docs need to be updated, run npm run update:eslint-docs


<!-- end auto-generated rule header -->

> In React 18, components may render undefined, and React will render nothing to the DOM instead of throwing an error. However, accidentally rendering nothing in a component could still cause surprises. This rule will warn if the `return` statement in a React Component returns undefined.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
> In React 18, components may render undefined, and React will render nothing to the DOM instead of throwing an error. However, accidentally rendering nothing in a component could still cause surprises. This rule will warn if the `return` statement in a React Component returns undefined.
> Starting in React 18, components may render `undefined`, and React will render nothing to the DOM instead of throwing an error. However, accidentally rendering nothing in a component could still cause surprises. This rule will warn if the `return` statement in a React Component returns `undefined`.


> In React 18, components may render undefined, and React will render nothing to the DOM instead of throwing an error. However, accidentally rendering nothing in a component could still cause surprises. This rule will warn if the `return` statement in a React Component returns undefined.
Issue: [#3020](https://github.com/jsx-eslint/eslint-plugin-react/issues/3020)
Copy link
Member

Choose a reason for hiding this comment

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

i'm not sure linking to the issue is needed


## Rule Details

This rule will warn if the `return` statement in a React component returns undefined.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
This rule will warn if the `return` statement in a React component returns undefined.
This rule will warn if the `return` statement in a React component returns `undefined`.


This rule will warn if the `return` statement in a React component returns undefined.

Examples of **incorrect** code for this rule:
Copy link
Member

Choose a reason for hiding this comment

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

let's add one that uses void

},

create(context) {
function getReturnValue(returnNode) {
Copy link
Member

Choose a reason for hiding this comment

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

can this take context and be defined at module level instead?

}
case 'ConditionalExpression': {
const possibleReturnValue = [getReturnValue(returnNode.consequent), getReturnValue(returnNode.alternate)];
const returnsUndefined = possibleReturnValue.some((val) => val === undefined);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const returnsUndefined = possibleReturnValue.some((val) => val === undefined);
const returnsUndefined = possibleReturnValue.some((val) => typeof val === 'undefined');

case 'ConditionalExpression': {
const possibleReturnValue = [getReturnValue(returnNode.consequent), getReturnValue(returnNode.alternate)];
const returnsUndefined = possibleReturnValue.some((val) => val === undefined);
if (returnsUndefined) return undefined;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (returnsUndefined) return undefined;
if (returnsUndefined) return;


return !returnStatement
|| returnIdentifierName === 'undefined'
|| returnIdentifierValue === undefined
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
|| returnIdentifierValue === undefined
|| typeof returnIdentifierValue === 'undefined'

package.json Outdated
@@ -15,7 +15,7 @@
"test": "npm run unit-test",
"posttest": "aud --production",
"type-check": "tsc",
"unit-test": "istanbul cover node_modules/mocha/bin/_mocha tests/lib/**/*.js tests/util/**/*.js tests/index.js",
"unit-test": "istanbul cover node_modules/mocha/bin/_mocha tests/lib/rules/no-render-return-undefined.js",
Copy link
Member

Choose a reason for hiding this comment

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

commenting here to ensure this doesn't get merged. fwiw you can npx mocha tests/lib/rules/no-render-return-undefined.js to run just that one file locally :-)

@ljharb ljharb marked this pull request as draft May 24, 2024 16:34
@akulsr0 akulsr0 force-pushed the akul/no-render-return-undefined branch from 6d3cfec to d1814c1 Compare May 25, 2024 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

React 18: Warn when components render undefined
2 participants