@panates/eslint-config-react 2.3.5 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -60,6 +60,29 @@ export default [...panatesReact.configs.react, { settings: { 'react-x': { versio
60
60
  library's own dev copy, usually the newest - and asks for React 19's `ref` prop in place of
61
61
  `forwardRef`, or `use()` in place of `useContext`, which its React 18 consumers do not have.
62
62
 
63
+ The same setting drives `panates-react/inert`, this package's own rule. React 18 and 19 render
64
+ `inert` in opposite ways: 18 drops a boolean (`inert={true}`) and needs the empty string, 19 drops
65
+ the empty string (`inert=""`) and needs a boolean. Below 19 the rule reports `inert={...}` and the
66
+ bare `inert`; from 19 it reports `inert=""`. A library on `react >=18` writes the attribute through
67
+ a spread chosen by `React.version`:
68
+
69
+ ```tsx
70
+ import React from 'react';
71
+
72
+ const REACT_18 = Number(React.version.split('.')[0]) < 19;
73
+
74
+ /** `inert` the way the running React renders it - nothing when `on` is false. React 19's types
75
+ * take a boolean only, so the string 18 needs is cast. */
76
+ export function inertProps(on: boolean): { inert?: boolean } {
77
+ if (!on) return {};
78
+ return REACT_18 ? ({ inert: '' } as unknown as { inert?: boolean }) : { inert: true };
79
+ }
80
+
81
+ <div {...inertProps(!open)}>...</div>;
82
+ ```
83
+
84
+ Without a version in the settings the rule reports nothing.
85
+
63
86
  ## Notes
64
87
 
65
88
  - **`@eslint-react`, not `eslint-plugin-react`**: the latter, and `eslint-plugin-jsx-a11y`, support
@@ -1,5 +1,6 @@
1
1
  import eslintReact from '@eslint-react/eslint-plugin';
2
2
  import reactHooks from 'eslint-plugin-react-hooks';
3
+ import inert from '../rules/inert.js';
3
4
 
4
5
  /** Every file a component or a hook can live in - a hook in a plain `.ts` included. */
5
6
  const FILES = ['**/*.{ts,tsx,mts,cts,jsx}'];
@@ -40,6 +41,10 @@ const duplicatesOff = Object.fromEntries(
40
41
  * actions with no identity of its own - and the rule cannot tell such a list from one that reorders.
41
42
  * Measured on `panates/peacox-ui`: 12 reports, each a positional list or a list with no other key.
42
43
  * Keying a list that does reorder by something stable stays the author's call.
44
+ *
45
+ * `panates-react/inert` is this package's own rule: React 18 and 19 render `inert` in opposite ways,
46
+ * so it holds the attribute to the oldest React in `settings['react-x'].version` - see
47
+ * `rules/inert.js`. Without the setting it reports nothing.
43
48
  */
44
49
  export default [
45
50
  {
@@ -55,8 +60,10 @@ export default [
55
60
  {
56
61
  name: 'panates/react-patch',
57
62
  files: FILES,
63
+ plugins: { 'panates-react': { rules: { inert } } },
58
64
  rules: {
59
65
  ...duplicatesOff,
66
+ 'panates-react/inert': 'error',
60
67
  'import-x/extensions': 'off',
61
68
  '@eslint-react/no-array-index-key': 'off',
62
69
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@panates/eslint-config-react",
3
- "version": "2.3.5",
3
+ "version": "2.4.0",
4
4
  "description": "React rules for Panates ESLint config, following our styleguide",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -11,7 +11,7 @@
11
11
  "./package.json": "./package.json"
12
12
  },
13
13
  "dependencies": {
14
- "@panates/eslint-config-ts": "^2.3.5"
14
+ "@panates/eslint-config-ts": "^2.4.0"
15
15
  },
16
16
  "peerDependencies": {
17
17
  "@eslint-react/eslint-plugin": ">=5",
@@ -65,8 +65,8 @@
65
65
  "Bircan Yuruk <b.yuruk@panates.com>"
66
66
  ],
67
67
  "devDependencies": {
68
- "@eslint-react/eslint-plugin": "^5.28.8",
69
- "@next/eslint-plugin-next": "^16.4.5",
68
+ "@eslint-react/eslint-plugin": "^5.30.2",
69
+ "@next/eslint-plugin-next": "^16.4.6",
70
70
  "@next/eslint-plugin-next-15": "npm:@next/eslint-plugin-next@15.5.28",
71
71
  "eslint-plugin-react-hooks": "^7.1.1",
72
72
  "eslint-plugin-react-refresh": "^0.5.7"
@@ -75,6 +75,7 @@
75
75
  "index.js",
76
76
  "index.d.ts",
77
77
  "configs",
78
+ "rules",
78
79
  "README.md",
79
80
  "LICENSE"
80
81
  ]
package/rules/inert.js ADDED
@@ -0,0 +1,68 @@
1
+ /**
2
+ * `inert` written the way the oldest React a project supports renders it, read from
3
+ * `settings['react-x'].version` - the setting `@eslint-react` already reads. Without the setting
4
+ * it reports nothing.
5
+ *
6
+ * - Below 19: `inert={...}` and the bare `inert` shorthand are reported. React 18 drops a boolean
7
+ * `inert`, so the element stays focusable and readable; write `''` on 18 and `true` on 19.
8
+ * - 19 and above: `inert=""` is reported. React 19 reads the empty string as `false` and drops it.
9
+ */
10
+ /* Measured from panates/peacox-ui with react-dom 18.3.1 and 19.2.0, through `renderToString` and
11
+ * `createRoot` alike: `inert={true}` - 18 writes nothing and warns "Received `true` for a non-boolean
12
+ * attribute `inert`", 19 writes `inert=""`. `inert=""` - 18 writes `inert=""`, 19 writes nothing and
13
+ * warns "Received an empty string for a boolean attribute `inert`". `false` and `undefined` write
14
+ * nothing on either. A library on `react >=18` writing `inert={open}` therefore leaves closed content
15
+ * focusable on 18 while its tests, on 19, never show it: 9 places in peacox-ui (Collapsible,
16
+ * Dropdown, Popover, Skeleton, Table).
17
+ *
18
+ * Below 19 a string-valued expression is reported too, though 18 renders it: a library whose lowest
19
+ * React is 18 also runs on 19, where the same string is dropped. Only a spread chosen by
20
+ * `React.version` is right on both - and a spread is not a JSX attribute, so it is never reported. */
21
+ export default {
22
+ meta: {
23
+ type: 'problem',
24
+ docs: {
25
+ description: "Write `inert` the way the oldest supported React renders it (settings['react-x'].version)",
26
+ },
27
+ schema: [],
28
+ messages: {
29
+ react18:
30
+ 'React 18 drops a boolean `inert` and React 19 drops an empty string, so this element stays focusable on one of ' +
31
+ "them. Spread a value chosen by React.version instead: `{ inert: '' }` below 19, `{ inert: true }` from 19, `{}` when off.",
32
+ react19:
33
+ 'React 19 reads `inert=""` as false and writes nothing, so the element stays focusable. Write `inert` or `inert={true}`.',
34
+ },
35
+ },
36
+ create(context) {
37
+ const major = reactMajor(context.settings);
38
+ if (major === undefined) return {};
39
+ return {
40
+ JSXAttribute(node) {
41
+ if (node.name.type !== 'JSXIdentifier' || node.name.name !== 'inert') return;
42
+ if (major < 19) {
43
+ if (node.value === null || node.value.type === 'JSXExpressionContainer') {
44
+ context.report({ node, messageId: 'react18' });
45
+ }
46
+ } else if (isEmptyString(node.value)) {
47
+ context.report({ node, messageId: 'react19' });
48
+ }
49
+ },
50
+ };
51
+ },
52
+ };
53
+
54
+ /** The major of `settings['react-x'].version` - `undefined` when it is not set, or is `'detect'`. */
55
+ function reactMajor(settings) {
56
+ const version = settings?.['react-x']?.version;
57
+ const match = typeof version === 'string' ? /^\D*(\d+)/.exec(version) : null;
58
+ return match ? Number(match[1]) : undefined;
59
+ }
60
+
61
+ /** `""`, `{''}` or `` {``} `` - the empty string, however it is written. */
62
+ function isEmptyString(value) {
63
+ if (!value) return false;
64
+ const node = value.type === 'JSXExpressionContainer' ? value.expression : value;
65
+ if (node.type === 'Literal') return node.value === '';
66
+ if (node.type === 'TemplateLiteral') return node.expressions.length === 0 && node.quasis[0].value.cooked === '';
67
+ return false;
68
+ }