|
| 1 | +--- |
| 2 | +title: no-misused-capture-owner-stack |
| 3 | +--- |
| 4 | + |
| 5 | +**Full Name in `eslint-plugin-react-x@beta`** |
| 6 | + |
| 7 | +```plain copy |
| 8 | +react-x/no-misused-capture-owner-stack |
| 9 | +``` |
| 10 | + |
| 11 | +**Full Name in `@eslint-react/eslint-plugin@beta`** |
| 12 | + |
| 13 | +```plain copy |
| 14 | +@eslint-react/no-misused-capture-owner-stack |
| 15 | +``` |
| 16 | + |
| 17 | +## Description |
| 18 | + |
| 19 | +Prevents incorrect usage of `captureOwnerStack`. |
| 20 | + |
| 21 | +The `captureOwnerStack` is only available in development builds of React and must be: |
| 22 | + |
| 23 | +1. Imported via namespace to avoid direct named imports. |
| 24 | +2. Conditionally accessed within an `if (process.env.NODE_ENV !== 'production') {...}` block to prevent execution in production environments. |
| 25 | +3. The call of `captureOwnerStack` happened inside of a React controlled function (**not implemented yet**). |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +### Failing |
| 30 | + |
| 31 | +```tsx |
| 32 | +// Failing: Using named import directly |
| 33 | +import { captureOwnerStack } from "react"; |
| 34 | +// ^^^^^^^^^^^^^^^^^ |
| 35 | +// - Don't use named imports of `captureOwnerStack` in files that are bundled for development and production. Use a namespace import instead. |
| 36 | + |
| 37 | +if (process.env.NODE_ENV !== "production") { |
| 38 | + const ownerStack = React.captureOwnerStack(); |
| 39 | + console.log("Owner Stack", ownerStack); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +```tsx |
| 44 | +// Failing: Missing environment check |
| 45 | +import * as React from "react"; |
| 46 | + |
| 47 | +const ownerStack = React.captureOwnerStack(); |
| 48 | +// ^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 49 | +// - `captureOwnerStack` should only be used in development builds. Use an environment check to ensure it is not executed in production. |
| 50 | +console.log(ownerStack); |
| 51 | +``` |
| 52 | + |
| 53 | +### Passing |
| 54 | + |
| 55 | +```tsx |
| 56 | +// Passing: Correct namespace import with environment check |
| 57 | +import * as React from "react"; |
| 58 | + |
| 59 | +if (process.env.NODE_ENV !== "production") { |
| 60 | + const ownerStack = React.captureOwnerStack(); |
| 61 | + console.log("Owner Stack", ownerStack); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Implementation |
| 66 | + |
| 67 | +- [Rule source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x/src/rules/no-misused-capture-owner-stack.ts) |
| 68 | +- [Test source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-x/src/rules/no-misused-capture-owner-stack.spec.ts) |
| 69 | + |
| 70 | +## Further Reading |
| 71 | + |
| 72 | +- [React: APIs `captureOwnerStack`](https://react.dev/reference/react/captureOwnerStack) |
| 73 | + - [The Owner Stack is `null`](https://react.dev/reference/react/captureOwnerStack#the-owner-stack-is-null) |
| 74 | + - [`captureOwnerStack` is not available](https://react.dev/reference/react/captureOwnerStack#captureownerstack-is-not-available) |
0 commit comments