@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.
- package/README.md +59 -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 +16 -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/hover-gate.d.ts +2 -0
- package/dist/foldkit-stylex/hover-gate.d.ts.map +1 -0
- package/dist/foldkit-stylex/hover-gate.js +78 -0
- package/dist/foldkit-stylex/hover-gate.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 +94 -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 +22 -0
- package/dist/foldkit-stylex.d.ts.map +1 -0
- package/dist/foldkit-stylex.js +26 -0
- package/dist/foldkit-stylex.js.map +1 -0
- package/dist/foldkit-stylex.json +26 -0
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +16 -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/rules/self-contained-function.d.ts +2 -0
- package/dist/rules/self-contained-function.d.ts.map +1 -0
- package/dist/rules/self-contained-function.js +56 -0
- package/dist/rules/self-contained-function.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/hover-gate.ts +96 -0
- package/src/foldkit-stylex/styles.ts +170 -0
- package/src/foldkit-stylex/unstyled-view.ts +121 -0
- package/src/foldkit-stylex/vite.ts +29 -0
- package/src/foldkit-stylex.ts +30 -0
- package/src/plugin.ts +16 -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/rules/self-contained-function.ts +76 -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,96 @@
|
|
|
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
|
+
import { mediaQueryOf } from "./condition-key.ts";
|
|
7
|
+
|
|
8
|
+
const stylex = "@stylexjs/stylex";
|
|
9
|
+
|
|
10
|
+
// A pseudo-class key that names :hover, alone or in a chain such as ':hover:not(:focus-visible)'.
|
|
11
|
+
// Media keys never start with ':', so '@media (hover:hover)' is not one.
|
|
12
|
+
const isHover = (key: string) => key.startsWith(":") && /:hover\b/.test(key);
|
|
13
|
+
|
|
14
|
+
// A media query that only pointers able to hover match, such as '@media (hover: hover)' or
|
|
15
|
+
// '@media (hover: hover) and (min-width: 640px)'. A query with 'not', 'or' or a comma is not one.
|
|
16
|
+
const isGate = (key: string) => mediaQueryOf(key)?.features.has("(hover:hover)") === true;
|
|
17
|
+
|
|
18
|
+
export const hoverGate = defineRule({
|
|
19
|
+
meta: {
|
|
20
|
+
type: "problem",
|
|
21
|
+
docs: {
|
|
22
|
+
description: "Require StyleX :hover styles to sit under @media (hover: hover).",
|
|
23
|
+
},
|
|
24
|
+
schema: [],
|
|
25
|
+
messages: {
|
|
26
|
+
gate: "Put this hover style under @media (hover: hover): touch browsers keep :hover after a tap, so the style sticks.",
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
createOnce(context) {
|
|
30
|
+
let imports: Imports = new Map();
|
|
31
|
+
|
|
32
|
+
// The condition a key adds, such as ':hover' for `[stylex.when.ancestor(":hover")]`, whose
|
|
33
|
+
// first argument is the condition on the other element.
|
|
34
|
+
const conditionOf = (property: ESTree.ObjectProperty) => {
|
|
35
|
+
if (!property.computed) return keyOf(property);
|
|
36
|
+
|
|
37
|
+
const { key } = property;
|
|
38
|
+
|
|
39
|
+
if (
|
|
40
|
+
key.type !== "CallExpression" ||
|
|
41
|
+
!exportOf(key.callee, imports, stylex)?.startsWith("when.")
|
|
42
|
+
) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const [condition] = key.arguments;
|
|
47
|
+
|
|
48
|
+
return condition?.type === "Literal" ? String(condition.value) : undefined;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// Reports each value set under a :hover key and outside a (hover: hover) query, inside it or
|
|
52
|
+
// around it. A null value sets nothing, so
|
|
53
|
+
// `":hover": { default: null, "@media (hover: hover)": … }` passes.
|
|
54
|
+
const checkStyle = (style: ESTree.Node, hovered: boolean, gated: boolean) => {
|
|
55
|
+
// A dynamic style's conditions are in the object its function returns.
|
|
56
|
+
const object = style.type === "ArrowFunctionExpression" ? style.body : style;
|
|
57
|
+
|
|
58
|
+
if (object.type !== "ObjectExpression") return;
|
|
59
|
+
|
|
60
|
+
for (const property of object.properties) {
|
|
61
|
+
if (property.type !== "Property") continue;
|
|
62
|
+
|
|
63
|
+
const condition = conditionOf(property) ?? "";
|
|
64
|
+
const isHovered = hovered || isHover(condition);
|
|
65
|
+
const isGated = gated || isGate(condition);
|
|
66
|
+
const { value } = property;
|
|
67
|
+
|
|
68
|
+
if (value.type === "ObjectExpression") {
|
|
69
|
+
checkStyle(value, isHovered, isGated);
|
|
70
|
+
} else if (isHovered && !isGated && !(value.type === "Literal" && value.value === null)) {
|
|
71
|
+
context.report({ node: property.key, messageId: "gate" });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
Program(program) {
|
|
78
|
+
imports = importsOf(program);
|
|
79
|
+
},
|
|
80
|
+
CallExpression(node) {
|
|
81
|
+
const [styles] = node.arguments;
|
|
82
|
+
|
|
83
|
+
if (
|
|
84
|
+
exportOf(node.callee, imports, stylex) !== "create" ||
|
|
85
|
+
styles?.type !== "ObjectExpression"
|
|
86
|
+
) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
for (const entry of styles.properties) {
|
|
91
|
+
if (entry.type === "Property") checkStyle(entry.value, false, false);
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
});
|
|
@@ -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,121 @@
|
|
|
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: "Import {{name}} from @kutur-kit/foldkit-stylex-theme/{{module}} in place of @foldkit/ui's: the theme's {{name}} styles {{view}} by default. For a look of your own, wrap {{view}} once at module scope with {{wrapper}}({{view}}, styles) from @kutur-kit/foldkit-stylex and use the result here. Both style every part and add 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: {
|
|
112
|
+
name,
|
|
113
|
+
module: name.replace(/(?<=.)[A-Z]/g, (letter) => `-${letter}`).toLowerCase(),
|
|
114
|
+
view,
|
|
115
|
+
wrapper: [`${name}Style`, ...rest, viewOf(name)].join("."),
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
});
|
|
@@ -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,30 @@
|
|
|
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-hover-gate": "error",
|
|
18
|
+
"kit/foldkit-stylex-styles": "error",
|
|
19
|
+
"kit/foldkit-stylex-unstyled-view": "error",
|
|
20
|
+
"kit/foldkit-stylex-vite": "error",
|
|
21
|
+
"stylex/enforce-extension": "error",
|
|
22
|
+
"stylex/no-legacy-contextual-styles": "error",
|
|
23
|
+
"stylex/no-nonstandard-styles": "error",
|
|
24
|
+
"stylex/no-unused": "error",
|
|
25
|
+
"stylex/valid-shorthands": "error",
|
|
26
|
+
"stylex/valid-styles": "error",
|
|
27
|
+
},
|
|
28
|
+
} satisfies OxlintConfig;
|
|
29
|
+
|
|
30
|
+
export default foldkitStylex;
|
package/src/plugin.ts
CHANGED
|
@@ -1,14 +1,30 @@
|
|
|
1
1
|
import { definePlugin } from "@oxlint/plugins";
|
|
2
2
|
|
|
3
|
+
import { attributes } from "./foldkit-stylex/attributes.ts";
|
|
4
|
+
import { conditionKey } from "./foldkit-stylex/condition-key.ts";
|
|
5
|
+
import { hoverGate } from "./foldkit-stylex/hover-gate.ts";
|
|
6
|
+
import { styles } from "./foldkit-stylex/styles.ts";
|
|
7
|
+
import { unstyledView } from "./foldkit-stylex/unstyled-view.ts";
|
|
8
|
+
import { vite } from "./foldkit-stylex/vite.ts";
|
|
3
9
|
import { commentProse } from "./rules/comment-prose.ts";
|
|
10
|
+
import { noManualEffectRuntimeInTests } from "./rules/no-manual-effect-runtime-in-tests.ts";
|
|
4
11
|
import { noSuppressionDirectives } from "./rules/no-suppression-directives.ts";
|
|
5
12
|
import { noYieldInIf } from "./rules/no-yield-in-if.ts";
|
|
13
|
+
import { selfContainedFunction } from "./rules/self-contained-function.ts";
|
|
6
14
|
|
|
7
15
|
export default definePlugin({
|
|
8
16
|
meta: { name: "@kutur-kit/oxc" },
|
|
9
17
|
rules: {
|
|
10
18
|
"comment-prose": commentProse,
|
|
19
|
+
"foldkit-stylex-attributes": attributes,
|
|
20
|
+
"foldkit-stylex-condition-key": conditionKey,
|
|
21
|
+
"foldkit-stylex-hover-gate": hoverGate,
|
|
22
|
+
"foldkit-stylex-styles": styles,
|
|
23
|
+
"foldkit-stylex-unstyled-view": unstyledView,
|
|
24
|
+
"foldkit-stylex-vite": vite,
|
|
25
|
+
"no-manual-effect-runtime-in-tests": noManualEffectRuntimeInTests,
|
|
11
26
|
"no-suppression-directives": noSuppressionDirectives,
|
|
12
27
|
"no-yield-in-if": noYieldInIf,
|
|
28
|
+
"self-contained-function": selfContainedFunction,
|
|
13
29
|
},
|
|
14
30
|
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a name refers to through an import: the module and the export path inside it. After
|
|
5
|
+
* `import { Popover } from "@foldkit/ui"`, `Popover.view` is `@foldkit/ui` with `Popover.view`.
|
|
6
|
+
*/
|
|
7
|
+
export type Imported = Readonly<{ source: string; path: string }>;
|
|
8
|
+
|
|
9
|
+
export type Imports = ReadonlyMap<string, Imported>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The import that each local name binds. Rules recognise calls such as `sx(...)` or
|
|
13
|
+
* `Effect.runSync(...)` by these, so a local function that happens to be called `sx` is not one.
|
|
14
|
+
*/
|
|
15
|
+
export const importsOf = (program: ESTree.Program): Imports =>
|
|
16
|
+
new Map(
|
|
17
|
+
program.body.flatMap((statement) =>
|
|
18
|
+
statement.type === "ImportDeclaration"
|
|
19
|
+
? statement.specifiers.map((specifier): [string, Imported] => [
|
|
20
|
+
specifier.local.name,
|
|
21
|
+
{ source: statement.source.value, path: pathOf(specifier) },
|
|
22
|
+
])
|
|
23
|
+
: [],
|
|
24
|
+
),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const pathOf = (specifier: ESTree.ImportDeclarationSpecifier) => {
|
|
28
|
+
if (specifier.type === "ImportNamespaceSpecifier") return "";
|
|
29
|
+
|
|
30
|
+
if (specifier.type === "ImportDefaultSpecifier") return "default";
|
|
31
|
+
|
|
32
|
+
return specifier.imported.type === "Literal" ? specifier.imported.value : specifier.imported.name;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const join = (path: string, name: string) => (path === "" ? name : `${path}.${name}`);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Resolves an identifier, a member chain such as `Ui.Popover.view` or a qualified type name such as
|
|
39
|
+
* `PopoverStyle.Styles` to the import it starts from, or `undefined` for anything else.
|
|
40
|
+
*/
|
|
41
|
+
export const resolve = (node: ESTree.Node, imports: Imports): Imported | undefined => {
|
|
42
|
+
if (node.type === "Identifier") return imports.get(node.name);
|
|
43
|
+
|
|
44
|
+
const [object, property] =
|
|
45
|
+
node.type === "MemberExpression" && !node.computed
|
|
46
|
+
? [node.object, node.property]
|
|
47
|
+
: node.type === "TSQualifiedName"
|
|
48
|
+
? [node.left, node.right]
|
|
49
|
+
: [];
|
|
50
|
+
|
|
51
|
+
if (object === undefined || property?.type !== "Identifier") return undefined;
|
|
52
|
+
|
|
53
|
+
const resolved = resolve(object, imports);
|
|
54
|
+
|
|
55
|
+
return resolved && { source: resolved.source, path: join(resolved.path, property.name) };
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/** The export path `node` names in `source`, such as `create` for `stylex.create`. */
|
|
59
|
+
export const exportOf = (node: ESTree.Node, imports: Imports, source: string) => {
|
|
60
|
+
const resolved = resolve(node, imports);
|
|
61
|
+
|
|
62
|
+
return resolved?.source === source ? resolved.path : undefined;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The export `imported` names in an Effect module, read from the barrel (`Effect.runSync` from
|
|
67
|
+
* "effect") or the module's own path (`runSync` from "effect/Effect").
|
|
68
|
+
*/
|
|
69
|
+
export const effectMemberOf = (imported: Imported | undefined, module: string) => {
|
|
70
|
+
if (imported?.source === `effect/${module}`) return imported.path;
|
|
71
|
+
|
|
72
|
+
return imported?.source === "effect" && imported.path.startsWith(`${module}.`)
|
|
73
|
+
? imported.path.slice(module.length + 1)
|
|
74
|
+
: undefined;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** The identifier a member chain such as `tab.tab.attributes` starts from. */
|
|
78
|
+
export const rootOf = (node: ESTree.Expression): ESTree.IdentifierReference | undefined => {
|
|
79
|
+
if (node.type === "Identifier") return node;
|
|
80
|
+
|
|
81
|
+
return node.type === "MemberExpression" ? rootOf(node.object) : undefined;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/** A call of a method named `name` on any object, such as `h.Class(...)`. */
|
|
85
|
+
export const isMethodCall = (node: ESTree.Node, name: string): node is ESTree.CallExpression =>
|
|
86
|
+
node.type === "CallExpression" &&
|
|
87
|
+
node.callee.type === "MemberExpression" &&
|
|
88
|
+
!node.callee.computed &&
|
|
89
|
+
node.callee.property.type === "Identifier" &&
|
|
90
|
+
node.callee.property.name === name;
|
|
91
|
+
|
|
92
|
+
/** A property's key as written, for identifier and string keys. */
|
|
93
|
+
export const keyOf = (property: Readonly<{ computed: boolean; key: ESTree.PropertyKey }>) => {
|
|
94
|
+
if (property.computed) return undefined;
|
|
95
|
+
|
|
96
|
+
if (property.key.type === "Identifier") return property.key.name;
|
|
97
|
+
|
|
98
|
+
return property.key.type === "Literal" ? String(property.key.value) : undefined;
|
|
99
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import { defineRule } from "@oxlint/plugins";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
effectMemberOf,
|
|
7
|
+
type Imported,
|
|
8
|
+
type Imports,
|
|
9
|
+
importsOf,
|
|
10
|
+
keyOf,
|
|
11
|
+
resolve,
|
|
12
|
+
} from "./imports.ts";
|
|
13
|
+
import { variableOf } from "./scope.ts";
|
|
14
|
+
|
|
15
|
+
// The runners of Effect and of a ManagedRuntime. Effect's `<runner>With` forms take a Context.
|
|
16
|
+
const runners = new Set([
|
|
17
|
+
"runCallback",
|
|
18
|
+
"runFork",
|
|
19
|
+
"runPromise",
|
|
20
|
+
"runPromiseExit",
|
|
21
|
+
"runSync",
|
|
22
|
+
"runSyncExit",
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
const isEffectRunner = (imported: Imported | undefined) =>
|
|
26
|
+
runners.has(effectMemberOf(imported, "Effect")?.replace(/With$/, "") ?? "");
|
|
27
|
+
|
|
28
|
+
const makesRuntime = (node: ESTree.Node | null, imports: Imports) =>
|
|
29
|
+
node?.type === "CallExpression" &&
|
|
30
|
+
effectMemberOf(resolve(node.callee, imports), "ManagedRuntime") === "make";
|
|
31
|
+
|
|
32
|
+
export const noManualEffectRuntimeInTests = defineRule({
|
|
33
|
+
meta: {
|
|
34
|
+
type: "problem",
|
|
35
|
+
docs: { description: "Disallow running effects by hand in tests." },
|
|
36
|
+
schema: [],
|
|
37
|
+
messages: {
|
|
38
|
+
manualRuntime:
|
|
39
|
+
"Manual Effect runner `{{runner}}` in a test. It runs the effect on the real clock and outside the test's Layers and scope, so time-based code waits for real time and each test wires its services by hand. Fix: write the test with `it.effect` from `@effect/vitest`, provide services with Layers and control time with `TestClock` from `effect/testing`.",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
createOnce(context) {
|
|
43
|
+
let imports: Imports = new Map();
|
|
44
|
+
|
|
45
|
+
// `ManagedRuntime.make(...)`, or a variable it is assigned to, where it is declared or later, as
|
|
46
|
+
// in `beforeAll(() => { runtime = ManagedRuntime.make(layer); })`.
|
|
47
|
+
const isRuntime = (node: ESTree.Expression | null): boolean => {
|
|
48
|
+
if (node?.type !== "Identifier") return makesRuntime(node, imports);
|
|
49
|
+
|
|
50
|
+
const references = variableOf(context.sourceCode, node)?.references ?? [];
|
|
51
|
+
|
|
52
|
+
return references.some(({ writeExpr }) => makesRuntime(writeExpr, imports));
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const report = (node: ESTree.Node) =>
|
|
56
|
+
context.report({
|
|
57
|
+
node,
|
|
58
|
+
messageId: "manualRuntime",
|
|
59
|
+
data: { runner: context.sourceCode.getText(node) },
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
Program(program) {
|
|
64
|
+
imports = importsOf(program);
|
|
65
|
+
},
|
|
66
|
+
ImportSpecifier(node) {
|
|
67
|
+
if (isEffectRunner(imports.get(node.local.name))) report(node.imported);
|
|
68
|
+
},
|
|
69
|
+
// `const { runPromise } = runtime`.
|
|
70
|
+
VariableDeclarator({ id, init }) {
|
|
71
|
+
if (id.type !== "ObjectPattern" || !isRuntime(init)) return;
|
|
72
|
+
|
|
73
|
+
for (const property of id.properties) {
|
|
74
|
+
if (property.type === "Property" && runners.has(keyOf(property) ?? "")) {
|
|
75
|
+
report(property.key);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
MemberExpression(node) {
|
|
80
|
+
if (isEffectRunner(resolve(node, imports))) {
|
|
81
|
+
report(node);
|
|
82
|
+
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const { object, property } = node;
|
|
87
|
+
|
|
88
|
+
if (
|
|
89
|
+
!node.computed &&
|
|
90
|
+
property.type === "Identifier" &&
|
|
91
|
+
runners.has(property.name) &&
|
|
92
|
+
isRuntime(object)
|
|
93
|
+
) {
|
|
94
|
+
report(node);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
},
|
|
99
|
+
});
|