eslint-plugin-kerfjs 5.0.0-beta.97 → 5.0.0-beta.99

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.
@@ -4,6 +4,10 @@ Checks literal JSX and direct component-function calls against each component's
4
4
  cataloged `cssValueProps` contracts. The catalog keeps length, size, flex,
5
5
  color, declaration-list, media-query, and numeric-pixel grammars distinct and
6
6
  names their accepted helpers, finite shorthands, and raw-value policy.
7
+ A contract path is dot-separated object members, each optionally suffixed with
8
+ `[]` to visit every array element: `gap`, `choices[].swatch`, `iconTile.color`.
9
+ A member that is not a literal object or array (a tone string such as
10
+ `iconTile="brand"`, a variable) is skipped.
7
11
 
8
12
  Stable diagnostics are `KUI-L013` (raw or unknown shorthand), `KUI-L014`
9
13
  (wrong property helper), `KUI-L015` (non-standalone expression), `KUI-L016`
@@ -22,5 +26,26 @@ JSX itself uses to tell a component tag from an intrinsic element; a default
22
26
  import keeps its subpath's entry. `@kerfjs/ui`'s catalog check enforces the
23
27
  convention for the package, and `kerf-ui-analyze` applies the same rule.
24
28
 
29
+ A helper is identified by its imported name when its module is the
30
+ component's package or any subpath of it, the same way `kerf-ui-analyze`
31
+ resolves it. Named, aliased, and namespace imports all count, so these are
32
+ equivalent:
33
+
34
+ ```tsx
35
+ import { ListItem } from '@kerfjs/ui/list-item';
36
+ import { foregroundColor } from '@kerfjs/ui/css-values';
37
+ import { foregroundColor as fg } from '@kerfjs/ui';
38
+ import * as css from '@kerfjs/ui/css-values';
39
+
40
+ // Each reports KUI-L014: a fill takes `fillColor()`, `uiColor()`, or `colorVar()`.
41
+ <ListItem label="A" iconTile={{ color: foregroundColor('#000') }} />;
42
+ <ListItem label="B" iconTile={{ color: fg('#000') }} />;
43
+ <ListItem label="C" iconTile={{ color: css.foregroundColor('#000') }} />;
44
+ ```
45
+
46
+ A same-named helper from another package (`other-ui/css-values`, or a
47
+ package that only shares the prefix, such as `@kerfjs/ui-extras`) is not a
48
+ cataloged helper, and the rule leaves its call alone.
49
+
25
50
  Third-party component suites can provide the same contracts in a composition
26
51
  catalog through `settings.kerfjs.ui.catalog` and its matching selection catalog.
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.97' },
18
+ meta: { name: 'eslint-plugin-kerfjs', version: '5.0.0-beta.99' },
19
19
  rules: {
20
20
  'no-inline-jsx-event-handlers': noInlineJsxEventHandlers,
21
21
  'no-raw-with-dynamic-arg': noRawWithDynamicArg,
@@ -36,7 +36,7 @@ function ownershipFacts(contract) {
36
36
  for (const root of roots) {
37
37
  privatePrefixes.push({ prefix: `--_${root}`, entry });
38
38
  for (const contractProp of entry.cssValueProps ?? []) {
39
- const [head] = contractProp.path.split('[].');
39
+ const [head] = contractProp.path.split(/\[\]|\./);
40
40
  const token = `--${root}-${kebab(head)}`;
41
41
  if (tokens.has(token) && !typedTokens.has(token))
42
42
  typedTokens.set(token, { entry, path: contractProp.path });
@@ -1,5 +1,4 @@
1
1
  import {
2
- helperCall,
3
2
  importRegistry,
4
3
  isExcepted,
5
4
  jsxKey,
@@ -43,27 +42,84 @@ function jsxValue(attribute) {
43
42
  return undefined;
44
43
  }
45
44
 
46
- function nestedValues(value, tail) {
47
- if (!tail) return [value];
48
- if (value?.type !== 'ArrayExpression') return [];
49
- return value.elements.flatMap((element) => {
50
- if (element?.type !== 'ObjectExpression') return [];
51
- const property = objectProperty(element, tail);
52
- return property ? [property.value] : [];
53
- });
45
+ // A catalog `cssValueProps` path is dot-separated object members, each
46
+ // optionally suffixed with `[]` to visit every array element: `gap`,
47
+ // `choices[].swatch`, `iconTile.color`. A member that does not resolve to a
48
+ // literal object or array (a tone string, a variable, a spread) is skipped.
49
+ function pathSegments(path) {
50
+ return path
51
+ .split('.')
52
+ .map((segment) =>
53
+ segment.endsWith('[]')
54
+ ? { name: segment.slice(0, -2), each: true }
55
+ : { name: segment, each: false },
56
+ );
57
+ }
58
+
59
+ function expandElements(values, each) {
60
+ if (!each) return values;
61
+ return values.flatMap((value) =>
62
+ value?.type === 'ArrayExpression' ? value.elements : [],
63
+ );
64
+ }
65
+
66
+ function nestedValues(value, segments) {
67
+ let values = expandElements([value], segments[0].each);
68
+ for (const { name, each } of segments.slice(1))
69
+ values = expandElements(
70
+ values.flatMap((item) => {
71
+ if (item?.type !== 'ObjectExpression') return [];
72
+ const property = objectProperty(item, name);
73
+ return property ? [property.value] : [];
74
+ }),
75
+ each,
76
+ );
77
+ return values;
54
78
  }
55
79
 
56
80
  function contractValuesFromJsx(opening, path) {
57
- const [head, tail] = path.split('[].');
58
- return nestedValues(jsxValue(jsxAttribute(opening, head)), tail);
81
+ const segments = pathSegments(path);
82
+ return nestedValues(
83
+ jsxValue(jsxAttribute(opening, segments[0].name)),
84
+ segments,
85
+ );
59
86
  }
60
87
 
61
88
  function contractValuesFromCall(call, path) {
62
89
  const options = call.arguments[0];
63
90
  if (options?.type !== 'ObjectExpression') return [];
64
- const [head, tail] = path.split('[].');
65
- const property = objectProperty(options, head);
66
- return property ? nestedValues(property.value, tail) : [];
91
+ const segments = pathSegments(path);
92
+ const property = objectProperty(options, segments[0].name);
93
+ return property ? nestedValues(property.value, segments) : [];
94
+ }
95
+
96
+ // The CSS-value helper behind a call, resolved exactly as `kerf-ui-analyze`'s
97
+ // `importedName()` resolves it: a named (possibly aliased) or default import,
98
+ // or a `namespace.helper()` member call, whose module is the inspected entry's
99
+ // package or ANY subpath of it. Helpers are documented at
100
+ // `@kerfjs/ui/css-values` and re-exported from the root and component
101
+ // subpaths, so the import path never decides a helper's identity; only its
102
+ // imported name does. A module outside the entry's package is never a
103
+ // cataloged helper.
104
+ export function cssValueHelper(callee, registry, packageName) {
105
+ let helper;
106
+ if (callee.type === 'Identifier') helper = registry.helpers.get(callee.name);
107
+ else if (
108
+ callee.type === 'MemberExpression' &&
109
+ !callee.computed &&
110
+ callee.object.type === 'Identifier' &&
111
+ callee.property.type === 'Identifier' &&
112
+ registry.namespaces.has(callee.object.name)
113
+ )
114
+ helper = {
115
+ imported: callee.property.name,
116
+ source: registry.namespaces.get(callee.object.name),
117
+ };
118
+ if (!helper || typeof packageName !== 'string') return undefined;
119
+ return helper.source === packageName ||
120
+ helper.source.startsWith(`${packageName}/`)
121
+ ? helper.imported
122
+ : undefined;
67
123
  }
68
124
 
69
125
  const CONTRACT_MESSAGES = {
@@ -184,9 +240,9 @@ function createVisitors(context, report) {
184
240
  continue;
185
241
  }
186
242
  if (value.type !== 'CallExpression') continue;
187
- const helper = helperCall(value.callee, registry, loaded);
243
+ const helper = cssValueHelper(value.callee, registry, entry.package);
188
244
  if (!helper) continue;
189
- if (cssContract.nonStandaloneHelpers?.includes(helper.imported)) {
245
+ if (cssContract.nonStandaloneHelpers?.includes(helper)) {
190
246
  if (!isExcepted(loaded, CODES.expression, filename))
191
247
  report({
192
248
  node: value,
@@ -194,12 +250,12 @@ function createVisitors(context, report) {
194
250
  data: {
195
251
  component: entry.name,
196
252
  path: cssContract.path,
197
- helper: helper.imported,
253
+ helper,
198
254
  },
199
255
  });
200
256
  } else if (
201
- !cssContract.helpers?.includes(helper.imported) &&
202
- cssContract.unsafeHelper !== helper.imported
257
+ !cssContract.helpers?.includes(helper) &&
258
+ cssContract.unsafeHelper !== helper
203
259
  ) {
204
260
  if (!isExcepted(loaded, CODES.helper, filename))
205
261
  report({
@@ -208,7 +264,7 @@ function createVisitors(context, report) {
208
264
  data: {
209
265
  component: entry.name,
210
266
  path: cssContract.path,
211
- helper: helper.imported,
267
+ helper,
212
268
  preferred,
213
269
  },
214
270
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-kerfjs",
3
- "version": "5.0.0-beta.97",
3
+ "version": "5.0.0-beta.99",
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",