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

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,116 @@
1
+ import { resolve } from 'node:path';
2
+
3
+ import {
4
+ customElementKey,
5
+ importRegistry,
6
+ isExcepted,
7
+ jsxKey,
8
+ loadUiContract,
9
+ UI_CONTRACT_LOAD_CODE,
10
+ UI_RULE_SCHEMA,
11
+ } from '../ui-contract.js';
12
+
13
+ const CODE = 'KUI-L301';
14
+ const SCOPE_CODE = 'KUI-L302';
15
+ const FORM_CODE = 'KUI-L303';
16
+ /** Web Awesome appearance-scope classes, which need its theme to do anything. */
17
+ const WA_SCOPE_CLASS =
18
+ /(?:^|\s)(wa-(?:light|dark|invert|theme-default))(?=\s|$)/g;
19
+
20
+ /** The static class text of a `class` / `className` attribute value. */
21
+ function classText(value) {
22
+ if (!value) return [];
23
+ if (value.type === 'Literal' && typeof value.value === 'string')
24
+ return [value.value];
25
+ if (value.type !== 'JSXExpressionContainer') return [];
26
+ const expression = value.expression;
27
+ if (expression.type === 'Literal' && typeof expression.value === 'string')
28
+ return [expression.value];
29
+ if (expression.type === 'TemplateLiteral')
30
+ return expression.quasis.map((quasi) => quasi.value.cooked ?? '');
31
+ return [];
32
+ }
33
+
34
+ export default {
35
+ meta: {
36
+ type: 'suggestion',
37
+ docs: {
38
+ description: 'Apply application-profile preferred UI component choices.',
39
+ url: 'https://github.com/brianwestphal/kerf/blob/main/eslint-plugin/docs/rules/ui-preferences.md',
40
+ },
41
+ schema: UI_RULE_SCHEMA,
42
+ messages: {
43
+ config: `${UI_CONTRACT_LOAD_CODE}: Kerf UI catalog/profile could not be loaded: {{error}}`,
44
+ preferred:
45
+ 'KUI-L301: `{{actual}}` is discouraged for `{{concept}}`; this profile prefers `{{preferred}}`. {{rationale}}',
46
+ rawForm:
47
+ 'KUI-L303: a raw `<form>` has no Kerf UI form contract. Render `Form` from `@kerfjs/ui/form` (with `wireForms` installed) so a submission reaches your `submit` handler without navigating the page.',
48
+ scope:
49
+ 'KUI-L302: `{{name}}` is a Web Awesome appearance scope that does nothing without its theme. Use the `scheme` prop on ContentItem, Pane, NavStack, or TabNavigator, or the `.kui-light` / `.kui-dark` / `.kui-invert` scope class.',
50
+ },
51
+ },
52
+ create(context) {
53
+ const contract = loadUiContract(context);
54
+ const filename = context.filename ?? context.getFilename();
55
+ let registry;
56
+ return {
57
+ JSXAttribute(node) {
58
+ if (contract.error || isExcepted(contract, SCOPE_CODE, filename))
59
+ return;
60
+ const name = node.name.type === 'JSXIdentifier' ? node.name.name : '';
61
+ if (name !== 'class' && name !== 'className') return;
62
+ for (const text of classText(node.value))
63
+ for (const match of text.matchAll(WA_SCOPE_CLASS))
64
+ context.report({
65
+ node: node.value,
66
+ messageId: 'scope',
67
+ data: { name: match[1] },
68
+ });
69
+ },
70
+ Program(node) {
71
+ if (contract.error)
72
+ context.report({
73
+ node,
74
+ messageId: 'config',
75
+ data: { error: contract.error },
76
+ });
77
+ else registry = importRegistry(node, contract, filename);
78
+ },
79
+ JSXOpeningElement(node) {
80
+ if (!registry) return;
81
+ // A declared component's own source is the wrapper that owns the
82
+ // element it renders; the preference governs its callers.
83
+ if (contract.componentSources.has(resolve(filename))) return;
84
+ // KF-JPE38T: a native form navigates on submit unless something
85
+ // cancels it; Kerf UI's Form plus wireForms is that contract.
86
+ if (
87
+ node.name.type === 'JSXIdentifier' &&
88
+ node.name.name === 'form' &&
89
+ !isExcepted(contract, FORM_CODE, filename)
90
+ )
91
+ context.report({ node: node.name, messageId: 'rawForm' });
92
+ if (isExcepted(contract, CODE, filename)) return;
93
+ const actual =
94
+ jsxKey(node.name, registry, contract) ??
95
+ customElementKey(node.name, contract);
96
+ if (!actual) return;
97
+ for (const [concept, preference] of Object.entries(
98
+ contract.profile.preferences ?? {},
99
+ ))
100
+ if ((preference.avoid ?? []).includes(actual))
101
+ context.report({
102
+ node: node.name,
103
+ messageId: 'preferred',
104
+ data: {
105
+ actual,
106
+ concept,
107
+ preferred: preference.preferred,
108
+ rationale:
109
+ preference.rationale ??
110
+ 'Use the project preference unless an exact profile exception applies.',
111
+ },
112
+ });
113
+ },
114
+ };
115
+ },
116
+ };
@@ -0,0 +1,196 @@
1
+ import { resolve } from 'node:path';
2
+
3
+ import {
4
+ importRegistry,
5
+ isExcepted,
6
+ jsxKey,
7
+ loadUiContract,
8
+ UI_CONTRACT_LOAD_CODE,
9
+ UI_RULE_SCHEMA,
10
+ } from '../ui-contract.js';
11
+ import {
12
+ htmlTemplateClassAttributes,
13
+ staticClassNames,
14
+ } from '../html-template-classes.js';
15
+
16
+ const CLASS_CODE = 'KUI-L101';
17
+ const TOKEN_CODE = 'KUI-L102';
18
+ const PLACEMENT_CODE = 'KUI-L103';
19
+
20
+ const HTML_SOURCE = 'kerfjs/html';
21
+
22
+ // The `html` tag imported from `kerfjs/html`, by name or through a namespace.
23
+ function isHtmlTag(tag, registry) {
24
+ if (tag.type === 'Identifier') {
25
+ const helper = registry.helpers.get(tag.name);
26
+ return helper?.imported === 'html' && helper.source === HTML_SOURCE;
27
+ }
28
+ return (
29
+ tag.type === 'MemberExpression' &&
30
+ !tag.computed &&
31
+ tag.object.type === 'Identifier' &&
32
+ tag.property.type === 'Identifier' &&
33
+ tag.property.name === 'html' &&
34
+ registry.namespaces.get(tag.object.name) === HTML_SOURCE
35
+ );
36
+ }
37
+
38
+ export default {
39
+ meta: {
40
+ type: 'problem',
41
+ docs: {
42
+ description:
43
+ 'Restrict Kerf UI classes and tokens to cataloged public boundaries.',
44
+ url: 'https://github.com/brianwestphal/kerf/blob/main/eslint-plugin/docs/rules/ui-public-boundaries.md',
45
+ },
46
+ schema: UI_RULE_SCHEMA,
47
+ messages: {
48
+ config: `${UI_CONTRACT_LOAD_CODE}: Kerf UI catalog/profile could not be loaded: {{error}}`,
49
+ class:
50
+ 'KUI-L101: `{{name}}` is private or uncataloged Kerf UI anatomy. Use a cataloged public class, prop, token, or component instead.',
51
+ token:
52
+ 'KUI-L102: `{{name}}` is not a cataloged public Kerf UI token. Use a public semantic/component token.',
53
+ component:
54
+ "KUI-L103: `{{name}}` is {{component}}'s rendered anatomy, not a class to place on your own element. Render {{render}} and configure it through its props; components own their styles.",
55
+ componentRoot:
56
+ 'KUI-L103: a plain `<{{element}}>` carrying `{{name}}` is exactly what {{component}} renders. Render {{render}} instead; the class stays placeable only on another carrier element (such as a `<ul>` or a `<footer>`).',
57
+ },
58
+ },
59
+ create(context) {
60
+ const contract = loadUiContract(context);
61
+ const filename = context.filename ?? context.getFilename();
62
+ let configured = false;
63
+ let registry;
64
+ // A cataloged component's anatomy class on an element the application
65
+ // writes recreates the component by class instead of rendering it. The
66
+ // component's own element (`<Toolbar className="kui-toolbar">`) is exempt.
67
+ // A placeable class whose entry names a `rootElement` is reported only on
68
+ // a plain element of that tag (`<div class="kui-content-item">` is
69
+ // ContentItem); another carrier element keeps it.
70
+ // `element` is the JSX opening element (or `{ tag }` for markup a
71
+ // `kerfjs/html` template writes) that carries the class.
72
+ const inspectPlacement = (node, names, element) => {
73
+ if (isExcepted(contract, PLACEMENT_CODE, filename)) return;
74
+ const opening = element?.type === 'JSXOpeningElement';
75
+ const renderedKey =
76
+ registry && opening
77
+ ? jsxKey(element.name, registry, contract)
78
+ : undefined;
79
+ const intrinsicTag = opening
80
+ ? element.name.type === 'JSXIdentifier'
81
+ ? element.name.name
82
+ : undefined
83
+ : element?.tag;
84
+ for (const name of names) {
85
+ const owner = contract.componentClasses?.get(name);
86
+ if (!owner || owner.key === renderedKey) continue;
87
+ // A component's own source file is where its anatomy is written.
88
+ if (owner.source && owner.source === resolve(filename)) continue;
89
+ if (owner.element && owner.element !== intrinsicTag) continue;
90
+ context.report({
91
+ node,
92
+ messageId: owner.element ? 'componentRoot' : 'component',
93
+ data: {
94
+ name,
95
+ element: owner.element,
96
+ component: owner.render.join(' / '),
97
+ render: owner.render.map((item) => `\`${item}\``).join(' or '),
98
+ },
99
+ });
100
+ }
101
+ };
102
+ const inspect = (
103
+ node,
104
+ value,
105
+ classContext = false,
106
+ tokens = true,
107
+ element = node.parent,
108
+ ) => {
109
+ if (typeof value !== 'string') return;
110
+ const names = classContext ? value.split(/\s+/).filter(Boolean) : [];
111
+ if (classContext && !isExcepted(contract, CLASS_CODE, filename))
112
+ for (const name of names)
113
+ if (name.startsWith('kui-') && !contract.publicClasses.has(name))
114
+ context.report({ node, messageId: 'class', data: { name } });
115
+ // Placement covers every cataloged class, not only `kui-*`: a declared
116
+ // component package's anatomy (`acme-meter`) is checked the same way.
117
+ if (classContext) inspectPlacement(node, names, element);
118
+ if (tokens && !isExcepted(contract, TOKEN_CODE, filename))
119
+ for (const match of value.matchAll(/--kui-[a-z0-9-]+/g))
120
+ if (!contract.publicTokens.has(match[0]))
121
+ context.report({
122
+ node,
123
+ messageId: 'token',
124
+ data: { name: match[0] },
125
+ });
126
+ };
127
+ return {
128
+ Program(node) {
129
+ if (contract.error && !configured) {
130
+ configured = true;
131
+ context.report({
132
+ node,
133
+ messageId: 'config',
134
+ data: { error: contract.error },
135
+ });
136
+ }
137
+ if (!contract.error)
138
+ registry = importRegistry(node, contract, filename);
139
+ },
140
+ JSXAttribute(node) {
141
+ if (contract.error || !['class', 'className'].includes(node.name?.name))
142
+ return;
143
+ if (node.value?.type === 'Literal')
144
+ inspect(node, node.value.value, true);
145
+ if (
146
+ node.value?.type === 'JSXExpressionContainer' &&
147
+ node.value.expression.type === 'Literal'
148
+ )
149
+ inspect(node, node.value.expression.value, true);
150
+ // A template literal's static, whitespace-delimited class names
151
+ // (`class={`kui-content-item ${extra}`}`); a name an interpolation
152
+ // completes (`kui-toolbar-${size}`) is not known and is skipped.
153
+ if (
154
+ node.value?.type === 'JSXExpressionContainer' &&
155
+ node.value.expression.type === 'TemplateLiteral'
156
+ )
157
+ inspect(
158
+ node,
159
+ staticClassNames(
160
+ node.value.expression.quasis
161
+ .map((quasi) => quasi.value.cooked ?? quasi.value.raw)
162
+ .join('\0'),
163
+ ).join(' '),
164
+ true,
165
+ // Its tokens are inspected once, by TemplateElement below.
166
+ false,
167
+ );
168
+ },
169
+ // `kerfjs/html` tagged templates write the same markup without JSX:
170
+ // their static `class` attributes are inspected like JSX `class`
171
+ // values, on the plain element that carries them. Tokens are left to
172
+ // TemplateElement below.
173
+ TaggedTemplateExpression(node) {
174
+ if (contract.error || !registry || !isHtmlTag(node.tag, registry))
175
+ return;
176
+ const { quasis } = node.quasi;
177
+ for (const { quasi, tag, value } of htmlTemplateClassAttributes(
178
+ quasis.map((item) => item.value.cooked ?? item.value.raw),
179
+ ))
180
+ inspect(
181
+ quasis[quasi],
182
+ staticClassNames(value).join(' '),
183
+ true,
184
+ false,
185
+ { tag },
186
+ );
187
+ },
188
+ Literal(node) {
189
+ if (!contract.error) inspect(node, node.value, false);
190
+ },
191
+ TemplateElement(node) {
192
+ if (!contract.error) inspect(node, node.value.raw, false);
193
+ },
194
+ };
195
+ },
196
+ };