eslint-plugin-kerfjs 5.0.0-beta.110 → 5.0.0-beta.111

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.
@@ -14,6 +14,9 @@ each(items, (item) => {
14
14
  });
15
15
 
16
16
  each(items, (item) => <>{item.name}</>);
17
+
18
+ // A dynamic object does not prove that a component receives a row key.
19
+ each(items, (item) => <ListActionRow rootAttributes={item.attrs} />);
17
20
  ```
18
21
 
19
22
  ## ✅ Correct
@@ -23,6 +26,11 @@ each(items, (item) => <li data-key={item.id}>{item.name}</li>);
23
26
 
24
27
  each(items, (item) => <li id={item.id}>{item.name}</li>);
25
28
 
29
+ // Component roots can pass a statically named key through rootAttributes.
30
+ each(items, (item) => (
31
+ <ListActionRow rootAttributes={{ "data-key": item.id }} />
32
+ ));
33
+
26
34
  // Spread attributes are conservatively allowed — they may include the key.
27
35
  each(items, (item) => <li {...item.attrs}>{item.name}</li>);
28
36
  ```
@@ -32,3 +40,5 @@ each(items, (item) => <li {...item.attrs}>{item.name}</li>);
32
40
  - Non-inline callbacks: `each(items, renderRow)` — the rule only inspects arrow / function-expression callbacks passed directly to `each()`.
33
41
  - `each` calls qualified by a namespace: `MyLib.each(items, …)` — the rule only fires on a bare `each` identifier.
34
42
  - Computed key keys behind a runtime branch: if the JSX root only sometimes carries a `data-key`, the rule reports the missing static attribute. Fix by always setting it.
43
+ - A component's dynamic `rootAttributes` object or unknown computed property name is not treated as proof of a key. Use an object literal with a `"data-key"` or `id` property where the component forwards it to its single root. A later object spread or computed property may overwrite an earlier key, so put the key last.
44
+ - The `rootAttributes` exception applies to component JSX roots, not native elements or fragment roots. The rule cannot verify a component's implementation; confirm that it forwards the key to the rendered root.
package/index.js CHANGED
@@ -15,7 +15,7 @@ import uiPublicBoundaries from './lib/rules/ui-public-boundaries.js';
15
15
  import uiWiring from './lib/rules/ui-wiring.js';
16
16
 
17
17
  const plugin = {
18
- meta: { name: 'eslint-plugin-kerfjs', version: '5.0.0-beta.110' },
18
+ meta: { name: 'eslint-plugin-kerfjs', version: '5.0.0-beta.111' },
19
19
  rules: {
20
20
  'no-inline-jsx-event-handlers': noInlineJsxEventHandlers,
21
21
  'no-raw-with-dynamic-arg': noRawWithDynamicArg,
@@ -19,6 +19,32 @@ function findRootJSXFromBody(body) {
19
19
  return null;
20
20
  }
21
21
 
22
+ function isComponentRoot(name) {
23
+ return (
24
+ name.type === 'JSXMemberExpression' ||
25
+ (name.type === 'JSXIdentifier' && /^[A-Z]/.test(name.name))
26
+ );
27
+ }
28
+
29
+ function isKeyName(name) {
30
+ return name === 'data-key' || name === 'id';
31
+ }
32
+
33
+ function hasKeyInRootAttributes(attribute) {
34
+ const value = attribute.value;
35
+ const object = value?.type === 'JSXExpressionContainer' && value.expression;
36
+ if (!object || object.type !== 'ObjectExpression') return false;
37
+ for (let index = object.properties.length - 1; index >= 0; index--) {
38
+ const property = object.properties[index];
39
+ // A later spread or computed name could overwrite an earlier key.
40
+ if (property.type !== 'Property' || property.computed) return false;
41
+ const key = property.key;
42
+ if (isKeyName(key.type === 'Identifier' ? key.name : key.value))
43
+ return true;
44
+ }
45
+ return false;
46
+ }
47
+
22
48
  const meta = {
23
49
  type: 'problem',
24
50
  docs: {
@@ -54,11 +80,15 @@ function create(context) {
54
80
  return;
55
81
  }
56
82
  const attrs = root.openingElement.attributes;
83
+ const componentRoot = isComponentRoot(root.openingElement.name);
57
84
  const hasKey = attrs.some((a) => {
58
85
  if (a.type === 'JSXSpreadAttribute') return true;
59
86
  if (a.type !== 'JSXAttribute') return false;
60
87
  const n = a.name && a.name.name;
61
- return n === 'data-key' || n === 'id';
88
+ return (
89
+ isKeyName(n) ||
90
+ (componentRoot && n === 'rootAttributes' && hasKeyInRootAttributes(a))
91
+ );
62
92
  });
63
93
  if (!hasKey) {
64
94
  context.report({ node: root.openingElement, messageId: 'missingKey' });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-kerfjs",
3
- "version": "5.0.0-beta.110",
3
+ "version": "5.0.0-beta.111",
4
4
  "description": "ESLint rules that enforce kerf's hard rules — catches AI-shaped bugs at edit time.",
5
5
  "type": "module",
6
6
  "license": "MIT",