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.
- package/README.md +85 -21
- package/docs/rules/ai-assistant-configs.md +17 -6
- package/docs/rules/no-generated-view-edits.md +42 -0
- package/docs/rules/no-inline-jsx-event-handlers.md +2 -2
- package/docs/rules/no-nested-mount.md +6 -1
- package/docs/rules/no-raw-with-dynamic-arg.md +7 -7
- package/docs/rules/prefer-attr-selector.md +7 -7
- package/docs/rules/prefer-module-jsx-augmentation.md +8 -4
- package/docs/rules/require-data-key-in-each.md +16 -6
- package/docs/rules/require-delegate-disposer.md +10 -5
- package/docs/rules/ui-component-ownership.md +43 -0
- package/docs/rules/ui-composition.md +116 -0
- package/docs/rules/ui-css-values.md +51 -0
- package/docs/rules/ui-exceptional-shorthands.md +22 -0
- package/docs/rules/ui-preferences.md +41 -0
- package/docs/rules/ui-public-boundaries.md +48 -0
- package/docs/rules/ui-wiring.md +57 -0
- package/index.js +51 -1
- package/lib/html-template-classes.js +104 -0
- package/lib/rules/ai-assistant-configs.js +184 -23
- package/lib/rules/no-generated-view-edits.js +81 -0
- package/lib/rules/no-inline-jsx-event-handlers.js +1 -1
- package/lib/rules/no-nested-mount.js +2 -1
- package/lib/rules/no-raw-with-dynamic-arg.js +7 -6
- package/lib/rules/prefer-module-jsx-augmentation.js +3 -2
- package/lib/rules/require-data-key-in-each.js +38 -3
- package/lib/rules/require-delegate-disposer.js +2 -1
- package/lib/rules/ui-component-ownership.js +211 -0
- package/lib/rules/ui-composition.js +425 -0
- package/lib/rules/ui-css-values.js +3 -0
- package/lib/rules/ui-exceptional-shorthands.js +3 -0
- package/lib/rules/ui-preferences.js +116 -0
- package/lib/rules/ui-public-boundaries.js +196 -0
- package/lib/rules/ui-wiring.js +440 -0
- package/lib/ui-contract.js +643 -0
- package/lib/ui-css-value-contracts.js +317 -0
- package/package.json +8 -2
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { dirname, resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
isExcepted,
|
|
6
|
+
loadUiContract,
|
|
7
|
+
UI_CONTRACT_LOAD_CODE,
|
|
8
|
+
UI_RULE_SCHEMA,
|
|
9
|
+
} from '../ui-contract.js';
|
|
10
|
+
|
|
11
|
+
const PRIVATE_CODE = 'KUI-L020';
|
|
12
|
+
const TYPED_CODE = 'KUI-L021';
|
|
13
|
+
|
|
14
|
+
const kebab = (value) =>
|
|
15
|
+
value.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
16
|
+
|
|
17
|
+
// The same catalog-derived facts `kerf-ui-analyze` uses for KUI-L020 /
|
|
18
|
+
// KUI-L021 (`@kerfjs/ui/analyzer/component-ownership.mjs`): every root-like
|
|
19
|
+
// public class names a component, `--_<root>-*` is that component's private
|
|
20
|
+
// variable, and `--<root>-<prop>` is the token its typed `<prop>` sets.
|
|
21
|
+
const factsCache = new WeakMap();
|
|
22
|
+
function ownershipFacts(contract) {
|
|
23
|
+
if (factsCache.has(contract)) return factsCache.get(contract);
|
|
24
|
+
const privatePrefixes = [];
|
|
25
|
+
const typedTokens = new Map();
|
|
26
|
+
for (const entry of contract.entries.values()) {
|
|
27
|
+
const boundaries = entry.boundaries ?? {};
|
|
28
|
+
const roots = [
|
|
29
|
+
...new Set(
|
|
30
|
+
[boundaries.rootClass, ...(boundaries.publicClasses ?? [])].filter(
|
|
31
|
+
(name) => name && !name.includes('__') && !name.includes('--'),
|
|
32
|
+
),
|
|
33
|
+
),
|
|
34
|
+
];
|
|
35
|
+
const tokens = new Set(boundaries.publicTokens ?? []);
|
|
36
|
+
for (const root of roots) {
|
|
37
|
+
privatePrefixes.push({ prefix: `--_${root}`, entry });
|
|
38
|
+
for (const contractProp of entry.cssValueProps ?? []) {
|
|
39
|
+
const [head] = contractProp.path.split(/\[\]|\./);
|
|
40
|
+
const token = `--${root}-${kebab(head)}`;
|
|
41
|
+
if (tokens.has(token) && !typedTokens.has(token))
|
|
42
|
+
typedTokens.set(token, { entry, path: contractProp.path });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
privatePrefixes.sort((a, b) => b.prefix.length - a.prefix.length);
|
|
47
|
+
const facts = { privatePrefixes, typedTokens };
|
|
48
|
+
factsCache.set(contract, facts);
|
|
49
|
+
return facts;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// The `name` of each directory's package.json, re-read when the manifest's
|
|
53
|
+
// modification time changes so a long-running editor ESLint server sees a
|
|
54
|
+
// renamed package.
|
|
55
|
+
const packageNames = new Map();
|
|
56
|
+
function manifestName(manifest) {
|
|
57
|
+
let mtime;
|
|
58
|
+
try {
|
|
59
|
+
mtime = statSync(manifest).mtimeMs;
|
|
60
|
+
} catch {
|
|
61
|
+
packageNames.delete(manifest);
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
const cached = packageNames.get(manifest);
|
|
65
|
+
if (cached?.mtime === mtime) return cached.name;
|
|
66
|
+
let name;
|
|
67
|
+
try {
|
|
68
|
+
name = JSON.parse(readFileSync(manifest, 'utf8')).name;
|
|
69
|
+
} catch {
|
|
70
|
+
name = undefined;
|
|
71
|
+
}
|
|
72
|
+
if (typeof name !== 'string') name = undefined;
|
|
73
|
+
packageNames.set(manifest, { mtime, name });
|
|
74
|
+
return name;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function ownPackage(filename) {
|
|
78
|
+
for (
|
|
79
|
+
let directory = dirname(resolve(filename));
|
|
80
|
+
;
|
|
81
|
+
directory = dirname(directory)
|
|
82
|
+
) {
|
|
83
|
+
const name = manifestName(resolve(directory, 'package.json'));
|
|
84
|
+
if (name !== undefined) return name;
|
|
85
|
+
if (dirname(directory) === directory) return undefined;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const gap = (entry) =>
|
|
90
|
+
`If no configuration covers this need, report the component gap to ${entry?.package ?? '@kerfjs/ui'} (open a feature request) instead of overriding it.`;
|
|
91
|
+
|
|
92
|
+
export default {
|
|
93
|
+
meta: {
|
|
94
|
+
type: 'problem',
|
|
95
|
+
docs: {
|
|
96
|
+
description:
|
|
97
|
+
"Keep code from reaching into a Kerf UI component's private variables or overriding a token its typed prop configures.",
|
|
98
|
+
url: 'https://github.com/brianwestphal/kerf/blob/main/eslint-plugin/docs/rules/ui-component-ownership.md',
|
|
99
|
+
},
|
|
100
|
+
schema: UI_RULE_SCHEMA,
|
|
101
|
+
messages: {
|
|
102
|
+
config: `${UI_CONTRACT_LOAD_CODE}: Kerf UI catalog/profile could not be loaded: {{error}}`,
|
|
103
|
+
private: `${PRIVATE_CODE}: \`{{name}}\` is {{owner}}'s private variable; components own their styles. Configure it through its typed props, variants, or public tokens. {{gap}}`,
|
|
104
|
+
typed: `${TYPED_CODE}: \`{{name}}\` is what {{component}}'s typed \`{{prop}}\` prop sets; set the prop on {{component}} instead of overriding the token. {{gap}}`,
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
create(context) {
|
|
108
|
+
const contract = loadUiContract(context);
|
|
109
|
+
const filename = context.filename ?? context.getFilename();
|
|
110
|
+
let configured = false;
|
|
111
|
+
if (contract.error)
|
|
112
|
+
return {
|
|
113
|
+
Program(node) {
|
|
114
|
+
if (configured) return;
|
|
115
|
+
configured = true;
|
|
116
|
+
context.report({
|
|
117
|
+
node,
|
|
118
|
+
messageId: 'config',
|
|
119
|
+
data: { error: contract.error },
|
|
120
|
+
});
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
const facts = ownershipFacts(contract);
|
|
124
|
+
const own = ownPackage(filename);
|
|
125
|
+
const checkPrivate = !isExcepted(contract, PRIVATE_CODE, filename);
|
|
126
|
+
const checkTyped = !isExcepted(contract, TYPED_CODE, filename);
|
|
127
|
+
|
|
128
|
+
const privateOwner = (name) => {
|
|
129
|
+
const match = facts.privatePrefixes.find(
|
|
130
|
+
({ prefix }) => name === prefix || name.startsWith(`${prefix}-`),
|
|
131
|
+
);
|
|
132
|
+
if (match) return match.entry;
|
|
133
|
+
return name.startsWith('--_kui-') ? null : undefined;
|
|
134
|
+
};
|
|
135
|
+
const reportPrivate = (node, text) => {
|
|
136
|
+
if (!checkPrivate) return;
|
|
137
|
+
for (const [name] of text.matchAll(/--_[a-z0-9_-]+/gi)) {
|
|
138
|
+
const owner = privateOwner(name);
|
|
139
|
+
if (owner === undefined || (owner && owner.package === own)) continue;
|
|
140
|
+
context.report({
|
|
141
|
+
node,
|
|
142
|
+
messageId: 'private',
|
|
143
|
+
data: {
|
|
144
|
+
name,
|
|
145
|
+
owner: owner ? owner.name : 'a Kerf component',
|
|
146
|
+
gap: gap(owner),
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
const reportTyped = (node, token) => {
|
|
152
|
+
if (!checkTyped) return;
|
|
153
|
+
const typed = facts.typedTokens.get(token);
|
|
154
|
+
if (!typed || typed.entry.package === own) return;
|
|
155
|
+
context.report({
|
|
156
|
+
node,
|
|
157
|
+
messageId: 'typed',
|
|
158
|
+
data: {
|
|
159
|
+
name: token,
|
|
160
|
+
component: typed.entry.name,
|
|
161
|
+
prop: typed.path,
|
|
162
|
+
gap: gap(typed.entry),
|
|
163
|
+
},
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
const inspectText = (node, text) => {
|
|
167
|
+
if (typeof text !== 'string') return;
|
|
168
|
+
reportPrivate(node, text);
|
|
169
|
+
// `--token: value` inside a style string assigns the token.
|
|
170
|
+
for (const [, token] of text.matchAll(/(--[a-z0-9-]+)\s*:/gi))
|
|
171
|
+
reportTyped(node, token);
|
|
172
|
+
};
|
|
173
|
+
const keyName = (property) =>
|
|
174
|
+
property.computed
|
|
175
|
+
? property.key.type === 'Literal'
|
|
176
|
+
? property.key.value
|
|
177
|
+
: undefined
|
|
178
|
+
: (property.key.name ?? property.key.value);
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
Literal(node) {
|
|
182
|
+
if (node.parent?.type === 'Property' && node.parent.key === node)
|
|
183
|
+
return;
|
|
184
|
+
inspectText(node, node.value);
|
|
185
|
+
},
|
|
186
|
+
TemplateElement(node) {
|
|
187
|
+
inspectText(node, node.value.raw);
|
|
188
|
+
},
|
|
189
|
+
// `style={{ '--kui-list-gap': … }}` and any object keyed by a token.
|
|
190
|
+
Property(node) {
|
|
191
|
+
const key = keyName(node);
|
|
192
|
+
if (typeof key !== 'string' || !key.startsWith('--')) return;
|
|
193
|
+
reportPrivate(node.key, key);
|
|
194
|
+
reportTyped(node.key, key);
|
|
195
|
+
},
|
|
196
|
+
// `element.style.setProperty('--kui-list-gap', …)`.
|
|
197
|
+
CallExpression(node) {
|
|
198
|
+
const callee = node.callee;
|
|
199
|
+
const [first] = node.arguments;
|
|
200
|
+
if (
|
|
201
|
+
callee.type === 'MemberExpression' &&
|
|
202
|
+
!callee.computed &&
|
|
203
|
+
callee.property.name === 'setProperty' &&
|
|
204
|
+
first?.type === 'Literal' &&
|
|
205
|
+
typeof first.value === 'string'
|
|
206
|
+
)
|
|
207
|
+
reportTyped(first, first.value);
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
},
|
|
211
|
+
};
|
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
import {
|
|
2
|
+
customElementKey,
|
|
3
|
+
importRegistry,
|
|
4
|
+
isExcepted,
|
|
5
|
+
jsxExportName,
|
|
6
|
+
jsxKey,
|
|
7
|
+
loadUiContract,
|
|
8
|
+
UI_CONTRACT_LOAD_CODE,
|
|
9
|
+
UI_RULE_SCHEMA,
|
|
10
|
+
} from '../ui-contract.js';
|
|
11
|
+
|
|
12
|
+
const PARENT_CODE = 'KUI-L201';
|
|
13
|
+
const ZONE_CODE = 'KUI-L202';
|
|
14
|
+
const CARDINALITY_CODE = 'KUI-L203';
|
|
15
|
+
const INSET_CODE = 'KUI-L204';
|
|
16
|
+
const INTERACTIVE_CODE = 'KUI-L205';
|
|
17
|
+
|
|
18
|
+
const emptyShape = () => ({ min: 0, max: 0, items: [] });
|
|
19
|
+
|
|
20
|
+
function combine(shapes) {
|
|
21
|
+
return shapes.reduce(
|
|
22
|
+
(result, shape) => ({
|
|
23
|
+
min: result.min + shape.min,
|
|
24
|
+
max: result.max + shape.max,
|
|
25
|
+
items: [...result.items, ...shape.items],
|
|
26
|
+
}),
|
|
27
|
+
emptyShape(),
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function jsxLabel(name) {
|
|
32
|
+
if (name.type === 'JSXIdentifier') return `<${name.name}>`;
|
|
33
|
+
if (name.type === 'JSXMemberExpression')
|
|
34
|
+
return `${jsxLabel(name.object).slice(0, -1)}.${name.property.name}>`;
|
|
35
|
+
return '<unknown>';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function zoneShape(node, registry, contract) {
|
|
39
|
+
if (!node || node.type === 'JSXEmptyExpression') return emptyShape();
|
|
40
|
+
if (node.type === 'JSXElement') {
|
|
41
|
+
const key =
|
|
42
|
+
jsxKey(node.openingElement.name, registry, contract) ??
|
|
43
|
+
customElementKey(node.openingElement.name, contract);
|
|
44
|
+
const entry = contract.entries.get(key);
|
|
45
|
+
const exportName = jsxExportName(node.openingElement.name, registry);
|
|
46
|
+
// A second JSX export from one package entry may have its own element
|
|
47
|
+
// shape. It must not impersonate the entry's root in a parent's zone.
|
|
48
|
+
if (entry?.jsxExports?.[exportName])
|
|
49
|
+
return {
|
|
50
|
+
min: 1,
|
|
51
|
+
max: 1,
|
|
52
|
+
items: [{ label: `${key}:${exportName}` }],
|
|
53
|
+
};
|
|
54
|
+
const roots = entry?.rendersAs;
|
|
55
|
+
// A declared wrapper renders one of its roots, or nothing.
|
|
56
|
+
if (roots?.length)
|
|
57
|
+
return {
|
|
58
|
+
min: 0,
|
|
59
|
+
max: 1,
|
|
60
|
+
items: [{ key, roots, label: `${key} (renders ${roots.join(' | ')})` }],
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
min: 1,
|
|
64
|
+
max: 1,
|
|
65
|
+
items: [
|
|
66
|
+
{
|
|
67
|
+
key,
|
|
68
|
+
label: key ?? jsxLabel(node.openingElement.name),
|
|
69
|
+
element: node.openingElement,
|
|
70
|
+
},
|
|
71
|
+
],
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (node.type === 'JSXFragment')
|
|
75
|
+
return combine(
|
|
76
|
+
node.children.map((child) => zoneShape(child, registry, contract)),
|
|
77
|
+
);
|
|
78
|
+
if (node.type === 'JSXText')
|
|
79
|
+
return node.value.trim()
|
|
80
|
+
? { min: 1, max: 1, items: [{ label: 'text' }] }
|
|
81
|
+
: emptyShape();
|
|
82
|
+
if (node.type === 'JSXExpressionContainer')
|
|
83
|
+
return zoneShape(node.expression, registry, contract);
|
|
84
|
+
if (node.type === 'ArrayExpression')
|
|
85
|
+
return combine(
|
|
86
|
+
node.elements.map((child) => zoneShape(child, registry, contract)),
|
|
87
|
+
);
|
|
88
|
+
if (node.type === 'ConditionalExpression') {
|
|
89
|
+
const consequent = zoneShape(node.consequent, registry, contract);
|
|
90
|
+
const alternate = zoneShape(node.alternate, registry, contract);
|
|
91
|
+
return {
|
|
92
|
+
min: Math.min(consequent.min, alternate.min),
|
|
93
|
+
max: Math.max(consequent.max, alternate.max),
|
|
94
|
+
items: [...consequent.items, ...alternate.items],
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
if (node.type === 'LogicalExpression') {
|
|
98
|
+
const left = zoneShape(node.left, registry, contract);
|
|
99
|
+
const right = zoneShape(node.right, registry, contract);
|
|
100
|
+
return node.operator === '&&'
|
|
101
|
+
? { min: 0, max: right.max, items: right.items }
|
|
102
|
+
: {
|
|
103
|
+
min: Math.min(left.min, right.min),
|
|
104
|
+
max: Math.max(left.max, right.max),
|
|
105
|
+
items: [...left.items, ...right.items],
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (node.type === 'Literal')
|
|
109
|
+
return node.value === null || typeof node.value === 'boolean'
|
|
110
|
+
? emptyShape()
|
|
111
|
+
: { min: 1, max: 1, items: [{ label: 'text' }] };
|
|
112
|
+
return { min: 0, max: Number.POSITIVE_INFINITY, items: [] };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function directParentKey(node, registry, contract) {
|
|
116
|
+
let parent = node.parent?.parent;
|
|
117
|
+
while (
|
|
118
|
+
parent &&
|
|
119
|
+
['JSXExpressionContainer', 'JSXFragment'].includes(parent.type)
|
|
120
|
+
)
|
|
121
|
+
parent = parent.parent;
|
|
122
|
+
if (parent?.type === 'JSXAttribute') parent = parent.parent?.parent;
|
|
123
|
+
return parent?.type === 'JSXElement'
|
|
124
|
+
? { key: jsxKey(parent.openingElement.name, registry, contract) }
|
|
125
|
+
: undefined;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// A prop's statically written value: `{ known: true, value }`, where a bare
|
|
129
|
+
// boolean prop is `true` and an absent one is `undefined`, or
|
|
130
|
+
// `{ known: false }` when a spread or an expression could supply it.
|
|
131
|
+
function staticProp(openingElement, prop) {
|
|
132
|
+
const attribute = openingElement.attributes.find(
|
|
133
|
+
(candidate) =>
|
|
134
|
+
candidate.type === 'JSXAttribute' && candidate.name.name === prop,
|
|
135
|
+
);
|
|
136
|
+
if (!attribute)
|
|
137
|
+
return openingElement.attributes.some(
|
|
138
|
+
(candidate) => candidate.type === 'JSXSpreadAttribute',
|
|
139
|
+
)
|
|
140
|
+
? { known: false }
|
|
141
|
+
: { known: true, value: undefined };
|
|
142
|
+
if (attribute.value === null) return { known: true, value: true };
|
|
143
|
+
const value =
|
|
144
|
+
attribute.value.type === 'JSXExpressionContainer'
|
|
145
|
+
? attribute.value.expression
|
|
146
|
+
: attribute.value;
|
|
147
|
+
if (value.type === 'Literal') return { known: true, value: value.value };
|
|
148
|
+
if (value.type === 'TemplateLiteral' && value.expressions.length === 0)
|
|
149
|
+
return { known: true, value: value.quasis[0].value.cooked };
|
|
150
|
+
return { known: false };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const conditionLabel = ({ prop, equals }) =>
|
|
154
|
+
equals === true
|
|
155
|
+
? `\`${prop}\``
|
|
156
|
+
: typeof equals === 'string'
|
|
157
|
+
? `\`${prop}="${equals}"\``
|
|
158
|
+
: `\`${prop}={${equals}}\``;
|
|
159
|
+
|
|
160
|
+
// An element owns the shared content-item inset when its geometry owns its
|
|
161
|
+
// margin, border, and padding, unless one of its inset-free conditions holds.
|
|
162
|
+
// A condition the rule cannot read statically counts as holding.
|
|
163
|
+
function bringsInset(entry, element) {
|
|
164
|
+
const geometry = entry?.layout?.geometry;
|
|
165
|
+
if (
|
|
166
|
+
!geometry ||
|
|
167
|
+
!['margin', 'border', 'padding'].every(
|
|
168
|
+
(dimension) => geometry[dimension] === 'self',
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
return false;
|
|
172
|
+
return !(entry.insetFreeWhen ?? []).some((condition) => {
|
|
173
|
+
const prop = staticProp(element, condition.prop);
|
|
174
|
+
return !prop.known || prop.value === condition.equals;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// The cataloged roots an element stands for: a declared wrapper's `rendersAs`,
|
|
179
|
+
// otherwise the element's own entry.
|
|
180
|
+
const rootsOf = (key, contract) =>
|
|
181
|
+
contract.entries.get(key)?.rendersAs ?? [key];
|
|
182
|
+
|
|
183
|
+
// Whether an element renders a native control or link as its root (`root`)
|
|
184
|
+
// or inside it (`contains`), from its entry's `content` rules (KF-VTFKX8, the
|
|
185
|
+
// JSX side of validateView's KUI-V014). An export with rules of its own uses
|
|
186
|
+
// them; a `whenProps` rule needs one of its props written, and a literal
|
|
187
|
+
// `false` or `null` does not count.
|
|
188
|
+
function interactiveOf(openingElement, registry, contract) {
|
|
189
|
+
const key =
|
|
190
|
+
jsxKey(openingElement.name, registry, contract) ??
|
|
191
|
+
customElementKey(openingElement.name, contract);
|
|
192
|
+
const content = contract.entries.get(key)?.content;
|
|
193
|
+
if (!content) return undefined;
|
|
194
|
+
const exportName = jsxExportName(openingElement.name, registry);
|
|
195
|
+
const rules =
|
|
196
|
+
content.exports?.[exportName]?.interactive ?? content.interactive ?? [];
|
|
197
|
+
const written = (prop) => {
|
|
198
|
+
const value = staticProp(openingElement, prop);
|
|
199
|
+
return (
|
|
200
|
+
openingElement.attributes.some(
|
|
201
|
+
(attribute) =>
|
|
202
|
+
attribute.type === 'JSXAttribute' && attribute.name.name === prop,
|
|
203
|
+
) && !(value.known && (value.value === false || value.value === null))
|
|
204
|
+
);
|
|
205
|
+
};
|
|
206
|
+
const kinds = rules
|
|
207
|
+
.filter((rule) => !rule.whenProps || rule.whenProps.some(written))
|
|
208
|
+
.map((rule) => rule.kind);
|
|
209
|
+
const kind = kinds.includes('root') ? 'root' : kinds[0];
|
|
210
|
+
return kind ? { key, kind } : undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// The nearest enclosing element whose root is a control or link, through
|
|
214
|
+
// children, zone props, and callbacks written inside them.
|
|
215
|
+
function interactiveHost(openingElement, registry, contract) {
|
|
216
|
+
for (
|
|
217
|
+
let ancestor = openingElement.parent.parent;
|
|
218
|
+
ancestor;
|
|
219
|
+
ancestor = ancestor.parent
|
|
220
|
+
) {
|
|
221
|
+
if (ancestor.type !== 'JSXElement') continue;
|
|
222
|
+
const host = interactiveOf(ancestor.openingElement, registry, contract);
|
|
223
|
+
if (host?.kind === 'root') return host;
|
|
224
|
+
}
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function boundZone(openingElement, zone, registry, contract) {
|
|
229
|
+
const prop = zone.jsx?.prop;
|
|
230
|
+
if (!prop) return undefined;
|
|
231
|
+
if (prop === 'children')
|
|
232
|
+
return {
|
|
233
|
+
node: openingElement.name,
|
|
234
|
+
shape: combine(
|
|
235
|
+
(openingElement.parent?.children ?? []).map((child) =>
|
|
236
|
+
zoneShape(child, registry, contract),
|
|
237
|
+
),
|
|
238
|
+
),
|
|
239
|
+
};
|
|
240
|
+
const attribute = openingElement.attributes.find(
|
|
241
|
+
(candidate) =>
|
|
242
|
+
candidate.type === 'JSXAttribute' && candidate.name.name === prop,
|
|
243
|
+
);
|
|
244
|
+
return {
|
|
245
|
+
node: attribute ?? openingElement.name,
|
|
246
|
+
shape: zoneShape(attribute?.value, registry, contract),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export default {
|
|
251
|
+
meta: {
|
|
252
|
+
type: 'problem',
|
|
253
|
+
docs: {
|
|
254
|
+
description:
|
|
255
|
+
'Enforce low-false-positive catalog parent and zone contracts.',
|
|
256
|
+
url: 'https://github.com/brianwestphal/kerf/blob/main/eslint-plugin/docs/rules/ui-composition.md',
|
|
257
|
+
},
|
|
258
|
+
schema: UI_RULE_SCHEMA,
|
|
259
|
+
messages: {
|
|
260
|
+
config: `${UI_CONTRACT_LOAD_CODE}: Kerf UI catalog/profile could not be loaded: {{error}}`,
|
|
261
|
+
parent:
|
|
262
|
+
'KUI-L201: `{{child}}` requires one of these cataloged parents: {{parents}}.',
|
|
263
|
+
zone: 'KUI-L202: `{{component}}` zone `{{zone}}` does not accept {{child}}; catalog accepts: {{accepts}}.',
|
|
264
|
+
cardinality:
|
|
265
|
+
'KUI-L203: `{{component}}` zone `{{zone}}` requires {{cardinality}} direct children; statically found {{count}}.',
|
|
266
|
+
inset:
|
|
267
|
+
'KUI-L204: `{{child}}` brings its own content-item inset (1px border + 8px padding) into `{{component}}` with {{condition}}, which expects inset-free content and double-insets it; {{fix}}.',
|
|
268
|
+
interactive:
|
|
269
|
+
'KUI-L205: `{{child}}` renders a control or link, so it cannot sit inside `{{host}}`, which renders one itself; the browser splits the nested markup. Place it beside the host instead.',
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
create(context) {
|
|
273
|
+
const contract = loadUiContract(context);
|
|
274
|
+
const filename = context.filename ?? context.getFilename();
|
|
275
|
+
let registry;
|
|
276
|
+
return {
|
|
277
|
+
Program(node) {
|
|
278
|
+
if (contract.error)
|
|
279
|
+
context.report({
|
|
280
|
+
node,
|
|
281
|
+
messageId: 'config',
|
|
282
|
+
data: { error: contract.error },
|
|
283
|
+
});
|
|
284
|
+
else registry = importRegistry(node, contract, filename);
|
|
285
|
+
},
|
|
286
|
+
JSXOpeningElement(node) {
|
|
287
|
+
if (!registry) return;
|
|
288
|
+
const inner = interactiveOf(node, registry, contract);
|
|
289
|
+
const host =
|
|
290
|
+
inner &&
|
|
291
|
+
!isExcepted(contract, INTERACTIVE_CODE, filename) &&
|
|
292
|
+
interactiveHost(node, registry, contract);
|
|
293
|
+
if (host)
|
|
294
|
+
context.report({
|
|
295
|
+
node: node.name,
|
|
296
|
+
messageId: 'interactive',
|
|
297
|
+
data: { child: inner.key, host: host.key },
|
|
298
|
+
});
|
|
299
|
+
const key = jsxKey(node.name, registry, contract);
|
|
300
|
+
const entry = contract.entries.get(key);
|
|
301
|
+
if (!entry) return;
|
|
302
|
+
const exportName = jsxExportName(node.name, registry);
|
|
303
|
+
const exportContract = entry.jsxExports?.[exportName];
|
|
304
|
+
// A wrapper answers to its own parent contract and to each root's.
|
|
305
|
+
// Report one violated requirement per placement, with roots first.
|
|
306
|
+
const placed = exportContract
|
|
307
|
+
? [
|
|
308
|
+
{
|
|
309
|
+
entry: { ...entry, parents: exportContract.parents },
|
|
310
|
+
child: `${key}:${exportName}`,
|
|
311
|
+
},
|
|
312
|
+
]
|
|
313
|
+
: [
|
|
314
|
+
...(entry.rendersAs ?? []).flatMap((root) => {
|
|
315
|
+
const rootEntry = contract.entries.get(root);
|
|
316
|
+
return rootEntry
|
|
317
|
+
? [{ entry: rootEntry, child: `${key} (renders ${root})` }]
|
|
318
|
+
: [];
|
|
319
|
+
}),
|
|
320
|
+
{ entry, child: key },
|
|
321
|
+
];
|
|
322
|
+
const constrained = placed.filter(
|
|
323
|
+
({ entry: placedEntry }) => placedEntry.parents?.mode === 'listed',
|
|
324
|
+
);
|
|
325
|
+
if (
|
|
326
|
+
constrained.length &&
|
|
327
|
+
!isExcepted(contract, PARENT_CODE, filename)
|
|
328
|
+
) {
|
|
329
|
+
const parent = directParentKey(node, registry, contract);
|
|
330
|
+
if (parent) {
|
|
331
|
+
const violation = constrained.find(
|
|
332
|
+
({ entry: placedEntry }) =>
|
|
333
|
+
!rootsOf(parent.key, contract).every((root) =>
|
|
334
|
+
placedEntry.parents.entries.includes(root),
|
|
335
|
+
),
|
|
336
|
+
);
|
|
337
|
+
if (violation)
|
|
338
|
+
context.report({
|
|
339
|
+
node: node.name,
|
|
340
|
+
messageId: 'parent',
|
|
341
|
+
data: {
|
|
342
|
+
child: violation.child,
|
|
343
|
+
parents: violation.entry.parents.entries.join(', '),
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
const insetCondition = exportContract
|
|
349
|
+
? undefined
|
|
350
|
+
: entry.insetFreeChildren?.find((condition) => {
|
|
351
|
+
const prop = staticProp(node, condition.prop);
|
|
352
|
+
return prop.known && prop.value === condition.equals;
|
|
353
|
+
});
|
|
354
|
+
if (insetCondition && !isExcepted(contract, INSET_CODE, filename))
|
|
355
|
+
for (const child of combine(
|
|
356
|
+
(node.parent?.children ?? []).map((child) =>
|
|
357
|
+
zoneShape(child, registry, contract),
|
|
358
|
+
),
|
|
359
|
+
).items) {
|
|
360
|
+
const childEntry = child.element && contract.entries.get(child.key);
|
|
361
|
+
if (!childEntry || !bringsInset(childEntry, child.element))
|
|
362
|
+
continue;
|
|
363
|
+
context.report({
|
|
364
|
+
node: child.element.name,
|
|
365
|
+
messageId: 'inset',
|
|
366
|
+
data: {
|
|
367
|
+
child: child.key,
|
|
368
|
+
component: key,
|
|
369
|
+
condition: conditionLabel(insetCondition),
|
|
370
|
+
fix: childEntry.insetFreeWhen?.length
|
|
371
|
+
? `set ${childEntry.insetFreeWhen.map(conditionLabel).join(' or ')}`
|
|
372
|
+
: 'use an inset-free child such as `Text flush`, or inline content',
|
|
373
|
+
},
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
for (const zone of exportContract ? [] : (entry.zones ?? [])) {
|
|
377
|
+
const bound = boundZone(node, zone, registry, contract);
|
|
378
|
+
if (!bound) continue;
|
|
379
|
+
const { node: reportNode, shape } = bound;
|
|
380
|
+
const allowed = new Set(
|
|
381
|
+
zone.accepts
|
|
382
|
+
.map((accepted) =>
|
|
383
|
+
accepted.includes(':')
|
|
384
|
+
? accepted
|
|
385
|
+
: `${entry.package}:${accepted}`,
|
|
386
|
+
)
|
|
387
|
+
.filter((accepted) => contract.entries.has(accepted)),
|
|
388
|
+
);
|
|
389
|
+
if (allowed.size && !isExcepted(contract, ZONE_CODE, filename))
|
|
390
|
+
for (const child of shape.items)
|
|
391
|
+
if (
|
|
392
|
+
child.roots
|
|
393
|
+
? !child.roots.every((root) => allowed.has(root))
|
|
394
|
+
: !child.key || !allowed.has(child.key)
|
|
395
|
+
)
|
|
396
|
+
context.report({
|
|
397
|
+
node: reportNode,
|
|
398
|
+
messageId: 'zone',
|
|
399
|
+
data: {
|
|
400
|
+
component: key,
|
|
401
|
+
zone: zone.id,
|
|
402
|
+
child: child.label,
|
|
403
|
+
accepts: [...allowed].join(', '),
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
if (isExcepted(contract, CARDINALITY_CODE, filename)) continue;
|
|
407
|
+
const { min, max } = zone.cardinality;
|
|
408
|
+
const tooFew = shape.max < min;
|
|
409
|
+
const tooMany = max !== 'unbounded' && shape.min > max;
|
|
410
|
+
if (tooFew || tooMany)
|
|
411
|
+
context.report({
|
|
412
|
+
node: reportNode,
|
|
413
|
+
messageId: 'cardinality',
|
|
414
|
+
data: {
|
|
415
|
+
component: key,
|
|
416
|
+
zone: zone.id,
|
|
417
|
+
cardinality: `${min}..${max}`,
|
|
418
|
+
count: tooFew ? shape.max : shape.min,
|
|
419
|
+
},
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
},
|
|
423
|
+
};
|
|
424
|
+
},
|
|
425
|
+
};
|