@kutur-kit/oxc 0.1.0-alpha.4 → 0.1.0-alpha.5
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 +45 -7
- package/dist/effect.d.ts.map +1 -1
- package/dist/effect.js +6 -0
- package/dist/effect.js.map +1 -1
- package/dist/foldkit-stylex/attributes.d.ts +2 -0
- package/dist/foldkit-stylex/attributes.d.ts.map +1 -0
- package/dist/foldkit-stylex/attributes.js +191 -0
- package/dist/foldkit-stylex/attributes.js.map +1 -0
- package/dist/foldkit-stylex/components.d.ts +12 -0
- package/dist/foldkit-stylex/components.d.ts.map +1 -0
- package/dist/foldkit-stylex/components.js +41 -0
- package/dist/foldkit-stylex/components.js.map +1 -0
- package/dist/foldkit-stylex/condition-key.d.ts +2 -0
- package/dist/foldkit-stylex/condition-key.d.ts.map +1 -0
- package/dist/foldkit-stylex/condition-key.js +255 -0
- package/dist/foldkit-stylex/condition-key.js.map +1 -0
- package/dist/foldkit-stylex/styles.d.ts +2 -0
- package/dist/foldkit-stylex/styles.d.ts.map +1 -0
- package/dist/foldkit-stylex/styles.js +131 -0
- package/dist/foldkit-stylex/styles.js.map +1 -0
- package/dist/foldkit-stylex/unstyled-view.d.ts +2 -0
- package/dist/foldkit-stylex/unstyled-view.d.ts.map +1 -0
- package/dist/foldkit-stylex/unstyled-view.js +89 -0
- package/dist/foldkit-stylex/unstyled-view.js.map +1 -0
- package/dist/foldkit-stylex/vite.d.ts +2 -0
- package/dist/foldkit-stylex/vite.d.ts.map +1 -0
- package/dist/foldkit-stylex/vite.js +26 -0
- package/dist/foldkit-stylex/vite.js.map +1 -0
- package/dist/foldkit-stylex.d.ts +21 -0
- package/dist/foldkit-stylex.d.ts.map +1 -0
- package/dist/foldkit-stylex.js +25 -0
- package/dist/foldkit-stylex.js.map +1 -0
- package/dist/foldkit-stylex.json +25 -0
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +12 -0
- package/dist/plugin.js.map +1 -1
- package/dist/rules/imports.d.ts +37 -0
- package/dist/rules/imports.d.ts.map +1 -0
- package/dist/rules/imports.js +72 -0
- package/dist/rules/imports.js.map +1 -0
- package/dist/rules/no-manual-effect-runtime-in-tests.d.ts +2 -0
- package/dist/rules/no-manual-effect-runtime-in-tests.d.ts.map +1 -0
- package/dist/rules/no-manual-effect-runtime-in-tests.js +74 -0
- package/dist/rules/no-manual-effect-runtime-in-tests.js.map +1 -0
- package/dist/rules/scope.d.ts +9 -0
- package/dist/rules/scope.d.ts.map +1 -0
- package/dist/rules/scope.js +15 -0
- package/dist/rules/scope.js.map +1 -0
- package/dist/vendor/anti-slop/rules/no-shape-in-symbol-names.d.ts.map +1 -1
- package/dist/vendor/anti-slop/rules/no-shape-in-symbol-names.js +17 -1
- package/dist/vendor/anti-slop/rules/no-shape-in-symbol-names.js.map +1 -1
- package/package.json +14 -4
- package/src/effect.ts +6 -0
- package/src/foldkit-stylex/attributes.ts +282 -0
- package/src/foldkit-stylex/components.ts +41 -0
- package/src/foldkit-stylex/condition-key.ts +321 -0
- package/src/foldkit-stylex/styles.ts +170 -0
- package/src/foldkit-stylex/unstyled-view.ts +116 -0
- package/src/foldkit-stylex/vite.ts +29 -0
- package/src/foldkit-stylex.ts +29 -0
- package/src/plugin.ts +12 -0
- package/src/rules/imports.ts +99 -0
- package/src/rules/no-manual-effect-runtime-in-tests.ts +99 -0
- package/src/rules/scope.ts +24 -0
- package/src/vendor/anti-slop/UPSTREAM.md +2 -0
- package/src/vendor/anti-slop/rules/no-shape-in-symbol-names.ts +22 -1
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import { defineRule } from "@oxlint/plugins";
|
|
4
|
+
|
|
5
|
+
import { exportOf, type Imports, importsOf, keyOf } from "../rules/imports.ts";
|
|
6
|
+
|
|
7
|
+
// Hooks @foldkit/ui derives from state that the kit exposes as flags, native states that
|
|
8
|
+
// @foldkit/ui's aria-disabled controls never enter, and ARIA attributes.
|
|
9
|
+
const stateHook =
|
|
10
|
+
/data-(checked|indeterminate|open|active|selected|disabled|readonly|invalid|focused|current|drag-over|dragging|today|outside-month|state|variant|closed|swipe)\b|:(disabled|enabled|checked|indeterminate|read-only|read-write|invalid|open)\b|\[aria-/;
|
|
11
|
+
|
|
12
|
+
// Every Toast entry shares one class, which cannot take state, so its animation and swipe key on
|
|
13
|
+
// these hooks.
|
|
14
|
+
const toastEntryHooks = /data-(closed|swipe)\b/g;
|
|
15
|
+
|
|
16
|
+
type Bound = Readonly<{ value: number; inclusive: boolean }>;
|
|
17
|
+
|
|
18
|
+
// The viewport widths a media query matches, in px.
|
|
19
|
+
type WidthRange = Readonly<{ min: Bound; max: Bound }>;
|
|
20
|
+
|
|
21
|
+
// A media query's widths and its other features, such as `(prefers-reduced-motion:reduce)`, without
|
|
22
|
+
// whitespace.
|
|
23
|
+
type MediaQuery = Readonly<{ range: WidthRange; features: ReadonlySet<string> }>;
|
|
24
|
+
|
|
25
|
+
const unbounded: WidthRange = {
|
|
26
|
+
min: { value: -Infinity, inclusive: true },
|
|
27
|
+
max: { value: Infinity, inclusive: true },
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const length = String.raw`(\d*\.?\d+)(px|rem|em)`;
|
|
31
|
+
|
|
32
|
+
const feature = new RegExp(String.raw`^\((min|max)-width:\s*${length}\)$`);
|
|
33
|
+
|
|
34
|
+
const comparison = new RegExp(String.raw`^\(width\s*(<=?|>=?)\s*${length}\)$`);
|
|
35
|
+
|
|
36
|
+
const narrow = (range: WidthRange, side: "min" | "max", bound: Bound): WidthRange => ({
|
|
37
|
+
...range,
|
|
38
|
+
[side]: bound,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// rem and em resolve against the browser's default font size in media queries.
|
|
42
|
+
const toPixels = (value: string, unit: string) => Number(value) * (unit === "px" ? 1 : 16);
|
|
43
|
+
|
|
44
|
+
// Parses `@media (min-width: 40rem) and (prefers-reduced-motion: reduce)`, with widths also in the
|
|
45
|
+
// `(width >= 640px)` form. Returns `undefined` for a query with a media type, `or`, `not` or a
|
|
46
|
+
// comma.
|
|
47
|
+
const mediaQueryOf = (key: string): MediaQuery | undefined => {
|
|
48
|
+
if (!key.startsWith("@media ")) return undefined;
|
|
49
|
+
|
|
50
|
+
let range = unbounded;
|
|
51
|
+
|
|
52
|
+
const features = new Set<string>();
|
|
53
|
+
|
|
54
|
+
for (const condition of key
|
|
55
|
+
.slice("@media ".length)
|
|
56
|
+
.trim()
|
|
57
|
+
.split(/\s+and\s+/)) {
|
|
58
|
+
const minMax = feature.exec(condition);
|
|
59
|
+
const compared = comparison.exec(condition);
|
|
60
|
+
|
|
61
|
+
if (minMax !== null) {
|
|
62
|
+
const [, side = "", value = "", unit = ""] = minMax;
|
|
63
|
+
|
|
64
|
+
range = narrow(range, side === "min" ? "min" : "max", {
|
|
65
|
+
value: toPixels(value, unit),
|
|
66
|
+
inclusive: true,
|
|
67
|
+
});
|
|
68
|
+
} else if (compared !== null) {
|
|
69
|
+
const [, operator = "", value = "", unit = ""] = compared;
|
|
70
|
+
|
|
71
|
+
range = narrow(range, operator.startsWith(">") ? "min" : "max", {
|
|
72
|
+
value: toPixels(value, unit),
|
|
73
|
+
inclusive: operator.endsWith("="),
|
|
74
|
+
});
|
|
75
|
+
} else if (/^\([^()]*\)$/.test(condition)) {
|
|
76
|
+
features.add(condition.replaceAll(/\s/g, ""));
|
|
77
|
+
} else {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return { range, features };
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const atOrBelow = (a: Bound, b: Bound) =>
|
|
86
|
+
a.value < b.value || (a.value === b.value && (b.inclusive || !a.inclusive));
|
|
87
|
+
|
|
88
|
+
const atOrAbove = (a: Bound, b: Bound) =>
|
|
89
|
+
a.value > b.value || (a.value === b.value && (b.inclusive || !a.inclusive));
|
|
90
|
+
|
|
91
|
+
// Whether `outer` matches wherever `inner` does: it needs no feature `inner` lacks, and its widths
|
|
92
|
+
// include all of `inner`'s.
|
|
93
|
+
const covers = (outer: MediaQuery, inner: MediaQuery) =>
|
|
94
|
+
[...outer.features].every((name) => inner.features.has(name)) &&
|
|
95
|
+
atOrAbove(inner.range.min, outer.range.min) &&
|
|
96
|
+
atOrBelow(inner.range.max, outer.range.max);
|
|
97
|
+
|
|
98
|
+
// PSEUDO_CLASS_PRIORITIES of @stylexjs/babel-plugin 0.19.1, less its entries of 40 and its focus
|
|
99
|
+
// entries. StyleX ranks a key by the name before its first '(', and an unlisted name, such as ':is',
|
|
100
|
+
// 40. It ranks a chain without '(', such as ':hover:active', by the sum of its parts; this rule
|
|
101
|
+
// reads a chain as unlisted, so it never reports one.
|
|
102
|
+
const pseudoClassRank = new Map([
|
|
103
|
+
[":has", 45],
|
|
104
|
+
[":dir", 50],
|
|
105
|
+
[":lang", 51],
|
|
106
|
+
[":first-child", 52],
|
|
107
|
+
[":first-of-type", 53],
|
|
108
|
+
[":last-child", 54],
|
|
109
|
+
[":last-of-type", 55],
|
|
110
|
+
[":only-child", 56],
|
|
111
|
+
[":only-of-type", 57],
|
|
112
|
+
[":nth-child", 60],
|
|
113
|
+
[":nth-last-child", 61],
|
|
114
|
+
[":nth-of-type", 62],
|
|
115
|
+
[":nth-last-of-type", 63],
|
|
116
|
+
[":empty", 70],
|
|
117
|
+
[":link", 80],
|
|
118
|
+
[":any-link", 81],
|
|
119
|
+
[":local-link", 82],
|
|
120
|
+
[":target-within", 83],
|
|
121
|
+
[":target", 84],
|
|
122
|
+
[":visited", 85],
|
|
123
|
+
[":enabled", 91],
|
|
124
|
+
[":disabled", 92],
|
|
125
|
+
[":required", 93],
|
|
126
|
+
[":optional", 94],
|
|
127
|
+
[":read-only", 95],
|
|
128
|
+
[":read-write", 96],
|
|
129
|
+
[":placeholder-shown", 97],
|
|
130
|
+
[":in-range", 98],
|
|
131
|
+
[":out-of-range", 99],
|
|
132
|
+
[":default", 100],
|
|
133
|
+
[":checked", 101],
|
|
134
|
+
[":indeterminate", 101],
|
|
135
|
+
[":blank", 102],
|
|
136
|
+
[":valid", 103],
|
|
137
|
+
[":invalid", 104],
|
|
138
|
+
[":user-invalid", 105],
|
|
139
|
+
[":autofill", 110],
|
|
140
|
+
[":picture-in-picture", 120],
|
|
141
|
+
[":modal", 121],
|
|
142
|
+
[":fullscreen", 122],
|
|
143
|
+
[":paused", 123],
|
|
144
|
+
[":playing", 124],
|
|
145
|
+
[":current", 125],
|
|
146
|
+
[":past", 126],
|
|
147
|
+
[":future", 127],
|
|
148
|
+
[":hover", 130],
|
|
149
|
+
[":focus", 150],
|
|
150
|
+
[":active", 170],
|
|
151
|
+
]);
|
|
152
|
+
|
|
153
|
+
// The ranks StyleX's table means focus keys to have. It lists ':focus-visible' and ':focus-within'
|
|
154
|
+
// as ':focusVisible' and ':focusWithin', so they rank 40, below every listed pseudo-class.
|
|
155
|
+
// ':has(:focus-visible)', which styles focus on an element whose input is hidden, ranks 45 as a
|
|
156
|
+
// ':has' key and takes the rank of ':focus-visible' here.
|
|
157
|
+
const intendedRank = new Map([
|
|
158
|
+
[":focus-visible", 160],
|
|
159
|
+
[":focus-within", 140],
|
|
160
|
+
[":has(:focus-visible)", 160],
|
|
161
|
+
]);
|
|
162
|
+
|
|
163
|
+
const rankOf = (key: string) => pseudoClassRank.get(key.replace(/\(.*/, "")) ?? 40;
|
|
164
|
+
|
|
165
|
+
type Candidate = Readonly<{ entry: string; key: string; node: ESTree.Node }>;
|
|
166
|
+
|
|
167
|
+
export const conditionKey = defineRule({
|
|
168
|
+
meta: {
|
|
169
|
+
type: "problem",
|
|
170
|
+
docs: {
|
|
171
|
+
description:
|
|
172
|
+
"Disallow StyleX condition keys that never match or that rank differently than written.",
|
|
173
|
+
},
|
|
174
|
+
hasSuggestions: true,
|
|
175
|
+
schema: [],
|
|
176
|
+
messages: {
|
|
177
|
+
stateAttribute:
|
|
178
|
+
"Style '{{key}}' from state: (state) => [styles.base, state.isChecked && styles.checked] in a part's styles, or sx(styles.base, isChecked && styles.checked) on your own element. @foldkit/ui's hooks differ across components (Checkbox, Switch, Button and Listbox use aria-disabled, so :disabled never matches them), and StyleX ranks ':is()' keys of one property equally.",
|
|
179
|
+
bareAttribute:
|
|
180
|
+
"Write ':is({{key}})' instead of '{{key}}'. StyleX adds 3000 to a bare attribute condition's priority, above :hover (+130) and media queries (+200), and types its value as unknown, which typed style props reject. The :is() form ranks with pseudo-classes, so check which condition wins where they overlap.",
|
|
181
|
+
mediaOrder:
|
|
182
|
+
"'{{earlier}}' never matches: StyleX rewrites each media query to exclude the ones after it in the same property, and '{{later}}' matches wherever it does. Put the query that covers the other first: min-width queries from narrow to wide, max-width queries from wide to narrow.",
|
|
183
|
+
focusRank:
|
|
184
|
+
"StyleX 0.19 ranks '{{key}}' below '{{above}}', so '{{above}}' wins wherever both match and hides the focus style. Style focus on a property that '{{above}}' does not set, or write '{{above}}:not({{key}})' so the two never match together.",
|
|
185
|
+
wrapInIs: "Wrap the key in :is().",
|
|
186
|
+
},
|
|
187
|
+
},
|
|
188
|
+
createOnce(context) {
|
|
189
|
+
let imports: Imports = new Map();
|
|
190
|
+
// State hook keys, reported at the end of the file once every Toast entry is known.
|
|
191
|
+
let candidates: Array<Candidate> = [];
|
|
192
|
+
// Style keys passed as a Toast `entry`, such as `entry` in `entry: styles.entry`.
|
|
193
|
+
let toastEntries = new Set<string>();
|
|
194
|
+
|
|
195
|
+
const checkMediaOrder = (properties: ReadonlyArray<ESTree.ObjectProperty>) => {
|
|
196
|
+
const queries = properties.flatMap((property) => {
|
|
197
|
+
const key = keyOf(property);
|
|
198
|
+
const query = key === undefined ? undefined : mediaQueryOf(key);
|
|
199
|
+
|
|
200
|
+
return key === undefined || query === undefined ? [] : [{ key, query, property }];
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
for (const [index, earlier] of queries.entries()) {
|
|
204
|
+
const later = queries.slice(index + 1).find(({ query }) => covers(query, earlier.query));
|
|
205
|
+
|
|
206
|
+
if (later !== undefined) {
|
|
207
|
+
context.report({
|
|
208
|
+
node: earlier.property.key,
|
|
209
|
+
messageId: "mediaOrder",
|
|
210
|
+
data: { earlier: earlier.key, later: later.key },
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
// A focus key next to a key that StyleX ranks above it but its table puts below it. Keys of
|
|
217
|
+
// one rank, such as ':focus-visible' and ':is()', tie and are not reported.
|
|
218
|
+
const checkFocusRank = (properties: ReadonlyArray<ESTree.ObjectProperty>) => {
|
|
219
|
+
const keys = properties.flatMap((property) => keyOf(property) ?? []);
|
|
220
|
+
|
|
221
|
+
for (const property of properties) {
|
|
222
|
+
const key = keyOf(property) ?? "";
|
|
223
|
+
const intended = intendedRank.get(key);
|
|
224
|
+
|
|
225
|
+
const above =
|
|
226
|
+
intended === undefined
|
|
227
|
+
? undefined
|
|
228
|
+
: keys.find((other) => rankOf(other) > rankOf(key) && rankOf(other) < intended);
|
|
229
|
+
|
|
230
|
+
if (above !== undefined) {
|
|
231
|
+
context.report({ node: property.key, messageId: "focusRank", data: { key, above } });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const checkKey = (entry: string, key: string, property: ESTree.ObjectProperty) => {
|
|
237
|
+
if (stateHook.test(key)) {
|
|
238
|
+
candidates.push({ entry, key, node: property.key });
|
|
239
|
+
} else if (key.startsWith("[")) {
|
|
240
|
+
context.report({
|
|
241
|
+
node: property.key,
|
|
242
|
+
messageId: "bareAttribute",
|
|
243
|
+
data: { key },
|
|
244
|
+
suggest: [
|
|
245
|
+
{
|
|
246
|
+
messageId: "wrapInIs",
|
|
247
|
+
fix: (fixer) => fixer.replaceText(property.key, JSON.stringify(`:is(${key})`)),
|
|
248
|
+
},
|
|
249
|
+
],
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const checkStyle = (entry: string, style: ESTree.Node) => {
|
|
255
|
+
// A dynamic style's conditions are in the object its function returns.
|
|
256
|
+
const object = style.type === "ArrowFunctionExpression" ? style.body : style;
|
|
257
|
+
|
|
258
|
+
if (object.type !== "ObjectExpression") return;
|
|
259
|
+
|
|
260
|
+
const properties = object.properties.filter((property) => property.type === "Property");
|
|
261
|
+
|
|
262
|
+
checkMediaOrder(properties);
|
|
263
|
+
checkFocusRank(properties);
|
|
264
|
+
|
|
265
|
+
for (const property of properties) {
|
|
266
|
+
const key = keyOf(property);
|
|
267
|
+
|
|
268
|
+
if (key !== undefined) checkKey(entry, key, property);
|
|
269
|
+
|
|
270
|
+
checkStyle(entry, property.value);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
return {
|
|
275
|
+
Program(program) {
|
|
276
|
+
imports = importsOf(program);
|
|
277
|
+
candidates = [];
|
|
278
|
+
toastEntries = new Set();
|
|
279
|
+
},
|
|
280
|
+
CallExpression(node) {
|
|
281
|
+
const [styles] = node.arguments;
|
|
282
|
+
|
|
283
|
+
if (
|
|
284
|
+
exportOf(node.callee, imports, "@stylexjs/stylex") !== "create" ||
|
|
285
|
+
styles?.type !== "ObjectExpression"
|
|
286
|
+
) {
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
for (const entry of styles.properties) {
|
|
291
|
+
const name = entry.type === "Property" ? keyOf(entry) : undefined;
|
|
292
|
+
|
|
293
|
+
if (entry.type === "Property" && name !== undefined) checkStyle(name, entry.value);
|
|
294
|
+
}
|
|
295
|
+
},
|
|
296
|
+
MemberExpression(node) {
|
|
297
|
+
if (node.computed || node.property.type !== "Identifier") return;
|
|
298
|
+
|
|
299
|
+
for (
|
|
300
|
+
let parent: ESTree.Node | null = node.parent;
|
|
301
|
+
parent !== null;
|
|
302
|
+
parent = parent.parent
|
|
303
|
+
) {
|
|
304
|
+
if (parent.type === "Property" && keyOf(parent) === "entry") {
|
|
305
|
+
toastEntries.add(node.property.name);
|
|
306
|
+
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
"Program:exit"() {
|
|
312
|
+
for (const { entry, key, node } of candidates) {
|
|
313
|
+
const entryOnly =
|
|
314
|
+
toastEntries.has(entry) && !stateHook.test(key.replaceAll(toastEntryHooks, ""));
|
|
315
|
+
|
|
316
|
+
if (!entryOnly) context.report({ node, messageId: "stateAttribute", data: { key } });
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
};
|
|
320
|
+
},
|
|
321
|
+
});
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import { defineRule } from "@oxlint/plugins";
|
|
4
|
+
|
|
5
|
+
import { exportOf, type Imports, importsOf, isMethodCall, keyOf } from "../rules/imports.ts";
|
|
6
|
+
|
|
7
|
+
const stylex = "@stylexjs/stylex";
|
|
8
|
+
|
|
9
|
+
const tokens = "@kutur-kit/foldkit-stylex-theme/tokens.stylex";
|
|
10
|
+
|
|
11
|
+
// The prefix of the custom properties the theme's tokens read at :root.
|
|
12
|
+
const tokenProperty = "--fks-";
|
|
13
|
+
|
|
14
|
+
const isThemeOverrides = (type: ESTree.TSType, imports: Imports) =>
|
|
15
|
+
type.type === "TSTypeReference" && exportOf(type.typeName, imports, tokens) === "ThemeOverrides";
|
|
16
|
+
|
|
17
|
+
// A styles type, such as `PopoverStyle.Styles`.
|
|
18
|
+
const isStyles = (type: ESTree.TSType, imports: Imports): type is ESTree.TSTypeReference =>
|
|
19
|
+
type.type === "TSTypeReference" &&
|
|
20
|
+
/^\w+Style\.Styles$/.test(exportOf(type.typeName, imports, "@kutur-kit/foldkit-stylex") ?? "");
|
|
21
|
+
|
|
22
|
+
// The element type of an array type: `X[]`, `readonly X[]`, `Array<X>` or `ReadonlyArray<X>`.
|
|
23
|
+
const elementOf = (type: ESTree.TSType): ESTree.TSType | undefined => {
|
|
24
|
+
if (type.type === "TSArrayType") return type.elementType;
|
|
25
|
+
|
|
26
|
+
if (type.type === "TSTypeOperator") {
|
|
27
|
+
return type.operator === "readonly" ? elementOf(type.typeAnnotation) : undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return type.type === "TSTypeReference" &&
|
|
31
|
+
type.typeName.type === "Identifier" &&
|
|
32
|
+
(type.typeName.name === "Array" || type.typeName.name === "ReadonlyArray")
|
|
33
|
+
? type.typeArguments?.params[0]
|
|
34
|
+
: undefined;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// The expression under the `as`, `satisfies` and `<T>` assertions around it.
|
|
38
|
+
const asserted = (node: ESTree.Expression): ESTree.Expression =>
|
|
39
|
+
node.type === "TSAsExpression" ||
|
|
40
|
+
node.type === "TSSatisfiesExpression" ||
|
|
41
|
+
node.type === "TSTypeAssertion"
|
|
42
|
+
? asserted(node.expression)
|
|
43
|
+
: node;
|
|
44
|
+
|
|
45
|
+
const setsThemeTokens = (object: ESTree.Node) =>
|
|
46
|
+
object.type === "ObjectExpression" &&
|
|
47
|
+
object.properties.some(
|
|
48
|
+
(property) => property.type === "Property" && keyOf(property)?.startsWith(tokenProperty),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
export const styles = defineRule({
|
|
52
|
+
meta: {
|
|
53
|
+
type: "problem",
|
|
54
|
+
docs: {
|
|
55
|
+
description:
|
|
56
|
+
"Require theme overrides that a type checks and that reach the tokens, and styles objects that keep their narrow type.",
|
|
57
|
+
},
|
|
58
|
+
schema: [],
|
|
59
|
+
messages: {
|
|
60
|
+
createTheme:
|
|
61
|
+
"createTheme re-declares every token of its group on the element, so tokens you leave out reset to their defaults, and it never reaches the panels @foldkit/ui portals to <body>. Set single tokens as keys of a style, stylex.create({ x: { [color.accent]: '…' } }), in a part's styles or with sx(...); set app-wide values in a .stylex.ts file with defineVars and --fks-* keys.",
|
|
62
|
+
overrides:
|
|
63
|
+
"Write defineVars({ ... } satisfies ThemeOverrides), with ThemeOverrides from @kutur-kit/foldkit-stylex-theme/tokens.stylex. A misspelt --fks-* name otherwise compiles to a variable no token reads; `as ThemeOverrides` does not check names.",
|
|
64
|
+
elementToken:
|
|
65
|
+
"'{{key}}' changes nothing on an element: the tokens read it once, at :root. Set the token itself as a key of a static style, { [color.accent]: '…' } with color from @kutur-kit/foldkit-stylex-theme/tokens.stylex; for a runtime value, write { [color.accent]: 'var(--accent)' } and set --accent with h.Style. Set app-wide values in a .stylex.ts file with defineVars({ ... } satisfies ThemeOverrides).",
|
|
66
|
+
annotation:
|
|
67
|
+
"Check these styles with satisfies {{type}} instead. An annotation or `as` widens each part to any style, so XStyle.view can no longer see which properties it sets, and rejects it.",
|
|
68
|
+
arrayAnnotation:
|
|
69
|
+
"Check these styles with satisfies {{type}} instead, and pass them to the view where they are written. An array annotated with a styles type hides the properties each part sets, so XStyle.view skips every check on the styles spread from it.",
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
createOnce(context) {
|
|
73
|
+
let imports: Imports = new Map();
|
|
74
|
+
|
|
75
|
+
// An annotation or assertion with an array of a styles type, such as `PopoverStyle.Styles[]`.
|
|
76
|
+
const checkArray = (type: ESTree.TSType) => {
|
|
77
|
+
const element = elementOf(type);
|
|
78
|
+
|
|
79
|
+
if (element !== undefined && isStyles(element, imports)) {
|
|
80
|
+
context.report({
|
|
81
|
+
node: type,
|
|
82
|
+
messageId: "arrayAnnotation",
|
|
83
|
+
data: { type: context.sourceCode.getText(type) },
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// An annotation or assertion with a styles type, such as `PopoverStyle.Styles`, or an array
|
|
89
|
+
// of one.
|
|
90
|
+
const checkType = (type: ESTree.TSType | undefined) => {
|
|
91
|
+
if (type === undefined) return;
|
|
92
|
+
|
|
93
|
+
if (isStyles(type, imports)) {
|
|
94
|
+
context.report({
|
|
95
|
+
node: type,
|
|
96
|
+
messageId: "annotation",
|
|
97
|
+
data: { type: context.sourceCode.getText(type.typeName) },
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
checkArray(type);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const checkAssertion = ({ typeAnnotation }: ESTree.TSAsExpression | ESTree.TSTypeAssertion) =>
|
|
105
|
+
checkType(typeAnnotation);
|
|
106
|
+
|
|
107
|
+
// A parameter typed with a styles type fails XStyle.view's own check, but one typed with an
|
|
108
|
+
// array of them, such as `...extras: PopoverStyle.Styles[]`, passes it.
|
|
109
|
+
const checkParameters = ({ params }: ESTree.Function | ESTree.ArrowFunctionExpression) => {
|
|
110
|
+
for (const param of params) {
|
|
111
|
+
const type =
|
|
112
|
+
param.type === "Identifier" || param.type === "RestElement"
|
|
113
|
+
? param.typeAnnotation?.typeAnnotation
|
|
114
|
+
: undefined;
|
|
115
|
+
|
|
116
|
+
if (type !== undefined) checkArray(type);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
Program(program) {
|
|
122
|
+
imports = importsOf(program);
|
|
123
|
+
},
|
|
124
|
+
CallExpression(node) {
|
|
125
|
+
const name = exportOf(node.callee, imports, stylex);
|
|
126
|
+
|
|
127
|
+
if (name === "createTheme") context.report({ node, messageId: "createTheme" });
|
|
128
|
+
|
|
129
|
+
const [vars] = node.arguments;
|
|
130
|
+
|
|
131
|
+
if (name !== "defineVars" || vars === undefined || vars.type === "SpreadElement") return;
|
|
132
|
+
|
|
133
|
+
const object = asserted(vars);
|
|
134
|
+
|
|
135
|
+
const checked =
|
|
136
|
+
object.parent.type === "TSSatisfiesExpression" &&
|
|
137
|
+
isThemeOverrides(object.parent.typeAnnotation, imports);
|
|
138
|
+
|
|
139
|
+
if (!checked && setsThemeTokens(object)) {
|
|
140
|
+
context.report({ node: vars, messageId: "overrides" });
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
Property(node) {
|
|
144
|
+
const key = keyOf(node);
|
|
145
|
+
|
|
146
|
+
if (!key?.startsWith(tokenProperty)) return;
|
|
147
|
+
|
|
148
|
+
// The call whose argument holds the key, through nested styles, functions and conditions.
|
|
149
|
+
let call: ESTree.Node | null = node.parent;
|
|
150
|
+
|
|
151
|
+
while (call !== null && call.type !== "CallExpression") call = call.parent;
|
|
152
|
+
|
|
153
|
+
if (
|
|
154
|
+
call !== null &&
|
|
155
|
+
(exportOf(call.callee, imports, stylex) === "create" || isMethodCall(call, "Style"))
|
|
156
|
+
) {
|
|
157
|
+
context.report({ node: node.key, messageId: "elementToken", data: { key } });
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
VariableDeclarator({ id }) {
|
|
161
|
+
if (id.type === "Identifier") checkType(id.typeAnnotation?.typeAnnotation);
|
|
162
|
+
},
|
|
163
|
+
TSAsExpression: checkAssertion,
|
|
164
|
+
TSTypeAssertion: checkAssertion,
|
|
165
|
+
FunctionDeclaration: checkParameters,
|
|
166
|
+
FunctionExpression: checkParameters,
|
|
167
|
+
ArrowFunctionExpression: checkParameters,
|
|
168
|
+
};
|
|
169
|
+
},
|
|
170
|
+
});
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import { defineRule } from "@oxlint/plugins";
|
|
4
|
+
|
|
5
|
+
import { type Imports, importsOf, resolve } from "../rules/imports.ts";
|
|
6
|
+
import { components, viewOf } from "./components.ts";
|
|
7
|
+
|
|
8
|
+
const ui = "@foldkit/ui";
|
|
9
|
+
|
|
10
|
+
// `@foldkit/ui/datePicker` exports what `@foldkit/ui` exports as `DatePicker`.
|
|
11
|
+
const uiPathOf = (node: ESTree.Node, imports: Imports) => {
|
|
12
|
+
const resolved = resolve(node, imports);
|
|
13
|
+
|
|
14
|
+
if (resolved?.source === ui) return resolved.path;
|
|
15
|
+
|
|
16
|
+
if (!resolved?.source.startsWith(`${ui}/`)) return undefined;
|
|
17
|
+
|
|
18
|
+
const subpath = resolved.source.slice(ui.length + 1);
|
|
19
|
+
const name = subpath.charAt(0).toUpperCase() + subpath.slice(1);
|
|
20
|
+
|
|
21
|
+
return resolved.path === "" ? name : `${name}.${resolved.path}`;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Module-scope `const X = Listbox.create()`, `Listbox.Multi.create()` or `Toast.make(...)`: the
|
|
25
|
+
// local name and the component path its view belongs to, such as `Listbox.Multi`.
|
|
26
|
+
const createdIn = (program: ESTree.Program, imports: Imports) => {
|
|
27
|
+
const created = new Map<string, string>();
|
|
28
|
+
|
|
29
|
+
for (const statement of program.body) {
|
|
30
|
+
const declaration =
|
|
31
|
+
statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
32
|
+
|
|
33
|
+
if (declaration?.type !== "VariableDeclaration") continue;
|
|
34
|
+
|
|
35
|
+
for (const { id, init } of declaration.declarations) {
|
|
36
|
+
const path = init?.type === "CallExpression" ? uiPathOf(init.callee, imports) : undefined;
|
|
37
|
+
const component = path?.replace(/\.(create|make)$/, "");
|
|
38
|
+
|
|
39
|
+
if (id.type === "Identifier" && component !== undefined && component !== path) {
|
|
40
|
+
created.set(id.name, component);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return created;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const unstyledView = defineRule({
|
|
49
|
+
meta: {
|
|
50
|
+
type: "problem",
|
|
51
|
+
docs: {
|
|
52
|
+
description:
|
|
53
|
+
"Require @foldkit/ui views to be wrapped with their @kutur-kit/foldkit-stylex namespace.",
|
|
54
|
+
},
|
|
55
|
+
schema: [],
|
|
56
|
+
messages: {
|
|
57
|
+
wrap: "Wrap {{view}} once at module scope with {{wrapper}}({{view}}, styles) from @kutur-kit/foldkit-stylex and use the result here. It styles every part and adds the CSS @foldkit/ui leaves to the app, such as a backdrop that covers the page and the hiding of a closed dialog.",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
createOnce(context) {
|
|
61
|
+
let imports: Imports = new Map();
|
|
62
|
+
let created = new Map<string, string>();
|
|
63
|
+
|
|
64
|
+
// The component path whose view `node` reads, such as `Listbox.Multi`.
|
|
65
|
+
const componentOf = (node: ESTree.MemberExpression) => {
|
|
66
|
+
if (node.computed || node.property.type !== "Identifier") return undefined;
|
|
67
|
+
|
|
68
|
+
const component =
|
|
69
|
+
node.object.type === "Identifier" && created.has(node.object.name)
|
|
70
|
+
? created.get(node.object.name)
|
|
71
|
+
: uiPathOf(node.object, imports);
|
|
72
|
+
|
|
73
|
+
const name = component?.split(".")[0];
|
|
74
|
+
|
|
75
|
+
return name !== undefined && components.has(name) && node.property.name === viewOf(name)
|
|
76
|
+
? component
|
|
77
|
+
: undefined;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// The view, or a view factory called in place such as `VirtualList.view<Item>()`, is the
|
|
81
|
+
// first argument of a @kutur-kit/foldkit-stylex call.
|
|
82
|
+
const isWrapped = (node: ESTree.MemberExpression) => {
|
|
83
|
+
const view =
|
|
84
|
+
node.parent.type === "CallExpression" && node.parent.callee === node ? node.parent : node;
|
|
85
|
+
|
|
86
|
+
const { parent } = view;
|
|
87
|
+
|
|
88
|
+
return (
|
|
89
|
+
parent.type === "CallExpression" &&
|
|
90
|
+
parent.arguments[0] === view &&
|
|
91
|
+
resolve(parent.callee, imports)?.source === "@kutur-kit/foldkit-stylex"
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
Program(program) {
|
|
97
|
+
imports = importsOf(program);
|
|
98
|
+
created = createdIn(program, imports);
|
|
99
|
+
},
|
|
100
|
+
MemberExpression(node) {
|
|
101
|
+
const component = componentOf(node);
|
|
102
|
+
|
|
103
|
+
if (component === undefined || isWrapped(node)) return;
|
|
104
|
+
|
|
105
|
+
const [name = "", ...rest] = component.split(".");
|
|
106
|
+
const view = context.sourceCode.getText(node);
|
|
107
|
+
|
|
108
|
+
context.report({
|
|
109
|
+
node,
|
|
110
|
+
messageId: "wrap",
|
|
111
|
+
data: { view, wrapper: [`${name}Style`, ...rest, viewOf(name)].join(".") },
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
const unplugin = "@stylexjs/unplugin";
|
|
4
|
+
|
|
5
|
+
export const vite = defineRule({
|
|
6
|
+
meta: {
|
|
7
|
+
type: "problem",
|
|
8
|
+
docs: { description: "Require foldkitStylex() instead of StyleX's own bundler plugin." },
|
|
9
|
+
schema: [],
|
|
10
|
+
messages: {
|
|
11
|
+
helper:
|
|
12
|
+
"Use foldkitStylex() from @kutur-kit/foldkit-stylex/vite. It compiles this package in dev, SSR and Vitest, where StyleX's own plugin leaves it uncompiled, keeps light-dark() colors valid and fails a build whose client links no CSS.",
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
createOnce(context) {
|
|
16
|
+
return {
|
|
17
|
+
ImportDeclaration(node) {
|
|
18
|
+
const source = node.source.value;
|
|
19
|
+
|
|
20
|
+
// A type import, such as the plugin's options, loads no plugin.
|
|
21
|
+
if (node.importKind === "type") return;
|
|
22
|
+
|
|
23
|
+
if (source === unplugin || source.startsWith(`${unplugin}/`)) {
|
|
24
|
+
context.report({ node, messageId: "helper" });
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { OxlintConfig } from "oxlint";
|
|
2
|
+
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
const extension = import.meta.url.endsWith(".ts") ? "ts" : "js";
|
|
6
|
+
|
|
7
|
+
// `satisfies` keeps each plugin entry an object, which the build's JSON form relies on.
|
|
8
|
+
const foldkitStylex = {
|
|
9
|
+
jsPlugins: [
|
|
10
|
+
// `/base` registers the same plugin with the same specifier, so Oxlint loads it once.
|
|
11
|
+
{ name: "kit", specifier: fileURLToPath(new URL(`./plugin.${extension}`, import.meta.url)) },
|
|
12
|
+
{ name: "stylex", specifier: fileURLToPath(import.meta.resolve("@stylexjs/eslint-plugin")) },
|
|
13
|
+
],
|
|
14
|
+
rules: {
|
|
15
|
+
"kit/foldkit-stylex-attributes": "error",
|
|
16
|
+
"kit/foldkit-stylex-condition-key": "error",
|
|
17
|
+
"kit/foldkit-stylex-styles": "error",
|
|
18
|
+
"kit/foldkit-stylex-unstyled-view": "error",
|
|
19
|
+
"kit/foldkit-stylex-vite": "error",
|
|
20
|
+
"stylex/enforce-extension": "error",
|
|
21
|
+
"stylex/no-legacy-contextual-styles": "error",
|
|
22
|
+
"stylex/no-nonstandard-styles": "error",
|
|
23
|
+
"stylex/no-unused": "error",
|
|
24
|
+
"stylex/valid-shorthands": "error",
|
|
25
|
+
"stylex/valid-styles": "error",
|
|
26
|
+
},
|
|
27
|
+
} satisfies OxlintConfig;
|
|
28
|
+
|
|
29
|
+
export default foldkitStylex;
|