Skip to content

Commit 3ab750a

Browse files
committed
docs(linter): Clarify react-in-jsx-scope rule docs. (#15749)
Mention that it is an unnecessary practice for most modern React projects. Generally, you do not need to do `import React from 'react';` anymore for React 17+ projects.
1 parent e7a826f commit 3ab750a

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

crates/oxc_linter/src/rules/react/react_in_jsx_scope.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
};
1111

1212
fn react_in_jsx_scope_diagnostic(span: Span) -> OxcDiagnostic {
13-
OxcDiagnostic::warn("'React' must be in scope when using JSX")
13+
OxcDiagnostic::warn("`React` must be in scope when using JSX.")
1414
.with_help("When using JSX, `<a />` expands to `React.createElement(\"a\")`. Therefore the `React` variable must be in scope.")
1515
.with_label(span)
1616
}
@@ -21,7 +21,18 @@ pub struct ReactInJsxScope;
2121
declare_oxc_lint!(
2222
/// ### What it does
2323
///
24-
/// Disallow missing React when using JSX
24+
/// Enforces that React is imported and in-scope when using JSX syntax.
25+
///
26+
/// Note that this rule is **not necessary** on React 17+ if you are using
27+
/// the new JSX Transform, and you can disable this rule and skip importing
28+
/// `React` in files with JSX syntax.
29+
///
30+
/// If your `tsconfig.json` has `jsx` set to `react-jsx` or `react-jsxdev`, you are using the new JSX Transform.
31+
/// For JavaScript projects using Babel, you are using the new JSX Transform if your React preset configuration
32+
/// (in `.babelrc` or `babel.config.js`) has `runtime: "automatic"`.
33+
///
34+
/// For more information, see
35+
/// [the React blog post on JSX Transform](https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#eslint).
2536
///
2637
/// ### Why is this bad?
2738
///
@@ -32,13 +43,13 @@ declare_oxc_lint!(
3243
///
3344
/// Examples of **incorrect** code for this rule:
3445
/// ```jsx
35-
/// var a = <a />;
46+
/// const a = <a />;
3647
/// ```
3748
///
3849
/// Examples of **correct** code for this rule:
3950
/// ```jsx
4051
/// import React from "react";
41-
/// var a = <a />;
52+
/// const a = <a />;
4253
/// ```
4354
ReactInJsxScope,
4455
react,

crates/oxc_linter/src/snapshots/react_react_in_jsx_scope.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
---
22
source: crates/oxc_linter/src/tester.rs
33
---
4-
eslint-plugin-react(react-in-jsx-scope): 'React' must be in scope when using JSX
4+
eslint-plugin-react(react-in-jsx-scope): `React` must be in scope when using JSX.
55
╭─[react_in_jsx_scope.tsx:1:15]
66
1var App, a = <App />;
77
· ───
88
╰────
99
help: When using JSX, `<a />` expands to `React.createElement("a")`. Therefore the `React` variable must be in scope.
1010

11-
eslint-plugin-react(react-in-jsx-scope): 'React' must be in scope when using JSX
11+
eslint-plugin-react(react-in-jsx-scope): `React` must be in scope when using JSX.
1212
╭─[react_in_jsx_scope.tsx:1:10]
1313
1var a = <App />;
1414
· ───
1515
╰────
1616
help: When using JSX, `<a />` expands to `React.createElement("a")`. Therefore the `React` variable must be in scope.
1717

18-
eslint-plugin-react(react-in-jsx-scope): 'React' must be in scope when using JSX
18+
eslint-plugin-react(react-in-jsx-scope): `React` must be in scope when using JSX.
1919
╭─[react_in_jsx_scope.tsx:1:10]
2020
1var a = <img />;
2121
· ───
2222
╰────
2323
help: When using JSX, `<a />` expands to `React.createElement("a")`. Therefore the `React` variable must be in scope.
2424

25-
eslint-plugin-react(react-in-jsx-scope): 'React' must be in scope when using JSX
25+
eslint-plugin-react(react-in-jsx-scope): `React` must be in scope when using JSX.
2626
╭─[react_in_jsx_scope.tsx:1:9]
2727
1var a = <>fragment</>;
2828
· ──
2929
╰────
3030
help: When using JSX, `<a />` expands to `React.createElement("a")`. Therefore the `React` variable must be in scope.
3131

32-
eslint-plugin-react(react-in-jsx-scope): 'React' must be in scope when using JSX
32+
eslint-plugin-react(react-in-jsx-scope): `React` must be in scope when using JSX.
3333
╭─[react_in_jsx_scope.tsx:1:15]
3434
1var Foo, a = <img />;
3535
· ───

0 commit comments

Comments
 (0)