eslint-plugin-kerfjs 5.0.0-beta.13 → 5.0.0-beta.131

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.
Files changed (37) hide show
  1. package/README.md +85 -21
  2. package/docs/rules/ai-assistant-configs.md +17 -6
  3. package/docs/rules/no-generated-view-edits.md +42 -0
  4. package/docs/rules/no-inline-jsx-event-handlers.md +2 -2
  5. package/docs/rules/no-nested-mount.md +6 -1
  6. package/docs/rules/no-raw-with-dynamic-arg.md +7 -7
  7. package/docs/rules/prefer-attr-selector.md +7 -7
  8. package/docs/rules/prefer-module-jsx-augmentation.md +8 -4
  9. package/docs/rules/require-data-key-in-each.md +16 -6
  10. package/docs/rules/require-delegate-disposer.md +10 -5
  11. package/docs/rules/ui-component-ownership.md +43 -0
  12. package/docs/rules/ui-composition.md +116 -0
  13. package/docs/rules/ui-css-values.md +51 -0
  14. package/docs/rules/ui-exceptional-shorthands.md +22 -0
  15. package/docs/rules/ui-preferences.md +41 -0
  16. package/docs/rules/ui-public-boundaries.md +48 -0
  17. package/docs/rules/ui-wiring.md +57 -0
  18. package/index.js +51 -1
  19. package/lib/html-template-classes.js +104 -0
  20. package/lib/rules/ai-assistant-configs.js +184 -23
  21. package/lib/rules/no-generated-view-edits.js +81 -0
  22. package/lib/rules/no-inline-jsx-event-handlers.js +1 -1
  23. package/lib/rules/no-nested-mount.js +2 -1
  24. package/lib/rules/no-raw-with-dynamic-arg.js +7 -6
  25. package/lib/rules/prefer-module-jsx-augmentation.js +3 -2
  26. package/lib/rules/require-data-key-in-each.js +38 -3
  27. package/lib/rules/require-delegate-disposer.js +2 -1
  28. package/lib/rules/ui-component-ownership.js +211 -0
  29. package/lib/rules/ui-composition.js +425 -0
  30. package/lib/rules/ui-css-values.js +3 -0
  31. package/lib/rules/ui-exceptional-shorthands.js +3 -0
  32. package/lib/rules/ui-preferences.js +116 -0
  33. package/lib/rules/ui-public-boundaries.js +196 -0
  34. package/lib/rules/ui-wiring.js +440 -0
  35. package/lib/ui-contract.js +643 -0
  36. package/lib/ui-css-value-contracts.js +317 -0
  37. package/package.json +8 -2
@@ -0,0 +1,317 @@
1
+ import {
2
+ importRegistry,
3
+ isExcepted,
4
+ jsxKey,
5
+ loadUiContract,
6
+ UI_CONTRACT_LOAD_CODE,
7
+ UI_RULE_SCHEMA,
8
+ } from './ui-contract.js';
9
+
10
+ const CODES = {
11
+ raw: 'KUI-L013',
12
+ helper: 'KUI-L014',
13
+ expression: 'KUI-L015',
14
+ declarations: 'KUI-L016',
15
+ exceptional: 'KUI-L017',
16
+ };
17
+
18
+ function propertyName(node) {
19
+ if (!node || node.computed) return undefined;
20
+ return node.key?.name ?? node.key?.value;
21
+ }
22
+
23
+ function objectProperty(object, name) {
24
+ return object?.properties?.find(
25
+ (property) =>
26
+ property.type === 'Property' && propertyName(property) === name,
27
+ );
28
+ }
29
+
30
+ function jsxAttribute(opening, name) {
31
+ return opening.attributes.find(
32
+ (attribute) =>
33
+ attribute.type === 'JSXAttribute' && attribute.name.name === name,
34
+ );
35
+ }
36
+
37
+ function jsxValue(attribute) {
38
+ if (!attribute?.value) return undefined;
39
+ if (attribute.value.type === 'Literal') return attribute.value;
40
+ if (attribute.value.type === 'JSXExpressionContainer')
41
+ return attribute.value.expression;
42
+ return undefined;
43
+ }
44
+
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;
78
+ }
79
+
80
+ function contractValuesFromJsx(opening, path) {
81
+ const segments = pathSegments(path);
82
+ return nestedValues(
83
+ jsxValue(jsxAttribute(opening, segments[0].name)),
84
+ segments,
85
+ );
86
+ }
87
+
88
+ function contractValuesFromCall(call, path) {
89
+ const options = call.arguments[0];
90
+ if (options?.type !== 'ObjectExpression') return [];
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;
123
+ }
124
+
125
+ const CONTRACT_MESSAGES = {
126
+ raw: 'KUI-L013: `{{component}}.{{path}}` uses {{grammar}} grammar; replace raw `{{value}}` with {{preferred}}.',
127
+ helper:
128
+ 'KUI-L014: `{{helper}}()` produces the wrong grammar for `{{component}}.{{path}}`; use {{preferred}}.',
129
+ expression:
130
+ 'KUI-L015: `{{helper}}()` is not a standalone CSS value for `{{component}}.{{path}}`; wrap it with an accepted composer such as `calc()`.',
131
+ declarations:
132
+ 'KUI-L016: `{{component}}.{{path}}` is a forbidden declaration-list escape hatch; use `className`, public tokens, or cataloged props.',
133
+ };
134
+
135
+ const REVIEW_MESSAGES = {
136
+ exceptional:
137
+ 'KUI-L017: `{{value}}` is an exceptional shorthand for `{{component}}.{{path}}`; prefer {{preferred}} unless the off-scale choice is deliberate.',
138
+ };
139
+
140
+ // One catalog walk backs two rules because ESLint assigns severity per rule,
141
+ // not per message. `KUI-L013`–`KUI-L016` are contract violations (analyzer
142
+ // severity `error`); `KUI-L017` is a review finding (analyzer severity
143
+ // `review`), so it lives in its own rule that the presets configure as `warn`.
144
+ // Reporting all five from one `error` rule made a deliberate off-scale choice
145
+ // fail lint while the analyzer and doctor queued it for review.
146
+ export function createCssValueRule({ review }) {
147
+ const slug = review ? 'ui-exceptional-shorthands' : 'ui-css-values';
148
+ const reported = review ? REVIEW_MESSAGES : CONTRACT_MESSAGES;
149
+ return {
150
+ meta: {
151
+ type: review ? 'suggestion' : 'problem',
152
+ docs: {
153
+ description: review
154
+ ? 'Flag valid but exceptional off-scale shorthands for Kerf UI components for review.'
155
+ : 'Enforce cataloged property-specific CSS value contracts for Kerf UI components.',
156
+ url: `https://github.com/brianwestphal/kerf/blob/main/eslint-plugin/docs/rules/${slug}.md`,
157
+ },
158
+ schema: UI_RULE_SCHEMA,
159
+ messages: {
160
+ config: `${UI_CONTRACT_LOAD_CODE}: Kerf UI catalog/profile could not be loaded: {{error}}`,
161
+ ...reported,
162
+ },
163
+ },
164
+ create(context) {
165
+ return createVisitors(context, (descriptor) => {
166
+ if (descriptor.messageId in reported) context.report(descriptor);
167
+ });
168
+ },
169
+ };
170
+ }
171
+
172
+ function createVisitors(context, report) {
173
+ const loaded = loadUiContract(context);
174
+ const filename = context.filename ?? context.getFilename();
175
+ let registry;
176
+
177
+ const inspect = (node, entry, valuesFor) => {
178
+ for (const cssContract of entry.cssValueProps ?? []) {
179
+ const preferred =
180
+ [
181
+ ...(cssContract.canonicalShorthands ?? []).map(
182
+ (item) => `\`${item}\``,
183
+ ),
184
+ ...(cssContract.helpers ?? []).map((item) => `\`${item}()\``),
185
+ ].join(' or ') || 'a cataloged component prop';
186
+ for (const value of valuesFor(cssContract.path)) {
187
+ if (!value) continue;
188
+ if (
189
+ cssContract.grammar === 'declarations' &&
190
+ cssContract.rawPolicy !== 'allow'
191
+ ) {
192
+ if (!isExcepted(loaded, CODES.declarations, filename))
193
+ report({
194
+ node: value,
195
+ messageId: 'declarations',
196
+ data: {
197
+ component: entry.name,
198
+ path: cssContract.path,
199
+ },
200
+ });
201
+ continue;
202
+ }
203
+ const literal =
204
+ value.type === 'Literal' && typeof value.value === 'string'
205
+ ? value.value
206
+ : value.type === 'TemplateLiteral' && value.expressions.length === 0
207
+ ? value.quasis[0].value.cooked
208
+ : undefined;
209
+ if (literal !== undefined) {
210
+ if (cssContract.exceptionalShorthands?.includes(literal)) {
211
+ if (!isExcepted(loaded, CODES.exceptional, filename))
212
+ report({
213
+ node: value,
214
+ messageId: 'exceptional',
215
+ data: {
216
+ component: entry.name,
217
+ path: cssContract.path,
218
+ value: literal,
219
+ preferred,
220
+ },
221
+ });
222
+ } else if (
223
+ !cssContract.shorthands?.includes(literal) &&
224
+ cssContract.rawPolicy !== 'allow'
225
+ ) {
226
+ const code = CODES.raw;
227
+ if (!isExcepted(loaded, code, filename))
228
+ report({
229
+ node: value,
230
+ messageId: 'raw',
231
+ data: {
232
+ component: entry.name,
233
+ path: cssContract.path,
234
+ grammar: cssContract.grammar,
235
+ value: literal,
236
+ preferred,
237
+ },
238
+ });
239
+ }
240
+ continue;
241
+ }
242
+ if (value.type !== 'CallExpression') continue;
243
+ const helper = cssValueHelper(value.callee, registry, entry.package);
244
+ if (!helper) continue;
245
+ if (cssContract.nonStandaloneHelpers?.includes(helper)) {
246
+ if (!isExcepted(loaded, CODES.expression, filename))
247
+ report({
248
+ node: value,
249
+ messageId: 'expression',
250
+ data: {
251
+ component: entry.name,
252
+ path: cssContract.path,
253
+ helper,
254
+ },
255
+ });
256
+ } else if (
257
+ !cssContract.helpers?.includes(helper) &&
258
+ cssContract.unsafeHelper !== helper
259
+ ) {
260
+ if (!isExcepted(loaded, CODES.helper, filename))
261
+ report({
262
+ node: value,
263
+ messageId: 'helper',
264
+ data: {
265
+ component: entry.name,
266
+ path: cssContract.path,
267
+ helper,
268
+ preferred,
269
+ },
270
+ });
271
+ }
272
+ }
273
+ }
274
+ };
275
+
276
+ return {
277
+ Program(node) {
278
+ if (loaded.error)
279
+ context.report({
280
+ node,
281
+ messageId: 'config',
282
+ data: { error: loaded.error },
283
+ });
284
+ else registry = importRegistry(node, loaded, filename);
285
+ },
286
+ JSXOpeningElement(node) {
287
+ if (!registry) return;
288
+ const key = jsxKey(node.name, registry, loaded);
289
+ const entry = loaded.entries.get(key);
290
+ if (entry)
291
+ inspect(node, entry, (path) => contractValuesFromJsx(node, path));
292
+ },
293
+ CallExpression(node) {
294
+ if (!registry) return;
295
+ let key;
296
+ if (node.callee.type === 'Identifier')
297
+ key = registry.locals.get(node.callee.name);
298
+ else if (
299
+ node.callee.type === 'MemberExpression' &&
300
+ !node.callee.computed &&
301
+ node.callee.object.type === 'Identifier' &&
302
+ node.callee.property.type === 'Identifier'
303
+ ) {
304
+ const source = registry.namespaces.get(node.callee.object.name);
305
+ const exportedKey = loaded.exports.get(node.callee.property.name);
306
+ if (
307
+ source === loaded.package ||
308
+ loaded.imports.get(source) === exportedKey
309
+ )
310
+ key = exportedKey;
311
+ }
312
+ const entry = loaded.entries.get(key);
313
+ if (entry)
314
+ inspect(node, entry, (path) => contractValuesFromCall(node, path));
315
+ },
316
+ };
317
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-kerfjs",
3
- "version": "5.0.0-beta.13",
3
+ "version": "5.0.0-beta.131",
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",
@@ -36,14 +36,20 @@
36
36
  "LICENSE"
37
37
  ],
38
38
  "scripts": {
39
+ "lint": "eslint --max-warnings 0 .",
39
40
  "test": "node --test tests/rules/*.test.js",
40
- "test:eslint-matrix": "node scripts/eslint-matrix.mjs"
41
+ "test:eslint-matrix": "node scripts/eslint-matrix.mjs",
42
+ "check": "node scripts/eslint-matrix.mjs --parity-only && npm run lint && npm test"
41
43
  },
42
44
  "peerDependencies": {
43
45
  "eslint": "^9.0.0 || ^10.0.0"
44
46
  },
45
47
  "devDependencies": {
48
+ "@eslint/js": "^9.39.5",
46
49
  "@typescript-eslint/parser": "^8.0.0",
47
50
  "eslint": "^9.0.0"
51
+ },
52
+ "overrides": {
53
+ "minimatch@>=7 <11.0.1": "^11.0.5"
48
54
  }
49
55
  }