Skip to content

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

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

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs: add rule documentation
  • Loading branch information
akulsr0 committed May 16, 2024
commit 639f15fde5313fc9f4d7977730f2c4bf9278a612
62 changes: 62 additions & 0 deletions docs/rules/no-render-return-undefined.md
Original file line number Diff line number Diff line change
@@ -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).

<!-- 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`.


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`.


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

Copy link
Member

Choose a reason for hiding this comment

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

ping


```jsx
function App() {
return undefined;
}

// OR

let ui;
function App() {
return ui;
}

// OR

function getUI() {
if (condition) return <h1>Hello</h1>;
}
function App() {
return getUI();
}
Comment on lines +33 to +38
Copy link
Member

Choose a reason for hiding this comment

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

this won't be possible across files, only if it's in the same file, so unless the function provides return type information (ie, TS) i don't think this is a check we can reliably do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it possible to continue without supporting this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ljharb Can we do something here?
Rest all changes, I've made

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I don't think we necessarily have to support this - I'm saying that the docs should be updated to reflect the caveats.

```

Examples of **correct** code for this rule:

```jsx
function App() {
return <div />;
}

// OR

let ui = <div />;
function App() {
return ui;
}

// OR

function getUI() {
if (condition) return <h1>Hello</h1>;
return null;
}
function App() {
return getUI();
}
```
51 changes: 51 additions & 0 deletions tests/lib/rules/no-render-return-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,25 @@ ruleTester.run('no-render-return-undefined', rule, {
}
`,
},
{
code: `
let ui = <Component />;
function App() {
return ui;
}
`,
},
{
code: `
function getUI() {
if(condition) return <h1>Hello</h1>;
return null;
}
function App() {
return getUI();
}
`,
},
{
code: `
function App() {
Expand Down Expand Up @@ -669,6 +688,38 @@ ruleTester.run('no-render-return-undefined', rule, {
`,
errors: [{ messageId: 'returnsUndefined' }],
},
{
code: `
let ui;
function App() {
return ui;
}
`,
errors: [{ messageId: 'returnsUndefined' }],
},
{
code: `
let ui;
function App() {
if(cond) {
ui = <div />;
}
return ui;
}
`,
errors: [{ messageId: 'returnsUndefined' }],
},
{
code: `
function getUI() {
if(condition) return <h1>Hello</h1>;
}
function App() {
return getUI();
}
`,
errors: [{ messageId: 'returnsUndefined' }],
},
{
code: `
function App() {
Expand Down