@sonsu-lee/oxlint-plugin 0.1.0 → 0.3.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.
Files changed (3) hide show
  1. package/README.md +36 -0
  2. package/dist/index.mjs +687 -7
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -28,6 +28,32 @@ Register the plugin with `name: "sonsu-lee"`. Without an alias, Oxlint derives t
28
28
 
29
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
30
 
31
+ To use the plugin with ESLint, install it with ESLint and the TypeScript parser:
32
+
33
+ ```sh
34
+ pnpm add -D eslint typescript@6 @typescript-eslint/parser @sonsu-lee/oxlint-plugin
35
+ ```
36
+
37
+ Install TypeScript 6 rather than 7: the typescript-eslint parser does not support TypeScript 7.
38
+
39
+ In `eslint.config.mjs`, register the plugin for JavaScript and TypeScript files, including JSX:
40
+
41
+ ```js
42
+ import plugin from '@sonsu-lee/oxlint-plugin';
43
+ import parser from '@typescript-eslint/parser';
44
+
45
+ export default [
46
+ {
47
+ files: ['**/*.{js,jsx,mjs,cjs,ts,tsx,mts,cts}'],
48
+ languageOptions: { parser, parserOptions: { ecmaFeatures: { jsx: true } } },
49
+ plugins: { 'sonsu-lee': plugin },
50
+ rules: { 'sonsu-lee/prefer-named-conditions': 'warn' },
51
+ },
52
+ ];
53
+ ```
54
+
55
+ These rules read only the AST, so `projectService` is not needed.
56
+
31
57
  ## Rules
32
58
 
33
59
  | Rule | Description |
@@ -37,6 +63,12 @@ With Yarn PnP, Oxlint cannot resolve a plugin by package name. Pass the resolved
37
63
  | [require-effect-reason](docs/rules/require-effect-reason.md) | Require a synchronization reason on React Effect calls |
38
64
  | [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
65
  | [no-machine-in-render](docs/rules/no-machine-in-render.md) | Report machine or actor creation directly during render |
66
+ | [no-query-data-mutation](docs/rules/no-query-data-mutation.md) | Report writes to data returned by a TanStack Query hook |
67
+ | [no-unchecked-fetch](docs/rules/no-unchecked-fetch.md) | Report reading a fetch response body without checking its status |
68
+ | [no-query-import-alias](docs/rules/no-query-import-alias.md) | Disallow aliasing, namespace-importing, or re-exporting TanStack Query APIs |
69
+ | [no-router-import-alias](docs/rules/no-router-import-alias.md) | Disallow aliasing, namespace-importing, or re-exporting TanStack Router APIs |
70
+ | [no-action-only-route](docs/rules/no-action-only-route.md) | Disallow a route module that exports only an action |
71
+ | [no-misnamed-route-export](docs/rules/no-misnamed-route-export.md) | Report a route export that is a near miss for an export React Router reads |
40
72
  | [no-unstable-actor-selector](docs/rules/no-unstable-actor-selector.md) | Review object or array selectors without a comparator |
41
73
  | [prefer-machine-setup](docs/rules/prefer-machine-setup.md) | Review bare `createMachine` calls |
42
74
  | [require-invoke-on-error](docs/rules/require-invoke-on-error.md) | Review invokes with a success transition but no failure transition |
@@ -45,6 +77,10 @@ With Yarn PnP, Oxlint cannot resolve a plugin by package name. Pass the resolved
45
77
 
46
78
  See [CONTRIBUTING.md](../../CONTRIBUTING.md).
47
79
 
80
+ ## Related
81
+
82
+ See the [architecture guide](../../ARCHITECTURE.md) for how the packages fit together.
83
+
48
84
  ## License
49
85
 
50
86
  [MIT](LICENSE) © Sonsu Lee
package/dist/index.mjs CHANGED
@@ -1,4 +1,89 @@
1
1
  import { eslintCompatPlugin } from "@oxlint/plugins";
2
+ // src/utils/router-exports.ts
3
+ function exportedName(node) {
4
+ if ("name" in node) return node.name;
5
+ return node.value;
6
+ }
7
+ function addBindingNames(pattern, node, exports) {
8
+ switch (pattern.type) {
9
+ case "Identifier":
10
+ exports.set(pattern.name, node);
11
+ return;
12
+ case "ObjectPattern":
13
+ for (const property of pattern.properties) addBindingNames(property.type === "Property" ? property.value : property.argument, node, exports);
14
+ return;
15
+ case "ArrayPattern":
16
+ for (const element of pattern.elements) if (element) addBindingNames(element, node, exports);
17
+ return;
18
+ case "AssignmentPattern":
19
+ addBindingNames(pattern.left, node, exports);
20
+ return;
21
+ case "RestElement": addBindingNames(pattern.argument, node, exports);
22
+ }
23
+ }
24
+ /** Collects the value exports a React Router route module reads, with their declaration nodes. */
25
+ function collectRouteExports(program) {
26
+ const exports = /* @__PURE__ */ new Map();
27
+ let hasDefault = false;
28
+ let hasStarExport = false;
29
+ for (const statement of program.body) {
30
+ if (statement.type === "ExportDefaultDeclaration") {
31
+ hasDefault = true;
32
+ continue;
33
+ }
34
+ if (statement.type === "ExportAllDeclaration") {
35
+ if (statement.exportKind === "type" || statement.exported) continue;
36
+ hasStarExport = true;
37
+ continue;
38
+ }
39
+ if (statement.type !== "ExportNamedDeclaration" || statement.exportKind === "type") continue;
40
+ const declaration = statement.declaration;
41
+ if (declaration) {
42
+ if (declaration.type === "FunctionDeclaration" && declaration.id) exports.set(declaration.id.name, declaration);
43
+ else if (declaration.type === "VariableDeclaration") for (const declarator of declaration.declarations) addBindingNames(declarator.id, declarator, exports);
44
+ continue;
45
+ }
46
+ for (const specifier of statement.specifiers) {
47
+ if (specifier.type !== "ExportSpecifier" || specifier.exportKind === "type") continue;
48
+ const name = exportedName(specifier.exported);
49
+ if (name === "default") {
50
+ hasDefault = true;
51
+ continue;
52
+ }
53
+ exports.set(name, specifier);
54
+ }
55
+ }
56
+ return {
57
+ exports,
58
+ hasDefault,
59
+ hasStarExport
60
+ };
61
+ }
62
+ // src/rules/no-action-only-route.ts
63
+ const noActionOnlyRoute = {
64
+ meta: {
65
+ type: "problem",
66
+ docs: {
67
+ description: "Disallow a route module that exports only an action",
68
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/no-action-only-route.md"
69
+ },
70
+ messages: { actionOnly: "Add a loader, clientLoader, or default component. A route with only an action cannot answer a GET request, so a refresh or history navigation fails." },
71
+ schema: []
72
+ },
73
+ createOnce(context) {
74
+ return { Program(program) {
75
+ const { exports, hasDefault, hasStarExport } = collectRouteExports(program);
76
+ const mutates = exports.get("action") ?? exports.get("clientAction");
77
+ if (!mutates) return;
78
+ if (hasStarExport) return;
79
+ if (hasDefault || exports.has("loader") || exports.has("clientLoader")) return;
80
+ context.report({
81
+ node: mutates,
82
+ messageId: "actionOnly"
83
+ });
84
+ } };
85
+ }
86
+ };
2
87
  // src/rules/no-double-assertion.ts
3
88
  const noDoubleAssertion = {
4
89
  meta: {
@@ -12,10 +97,10 @@ const noDoubleAssertion = {
12
97
  },
13
98
  createOnce(context) {
14
99
  function check(node) {
15
- if (isTypeAssertion(node.parent)) return;
100
+ if (isTypeAssertion$1(node.parent)) return;
16
101
  let assertionCount = 0;
17
102
  let current = node;
18
- while (isTypeAssertion(current)) {
103
+ while (isTypeAssertion$1(current)) {
19
104
  if (!isConstAssertion(current)) assertionCount++;
20
105
  current = current.expression;
21
106
  }
@@ -30,7 +115,7 @@ const noDoubleAssertion = {
30
115
  };
31
116
  }
32
117
  };
33
- function isTypeAssertion(node) {
118
+ function isTypeAssertion$1(node) {
34
119
  return node.type === "TSAsExpression" || node.type === "TSTypeAssertion";
35
120
  }
36
121
  function isConstAssertion(node) {
@@ -64,6 +149,22 @@ function importSpecifierName({ imported }) {
64
149
  if (imported.type === "Identifier") return imported.name;
65
150
  return imported.value;
66
151
  }
152
+ function isTypeAssertion(node) {
153
+ return node.type === "TSAsExpression" || node.type === "TSSatisfiesExpression" || node.type === "TSNonNullExpression" || node.type === "TSTypeAssertion";
154
+ }
155
+ /** Returns the source of an unaliased named value import that this expression refers to. */
156
+ function localImportSource(context, node) {
157
+ let identifier = node;
158
+ while (isTypeAssertion(identifier)) identifier = identifier.expression;
159
+ if (identifier.type !== "Identifier") return null;
160
+ const def = context.sourceCode.getScope(identifier).set.get(identifier.name)?.defs[0];
161
+ if (def?.type !== "ImportBinding" || def.node.type !== "ImportSpecifier") return null;
162
+ const declaration = def.parent;
163
+ if (declaration?.type !== "ImportDeclaration") return null;
164
+ if (declaration.importKind === "type" || def.node.importKind === "type") return null;
165
+ if (importSpecifierName(def.node) !== identifier.name) return null;
166
+ return declaration.source.value;
167
+ }
67
168
  /** Resolves direct named imports and noncomputed members of default or namespace imports. */
68
169
  function importedName(context, callee, source) {
69
170
  let binding;
@@ -92,7 +193,7 @@ function importedName(context, callee, source) {
92
193
  return null;
93
194
  }
94
195
  // src/rules/no-machine-context-mutation.ts
95
- const mutators = {
196
+ const mutators$1 = {
96
197
  push: true,
97
198
  pop: true,
98
199
  shift: true,
@@ -107,6 +208,17 @@ const mutators = {
107
208
  delete: true,
108
209
  clear: true
109
210
  };
211
+ function mutatorName$1(member) {
212
+ if (!member.computed) {
213
+ if (member.property.type === "Identifier") return member.property.name;
214
+ return null;
215
+ }
216
+ if (member.property.type === "Literal" && typeof member.property.value === "string") return member.property.value;
217
+ return null;
218
+ }
219
+ function isObjectAssign$1(callee) {
220
+ return callee.type === "MemberExpression" && !callee.computed && callee.object.type === "Identifier" && callee.object.name === "Object" && callee.property.type === "Identifier" && callee.property.name === "assign";
221
+ }
110
222
  const noMachineContextMutation = {
111
223
  meta: {
112
224
  type: "problem",
@@ -172,7 +284,17 @@ const noMachineContextMutation = {
172
284
  CallExpression(node) {
173
285
  if (!importsXState) return;
174
286
  const { callee } = node;
175
- if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && Object.hasOwn(mutators, callee.property.name)) check(node, callee.object);
287
+ if (callee.type === "MemberExpression") {
288
+ const name = mutatorName$1(callee);
289
+ if (name !== null && Object.hasOwn(mutators$1, name)) {
290
+ check(node, callee.object);
291
+ return;
292
+ }
293
+ }
294
+ if (isObjectAssign$1(callee)) {
295
+ const [target] = node.arguments;
296
+ if (target) check(node, target);
297
+ }
176
298
  }
177
299
  };
178
300
  }
@@ -275,10 +397,562 @@ const noMachineInRender = {
275
397
  };
276
398
  }
277
399
  };
400
+ // src/rules/no-misnamed-route-export.ts
401
+ const ROUTE_EXPORTS = [
402
+ "loader",
403
+ "clientLoader",
404
+ "action",
405
+ "clientAction",
406
+ "ErrorBoundary",
407
+ "HydrateFallback",
408
+ "meta",
409
+ "links",
410
+ "headers",
411
+ "shouldRevalidate",
412
+ "handle",
413
+ "middleware",
414
+ "clientMiddleware"
415
+ ];
416
+ const ROUTE_EXPORT_SET = new Set(ROUTE_EXPORTS);
417
+ function withinOneEdit(left, right) {
418
+ if (left === right) return false;
419
+ if (Math.abs(left.length - right.length) > 1) return false;
420
+ let index = 0;
421
+ while (index < left.length && index < right.length && left[index] === right[index]) index++;
422
+ if (left.length === right.length) return left.slice(index + 1) === right.slice(index + 1);
423
+ const longer = left.length > right.length ? left : right;
424
+ const shorter = left.length > right.length ? right : left;
425
+ return longer.slice(index + 1) === shorter.slice(index);
426
+ }
427
+ function expectedExport(name) {
428
+ const lower = name.toLowerCase();
429
+ const sameName = ROUTE_EXPORTS.find((known) => known.toLowerCase() === lower);
430
+ if (sameName) return sameName;
431
+ if (name.length < 5) return void 0;
432
+ return ROUTE_EXPORTS.find((known) => withinOneEdit(lower, known.toLowerCase()));
433
+ }
434
+ const noMisnamedRouteExport = {
435
+ meta: {
436
+ type: "problem",
437
+ docs: {
438
+ description: "Report a route export that is a near miss for an export React Router reads",
439
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/no-misnamed-route-export.md"
440
+ },
441
+ messages: { misnamed: "'{{name}}' looks like a typo of the route export '{{expected}}'. React Router ignores an unknown export, so this code never runs." },
442
+ schema: [{
443
+ type: "object",
444
+ properties: { allow: {
445
+ type: "array",
446
+ items: { type: "string" }
447
+ } },
448
+ additionalProperties: false
449
+ }]
450
+ },
451
+ createOnce(context) {
452
+ return { Program(program) {
453
+ const allow = new Set((context.options?.[0])?.allow ?? []);
454
+ const { exports } = collectRouteExports(program);
455
+ for (const [name, node] of exports) {
456
+ if (ROUTE_EXPORT_SET.has(name) || allow.has(name)) continue;
457
+ const expected = expectedExport(name);
458
+ if (expected) context.report({
459
+ node,
460
+ messageId: "misnamed",
461
+ data: {
462
+ name,
463
+ expected
464
+ }
465
+ });
466
+ }
467
+ } };
468
+ }
469
+ };
470
+ // src/rules/no-query-data-mutation.ts
471
+ const querySources = [
472
+ "@tanstack/react-query",
473
+ "@tanstack/solid-query",
474
+ "@tanstack/vue-query",
475
+ "@tanstack/svelte-query",
476
+ "@tanstack/angular-query-experimental"
477
+ ];
478
+ const queryHooks = {
479
+ useQuery: true,
480
+ useSuspenseQuery: true,
481
+ useInfiniteQuery: true,
482
+ useSuspenseInfiniteQuery: true
483
+ };
484
+ const mutators = {
485
+ push: true,
486
+ pop: true,
487
+ shift: true,
488
+ unshift: true,
489
+ splice: true,
490
+ sort: true,
491
+ reverse: true,
492
+ fill: true,
493
+ copyWithin: true,
494
+ set: true,
495
+ add: true,
496
+ delete: true,
497
+ clear: true
498
+ };
499
+ function unwrap$1(node) {
500
+ let current = unwrapExpression(node);
501
+ while (current.type === "ChainExpression") current = unwrapExpression(current.expression);
502
+ return current;
503
+ }
504
+ function isDataProperty(member) {
505
+ if (member.computed) return member.property.type === "Literal" && member.property.value === "data";
506
+ return member.property.type === "Identifier" && member.property.name === "data";
507
+ }
508
+ function mutatorName(member) {
509
+ if (!member.computed) {
510
+ if (member.property.type === "Identifier") return member.property.name;
511
+ return null;
512
+ }
513
+ if (member.property.type === "Literal" && typeof member.property.value === "string") return member.property.value;
514
+ return null;
515
+ }
516
+ function isObjectAssign(callee) {
517
+ return callee.type === "MemberExpression" && !callee.computed && callee.object.type === "Identifier" && callee.object.name === "Object" && callee.property.type === "Identifier" && callee.property.name === "assign";
518
+ }
519
+ function queryHookName(context, callee) {
520
+ for (const source of querySources) {
521
+ const name = importedName(context, callee, source);
522
+ if (name !== null && Object.hasOwn(queryHooks, name)) return name;
523
+ }
524
+ return null;
525
+ }
526
+ const noQueryDataMutation = {
527
+ meta: {
528
+ type: "problem",
529
+ docs: {
530
+ description: "Disallow mutating data returned by a TanStack Query hook",
531
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/no-query-data-mutation.md"
532
+ },
533
+ messages: { dataMutation: "Do not mutate query data. Cached objects are shared; create a new value and update the cache with setQueryData, or refetch." },
534
+ schema: []
535
+ },
536
+ createOnce(context) {
537
+ const kinds = /* @__PURE__ */ new Map();
538
+ const mutations = [];
539
+ function variableOf(identifier) {
540
+ let scope = context.sourceCode.getScope(identifier);
541
+ while (scope) {
542
+ const binding = scope.set.get(identifier.name);
543
+ if (binding) return binding;
544
+ scope = scope.upper;
545
+ }
546
+ return null;
547
+ }
548
+ function mark(pattern, kind) {
549
+ if (pattern.type === "Identifier") {
550
+ const binding = variableOf(pattern);
551
+ if (binding) kinds.set(binding, kind);
552
+ return;
553
+ }
554
+ if (pattern.type === "AssignmentPattern") {
555
+ mark(pattern.left, kind);
556
+ return;
557
+ }
558
+ if (pattern.type === "RestElement") {
559
+ mark(pattern.argument, kind);
560
+ return;
561
+ }
562
+ if (pattern.type === "ArrayPattern") {
563
+ for (const element of pattern.elements) if (element) mark(element, kind);
564
+ return;
565
+ }
566
+ if (pattern.type !== "ObjectPattern") return;
567
+ for (const property of pattern.properties) {
568
+ if (property.type === "RestElement") {
569
+ mark(property.argument, kind);
570
+ continue;
571
+ }
572
+ if (property.type !== "Property" || property.computed) continue;
573
+ if (kind === "data") {
574
+ mark(property.value, "data");
575
+ continue;
576
+ }
577
+ if (property.key.type === "Identifier" && property.key.name === "data" || property.key.type === "Literal" && property.key.value === "data") mark(property.value, "data");
578
+ }
579
+ }
580
+ function isQueryResult(node) {
581
+ const expression = unwrap$1(node);
582
+ if (expression.type === "Identifier") {
583
+ const binding = variableOf(expression);
584
+ return binding !== null && kinds.get(binding) === "result";
585
+ }
586
+ return expression.type === "CallExpression" && queryHookName(context, expression.callee) !== null;
587
+ }
588
+ function isQueryData(node) {
589
+ const expression = unwrap$1(node);
590
+ if (expression.type === "Identifier") {
591
+ const binding = variableOf(expression);
592
+ return binding !== null && kinds.get(binding) === "data";
593
+ }
594
+ if (expression.type !== "MemberExpression") return false;
595
+ if (isDataProperty(expression) && isQueryResult(expression.object)) return true;
596
+ return isQueryData(expression.object);
597
+ }
598
+ function forget(target) {
599
+ const expression = unwrap$1(target);
600
+ if (expression.type !== "Identifier") return;
601
+ const binding = variableOf(expression);
602
+ if (binding) kinds.delete(binding);
603
+ }
604
+ function check(node, target, memberOnly = false) {
605
+ if (memberOnly && unwrap$1(target).type !== "MemberExpression") return;
606
+ if (isQueryData(target)) {
607
+ context.report({
608
+ node,
609
+ messageId: "dataMutation"
610
+ });
611
+ return;
612
+ }
613
+ mutations.push({
614
+ node,
615
+ target
616
+ });
617
+ }
618
+ return {
619
+ Program() {
620
+ kinds.clear();
621
+ mutations.length = 0;
622
+ },
623
+ VariableDeclarator(node) {
624
+ if (!node.init) return;
625
+ const value = unwrap$1(node.init);
626
+ if (isQueryResult(value)) mark(node.id, "result");
627
+ else if (isQueryData(value)) mark(node.id, "data");
628
+ },
629
+ AssignmentExpression(node) {
630
+ forget(node.left);
631
+ check(node, node.left, true);
632
+ },
633
+ UpdateExpression(node) {
634
+ forget(node.argument);
635
+ check(node, node.argument, true);
636
+ },
637
+ UnaryExpression(node) {
638
+ if (node.operator !== "delete") return;
639
+ forget(node.argument);
640
+ check(node, node.argument, true);
641
+ },
642
+ CallExpression(node) {
643
+ const callee = unwrap$1(node.callee);
644
+ if (callee.type === "CallExpression") return;
645
+ if (callee.type === "MemberExpression") {
646
+ const name = mutatorName(callee);
647
+ if (name !== null && Object.hasOwn(mutators, name)) {
648
+ check(node, callee.object);
649
+ return;
650
+ }
651
+ }
652
+ if (isObjectAssign(callee)) {
653
+ const [target] = node.arguments;
654
+ if (target) check(node, target);
655
+ }
656
+ },
657
+ "Program:exit"() {
658
+ for (const { node, target } of mutations) if (isQueryData(target)) context.report({
659
+ node,
660
+ messageId: "dataMutation"
661
+ });
662
+ }
663
+ };
664
+ }
665
+ };
666
+ // src/rules/no-query-import-alias.ts
667
+ const TANSTACK_QUERY = "@tanstack/react-query";
668
+ const noQueryImportAlias = {
669
+ meta: {
670
+ type: "problem",
671
+ docs: {
672
+ description: "Disallow aliasing or re-exporting TanStack Query APIs",
673
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/no-query-import-alias.md"
674
+ },
675
+ messages: {
676
+ alias: "Import {{name}} under its own name. An alias hides the call from the Query lint rules.",
677
+ namespace: "Import TanStack Query APIs directly. A namespace hides the calls from the Query lint rules.",
678
+ reexport: "Do not re-export TanStack Query APIs. Import them from @tanstack/react-query where they are used."
679
+ },
680
+ schema: []
681
+ },
682
+ createOnce(context) {
683
+ function sourceName(source) {
684
+ return source?.value ?? null;
685
+ }
686
+ function isQueryPackage(source) {
687
+ if (source === null) return false;
688
+ return source === TANSTACK_QUERY || source.startsWith(`${TANSTACK_QUERY}/`);
689
+ }
690
+ return {
691
+ ImportDeclaration(node) {
692
+ if (node.importKind === "type" || !isQueryPackage(sourceName(node.source))) return;
693
+ for (const specifier of node.specifiers) {
694
+ if (specifier.type === "ImportNamespaceSpecifier") {
695
+ context.report({
696
+ node: specifier,
697
+ messageId: "namespace"
698
+ });
699
+ continue;
700
+ }
701
+ if (specifier.type !== "ImportSpecifier") continue;
702
+ if (specifier.importKind === "type") continue;
703
+ const name = importSpecifierName(specifier);
704
+ if (name === specifier.local.name) continue;
705
+ context.report({
706
+ node: specifier,
707
+ messageId: "alias",
708
+ data: { name }
709
+ });
710
+ }
711
+ },
712
+ ExportAllDeclaration(node) {
713
+ if (node.exportKind === "type" || !isQueryPackage(sourceName(node.source))) return;
714
+ context.report({
715
+ node,
716
+ messageId: "reexport"
717
+ });
718
+ },
719
+ ExportDefaultDeclaration(node) {
720
+ if (isQueryPackage(localImportSource(context, node.declaration))) context.report({
721
+ node,
722
+ messageId: "reexport"
723
+ });
724
+ },
725
+ ExportNamedDeclaration(node) {
726
+ if (node.exportKind === "type") return;
727
+ if (node.source === null) {
728
+ if (node.specifiers.some((specifier) => specifier.exportKind !== "type" && isQueryPackage(localImportSource(context, specifier.local)))) context.report({
729
+ node,
730
+ messageId: "reexport"
731
+ });
732
+ return;
733
+ }
734
+ if (!isQueryPackage(sourceName(node.source))) return;
735
+ if (!node.specifiers.some((specifier) => specifier.type !== "ExportSpecifier" || specifier.exportKind !== "type")) return;
736
+ context.report({
737
+ node,
738
+ messageId: "reexport"
739
+ });
740
+ }
741
+ };
742
+ }
743
+ };
744
+ // src/rules/no-router-import-alias.ts
745
+ const ROUTER_PACKAGE = /^@tanstack\/[a-z0-9-]+-router$/;
746
+ const noRouterImportAlias = {
747
+ meta: {
748
+ type: "problem",
749
+ docs: {
750
+ description: "Disallow aliasing, namespace-importing, or re-exporting TanStack Router APIs",
751
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/no-router-import-alias.md"
752
+ },
753
+ messages: {
754
+ alias: "Import {{name}} under its own name. An alias hides the call from the Router lint rules.",
755
+ namespace: "Import TanStack Router APIs directly. A namespace hides the calls from the Router lint rules.",
756
+ reexport: "Do not re-export TanStack Router APIs. Import them where they are used."
757
+ },
758
+ schema: []
759
+ },
760
+ createOnce(context) {
761
+ function sourceName(source) {
762
+ return source?.value ?? null;
763
+ }
764
+ function isRouterPackage(source) {
765
+ if (source === null) return false;
766
+ return ROUTER_PACKAGE.test(source);
767
+ }
768
+ return {
769
+ ImportDeclaration(node) {
770
+ if (node.importKind === "type" || !isRouterPackage(sourceName(node.source))) return;
771
+ for (const specifier of node.specifiers) {
772
+ if (specifier.type === "ImportNamespaceSpecifier") {
773
+ context.report({
774
+ node: specifier,
775
+ messageId: "namespace"
776
+ });
777
+ continue;
778
+ }
779
+ if (specifier.type !== "ImportSpecifier") continue;
780
+ if (specifier.importKind === "type") continue;
781
+ const name = importSpecifierName(specifier);
782
+ if (name === specifier.local.name) continue;
783
+ context.report({
784
+ node: specifier,
785
+ messageId: "alias",
786
+ data: { name }
787
+ });
788
+ }
789
+ },
790
+ ExportAllDeclaration(node) {
791
+ if (node.exportKind === "type") return;
792
+ if (isRouterPackage(sourceName(node.source))) context.report({
793
+ node,
794
+ messageId: "reexport"
795
+ });
796
+ },
797
+ ExportDefaultDeclaration(node) {
798
+ if (isRouterPackage(localImportSource(context, node.declaration))) context.report({
799
+ node,
800
+ messageId: "reexport"
801
+ });
802
+ },
803
+ ExportNamedDeclaration(node) {
804
+ if (node.exportKind === "type" || node.declaration) return;
805
+ if (node.source === null) {
806
+ if (node.specifiers.some((specifier) => specifier.exportKind !== "type" && isRouterPackage(localImportSource(context, specifier.local)))) context.report({
807
+ node,
808
+ messageId: "reexport"
809
+ });
810
+ return;
811
+ }
812
+ if (!isRouterPackage(sourceName(node.source))) return;
813
+ if (node.specifiers.some((specifier) => specifier.type !== "ExportSpecifier" || specifier.exportKind !== "type")) context.report({
814
+ node,
815
+ messageId: "reexport"
816
+ });
817
+ }
818
+ };
819
+ }
820
+ };
821
+ // src/rules/no-unchecked-fetch.ts
822
+ const functionBoundaries = {
823
+ ArrowFunctionExpression: true,
824
+ FunctionExpression: true,
825
+ FunctionDeclaration: true
826
+ };
827
+ const bodyReaders = {
828
+ json: true,
829
+ text: true,
830
+ arrayBuffer: true,
831
+ blob: true,
832
+ formData: true
833
+ };
834
+ function unwrap(node) {
835
+ let current = node;
836
+ for (let next = unwrapExpression(current); next !== current; next = unwrapExpression(current)) current = next;
837
+ if (current.type === "AwaitExpression") return unwrap(current.argument);
838
+ if (current.type === "ChainExpression") return unwrap(current.expression);
839
+ return current;
840
+ }
841
+ const noUncheckedFetch = {
842
+ meta: {
843
+ type: "problem",
844
+ docs: {
845
+ description: "Report reading a fetch response body without checking its status",
846
+ url: "https://github.com/sonsu-lee/style-guide/blob/main/packages/oxlint-plugin/docs/rules/no-unchecked-fetch.md"
847
+ },
848
+ messages: { uncheckedFetch: "This fetch response is read without checking ok or status. An error response can still resolve, so check response.ok before consuming the body." },
849
+ schema: []
850
+ },
851
+ createOnce(context) {
852
+ const unchecked = /* @__PURE__ */ new Set();
853
+ const testStack = [];
854
+ /** The scope that declares the identifier, or null when it is unresolved. */
855
+ function scopeOf(identifier) {
856
+ let scope = context.sourceCode.getScope(identifier);
857
+ while (scope && !scope.set.has(identifier.name)) scope = scope.upper;
858
+ return scope;
859
+ }
860
+ function variableOf(identifier) {
861
+ return scopeOf(identifier)?.set.get(identifier.name) ?? null;
862
+ }
863
+ /** True for an unconditional assignment beside the declaration that introduced the binding. */
864
+ function isDirectReassignment(node, binding) {
865
+ const definition = binding.defs[0];
866
+ if (definition?.type !== "Variable" || definition.node.type !== "VariableDeclarator") return false;
867
+ const declaration = definition.node.parent;
868
+ if (declaration?.type !== "VariableDeclaration") return false;
869
+ const container = declaration.parent;
870
+ if (container?.type !== "BlockStatement" && container?.type !== "Program") return false;
871
+ return node.parent?.type === "ExpressionStatement" && node.parent.parent === container;
872
+ }
873
+ function hasLocalDefinition(identifier) {
874
+ const binding = variableOf(identifier);
875
+ if (binding === null) return false;
876
+ return binding.defs.some((definition) => definition.type !== "ImplicitGlobalVariable");
877
+ }
878
+ function isFetchCall(node) {
879
+ const expression = unwrap(node);
880
+ if (expression.type !== "CallExpression") return false;
881
+ const { callee } = expression;
882
+ if (callee.type !== "Identifier" || callee.name !== "fetch") return false;
883
+ return !hasLocalDefinition(callee);
884
+ }
885
+ function isInsideTest(node) {
886
+ const test = testStack.at(-1);
887
+ if (!test) return false;
888
+ let current = node;
889
+ while (current) {
890
+ if (Object.hasOwn(functionBoundaries, current.type)) return false;
891
+ if (current === test) return true;
892
+ current = current.parent;
893
+ }
894
+ return false;
895
+ }
896
+ /** The static property name, including a string-literal computed key. */
897
+ function staticPropertyName(member) {
898
+ if (!member.computed) return member.property.type === "Identifier" ? member.property.name : null;
899
+ return member.property.type === "Literal" && typeof member.property.value === "string" ? member.property.value : null;
900
+ }
901
+ function isCheckedProperty(member) {
902
+ const name = staticPropertyName(member);
903
+ return name === "ok" || name === "status";
904
+ }
905
+ function markStatusCheck(member) {
906
+ if (!isInsideTest(member) || !isCheckedProperty(member)) return;
907
+ const receiver = unwrap(member.object);
908
+ if (receiver.type !== "Identifier") return;
909
+ const binding = variableOf(receiver);
910
+ if (binding && unchecked.has(binding)) unchecked.delete(binding);
911
+ }
912
+ return {
913
+ Program() {
914
+ unchecked.clear();
915
+ testStack.length = 0;
916
+ },
917
+ VariableDeclarator(node) {
918
+ if (!node.init || node.id.type !== "Identifier" || !isFetchCall(node.init)) return;
919
+ const binding = variableOf(node.id);
920
+ if (binding) unchecked.add(binding);
921
+ },
922
+ "AssignmentExpression:exit"(node) {
923
+ if (node.operator !== "=" || node.left.type !== "Identifier") return;
924
+ const binding = scopeOf(node.left)?.set.get(node.left.name);
925
+ if (!binding || !isDirectReassignment(node, binding)) return;
926
+ if (isFetchCall(node.right)) unchecked.add(binding);
927
+ },
928
+ IfStatement(node) {
929
+ testStack.push(node.test);
930
+ },
931
+ "IfStatement:exit"() {
932
+ testStack.pop();
933
+ },
934
+ MemberExpression(node) {
935
+ markStatusCheck(node);
936
+ },
937
+ CallExpression(node) {
938
+ const callee = unwrap(node.callee);
939
+ if (callee.type !== "MemberExpression") return;
940
+ const name = staticPropertyName(callee);
941
+ if (name === null || !Object.hasOwn(bodyReaders, name)) return;
942
+ const receiver = unwrap(callee.object);
943
+ if (receiver.type !== "Identifier") return;
944
+ const binding = variableOf(receiver);
945
+ if (binding && unchecked.has(binding)) context.report({
946
+ node,
947
+ messageId: "uncheckedFetch"
948
+ });
949
+ }
950
+ };
951
+ }
952
+ };
278
953
  // src/rules/no-unstable-actor-selector.ts
279
954
  function createsCollection(node) {
280
- let current = node;
281
- while (current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression") current = current.expression;
955
+ const current = unwrapExpression(node);
282
956
  switch (current.type) {
283
957
  case "ObjectExpression":
284
958
  case "ArrayExpression": return true;
@@ -598,9 +1272,15 @@ function machineCall(invoke) {
598
1272
  const plugin = eslintCompatPlugin({
599
1273
  meta: { name: "@sonsu-lee/oxlint-plugin" },
600
1274
  rules: {
1275
+ "no-action-only-route": noActionOnlyRoute,
601
1276
  "no-double-assertion": noDoubleAssertion,
602
1277
  "no-machine-context-mutation": noMachineContextMutation,
1278
+ "no-misnamed-route-export": noMisnamedRouteExport,
1279
+ "no-query-data-mutation": noQueryDataMutation,
1280
+ "no-query-import-alias": noQueryImportAlias,
1281
+ "no-router-import-alias": noRouterImportAlias,
603
1282
  "no-machine-in-render": noMachineInRender,
1283
+ "no-unchecked-fetch": noUncheckedFetch,
604
1284
  "no-unstable-actor-selector": noUnstableActorSelector,
605
1285
  "prefer-machine-setup": preferMachineSetup,
606
1286
  "require-invoke-on-error": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sonsu-lee/oxlint-plugin",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Custom lint rules for Oxlint, also usable from ESLint",
5
5
  "license": "MIT",
6
6
  "repository": {