@kutur-kit/oxc 0.1.0-alpha.4 → 0.1.0-alpha.6

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 (76) hide show
  1. package/README.md +59 -7
  2. package/dist/effect.d.ts.map +1 -1
  3. package/dist/effect.js +6 -0
  4. package/dist/effect.js.map +1 -1
  5. package/dist/foldkit-stylex/attributes.d.ts +2 -0
  6. package/dist/foldkit-stylex/attributes.d.ts.map +1 -0
  7. package/dist/foldkit-stylex/attributes.js +191 -0
  8. package/dist/foldkit-stylex/attributes.js.map +1 -0
  9. package/dist/foldkit-stylex/components.d.ts +12 -0
  10. package/dist/foldkit-stylex/components.d.ts.map +1 -0
  11. package/dist/foldkit-stylex/components.js +41 -0
  12. package/dist/foldkit-stylex/components.js.map +1 -0
  13. package/dist/foldkit-stylex/condition-key.d.ts +16 -0
  14. package/dist/foldkit-stylex/condition-key.d.ts.map +1 -0
  15. package/dist/foldkit-stylex/condition-key.js +255 -0
  16. package/dist/foldkit-stylex/condition-key.js.map +1 -0
  17. package/dist/foldkit-stylex/hover-gate.d.ts +2 -0
  18. package/dist/foldkit-stylex/hover-gate.d.ts.map +1 -0
  19. package/dist/foldkit-stylex/hover-gate.js +78 -0
  20. package/dist/foldkit-stylex/hover-gate.js.map +1 -0
  21. package/dist/foldkit-stylex/styles.d.ts +2 -0
  22. package/dist/foldkit-stylex/styles.d.ts.map +1 -0
  23. package/dist/foldkit-stylex/styles.js +131 -0
  24. package/dist/foldkit-stylex/styles.js.map +1 -0
  25. package/dist/foldkit-stylex/unstyled-view.d.ts +2 -0
  26. package/dist/foldkit-stylex/unstyled-view.d.ts.map +1 -0
  27. package/dist/foldkit-stylex/unstyled-view.js +94 -0
  28. package/dist/foldkit-stylex/unstyled-view.js.map +1 -0
  29. package/dist/foldkit-stylex/vite.d.ts +2 -0
  30. package/dist/foldkit-stylex/vite.d.ts.map +1 -0
  31. package/dist/foldkit-stylex/vite.js +26 -0
  32. package/dist/foldkit-stylex/vite.js.map +1 -0
  33. package/dist/foldkit-stylex.d.ts +22 -0
  34. package/dist/foldkit-stylex.d.ts.map +1 -0
  35. package/dist/foldkit-stylex.js +26 -0
  36. package/dist/foldkit-stylex.js.map +1 -0
  37. package/dist/foldkit-stylex.json +26 -0
  38. package/dist/plugin.d.ts.map +1 -1
  39. package/dist/plugin.js +16 -0
  40. package/dist/plugin.js.map +1 -1
  41. package/dist/rules/imports.d.ts +37 -0
  42. package/dist/rules/imports.d.ts.map +1 -0
  43. package/dist/rules/imports.js +72 -0
  44. package/dist/rules/imports.js.map +1 -0
  45. package/dist/rules/no-manual-effect-runtime-in-tests.d.ts +2 -0
  46. package/dist/rules/no-manual-effect-runtime-in-tests.d.ts.map +1 -0
  47. package/dist/rules/no-manual-effect-runtime-in-tests.js +74 -0
  48. package/dist/rules/no-manual-effect-runtime-in-tests.js.map +1 -0
  49. package/dist/rules/scope.d.ts +9 -0
  50. package/dist/rules/scope.d.ts.map +1 -0
  51. package/dist/rules/scope.js +15 -0
  52. package/dist/rules/scope.js.map +1 -0
  53. package/dist/rules/self-contained-function.d.ts +2 -0
  54. package/dist/rules/self-contained-function.d.ts.map +1 -0
  55. package/dist/rules/self-contained-function.js +56 -0
  56. package/dist/rules/self-contained-function.js.map +1 -0
  57. package/dist/vendor/anti-slop/rules/no-shape-in-symbol-names.d.ts.map +1 -1
  58. package/dist/vendor/anti-slop/rules/no-shape-in-symbol-names.js +17 -1
  59. package/dist/vendor/anti-slop/rules/no-shape-in-symbol-names.js.map +1 -1
  60. package/package.json +14 -4
  61. package/src/effect.ts +6 -0
  62. package/src/foldkit-stylex/attributes.ts +282 -0
  63. package/src/foldkit-stylex/components.ts +41 -0
  64. package/src/foldkit-stylex/condition-key.ts +321 -0
  65. package/src/foldkit-stylex/hover-gate.ts +96 -0
  66. package/src/foldkit-stylex/styles.ts +170 -0
  67. package/src/foldkit-stylex/unstyled-view.ts +121 -0
  68. package/src/foldkit-stylex/vite.ts +29 -0
  69. package/src/foldkit-stylex.ts +30 -0
  70. package/src/plugin.ts +16 -0
  71. package/src/rules/imports.ts +99 -0
  72. package/src/rules/no-manual-effect-runtime-in-tests.ts +99 -0
  73. package/src/rules/scope.ts +24 -0
  74. package/src/rules/self-contained-function.ts +76 -0
  75. package/src/vendor/anti-slop/UPSTREAM.md +2 -0
  76. package/src/vendor/anti-slop/rules/no-shape-in-symbol-names.ts +22 -1
@@ -0,0 +1,282 @@
1
+ import type { ESTree } from "@oxlint/plugins";
2
+
3
+ import { defineRule } from "@oxlint/plugins";
4
+
5
+ import {
6
+ effectMemberOf,
7
+ exportOf,
8
+ type Imports,
9
+ importsOf,
10
+ isMethodCall,
11
+ keyOf,
12
+ resolve,
13
+ rootOf,
14
+ } from "../rules/imports.ts";
15
+ import { variableOf } from "../rules/scope.ts";
16
+
17
+ const kit = "@kutur-kit/foldkit-stylex";
18
+
19
+ // Callbacks through which @foldkit/ui hands the app attribute bundles for its parts.
20
+ const bundleCallbacks = new Set(["toView", "entryToView", "toCalendarView"]);
21
+
22
+ // View inputs whose attributes @foldkit/ui applies to a part after the part's class, such as
23
+ // `buttonAttributes`.
24
+ const attributesInput = /^(attributes|\w+Attributes)$/;
25
+
26
+ // Array methods and Effect Array functions that call a callback with each item of an array.
27
+ const iterators = new Set(["map", "flatMap"]);
28
+
29
+ // Array methods and Effect Array functions that return some items of an array.
30
+ const filters = new Set(["filter"]);
31
+
32
+ // The value of a `toView`-like property, as in `toView: (attributes) => ...` or `toView: view`.
33
+ const isBundleCallback = (node: ESTree.Node) =>
34
+ node.parent?.type === "Property" &&
35
+ node.parent.value === node &&
36
+ bundleCallbacks.has(keyOf(node.parent) ?? "");
37
+
38
+ const isSx = (node: ESTree.Node, imports: Imports) =>
39
+ node.type === "CallExpression" && exportOf(node.callee, imports, kit) === "sx";
40
+
41
+ // Whether `node` writes an element's class or its style, of which Foldkit keeps one each, and
42
+ // whether it writes it raw, as `h.Attribute("class", ...)`.
43
+ const writtenBy = (
44
+ node: ESTree.Node,
45
+ imports: Imports,
46
+ ): Readonly<{ attribute: "class" | "style"; raw: boolean }> | undefined => {
47
+ if (isMethodCall(node, "Attribute")) {
48
+ const [name] = node.arguments;
49
+
50
+ return name?.type === "Literal" && (name.value === "class" || name.value === "style")
51
+ ? { attribute: name.value, raw: true }
52
+ : undefined;
53
+ }
54
+
55
+ if (isSx(node, imports) || isMethodCall(node, "Class")) return { attribute: "class", raw: false };
56
+
57
+ return isMethodCall(node, "Style") ? { attribute: "style", raw: false } : undefined;
58
+ };
59
+
60
+ export const attributes = defineRule({
61
+ meta: {
62
+ type: "problem",
63
+ docs: {
64
+ description:
65
+ "Keep one Class per element: styles in one sx(...) call, after every spread, and never on a @foldkit/ui part.",
66
+ },
67
+ schema: [],
68
+ messages: {
69
+ partClass:
70
+ "This element is a @foldkit/ui part, whose Class its XStyle wrapper sets. Put these styles in that part's entry of its styles: Foldkit keeps one Class per element, so this replaces the part's styles and the functional ones.",
71
+ partStyle:
72
+ "This element is a @foldkit/ui part, whose inline style @foldkit/ui owns, and Foldkit keeps one Style per element, so this replaces it. Put static styles in that part's entry of its styles. For a runtime value, set a custom property with h.Style on your own element around the part and read it with var() in the part's styles; a portaled panel inherits from none of your elements, so set it on an element you render inside the panel and style that element.",
73
+ oneCall:
74
+ "Pass every style of this element to one sx(...) call, conditions included. Foldkit keeps one Class per element, and StyleX resolves conflicting properties only within one call.",
75
+ oneClass:
76
+ "Join these class names into one Class. Foldkit keeps one Class per element, so this one replaces the ones before it.",
77
+ last: "Put sx(...) last in the attribute list. Foldkit keeps the last Class, so a bundle spread after it replaces these styles.",
78
+ rawClass:
79
+ "Use sx(...) for StyleX styles or h.Class(...) for class names. Scene's toHaveClass cannot see a raw class attribute.",
80
+ rawStyle:
81
+ "Use h.Style(...). A raw style attribute throws when the element also gets a Style, as @foldkit/ui panels do.",
82
+ stylexRuntime:
83
+ "Use sx(...). stylex.props returns numbers that h.Style rejects and omits className when no style applies; stylex.attrs kebab-cases custom properties and returns a raw attribute Scene cannot see.",
84
+ notInElement:
85
+ "Write sx(...) directly in the attribute list of the element it styles, so this rule sees every Class that element gets. Put conditions inside the call: sx(isOpen ? styles.open : styles.closed).",
86
+ },
87
+ },
88
+ createOnce(context) {
89
+ let imports: Imports = new Map();
90
+
91
+ // A binding made by `DragAndDropStyle.draggable(...)`, which returns the draggable's attributes
92
+ // with its Class.
93
+ const isStyledDraggable = (callee: ESTree.Expression) =>
94
+ callee.type === "Identifier" &&
95
+ (variableOf(context.sourceCode, callee)?.defs.some(
96
+ ({ node }) =>
97
+ node.type === "VariableDeclarator" &&
98
+ node.init?.type === "CallExpression" &&
99
+ exportOf(node.init.callee, imports, kit) === "DragAndDropStyle.draggable",
100
+ ) ??
101
+ false);
102
+
103
+ // The Effect Array function `callee` names, such as `map` for `Array.map`.
104
+ const arrayFunctionOf = (callee: ESTree.Node) =>
105
+ effectMemberOf(resolve(callee, imports), "Array");
106
+
107
+ // Effect's `pipe`, from effect or effect/Function.
108
+ const isPipe = (callee: ESTree.Node) => {
109
+ const imported = resolve(callee, imports);
110
+
111
+ return (
112
+ (imported?.source === "effect" && imported.path === "pipe") ||
113
+ effectMemberOf(imported, "Function") === "pipe"
114
+ );
115
+ };
116
+
117
+ // The array a call of one of `names` reads: the receiver of `tabs.map(...)`, or the first
118
+ // argument of Effect's `Array.map(tabs, ...)`.
119
+ const arrayOf = (call: ESTree.CallExpression, names: ReadonlySet<string>) => {
120
+ const { callee } = call;
121
+ const effect = arrayFunctionOf(callee);
122
+
123
+ if (effect !== undefined) return names.has(effect) ? call.arguments[0] : undefined;
124
+
125
+ return callee.type === "MemberExpression" &&
126
+ !callee.computed &&
127
+ callee.property.type === "Identifier" &&
128
+ names.has(callee.property.name)
129
+ ? callee.object
130
+ : undefined;
131
+ };
132
+
133
+ // A part's bundle: a parameter of a function @foldkit/ui calls with bundles, a member of one
134
+ // such as `tab.tab`, bundles filtered as in `tabs.filter(...)` or `Array.filter(tabs, ...)`, or
135
+ // the result of a styled draggable.
136
+ const isBundle = (node: ESTree.Argument | undefined): boolean => {
137
+ if (node === undefined || node.type === "SpreadElement") return false;
138
+
139
+ if (node.type === "CallExpression") {
140
+ return isBundle(arrayOf(node, filters)) || isStyledDraggable(node.callee);
141
+ }
142
+
143
+ const root = rootOf(node);
144
+ const variable = root === undefined ? undefined : variableOf(context.sourceCode, root);
145
+
146
+ return (
147
+ variable?.defs.some((def) => def.type === "Parameter" && takesBundles(def.node)) ?? false
148
+ );
149
+ };
150
+
151
+ // The `const` or `function` declaration that names `fn`.
152
+ const declarationOf = (fn: ESTree.Node) => {
153
+ if (fn.type === "FunctionDeclaration") return fn;
154
+
155
+ return fn.parent?.type === "VariableDeclarator" && fn.parent.init === fn
156
+ ? fn.parent
157
+ : undefined;
158
+ };
159
+
160
+ // A function @foldkit/ui calls with bundles: one written as a `toView`-like property, one
161
+ // declared by name and passed as one, as in `toView: checkboxView`, or a callback that iterates
162
+ // bundles, as in `tabs.map(cb)`, `Array.map(tabs, cb)` or `pipe(tabs, Array.map(cb))`.
163
+ const takesBundles = (fn: ESTree.Node): boolean => {
164
+ if (isBundleCallback(fn)) return true;
165
+
166
+ const declaration = declarationOf(fn);
167
+
168
+ if (declaration !== undefined) {
169
+ return context.sourceCode
170
+ .getDeclaredVariables(declaration)
171
+ .some(
172
+ ({ defs, references }) =>
173
+ defs.some(({ type }) => type !== "Parameter") &&
174
+ references.some(({ identifier }) => isBundleCallback(identifier)),
175
+ );
176
+ }
177
+
178
+ const { parent } = fn;
179
+
180
+ if (parent?.type !== "CallExpression") return false;
181
+
182
+ const pipeline = parent.parent;
183
+
184
+ // A data-last step of a pipe gets what flows from the pipe's first argument.
185
+ if (
186
+ pipeline.type === "CallExpression" &&
187
+ isPipe(pipeline.callee) &&
188
+ parent.arguments.length === 1 &&
189
+ iterators.has(arrayFunctionOf(parent.callee) ?? "")
190
+ ) {
191
+ return isBundle(pipeline.arguments[0]);
192
+ }
193
+
194
+ return isBundle(arrayOf(parent, iterators));
195
+ };
196
+
197
+ // An attribute list that ends up on a part: one that spreads a part's bundle, or the
198
+ // `childAttributes([...])` of a view input such as `buttonAttributes`.
199
+ const isPartList = (list: ESTree.ArrayExpression) => {
200
+ const { parent } = list;
201
+
202
+ if (
203
+ parent.type === "CallExpression" &&
204
+ exportOf(parent.callee, imports, "foldkit/html") === "childAttributes"
205
+ ) {
206
+ return (
207
+ parent.parent.type === "Property" && attributesInput.test(keyOf(parent.parent) ?? "")
208
+ );
209
+ }
210
+
211
+ return list.elements.some(
212
+ (item) => item?.type === "SpreadElement" && isBundle(item.argument),
213
+ );
214
+ };
215
+
216
+ return {
217
+ Program(program) {
218
+ imports = importsOf(program);
219
+ },
220
+ CallExpression(node) {
221
+ const runtime = exportOf(node.callee, imports, "@stylexjs/stylex");
222
+
223
+ // A result the code throws away, as in a compile guard, reaches no element.
224
+ if (
225
+ (runtime === "props" || runtime === "attrs") &&
226
+ node.parent.type !== "ExpressionStatement"
227
+ ) {
228
+ context.report({ node, messageId: "stylexRuntime" });
229
+ }
230
+
231
+ if (isSx(node, imports) && node.parent.type !== "ArrayExpression") {
232
+ context.report({ node, messageId: "notInElement" });
233
+ }
234
+ },
235
+ ArrayExpression(node) {
236
+ const written = node.elements.flatMap((item) => {
237
+ if (item === null) return [];
238
+
239
+ const writes = writtenBy(item, imports);
240
+
241
+ return writes === undefined ? [] : [{ item, ...writes }];
242
+ });
243
+
244
+ if (isPartList(node)) {
245
+ for (const { item, attribute } of written) {
246
+ context.report({
247
+ node: item,
248
+ messageId: attribute === "class" ? "partClass" : "partStyle",
249
+ });
250
+ }
251
+
252
+ return;
253
+ }
254
+
255
+ for (const { item, attribute } of written.filter(({ raw }) => raw)) {
256
+ context.report({
257
+ node: item,
258
+ messageId: attribute === "class" ? "rawClass" : "rawStyle",
259
+ });
260
+ }
261
+
262
+ const classWriters = written.flatMap(({ item, attribute, raw }) =>
263
+ attribute === "class" && !raw ? [item] : [],
264
+ );
265
+
266
+ const oneCall = classWriters.some((item) => isSx(item, imports)) ? "oneCall" : "oneClass";
267
+
268
+ for (const item of classWriters.slice(1)) {
269
+ context.report({ node: item, messageId: oneCall });
270
+ }
271
+
272
+ const items = node.elements.filter((item) => item !== null);
273
+
274
+ const lastSpread = items.map((item) => item.type).lastIndexOf("SpreadElement");
275
+
276
+ for (const item of items.slice(0, Math.max(lastSpread, 0))) {
277
+ if (isSx(item, imports)) context.report({ node: item, messageId: "last" });
278
+ }
279
+ },
280
+ };
281
+ },
282
+ });
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The @foldkit/ui namespaces that @kutur-kit/foldkit-stylex wraps, as `<Name>Style` namespaces.
3
+ * `foldkit-stylex-unstyled-view` asks for a wrapper only for these. A test compares the list with
4
+ * the core's `src/index.ts`, so add a name in the change that adds its namespace to the core.
5
+ */
6
+ export const components: ReadonlySet<string> = new Set([
7
+ "Animation",
8
+ "Button",
9
+ "Calendar",
10
+ "Checkbox",
11
+ "Combobox",
12
+ "DatePicker",
13
+ "Dialog",
14
+ "Disclosure",
15
+ "DragAndDrop",
16
+ "Fieldset",
17
+ "FileDrop",
18
+ "HoverIntent",
19
+ "Input",
20
+ "Listbox",
21
+ "Menu",
22
+ "Meter",
23
+ "Nav",
24
+ "Popover",
25
+ "Progress",
26
+ "RadioGroup",
27
+ "Select",
28
+ "Slider",
29
+ "Switch",
30
+ "Tabs",
31
+ "Textarea",
32
+ "Toast",
33
+ "Tooltip",
34
+ "VirtualList",
35
+ ]);
36
+
37
+ /**
38
+ * The view each `<Name>Style` namespace wraps under the same name. DragAndDrop has no `view`: its
39
+ * styled part is the bundle `draggable` returns.
40
+ */
41
+ export const viewOf = (component: string) => (component === "DragAndDrop" ? "draggable" : "view");
@@ -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
+ export 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
+ });