@sonsu-lee/eslint-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.
package/README.md CHANGED
@@ -22,7 +22,11 @@ export default [
22
22
  files: ['**/*.{ts,tsx,mts,cts}'],
23
23
  languageOptions: { parser, parserOptions: { projectService: true } },
24
24
  plugins: { 'sonsu-lee-eslint': plugin },
25
- rules: { 'sonsu-lee-eslint/no-ignored-rejection': 'error' },
25
+ rules: {
26
+ 'sonsu-lee-eslint/no-ignored-rejection': 'error',
27
+ 'sonsu-lee-eslint/no-query-rest-destructuring': 'error',
28
+ 'sonsu-lee-eslint/no-void-query-fn': 'error',
29
+ },
26
30
  },
27
31
  ];
28
32
  ```
@@ -32,3 +36,9 @@ export default [
32
36
  | Rule | Reports |
33
37
  | --- | --- |
34
38
  | [`sonsu-lee-eslint/no-ignored-rejection`](docs/rules/no-ignored-rejection.md) | Empty rejection handlers and discarded `undefined` or `null` fallbacks. |
39
+ | [`sonsu-lee-eslint/no-query-rest-destructuring`](docs/rules/no-query-rest-destructuring.md) | Object rest destructuring of a TanStack Query result. |
40
+ | [`sonsu-lee-eslint/no-void-query-fn`](docs/rules/no-void-query-fn.md) | TanStack Query functions that resolve with `void` or `undefined`. |
41
+
42
+ ## Related
43
+
44
+ See the [architecture guide](../../ARCHITECTURE.md) for how the packages fit together.
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { AST_NODE_TYPES, ESLintUtils } from "@typescript-eslint/utils";
2
2
  import { isIntrinsicAnyType, isIntrinsicUnknownType, isThenableType } from "ts-api-utils";
3
3
  // package.json
4
- var version = "0.1.0";
4
+ var version = "0.3.0";
5
5
  // src/rules/no-ignored-rejection.ts
6
6
  function rejectionHandler(name, args) {
7
7
  let argument;
@@ -97,12 +97,104 @@ const noIgnoredRejection = ESLintUtils.RuleCreator((name) => `https://github.com
97
97
  } };
98
98
  }
99
99
  });
100
+ // src/rules/no-query-rest-destructuring.ts
101
+ const tanstackPackage = /@tanstack[+/](?:query-core|react-query)(?:[@/]|$)/u;
102
+ const queryResultName = /^(?:Defined)?(?:Use(?:Suspense)?(?:Infinite)?QueryResult|(?:Infinite)?QueryObserver(?:Success|Loading|LoadingError|RefetchError|Error|Pending)?Result)$/u;
103
+ const noQueryRestDestructuring = ESLintUtils.RuleCreator((name) => `https://github.com/sonsu-lee/style-guide/blob/main/packages/eslint-plugin/docs/rules/${name}.md`)({
104
+ name: "no-query-rest-destructuring",
105
+ meta: {
106
+ type: "problem",
107
+ docs: { description: "Report rest destructuring a TanStack Query result." },
108
+ schema: [],
109
+ messages: { restDestructuring: "Rest destructuring a query result reads every field. Destructure only the fields this component uses." }
110
+ },
111
+ create(context) {
112
+ const services = ESLintUtils.getParserServices(context);
113
+ services.program.getTypeChecker();
114
+ function isTanStackSymbol(symbol) {
115
+ if (symbol === void 0) return false;
116
+ return (symbol.declarations ?? []).some((declaration) => tanstackPackage.test(declaration.getSourceFile().fileName));
117
+ }
118
+ function isQueryResultSymbol(symbol) {
119
+ return symbol !== void 0 && isTanStackSymbol(symbol) && queryResultName.test(symbol.name);
120
+ }
121
+ function isQueryResultType(type) {
122
+ if (isQueryResultSymbol(type.aliasSymbol) || isQueryResultSymbol(type.symbol)) return true;
123
+ if (type.isUnion()) return type.types.some((member) => isQueryResultType(member));
124
+ if (type.aliasSymbol?.getName() === "Omit" && type.aliasTypeArguments?.[0] !== void 0) return isQueryResultType(type.aliasTypeArguments[0]);
125
+ return false;
126
+ }
127
+ return { VariableDeclarator(node) {
128
+ if (node.id.type !== AST_NODE_TYPES.ObjectPattern) return;
129
+ if (!node.id.properties.some((property) => property.type === AST_NODE_TYPES.RestElement)) return;
130
+ if (!node.init) return;
131
+ const type = services.getTypeAtLocation(node.init);
132
+ if (type.getProperty("data") === void 0) return;
133
+ if (!isQueryResultType(type)) return;
134
+ context.report({
135
+ node,
136
+ messageId: "restDestructuring"
137
+ });
138
+ } };
139
+ }
140
+ });
141
+ // src/rules/no-void-query-fn.ts
142
+ function isQueryFnProperty(node) {
143
+ if (node.key.type === AST_NODE_TYPES.Identifier) return !node.computed && node.key.name === "queryFn";
144
+ return node.key.type === AST_NODE_TYPES.Literal && node.key.value === "queryFn";
145
+ }
146
+ function returnTypes(type) {
147
+ if (type.isUnion()) return type.types.flatMap((member) => returnTypes(member));
148
+ const signature = type.getCallSignatures().at(0);
149
+ if (signature === void 0) return [];
150
+ return [signature.getReturnType()];
151
+ }
152
+ function isIllegalReturn(checker, type) {
153
+ const awaited = checker.getAwaitedType(type);
154
+ if (!awaited) return false;
155
+ if (awaited.isUnion()) return awaited.types.some((member) => isIllegalReturn(checker, member));
156
+ const name = checker.typeToString(awaited);
157
+ return name === "void" || name === "undefined";
158
+ }
159
+ const noVoidQueryFn = ESLintUtils.RuleCreator((name) => `https://github.com/sonsu-lee/style-guide/blob/main/packages/eslint-plugin/docs/rules/${name}.md`)({
160
+ name: "no-void-query-fn",
161
+ meta: {
162
+ type: "problem",
163
+ docs: { description: "Report query functions that resolve without a value." },
164
+ schema: [],
165
+ messages: { voidReturn: "This query function resolves without a value, so the cache stores `undefined`. Return the fetched data or throw." }
166
+ },
167
+ create(context) {
168
+ let tanstackQuery = false;
169
+ const services = ESLintUtils.getParserServices(context);
170
+ const checker = services.program.getTypeChecker();
171
+ return {
172
+ Program(node) {
173
+ tanstackQuery = node.body.some((statement) => statement.type === AST_NODE_TYPES.ImportDeclaration && statement.specifiers.length > 0 && statement.source.value.startsWith("@tanstack/") && statement.source.value.endsWith("-query"));
174
+ },
175
+ Property(node) {
176
+ if (!tanstackQuery || node.parent.type !== AST_NODE_TYPES.ObjectExpression) return;
177
+ if (!isQueryFnProperty(node)) return;
178
+ if (node.value.type === AST_NODE_TYPES.AssignmentPattern) return;
179
+ if (!returnTypes(services.getTypeAtLocation(node.value)).some((type) => isIllegalReturn(checker, type))) return;
180
+ context.report({
181
+ node: node.value,
182
+ messageId: "voidReturn"
183
+ });
184
+ }
185
+ };
186
+ }
187
+ });
100
188
  // src/index.ts
101
189
  const plugin = {
102
190
  meta: {
103
191
  name: "@sonsu-lee/eslint-plugin",
104
192
  version
105
193
  },
106
- rules: { "no-ignored-rejection": noIgnoredRejection }
194
+ rules: {
195
+ "no-ignored-rejection": noIgnoredRejection,
196
+ "no-query-rest-destructuring": noQueryRestDestructuring,
197
+ "no-void-query-fn": noVoidQueryFn
198
+ }
107
199
  };
108
200
  export { plugin as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sonsu-lee/eslint-plugin",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Type-aware lint rules for ESLint",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -27,8 +27,8 @@
27
27
  "ts-api-utils": "2.5.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@sonsu-lee/oxlint-config": "^0.1.0",
31
- "@types/node": "24.19.2",
30
+ "@sonsu-lee/oxlint-config": "^0.3.0",
31
+ "@types/node": "26.6.11",
32
32
  "@typescript-eslint/parser": "8.74.0",
33
33
  "@typescript-eslint/rule-tester": "8.74.0",
34
34
  "eslint": "10.15.0",