@sonsu-lee/oxlint-plugin 0.0.0-stage → 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sonsu Lee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,50 @@
1
- # Temporary Holding Version
1
+ # @sonsu-lee/oxlint-plugin
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Custom lint rules for Oxlint, also usable from ESLint.
4
+
5
+ The plugin is wrapped with `eslintCompatPlugin`, so the same rules run in Oxlint and ESLint. Rules use only the AST and scope analysis; rules that need TypeScript type information do not belong here.
6
+
7
+ ## Install
8
+
9
+ Most projects should use [`@sonsu-lee/oxlint-config`](../oxlint-config), which registers this plugin. To use the plugin on its own:
10
+
11
+ ```sh
12
+ pnpm add -D oxlint @sonsu-lee/oxlint-plugin
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ // oxlint.config.ts
19
+ import { defineConfig } from 'oxlint';
20
+
21
+ export default defineConfig({
22
+ jsPlugins: [{ name: 'sonsu-lee', specifier: '@sonsu-lee/oxlint-plugin' }],
23
+ rules: { 'sonsu-lee/no-double-assertion': 'error' },
24
+ });
25
+ ```
26
+
27
+ Register the plugin with `name: "sonsu-lee"`. Without an alias, Oxlint derives the namespace `@sonsu-lee` from the scoped package name.
28
+
29
+ With Yarn PnP, Oxlint cannot resolve a plugin by package name. Pass the resolved URL instead: `specifier: import.meta.resolve('@sonsu-lee/oxlint-plugin')`.
30
+
31
+ ## Rules
32
+
33
+ | Rule | Description |
34
+ | --- | --- |
35
+ | [no-double-assertion](docs/rules/no-double-assertion.md) | Disallow chaining type assertions, such as `value as unknown as Foo` |
36
+ | [prefer-named-conditions](docs/rules/prefer-named-conditions.md) | Review mixed operators, inline callbacks, and groups of unnamed predicates |
37
+ | [require-effect-reason](docs/rules/require-effect-reason.md) | Require a synchronization reason on React Effect calls |
38
+ | [no-machine-context-mutation](docs/rules/no-machine-context-mutation.md) | Report writes to machine context and calls by mutator name, regardless of receiver type |
39
+ | [no-machine-in-render](docs/rules/no-machine-in-render.md) | Report machine or actor creation directly during render |
40
+ | [no-unstable-actor-selector](docs/rules/no-unstable-actor-selector.md) | Review object or array selectors without a comparator |
41
+ | [prefer-machine-setup](docs/rules/prefer-machine-setup.md) | Review bare `createMachine` calls |
42
+ | [require-invoke-on-error](docs/rules/require-invoke-on-error.md) | Review invokes with a success transition but no failure transition |
43
+
44
+ ## Contributing
45
+
46
+ See [CONTRIBUTING.md](../../CONTRIBUTING.md).
47
+
48
+ ## License
49
+
50
+ [MIT](LICENSE) © Sonsu Lee
@@ -0,0 +1,3 @@
1
+ import { Plugin } from "@oxlint/plugins";
2
+ declare const plugin: Plugin;
3
+ export { plugin as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,669 @@
1
+ import { eslintCompatPlugin } from "@oxlint/plugins";
2
+ // src/rules/no-double-assertion.ts
3
+ const noDoubleAssertion = {
4
+ meta: {
5
+ type: "problem",
6
+ docs: {
7
+ description: "Disallow chaining type assertions, such as `value as unknown as Foo`",
8
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/no-double-assertion.md"
9
+ },
10
+ messages: { doubleAssertion: "Do not chain type assertions. Narrow the value with a type guard or parse it at the boundary." },
11
+ schema: []
12
+ },
13
+ createOnce(context) {
14
+ function check(node) {
15
+ if (isTypeAssertion(node.parent)) return;
16
+ let assertionCount = 0;
17
+ let current = node;
18
+ while (isTypeAssertion(current)) {
19
+ if (!isConstAssertion(current)) assertionCount++;
20
+ current = current.expression;
21
+ }
22
+ if (assertionCount >= 2) context.report({
23
+ node,
24
+ messageId: "doubleAssertion"
25
+ });
26
+ }
27
+ return {
28
+ TSAsExpression: check,
29
+ TSTypeAssertion: check
30
+ };
31
+ }
32
+ };
33
+ function isTypeAssertion(node) {
34
+ return node.type === "TSAsExpression" || node.type === "TSTypeAssertion";
35
+ }
36
+ function isConstAssertion(node) {
37
+ const { typeAnnotation } = node;
38
+ return typeAnnotation.type === "TSTypeReference" && typeAnnotation.typeName.type === "Identifier" && typeAnnotation.typeName.name === "const";
39
+ }
40
+ // src/utils/expression-wrappers.ts
41
+ function wrappedExpression(node, mode) {
42
+ switch (node.type) {
43
+ case "ParenthesizedExpression":
44
+ case "TSAsExpression":
45
+ case "TSSatisfiesExpression":
46
+ case "TSNonNullExpression":
47
+ case "TSTypeAssertion": return node.expression;
48
+ case "AwaitExpression": return mode === "condition" ? node.argument : void 0;
49
+ default: return;
50
+ }
51
+ }
52
+ function unwrapExpression(node, mode) {
53
+ let current = node;
54
+ for (let inner = wrappedExpression(current, mode); inner; inner = wrappedExpression(current, mode)) current = inner;
55
+ return current;
56
+ }
57
+ function outerExpression(node, mode) {
58
+ let current = node;
59
+ while (current.parent && wrappedExpression(current.parent, mode) === current) current = current.parent;
60
+ return current;
61
+ }
62
+ // src/utils/imported-name.ts
63
+ function importSpecifierName({ imported }) {
64
+ if (imported.type === "Identifier") return imported.name;
65
+ return imported.value;
66
+ }
67
+ /** Resolves direct named imports and noncomputed members of default or namespace imports. */
68
+ function importedName(context, callee, source) {
69
+ let binding;
70
+ let member = null;
71
+ if (callee.type === "Identifier") binding = callee;
72
+ else if (callee.type === "MemberExpression" && !callee.computed && callee.object.type === "Identifier" && callee.property.type === "Identifier") {
73
+ binding = callee.object;
74
+ member = callee.property.name;
75
+ } else return null;
76
+ let scope = context.sourceCode.getScope(binding);
77
+ while (scope) {
78
+ const variable = scope.set.get(binding.name);
79
+ if (variable) {
80
+ const def = variable.defs[0];
81
+ if (def?.type !== "ImportBinding" || def.parent?.type !== "ImportDeclaration" || def.parent.source.value !== source) return null;
82
+ if (def.node.type === "ImportSpecifier") {
83
+ const name = importSpecifierName(def.node);
84
+ if (name === "default") return member;
85
+ if (member !== null) return null;
86
+ return name;
87
+ }
88
+ return member;
89
+ }
90
+ scope = scope.upper;
91
+ }
92
+ return null;
93
+ }
94
+ // src/rules/no-machine-context-mutation.ts
95
+ const mutators = {
96
+ push: true,
97
+ pop: true,
98
+ shift: true,
99
+ unshift: true,
100
+ splice: true,
101
+ sort: true,
102
+ reverse: true,
103
+ fill: true,
104
+ copyWithin: true,
105
+ set: true,
106
+ add: true,
107
+ delete: true,
108
+ clear: true
109
+ };
110
+ const noMachineContextMutation = {
111
+ meta: {
112
+ type: "problem",
113
+ docs: {
114
+ description: "Disallow mutating an XState context parameter",
115
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/no-machine-context-mutation.md"
116
+ },
117
+ messages: { contextMutation: "Do not mutate machine context. Use assign and return the new value." },
118
+ schema: []
119
+ },
120
+ createOnce(context) {
121
+ let importsXState = false;
122
+ function check(node, target) {
123
+ const member = unwrapExpression(target);
124
+ if (member.type !== "MemberExpression") return;
125
+ let root = unwrapExpression(member.object);
126
+ while (root.type === "MemberExpression") root = unwrapExpression(root.object);
127
+ if (root.type !== "Identifier" || root.name !== "context") return;
128
+ let scope = context.sourceCode.getScope(root);
129
+ while (scope && !scope.set.has(root.name)) scope = scope.upper;
130
+ const definition = scope?.set.get(root.name)?.defs[0];
131
+ if (definition?.type !== "Parameter") return;
132
+ let binding = definition.name;
133
+ let parent = binding.parent;
134
+ let destructuresContext = false;
135
+ while (parent && parent !== definition.node) {
136
+ if (parent.type === "Property" && parent.value === binding && !parent.computed) {
137
+ destructuresContext = parent.key.type === "Identifier" ? parent.key.name === "context" : parent.key.type === "Literal" && parent.key.value === "context";
138
+ break;
139
+ }
140
+ binding = parent;
141
+ parent = binding.parent;
142
+ }
143
+ if (!destructuresContext) return;
144
+ let current = outerExpression(definition.node).parent;
145
+ while (current?.type !== "CallExpression") switch (current?.type) {
146
+ case "Property":
147
+ case "ObjectExpression":
148
+ case "ArrayExpression":
149
+ current = outerExpression(current).parent;
150
+ break;
151
+ default: return;
152
+ }
153
+ const { callee } = current;
154
+ if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && (callee.property.name === "createMachine" || callee.property.name === "provide") || importedName(context, callee, "xstate") !== null) context.report({
155
+ node,
156
+ messageId: "contextMutation"
157
+ });
158
+ }
159
+ return {
160
+ Program(node) {
161
+ importsXState = node.body.some((statement) => statement.type === "ImportDeclaration" && (statement.source.value === "xstate" || statement.source.value === "@xstate/react"));
162
+ },
163
+ AssignmentExpression(node) {
164
+ if (importsXState) check(node, node.left);
165
+ },
166
+ UpdateExpression(node) {
167
+ if (importsXState) check(node, node.argument);
168
+ },
169
+ UnaryExpression(node) {
170
+ if (importsXState && node.operator === "delete") check(node, node.argument);
171
+ },
172
+ CallExpression(node) {
173
+ if (!importsXState) return;
174
+ const { callee } = node;
175
+ if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && Object.hasOwn(mutators, callee.property.name)) check(node, callee.object);
176
+ }
177
+ };
178
+ }
179
+ };
180
+ // src/utils/machine-setup.ts
181
+ /** Finds the XState `setup` call behind an expression or a same-file `const`. */
182
+ function findSetupCall(context, node) {
183
+ return resolveSetup(context, node, /* @__PURE__ */ new Set());
184
+ }
185
+ function resolvesSetupBinding(context, identifier, seen) {
186
+ let scope = context.sourceCode.getScope(identifier);
187
+ while (scope && !scope.set.has(identifier.name)) scope = scope.upper;
188
+ const definition = scope?.set.get(identifier.name)?.defs[0];
189
+ if (definition?.type !== "Variable" || definition.node.type !== "VariableDeclarator") return null;
190
+ if (definition.node.id.type !== "Identifier") return null;
191
+ if (definition.parent?.type !== "VariableDeclaration" || definition.parent.kind !== "const") return null;
192
+ const { init } = definition.node;
193
+ if (init?.type !== "CallExpression" || seen.has(definition.node)) return null;
194
+ seen.add(definition.node);
195
+ return resolveSetup(context, init, seen);
196
+ }
197
+ function resolveSetup(context, node, seen) {
198
+ if (node.type === "Identifier") return resolvesSetupBinding(context, node, seen);
199
+ if (node.type !== "CallExpression") return null;
200
+ const { callee } = node;
201
+ if (importedName(context, callee, "xstate") === "setup") return node;
202
+ if (callee.type !== "MemberExpression" || callee.computed) return null;
203
+ if (callee.property.type !== "Identifier" || callee.property.name !== "extend") return null;
204
+ return resolveSetup(context, callee.object, seen);
205
+ }
206
+ // src/rules/no-machine-in-render.ts
207
+ function isFactory(name) {
208
+ return name === "createMachine" || name === "setup" || name === "createActor";
209
+ }
210
+ const renderName = /^(?:[A-Z]|use[A-Z0-9])/u;
211
+ const noMachineInRender = {
212
+ meta: {
213
+ type: "problem",
214
+ docs: {
215
+ description: "Disallow creating XState machines and actors during React render",
216
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/no-machine-in-render.md"
217
+ },
218
+ messages: { machineInRender: "Move machine or actor creation out of render. Define the machine at module scope and pass changing values as input." },
219
+ schema: []
220
+ },
221
+ createOnce(context) {
222
+ const factoryNames = /* @__PURE__ */ new Set();
223
+ const namespaceNames = /* @__PURE__ */ new Set();
224
+ return {
225
+ Program(node) {
226
+ factoryNames.clear();
227
+ namespaceNames.clear();
228
+ for (const statement of node.body) {
229
+ if (statement.type !== "ImportDeclaration" || statement.source.value !== "xstate") continue;
230
+ for (const specifier of statement.specifiers) if (specifier.type !== "ImportSpecifier") namespaceNames.add(specifier.local.name);
231
+ else if (isFactory(importSpecifierName(specifier))) factoryNames.add(specifier.local.name);
232
+ }
233
+ },
234
+ CallExpression(node) {
235
+ const { callee } = node;
236
+ let factoryCall = false;
237
+ if (callee.type === "Identifier") {
238
+ if (!factoryNames.has(callee.name)) return;
239
+ factoryCall = isFactory(importedName(context, callee, "xstate"));
240
+ } else {
241
+ if (callee.type !== "MemberExpression" || callee.computed) return;
242
+ const { object, property } = callee;
243
+ if (property.type !== "Identifier" || !isFactory(property.name)) return;
244
+ if (object.type === "Identifier" && namespaceNames.has(object.name)) factoryCall = isFactory(importedName(context, callee, "xstate"));
245
+ else if (property.name === "createMachine") {
246
+ const setup = findSetupCall(context, object);
247
+ if (!setup) return;
248
+ factoryCall = setup.range[0] < object.range[0] || setup.range[1] > object.range[1];
249
+ }
250
+ }
251
+ if (!factoryCall) return;
252
+ let current = node.parent;
253
+ while (current.type !== "Program") {
254
+ if (current.type === "FunctionDeclaration" || current.type === "FunctionExpression" || current.type === "ArrowFunctionExpression") {
255
+ let { parent } = current;
256
+ let componentName;
257
+ if (current.type === "FunctionDeclaration") componentName = current.id?.name;
258
+ else if (parent.type === "CallExpression" && parent.arguments[0] === current) {
259
+ const wrapper = importedName(context, parent.callee, "react");
260
+ if (wrapper !== "memo" && wrapper !== "forwardRef") return;
261
+ if (current.type === "FunctionExpression") componentName = current.id?.name;
262
+ parent = parent.parent;
263
+ }
264
+ const assignedName = parent.type === "VariableDeclarator" && parent.id.type === "Identifier" ? parent.id.name : void 0;
265
+ if (!renderName.test(componentName ?? "") && !renderName.test(assignedName ?? "")) return;
266
+ context.report({
267
+ node,
268
+ messageId: "machineInRender"
269
+ });
270
+ return;
271
+ }
272
+ current = current.parent;
273
+ }
274
+ }
275
+ };
276
+ }
277
+ };
278
+ // src/rules/no-unstable-actor-selector.ts
279
+ function createsCollection(node) {
280
+ let current = node;
281
+ while (current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression") current = current.expression;
282
+ switch (current.type) {
283
+ case "ObjectExpression":
284
+ case "ArrayExpression": return true;
285
+ case "LogicalExpression": return current.operator !== "&&" && createsCollection(current.left) || createsCollection(current.right);
286
+ case "ConditionalExpression": return createsCollection(current.consequent) || createsCollection(current.alternate);
287
+ default: return false;
288
+ }
289
+ }
290
+ const noUnstableActorSelector = {
291
+ meta: {
292
+ type: "suggestion",
293
+ docs: {
294
+ description: "Disallow allocating an object or array in an XState selector without a comparator",
295
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/no-unstable-actor-selector.md"
296
+ },
297
+ messages: { unstableSelector: "Select fields separately or pass a comparator so an equivalent selector result does not re-render the component." },
298
+ schema: []
299
+ },
300
+ createOnce(context) {
301
+ let importsXStateReact = false;
302
+ function isActorContextSelector(callee) {
303
+ if (callee.type !== "MemberExpression" || callee.computed || callee.property.type !== "Identifier" || callee.property.name !== "useSelector" || callee.object.type !== "Identifier") return false;
304
+ const binding = callee.object;
305
+ let scope = context.sourceCode.getScope(binding);
306
+ while (scope && !scope.set.has(binding.name)) scope = scope.upper;
307
+ const definition = scope?.set.get(binding.name)?.defs[0];
308
+ return definition?.type === "Variable" && definition.node.type === "VariableDeclarator" && definition.node.id.type === "Identifier" && definition.node.init?.type === "CallExpression" && importedName(context, definition.node.init.callee, "@xstate/react") === "createActorContext";
309
+ }
310
+ function check(node, selector, result) {
311
+ const [first, second, third] = node.arguments;
312
+ const { callee } = node;
313
+ if (first === selector && second === void 0 && isActorContextSelector(callee) || second === selector && third === void 0 && importedName(context, callee, "@xstate/react") === "useSelector") context.report({
314
+ node: result,
315
+ messageId: "unstableSelector"
316
+ });
317
+ }
318
+ return {
319
+ Program(node) {
320
+ importsXStateReact = node.body.some((statement) => statement.type === "ImportDeclaration" && statement.source.value === "@xstate/react");
321
+ },
322
+ CallExpression(node) {
323
+ if (!importsXStateReact) return;
324
+ const [first, second, third] = node.arguments;
325
+ if (second === void 0 && first?.type === "ArrowFunctionExpression" && first.expression) {
326
+ if (createsCollection(first.body)) check(node, first, first.body);
327
+ } else if (second?.type === "ArrowFunctionExpression" && second.expression) {
328
+ if (third === void 0 && createsCollection(second.body)) check(node, second, second.body);
329
+ }
330
+ },
331
+ ReturnStatement(node) {
332
+ if (!importsXStateReact || node.argument === null || !createsCollection(node.argument)) return;
333
+ let current = node.parent;
334
+ while (current.type !== "FunctionDeclaration" && current.type !== "Program" && current.type !== "FunctionExpression" && current.type !== "ArrowFunctionExpression") current = current.parent;
335
+ if ((current.type === "FunctionExpression" || current.type === "ArrowFunctionExpression") && current.parent?.type === "CallExpression") check(current.parent, current, node.argument);
336
+ }
337
+ };
338
+ }
339
+ };
340
+ // src/rules/prefer-machine-setup.ts
341
+ const preferMachineSetup = {
342
+ meta: {
343
+ type: "suggestion",
344
+ docs: {
345
+ description: "Prefer XState setup().createMachine() over a bare createMachine() call",
346
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/prefer-machine-setup.md"
347
+ },
348
+ messages: { bareCreateMachine: "Use setup({...}).createMachine({...}) to declare implementation names and types together." },
349
+ schema: []
350
+ },
351
+ createOnce(context) {
352
+ let importsXState = false;
353
+ return {
354
+ Program(node) {
355
+ importsXState = node.body.some((statement) => statement.type === "ImportDeclaration" && statement.source.value === "xstate");
356
+ },
357
+ CallExpression(node) {
358
+ if (!importsXState) return;
359
+ const { callee } = node;
360
+ if (callee.type === "MemberExpression" && (callee.computed || callee.property.type !== "Identifier" || callee.property.name !== "createMachine")) return;
361
+ if (importedName(context, callee, "xstate") === "createMachine") context.report({
362
+ node,
363
+ messageId: "bareCreateMachine"
364
+ });
365
+ }
366
+ };
367
+ }
368
+ };
369
+ // src/rules/prefer-named-conditions.ts
370
+ const preserveOrder = " Keep short-circuit order: extract to a function when the right side must not run early.";
371
+ const preferNamedConditions = {
372
+ meta: {
373
+ type: "suggestion",
374
+ docs: {
375
+ description: "Suggest naming checks in complex inline conditions",
376
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/prefer-named-conditions.md"
377
+ },
378
+ messages: {
379
+ unnamedConditions: `This condition combines {{count}} unnamed checks. Name the checks that carry a business meaning.${preserveOrder}`,
380
+ mixedOperators: `This condition mixes && and || around unnamed checks. Name the grouped check.${preserveOrder}`,
381
+ inlineCallback: `This condition combines an inline callback with another check. Name each check.${preserveOrder}`
382
+ },
383
+ schema: []
384
+ },
385
+ createOnce(context) {
386
+ return { LogicalExpression(node) {
387
+ if (!isCondition(node)) return;
388
+ const root = outerExpression(node, "condition");
389
+ if (root.parent && isCondition(root.parent)) return;
390
+ if (isNamedCondition(root)) return;
391
+ const inJsxChild = root.parent?.type === "JSXExpressionContainer" && root.parent.parent.type !== "JSXAttribute";
392
+ let leafCount = 0;
393
+ let unnamedCount = 0;
394
+ let operator;
395
+ let hasMixedOperators = false;
396
+ let hasInlineCallback = false;
397
+ const pending = [node];
398
+ for (let next = pending.pop(); next !== void 0; next = pending.pop()) {
399
+ const current = unwrapExpression(next, "condition");
400
+ if (isCondition(current)) {
401
+ if (inJsxChild && current.operator === "&&" && rendersJsx(current.right)) pending.push(current.left);
402
+ else {
403
+ operator ??= current.operator;
404
+ if (current.operator !== operator) hasMixedOperators = true;
405
+ pending.push(current.right, current.left);
406
+ }
407
+ continue;
408
+ }
409
+ const kind = leafKind(current);
410
+ if (kind === "jsx") continue;
411
+ leafCount++;
412
+ if (kind === "inlineCallback") hasInlineCallback = true;
413
+ if (kind !== "named") {
414
+ unnamedCount++;
415
+ if (unnamedCount === 3) {
416
+ context.report({
417
+ node,
418
+ messageId: "unnamedConditions",
419
+ data: { count: unnamedCount }
420
+ });
421
+ return;
422
+ }
423
+ }
424
+ }
425
+ if (hasMixedOperators && unnamedCount > 0) context.report({
426
+ node,
427
+ messageId: "mixedOperators"
428
+ });
429
+ else if (hasInlineCallback && leafCount >= 2) context.report({
430
+ node,
431
+ messageId: "inlineCallback"
432
+ });
433
+ } };
434
+ }
435
+ };
436
+ function isCondition(node) {
437
+ return node.type === "LogicalExpression" && node.operator !== "??";
438
+ }
439
+ function isNamedCondition(node) {
440
+ let current = node;
441
+ let parent = current.parent;
442
+ while (parent?.type === "UnaryExpression" && parent.operator === "!") {
443
+ current = outerExpression(parent, "condition");
444
+ parent = current.parent;
445
+ }
446
+ if (parent?.type === "VariableDeclarator") return parent.init === current;
447
+ if (parent?.type === "ArrowFunctionExpression") return parent.body === current && isNamedFunction(parent);
448
+ if (parent?.type === "ReturnStatement") {
449
+ const block = parent.parent;
450
+ return block.type === "BlockStatement" && block.body.length === 1 && isNamedFunction(block.parent);
451
+ }
452
+ return false;
453
+ }
454
+ function isNamedFunction(node) {
455
+ if (node.type === "FunctionDeclaration") return node.id !== null;
456
+ if (node.type !== "ArrowFunctionExpression" && node.type !== "FunctionExpression") return false;
457
+ const { parent } = node;
458
+ switch (parent.type) {
459
+ case "VariableDeclarator": return parent.init === node;
460
+ case "Property":
461
+ case "PropertyDefinition":
462
+ case "MethodDefinition": return parent.value === node && !parent.computed;
463
+ default: return false;
464
+ }
465
+ }
466
+ function rendersJsx(node) {
467
+ const current = withoutChain(node);
468
+ if (current.type === "JSXElement" || current.type === "JSXFragment") return true;
469
+ if (current.type !== "CallExpression") return false;
470
+ const { arguments: args, callee } = current;
471
+ if (callee.type !== "MemberExpression" || callee.computed) return false;
472
+ const { property } = callee;
473
+ if (property.type !== "Identifier" || property.name !== "map") return false;
474
+ const callback = args[0] && unwrapExpression(args[0]);
475
+ if (callback?.type !== "ArrowFunctionExpression" && callback?.type !== "FunctionExpression") return false;
476
+ let body = callback.body;
477
+ if (body?.type === "BlockStatement") {
478
+ const statement = body.body[0];
479
+ if (body.body.length !== 1 || statement?.type !== "ReturnStatement") return false;
480
+ body = statement.argument;
481
+ }
482
+ if (!body) return false;
483
+ const result = unwrapExpression(body);
484
+ return result.type === "JSXElement" || result.type === "JSXFragment";
485
+ }
486
+ function leafKind(node) {
487
+ let current = withoutChain(node);
488
+ while (current.type === "UnaryExpression" && current.operator === "!") current = withoutChain(current.argument);
489
+ switch (current.type) {
490
+ case "JSXElement":
491
+ case "JSXFragment": return "jsx";
492
+ case "Identifier":
493
+ case "Literal": return "named";
494
+ case "CallExpression":
495
+ for (const argument of current.arguments) {
496
+ const value = unwrapExpression(argument);
497
+ if (value.type === "ArrowFunctionExpression" || value.type === "FunctionExpression") return "inlineCallback";
498
+ }
499
+ return "named";
500
+ case "MemberExpression":
501
+ while (current.type === "MemberExpression") {
502
+ if (current.computed) return "unnamed";
503
+ current = withoutChain(current.object);
504
+ }
505
+ return "named";
506
+ default: return "unnamed";
507
+ }
508
+ }
509
+ function withoutChain(node) {
510
+ const current = unwrapExpression(node, "condition");
511
+ if (current.type === "ChainExpression") return unwrapExpression(current.expression, "condition");
512
+ return current;
513
+ }
514
+ // src/rules/require-effect-reason.ts
515
+ function isEffectHook(name) {
516
+ return name === "useEffect" || name === "useLayoutEffect" || name === "useInsertionEffect";
517
+ }
518
+ /** True when the comment's first nonblank line reads `effect: <reason>`, ignoring `*` decoration. */
519
+ function isReasonComment(value) {
520
+ const [first] = value.split("\n").map((line) => line.trim().replace(/^\*+\s*/u, "")).filter(Boolean);
521
+ return first !== void 0 && /^effect:\s*\S/u.test(first);
522
+ }
523
+ const requireEffectReason = {
524
+ meta: {
525
+ type: "problem",
526
+ docs: {
527
+ description: "Require a reason comment for React Effects",
528
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/require-effect-reason.md"
529
+ },
530
+ messages: { missingReason: "Effects synchronize a component with a system outside React. Compute derived values during render and run event logic in the handler. If this Effect synchronizes with an external system, say which one in a comment directly above it: // effect: <what it keeps in sync and why>." },
531
+ schema: []
532
+ },
533
+ createOnce(context) {
534
+ let effectAliases;
535
+ return {
536
+ Program(node) {
537
+ effectAliases = void 0;
538
+ for (const statement of node.body) {
539
+ if (statement.type !== "ImportDeclaration" || statement.source.value !== "react") continue;
540
+ for (const specifier of statement.specifiers) {
541
+ if (specifier.type !== "ImportSpecifier") continue;
542
+ const name = importSpecifierName(specifier);
543
+ if (name !== specifier.local.name && isEffectHook(name)) (effectAliases ??= /* @__PURE__ */ new Set()).add(specifier.local.name);
544
+ }
545
+ }
546
+ },
547
+ CallExpression(node) {
548
+ const { callee } = node;
549
+ if (callee.type === "Identifier") {
550
+ if (!isEffectHook(callee.name) && effectAliases?.has(callee.name) !== true) return;
551
+ } else {
552
+ if (callee.type !== "MemberExpression" || callee.computed) return;
553
+ if (callee.property.type !== "Identifier" || !isEffectHook(callee.property.name)) return;
554
+ }
555
+ if (!isEffectHook(importedName(context, callee, "react"))) return;
556
+ const targets = [node];
557
+ let statement = node;
558
+ while (statement.parent) {
559
+ const parent = statement.parent;
560
+ if (parent.type === "BlockStatement" || parent.type === "Program") break;
561
+ statement = parent;
562
+ }
563
+ if (statement !== node) targets.push(statement);
564
+ if (targets.some((target) => context.sourceCode.getCommentsBefore(target).some((comment) => isReasonComment(comment.value)))) return;
565
+ context.report({
566
+ node: callee,
567
+ messageId: "missingReason"
568
+ });
569
+ }
570
+ };
571
+ }
572
+ };
573
+ // src/rules/require-invoke-on-error.ts
574
+ function propertyName(property) {
575
+ if (property.computed) return null;
576
+ if (property.key.type === "Identifier") return property.key.name;
577
+ if (property.key.type === "Literal" && typeof property.key.value === "string") return property.key.value;
578
+ return null;
579
+ }
580
+ function machineCall(invoke) {
581
+ let config = invoke.parent;
582
+ while (config?.type === "ObjectExpression") {
583
+ const expression = outerExpression(config);
584
+ const parent = expression.parent;
585
+ if (parent?.type === "CallExpression" && parent.arguments[0] === expression) return parent;
586
+ if (parent?.type !== "Property" || parent.value !== expression) return null;
587
+ const stateMap = parent.parent;
588
+ if (stateMap?.type !== "ObjectExpression") return null;
589
+ const mapExpression = outerExpression(stateMap);
590
+ const states = mapExpression.parent;
591
+ if (states?.type !== "Property") return null;
592
+ if (states.value !== mapExpression || propertyName(states) !== "states") return null;
593
+ config = states.parent;
594
+ }
595
+ return null;
596
+ }
597
+ // src/index.ts
598
+ const plugin = eslintCompatPlugin({
599
+ meta: { name: "@sonsu-lee/oxlint-plugin" },
600
+ rules: {
601
+ "no-double-assertion": noDoubleAssertion,
602
+ "no-machine-context-mutation": noMachineContextMutation,
603
+ "no-machine-in-render": noMachineInRender,
604
+ "no-unstable-actor-selector": noUnstableActorSelector,
605
+ "prefer-machine-setup": preferMachineSetup,
606
+ "require-invoke-on-error": {
607
+ meta: {
608
+ type: "suggestion",
609
+ docs: {
610
+ description: "Require a failure transition when an XState invoke handles success",
611
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/require-invoke-on-error.md"
612
+ },
613
+ messages: { missingOnError: "Add an onError transition so the machine can leave this state if the invoked actor fails." },
614
+ schema: []
615
+ },
616
+ createOnce(context) {
617
+ let importsXState = false;
618
+ function isMachineFactory(callee) {
619
+ if (importedName(context, callee, "xstate") === "createMachine") return true;
620
+ if (callee.type !== "MemberExpression" || callee.computed) return false;
621
+ if (callee.property.type !== "Identifier" || callee.property.name !== "createMachine") return false;
622
+ return findSetupCall(context, callee.object) !== null;
623
+ }
624
+ function check(entry, invoke) {
625
+ const node = unwrapExpression(entry);
626
+ if (node.type !== "ObjectExpression") return;
627
+ let hasSrc = false;
628
+ let hasOnDone = false;
629
+ for (const property of node.properties) {
630
+ if (property.type !== "Property") return;
631
+ const name = propertyName(property);
632
+ if (name === "onError") return;
633
+ if (name === "onDone") hasOnDone = true;
634
+ if (name === "src") hasSrc = true;
635
+ }
636
+ if (!hasSrc || !hasOnDone) return;
637
+ const call = machineCall(invoke);
638
+ if (call && isMachineFactory(call.callee)) context.report({
639
+ node,
640
+ messageId: "missingOnError"
641
+ });
642
+ }
643
+ function hasXStateImport(node) {
644
+ return node.body.some((statement) => statement.type === "ImportDeclaration" && statement.source.value === "xstate");
645
+ }
646
+ return {
647
+ before() {
648
+ return hasXStateImport(context.sourceCode.ast);
649
+ },
650
+ Program(node) {
651
+ importsXState = hasXStateImport(node);
652
+ },
653
+ Property(node) {
654
+ if (!importsXState || propertyName(node) !== "invoke") return;
655
+ const value = unwrapExpression(node.value);
656
+ if (value.type !== "ArrayExpression") {
657
+ check(value, node);
658
+ return;
659
+ }
660
+ for (const element of value.elements) if (element) check(element, node);
661
+ }
662
+ };
663
+ }
664
+ },
665
+ "prefer-named-conditions": preferNamedConditions,
666
+ "require-effect-reason": requireEffectReason
667
+ }
668
+ });
669
+ export { plugin as default };
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "@sonsu-lee/oxlint-plugin",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Custom lint rules for Oxlint, also usable from ESLint",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/sonsu-lee/style-guide.git",
9
+ "directory": "packages/oxlint-plugin"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "type": "module",
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.mts",
18
+ "default": "./dist/index.mjs"
19
+ }
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "dependencies": {
25
+ "@oxlint/plugins": "^1.90.0"
26
+ },
27
+ "devDependencies": {
28
+ "oxlint": "1.90.0",
29
+ "tsdown": "0.23.4",
30
+ "typescript": "7.0.2",
31
+ "vitest": "5.0.5"
32
+ },
33
+ "preferUnplugged": true,
34
+ "scripts": {
35
+ "build": "tsdown",
36
+ "test": "vitest run",
37
+ "typecheck": "tsc",
38
+ "lint": "oxlint"
39
+ }
6
40
  }