@sshkeda/check 1.1.0 → 2.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.
Files changed (102) hide show
  1. package/README.md +7 -6
  2. package/docs/how-to-make-check-pass.md +4 -14
  3. package/package.json +4 -4
  4. package/src/internal/ban-abbreviated-words-in-declared-names.ts +0 -4
  5. package/src/internal/ban-checked-members-outside-calls.ts +8 -8
  6. package/src/internal/ban-comments.ts +0 -1
  7. package/src/internal/ban-comparing-or-switching-on-tag.ts +50 -10
  8. package/src/internal/ban-deadlines-inside-tests.ts +3 -4
  9. package/src/internal/ban-declared-values.ts +2 -5
  10. package/src/internal/ban-dropped-failures-detached-fibers-sleeps-and-untraced-functions.ts +0 -2
  11. package/src/internal/ban-failures-collected-as-results.ts +0 -4
  12. package/src/internal/ban-fallbacks-of-named-recoveries.ts +1 -29
  13. package/src/internal/ban-function-overloads.ts +0 -4
  14. package/src/internal/ban-global-error-in-failure-types.ts +10 -15
  15. package/src/internal/ban-instanceof-in-effect-recovery-callbacks.ts +17 -26
  16. package/src/internal/ban-let-and-effect-mutable-ref.ts +0 -8
  17. package/src/internal/ban-logic-in-index-files.ts +0 -3
  18. package/src/internal/ban-namespaces-used-without-a-member.ts +0 -3
  19. package/src/internal/ban-node-builtins-that-effect-replaces.ts +0 -3
  20. package/src/internal/ban-parsing-outside-data-without-a-schema.ts +0 -4
  21. package/src/internal/ban-re-exports.ts +54 -18
  22. package/src/internal/ban-reading-the-clock-and-sleeping-outside-effect.ts +88 -4
  23. package/src/internal/ban-recovering-file-system-exists.ts +300 -0
  24. package/src/internal/ban-retired-words-in-names.ts +1 -4
  25. package/src/internal/ban-retries-without-a-wait.ts +292 -0
  26. package/src/internal/ban-scanning-directories-with-bun-glob.ts +0 -3
  27. package/src/internal/ban-shebang-lines.ts +0 -4
  28. package/src/internal/ban-timer-globals-and-the-global-object.ts +0 -1
  29. package/src/internal/ban-timer-schedules-without-deadlines.ts +129 -45
  30. package/src/internal/ban-type-and-interface-declarations.ts +0 -4
  31. package/src/internal/ban-type-predicates.ts +0 -4
  32. package/src/internal/ban-unknown-in-function-signatures.ts +49 -13
  33. package/src/internal/ban-vague-error-class-names.ts +0 -4
  34. package/src/internal/ban-vague-file-names.ts +3 -7
  35. package/src/internal/ban-what-a-file-is-not-granted.ts +37 -14
  36. package/src/internal/ban-while-do-while-and-for-loops.ts +0 -4
  37. package/src/internal/ban-writing-captured-objects-inside-effect-callbacks.ts +210 -0
  38. package/src/internal/confine-service-declarations-to-granted-files.ts +0 -7
  39. package/src/internal/confine-the-stdio-service-to-terminal-output.ts +2 -16
  40. package/src/internal/define-the-lint-plugin-and-configs.ts +6 -0
  41. package/src/internal/keep-assertions-in-the-test-home-and-ban-test-runners.ts +0 -6
  42. package/src/internal/keep-effect-testing-helpers-in-test-files.ts +2 -14
  43. package/src/internal/require-a-named-const-for-compound-conditions.ts +0 -4
  44. package/src/internal/require-capital-snake-case-and-as-const-on-literal-constants.ts +0 -4
  45. package/src/internal/require-service-keys-to-end-with-their-class-name.ts +5 -2
  46. package/src/internal/resolve-what-an-expression-imports.ts +115 -28
  47. package/src/internal/visit-every-declared-name.ts +13 -0
  48. package/src/lib/tasks/internal/declare-the-check-task.ts +2 -0
  49. package/src/lib/tasks/internal/declare-the-checker-queries-task.ts +2 -1
  50. package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/index.ts +2 -1
  51. package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/internal/check-effect-diagnostics-with-effect-tsgo.ts +17 -0
  52. package/src/lib/tasks/lib/check-import-boundaries/internal/decide-what-each-home-may-import.ts +62 -0
  53. package/src/lib/tasks/lib/check-import-boundaries/internal/find-import-boundary-violations.ts +215 -187
  54. package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/find-layer-provides-outside-program-starts.ts +13 -16
  55. package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/layer-provide-errors.ts +2 -1
  56. package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/resolve-imported-package-apis.ts +3 -1
  57. package/src/lib/tasks/lib/detect-duplicate-functions/index.ts +10 -0
  58. package/src/lib/tasks/lib/detect-duplicate-functions/internal/duplicate-function-errors.ts +23 -0
  59. package/src/lib/tasks/lib/detect-duplicate-functions/internal/find-duplicate-functions.ts +150 -0
  60. package/src/lib/tasks/lib/detect-duplicate-functions/internal/list-named-functions.ts +416 -0
  61. package/src/lib/tasks/lib/detect-inferrable-annotations/internal/find-inferrable-annotations.ts +31 -12
  62. package/src/lib/tasks/lib/enforce-file-layout/index.ts +6 -1
  63. package/src/lib/tasks/lib/enforce-file-layout/internal/file-layout-errors.ts +11 -1
  64. package/src/lib/tasks/lib/enforce-file-layout/internal/find-export-path-mismatches.ts +39 -7
  65. package/src/lib/tasks/lib/enforce-file-layout/internal/find-file-layout-violations.ts +11 -4
  66. package/src/lib/tasks/lib/enforce-file-layout/internal/find-module-layout-violations.ts +6 -4
  67. package/src/lib/tasks/lib/enforce-file-types/index.ts +6 -1
  68. package/src/lib/tasks/lib/format-with-bun-fmt/internal/declare-the-formatting-task.ts +3 -2
  69. package/src/lib/tasks/lib/judge-index-re-exports/internal/find-index-re-export-violations.ts +27 -21
  70. package/src/lib/tasks/lib/lint-with-oxlint/index.ts +2 -1
  71. package/src/lib/tasks/lib/lint-with-oxlint/internal/check-lint-rules-with-oxlint.ts +11 -8
  72. package/src/lib/tasks/lib/lint-with-oxlint/internal/read-oxlint-json-report.ts +18 -2
  73. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/export-placement-errors.ts +12 -1
  74. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-export-placement-violations.ts +75 -21
  75. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-package-entry-sources.ts +146 -0
  76. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-the-importer-graph.ts +24 -190
  77. package/src/lib/tasks/lib/refuse-disable-directives/index.ts +2 -1
  78. package/src/lib/tasks/lib/refuse-disable-directives/internal/find-disable-directives.ts +9 -3
  79. package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/internal/find-error-mappings-to-done.ts +108 -9
  80. package/src/lib/tasks/lib/require-separate-service-layers/internal/find-service-layer-violations.ts +1 -5
  81. package/src/lib/tasks/lib/require-type-checking-every-linted-file/index.ts +2 -1
  82. package/src/lib/tasks/lib/require-type-checking-every-linted-file/internal/find-linted-files-not-type-checked.ts +2 -4
  83. package/src/lib/tasks/shared/check-each-nested-package.ts +13 -1
  84. package/src/lib/tasks/shared/classify-files-by-role.ts +12 -2
  85. package/src/lib/tasks/shared/count-the-line-of-an-offset.ts +20 -2
  86. package/src/lib/tasks/shared/create-the-module-resolver.ts +39 -23
  87. package/src/lib/tasks/shared/declare-lint-config-task.ts +2 -1
  88. package/src/lib/tasks/shared/declare-typescript-config-task.ts +30 -2
  89. package/src/lib/tasks/shared/find-unmet-typescript-config-requirements.ts +19 -6
  90. package/src/lib/tasks/shared/list-ignored-files-imported-by-source.ts +25 -16
  91. package/src/lib/tasks/shared/load-the-lint-config.ts +14 -12
  92. package/src/lib/tasks/shared/parse-config-files-that-import-from-check.ts +6 -7
  93. package/src/lib/tasks/shared/project-config-and-tool-errors.ts +46 -2
  94. package/src/lib/tasks/shared/provide-project-query-caches.ts +11 -0
  95. package/src/lib/tasks/shared/query-the-typescript-project.ts +108 -43
  96. package/src/lib/tasks/shared/resolve-absolute-paths.ts +4 -1
  97. package/src/lib/tasks/shared/resolve-module-requests.ts +131 -90
  98. package/src/lib/tasks/shared/run-tools-and-decode-their-reports.ts +15 -11
  99. package/src/shared/bans-each-permission-lifts.ts +4 -21
  100. package/src/shared/find-retired-words.ts +1 -1
  101. package/src/shared/lint-rule-messages-and-patterns.ts +7 -12
  102. package/src/shared/record-and-grant-permissions-by-path.ts +41 -65
@@ -0,0 +1,292 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import type { ESTree, SourceCode } from "@oxlint/plugins";
3
+
4
+ import {
5
+ constantValue,
6
+ effectModuleOperation,
7
+ effectModuleReference,
8
+ isGlobalReference,
9
+ memberName,
10
+ propertyKeyName,
11
+ resolvedVariable,
12
+ returnedExpression,
13
+ unwrappedExpression,
14
+ } from "./resolve-what-an-expression-imports.ts";
15
+
16
+ const RETRYING_MODULES = ["Effect", "Stream"] as const;
17
+ const RETRY = "retry";
18
+ const RETRY_WITH_A_FALLBACK = "retryOrElse";
19
+ const SCHEDULE_OPTION = "schedule";
20
+ const COUNT_ONLY_SCHEDULE = "Schedule.recurs";
21
+ const LAST = -1;
22
+ const BEFORE_THE_FALLBACK = -2;
23
+
24
+ const PREDICATE_OPTIONS: ReadonlySet<string> = new Set(["while", "until"]);
25
+
26
+ const ARRAY_PREDICATES: ReadonlySet<string> = new Set(["every", "some", "includes"]);
27
+
28
+ const STRING_PREDICATES: ReadonlySet<string> = new Set(["includes", "startsWith", "endsWith"]);
29
+
30
+ const NUMBER_PREDICATES: ReadonlySet<string> = new Set([
31
+ "isFinite",
32
+ "isInteger",
33
+ "isNaN",
34
+ "isSafeInteger",
35
+ ]);
36
+
37
+ const NON_EFFECT_VALUES: ReadonlySet<string> = new Set([
38
+ "Literal",
39
+ "ArrayExpression",
40
+ "ObjectExpression",
41
+ "TemplateLiteral",
42
+ "UnaryExpression",
43
+ "BinaryExpression",
44
+ "UpdateExpression",
45
+ "ArrowFunctionExpression",
46
+ "FunctionExpression",
47
+ "FunctionDeclaration",
48
+ "ClassExpression",
49
+ "ClassDeclaration",
50
+ ]);
51
+
52
+ const isBooleanType = (annotation: ESTree.Node | undefined): boolean => {
53
+ if (annotation?.type === "TSParenthesizedType") {
54
+ return isBooleanType(annotation.typeAnnotation);
55
+ }
56
+ if (annotation?.type === "TSUnionType") {
57
+ return annotation.types.every(isBooleanType);
58
+ }
59
+ const isBooleanLiteral =
60
+ annotation?.type === "TSLiteralType" &&
61
+ annotation.literal.type === "Literal" &&
62
+ typeof annotation.literal.value === "boolean";
63
+ return annotation?.type === "TSBooleanKeyword" || isBooleanLiteral;
64
+ };
65
+
66
+ const hasBooleanReturnType = (node: ESTree.Node) =>
67
+ "returnType" in node && isBooleanType(node.returnType?.typeAnnotation);
68
+
69
+ const isBooleanCall = (callee: ESTree.Node, sourceCode: SourceCode) => {
70
+ const value = constantValue(callee, sourceCode);
71
+ const isBooleanConversion = isGlobalReference(value, sourceCode, "Boolean");
72
+ const isEffectPredicate = effectModuleOperation(value, sourceCode, "Effect") === "isEffect";
73
+ const isKnownBooleanCall =
74
+ isBooleanConversion || isEffectPredicate || hasBooleanReturnType(value);
75
+ if (isKnownBooleanCall) {
76
+ return true;
77
+ }
78
+ if (value.type !== "MemberExpression") {
79
+ return false;
80
+ }
81
+ const receiver = constantValue(value.object, sourceCode);
82
+ const method = memberName(value) ?? "";
83
+ if (receiver.type === "ArrayExpression") {
84
+ return ARRAY_PREDICATES.has(method);
85
+ }
86
+ const isString =
87
+ receiver.type === "TemplateLiteral" ||
88
+ (receiver.type === "Literal" && typeof receiver.value === "string");
89
+ if (isString) {
90
+ return STRING_PREDICATES.has(method);
91
+ }
92
+ const isRegexLiteral = receiver.type === "Literal" && "regex" in receiver;
93
+ if (isRegexLiteral) {
94
+ return method === "test";
95
+ }
96
+ if (receiver.type === "NewExpression") {
97
+ const isNativeCollection = ["Map", "Set"].some((name) =>
98
+ isGlobalReference(receiver.callee, sourceCode, name),
99
+ );
100
+ return isNativeCollection && (method === "has" || method === "delete");
101
+ }
102
+ const isArrayPredicate = method === "isArray" && isGlobalReference(receiver, sourceCode, "Array");
103
+ const isNumberPredicate =
104
+ NUMBER_PREDICATES.has(method) && isGlobalReference(receiver, sourceCode, "Number");
105
+ const isObjectPredicate = method === "is" && isGlobalReference(receiver, sourceCode, "Object");
106
+ return isArrayPredicate || isNumberPredicate || isObjectPredicate;
107
+ };
108
+
109
+ const returnedValuesIn = (node: ESTree.Node): readonly ESTree.Node[] => {
110
+ if (node.type === "ReturnStatement") {
111
+ return node.argument === null ? [] : [node.argument];
112
+ }
113
+ if (node.type === "BlockStatement") {
114
+ return node.body.flatMap(returnedValuesIn);
115
+ }
116
+ if (node.type === "IfStatement") {
117
+ return [
118
+ ...returnedValuesIn(node.consequent),
119
+ ...(node.alternate === null ? [] : returnedValuesIn(node.alternate)),
120
+ ];
121
+ }
122
+ if (node.type === "SwitchStatement") {
123
+ return node.cases.flatMap((branch) => branch.consequent.flatMap(returnedValuesIn));
124
+ }
125
+ if (node.type === "TryStatement") {
126
+ return [
127
+ ...returnedValuesIn(node.block),
128
+ ...(node.handler === null ? [] : returnedValuesIn(node.handler.body)),
129
+ ...(node.finalizer === null ? [] : returnedValuesIn(node.finalizer)),
130
+ ];
131
+ }
132
+ const isLoopOrLabel =
133
+ node.type === "ForStatement" ||
134
+ node.type === "ForInStatement" ||
135
+ node.type === "ForOfStatement" ||
136
+ node.type === "WhileStatement" ||
137
+ node.type === "DoWhileStatement" ||
138
+ node.type === "LabeledStatement" ||
139
+ node.type === "WithStatement";
140
+ return isLoopOrLabel ? returnedValuesIn(node.body) : [];
141
+ };
142
+
143
+ const returnedValuesOf = (callback: ESTree.Node): readonly ESTree.Node[] | undefined => {
144
+ const returned = returnedExpression(callback);
145
+ if (returned !== undefined) {
146
+ return [returned];
147
+ }
148
+ const isFunction =
149
+ callback.type === "ArrowFunctionExpression" ||
150
+ callback.type === "FunctionExpression" ||
151
+ callback.type === "FunctionDeclaration";
152
+ const body = isFunction ? callback.body : null;
153
+ return body?.type === "BlockStatement" ? returnedValuesIn(body) : undefined;
154
+ };
155
+
156
+ const mayReturnAnEffect = (
157
+ node: ESTree.Node,
158
+ sourceCode: SourceCode,
159
+ seen: ReadonlySet<ESTree.Node> = new Set(),
160
+ ): boolean => {
161
+ const value = constantValue(node, sourceCode);
162
+ if (seen.has(value)) {
163
+ return true;
164
+ }
165
+ const visited = new Set([...seen, value]);
166
+ if (value.type === "CallExpression") {
167
+ if (isBooleanCall(value.callee, sourceCode)) {
168
+ return false;
169
+ }
170
+ const returned = returnedValuesOf(constantValue(value.callee, sourceCode));
171
+ return (
172
+ returned === undefined ||
173
+ returned.some((result) => mayReturnAnEffect(result, sourceCode, visited))
174
+ );
175
+ }
176
+ if (value.type === "ChainExpression") {
177
+ return mayReturnAnEffect(value.expression, sourceCode, visited);
178
+ }
179
+ if (value.type === "SequenceExpression") {
180
+ const last = value.expressions.at(LAST);
181
+ return last !== undefined && mayReturnAnEffect(last, sourceCode, visited);
182
+ }
183
+ if (value.type === "AssignmentExpression") {
184
+ const isLogicalAssignment =
185
+ value.operator === "&&=" || value.operator === "||=" || value.operator === "??=";
186
+ const mayAssignAnEffect =
187
+ (value.operator === "=" || isLogicalAssignment) &&
188
+ mayReturnAnEffect(value.right, sourceCode, visited);
189
+ return (
190
+ mayAssignAnEffect ||
191
+ (isLogicalAssignment && mayReturnAnEffect(value.left, sourceCode, visited))
192
+ );
193
+ }
194
+ if (value.type === "ConditionalExpression") {
195
+ return (
196
+ mayReturnAnEffect(value.consequent, sourceCode, visited) ||
197
+ mayReturnAnEffect(value.alternate, sourceCode, visited)
198
+ );
199
+ }
200
+ if (value.type === "LogicalExpression") {
201
+ return (
202
+ mayReturnAnEffect(value.left, sourceCode, visited) ||
203
+ mayReturnAnEffect(value.right, sourceCode, visited)
204
+ );
205
+ }
206
+ if (value.type === "Identifier") {
207
+ const binding = resolvedVariable(value, sourceCode)?.defs[0]?.name;
208
+ const isBooleanBinding =
209
+ binding?.type === "Identifier" && isBooleanType(binding.typeAnnotation?.typeAnnotation);
210
+ return !isBooleanBinding && !isGlobalReference(value, sourceCode, "undefined");
211
+ }
212
+ return !NON_EFFECT_VALUES.has(value.type);
213
+ };
214
+
215
+ const predicateMayWait = (predicate: ESTree.Node, sourceCode: SourceCode) => {
216
+ const callback = constantValue(predicate, sourceCode);
217
+ if (hasBooleanReturnType(callback)) {
218
+ return false;
219
+ }
220
+ return (
221
+ returnedValuesOf(callback)?.some((returned) => mayReturnAnEffect(returned, sourceCode)) ?? false
222
+ );
223
+ };
224
+
225
+ export const noInstantRetries = defineRule({
226
+ meta: {
227
+ type: "problem",
228
+ messages: {
229
+ instant: "{{usage}} retries at once, with no wait between attempts",
230
+ },
231
+ },
232
+ create(context) {
233
+ const retryOf = (callee: ESTree.Node) =>
234
+ RETRYING_MODULES.flatMap((moduleName) => {
235
+ const operation = effectModuleOperation(callee, context.sourceCode, moduleName);
236
+ const isRetry = operation === RETRY || operation === RETRY_WITH_A_FALLBACK;
237
+ return isRetry ? [{ usage: `${moduleName}.${operation}`, operation }] : [];
238
+ }).at(0);
239
+
240
+ const isCountOnlySchedule = (node: ESTree.Node) =>
241
+ node.type === "CallExpression" &&
242
+ node.arguments.length === 1 &&
243
+ effectModuleReference(node.callee, context.sourceCode, "Schedule") === COUNT_ONLY_SCHEDULE;
244
+
245
+ const waitsNever = (schedule: ESTree.Node) => {
246
+ const value = unwrappedExpression(schedule);
247
+ return (
248
+ isGlobalReference(value, context.sourceCode, "undefined") || isCountOnlySchedule(value)
249
+ );
250
+ };
251
+
252
+ const hasNoSchedule = (options: ESTree.ObjectExpression) => {
253
+ const names = options.properties.map((property) => propertyKeyName(property));
254
+ const isEveryOptionNamed = names.every((name) => name !== undefined);
255
+ if (!isEveryOptionNamed) {
256
+ return false;
257
+ }
258
+ const hasWaitingPredicate = options.properties.some(
259
+ (property, index) =>
260
+ property.type === "Property" &&
261
+ PREDICATE_OPTIONS.has(names[index] ?? "") &&
262
+ predicateMayWait(property.value, context.sourceCode),
263
+ );
264
+ if (hasWaitingPredicate) {
265
+ return false;
266
+ }
267
+ const schedule = options.properties.find(
268
+ (property, index) => names[index] === SCHEDULE_OPTION && property.type === "Property",
269
+ );
270
+ return schedule?.type !== "Property" || waitsNever(schedule.value);
271
+ };
272
+
273
+ const isInstantPolicy = (policy: ESTree.Node) => {
274
+ const value = unwrappedExpression(policy);
275
+ return value.type === "ObjectExpression" ? hasNoSchedule(value) : isCountOnlySchedule(value);
276
+ };
277
+
278
+ return {
279
+ CallExpression(node: ESTree.CallExpression) {
280
+ const retry = retryOf(node.callee);
281
+ if (retry === undefined) {
282
+ return;
283
+ }
284
+ const policy = node.arguments.at(retry.operation === RETRY ? LAST : BEFORE_THE_FALLBACK);
285
+ const retriesAtOnce = policy !== undefined && isInstantPolicy(policy);
286
+ if (retriesAtOnce) {
287
+ context.report({ node: policy, messageId: "instant", data: { usage: retry.usage } });
288
+ }
289
+ },
290
+ };
291
+ },
292
+ });
@@ -55,9 +55,6 @@ const isGlobInstance = (
55
55
  export const noGlobScan = defineRule({
56
56
  meta: {
57
57
  type: "problem",
58
- docs: {
59
- description: "Allow Bun.Glob for matching strings but not for scanning directories.",
60
- },
61
58
  messages: {
62
59
  scan: "Bun.Glob {{method}} scans directories",
63
60
  },
@@ -3,10 +3,6 @@ import { defineRule } from "@oxlint/plugins";
3
3
  export const noShebang = defineRule({
4
4
  meta: {
5
5
  type: "suggestion",
6
- docs: {
7
- description:
8
- "Start source files with code instead of a shebang, so the runtime and its flags come from the caller or package.json.",
9
- },
10
6
  messages: {
11
7
  shebang: "file starts with a shebang line",
12
8
  },
@@ -13,7 +13,6 @@ export const globalRule = (ruleName: keyof typeof GLOBAL_RULES) => {
13
13
  return defineRule({
14
14
  meta: {
15
15
  type: "problem",
16
- docs: { description: `Refuse the globals ${[...restrictedGlobals].join(", ")}.` },
17
16
  messages: { global: "global {{name}} is used" },
18
17
  },
19
18
  create(context) {
@@ -7,7 +7,7 @@ import { decodeRuleOptions, ruleOptionsJsonSchema } from "./describe-and-decode-
7
7
  import {
8
8
  effectModuleReference,
9
9
  effectOperation,
10
- importedExpression,
10
+ isPipeFunction,
11
11
  } from "./resolve-what-an-expression-imports.ts";
12
12
 
13
13
  const TWO_ARGUMENTS = 2;
@@ -25,8 +25,30 @@ const FALLBACK_OPERATIONS = new Set(["retryOrElse", "repeatOrElse"]);
25
25
  export const DEADLINE_OPERATIONS = new Set(["timeout", "timeoutOption", "timeoutOrElse"]);
26
26
  const COUNT_ONLY_SCHEDULE = "Schedule.recurs";
27
27
  const OUTPUT_MAPPING = "Schedule.map";
28
- const SCHEDULE_A_GRANT_ALLOWS = "Schedule.spaced";
28
+ const EXPONENTIAL_SCHEDULE = "Schedule.exponential";
29
+ const JITTERED_SCHEDULE = "Schedule.jittered";
30
+ const SCHEDULES_A_GRANT_ALLOWS = new Set(["Schedule.spaced", EXPONENTIAL_SCHEDULE]);
29
31
  const PIPE_METHOD = "pipe";
32
+ const SCHEDULE_PREFIX = "Schedule.";
33
+ const UNTIMED_SCHEDULE_MEMBERS = new Set([
34
+ "concat",
35
+ "concatResult",
36
+ "during",
37
+ "identity",
38
+ "map",
39
+ "max",
40
+ "min",
41
+ "passthrough",
42
+ "recurs",
43
+ "setInputType",
44
+ "tap",
45
+ "upTo",
46
+ "while",
47
+ ]);
48
+
49
+ const isUntimed = (usage: string) =>
50
+ usage.startsWith(SCHEDULE_PREFIX) &&
51
+ UNTIMED_SCHEDULE_MEMBERS.has(usage.slice(SCHEDULE_PREFIX.length));
30
52
 
31
53
  const isFunctionExpression = (node: ESTree.Node) =>
32
54
  node.type === "ArrowFunctionExpression" || node.type === "FunctionExpression";
@@ -52,15 +74,6 @@ export const scheduleVisitors = ({
52
74
  return operation !== undefined && DEADLINE_OPERATIONS.has(operation);
53
75
  };
54
76
 
55
- const isPipeFunction = (node: ESTree.Node) => {
56
- const imported = importedExpression(node, context.sourceCode);
57
- return (
58
- imported !== undefined &&
59
- ((imported.module === "effect" && imported.members.join(".") === "pipe") ||
60
- (imported.module === "effect/Function" && imported.members.join(".") === "pipe"))
61
- );
62
- };
63
-
64
77
  const pollingOperation = (node: ESTree.Node) => {
65
78
  if (node.type !== "CallExpression") {
66
79
  return;
@@ -76,8 +89,9 @@ export const scheduleVisitors = ({
76
89
  return false;
77
90
  }
78
91
  if (ancestor.type === "CallExpression") {
79
- const isDeadlineAroundChild =
80
- isDeadlineCall(ancestor) && ancestor.arguments.some((argument) => argument === child);
92
+ const isTimedSource =
93
+ ancestor.arguments.length >= TWO_ARGUMENTS && ancestor.arguments[0] === child;
94
+ const isDeadlineAroundChild = isDeadlineCall(ancestor) && isTimedSource;
81
95
  if (isDeadlineAroundChild) {
82
96
  return true;
83
97
  }
@@ -86,7 +100,7 @@ export const scheduleVisitors = ({
86
100
  !ancestor.callee.computed &&
87
101
  ancestor.callee.property.type === "Identifier" &&
88
102
  ancestor.callee.property.name === "pipe";
89
- const isPipeCall = isMethodPipe || isPipeFunction(ancestor.callee);
103
+ const isPipeCall = isMethodPipe || isPipeFunction(ancestor.callee, context.sourceCode);
90
104
  if (isPipeCall) {
91
105
  const pipedArguments: readonly ESTree.Node[] = ancestor.arguments;
92
106
  const position = pipedArguments.indexOf(child);
@@ -109,27 +123,6 @@ export const scheduleVisitors = ({
109
123
  return enclosingPolling(ancestor);
110
124
  };
111
125
 
112
- const report = (node: ESTree.Node, usage: string, polling: ESTree.Node | null) => {
113
- const operation = polling === null ? undefined : pollingOperation(polling);
114
- const retries = operation !== undefined && RETRY_OPERATIONS.has(operation);
115
- if (timedSchedules === "banned") {
116
- context.report({ node, messageId: retries ? "retrying" : "polling", data: { usage } });
117
- return;
118
- }
119
- if (retries) {
120
- context.report({ node, messageId: "retryingEvenWhenGranted", data: { usage } });
121
- return;
122
- }
123
- if (usage !== SCHEDULE_A_GRANT_ALLOWS) {
124
- context.report({ node, messageId: "scheduleEvenWhenGranted", data: { usage } });
125
- return;
126
- }
127
- const lacksDeadline = polling === null || !hasDeadline(polling);
128
- if (lacksDeadline) {
129
- context.report({ node, messageId: "deadline", data: { usage } });
130
- }
131
- };
132
-
133
126
  const scheduleUsageOf = (node: ESTree.Node) =>
134
127
  effectModuleReference(node, context.sourceCode, "Schedule");
135
128
 
@@ -140,17 +133,67 @@ export const scheduleVisitors = ({
140
133
 
141
134
  const pipeOf = (call: ESTree.CallExpression) => {
142
135
  const callee = call.callee;
136
+ if (isPipeFunction(callee, context.sourceCode)) {
137
+ const [source, ...stages] = call.arguments;
138
+ return source === undefined ? undefined : { source, stages };
139
+ }
143
140
  const isMethodPipe =
144
141
  callee.type === "MemberExpression" &&
145
142
  !callee.computed &&
146
143
  callee.property.type === "Identifier" &&
147
144
  callee.property.name === PIPE_METHOD;
148
- if (isMethodPipe) {
149
- return { source: callee.object, stages: call.arguments };
145
+ return isMethodPipe ? { source: callee.object, stages: call.arguments } : undefined;
146
+ };
147
+
148
+ const isExponentialSchedule = (node: ESTree.Node | undefined): boolean => {
149
+ if (node?.type !== "CallExpression") {
150
+ return false;
151
+ }
152
+ const usage = scheduleUsageOf(node.callee);
153
+ if (usage === EXPONENTIAL_SCHEDULE) {
154
+ return true;
155
+ }
156
+ if (usage === JITTERED_SCHEDULE) {
157
+ return isExponentialSchedule(node.arguments[0]);
158
+ }
159
+ const piped = pipeOf(node);
160
+ return piped !== undefined && isExponentialSchedule(piped.source);
161
+ };
162
+
163
+ const isJitteredExponential = (node: ESTree.Node) => {
164
+ const call = node.parent;
165
+ if (call?.type !== "CallExpression") {
166
+ return false;
167
+ }
168
+ if (call.callee === node) {
169
+ return isExponentialSchedule(call.arguments[0]);
170
+ }
171
+ const piped = pipeOf(call);
172
+ return (
173
+ piped !== undefined &&
174
+ piped.stages.some((stage) => stage === node) &&
175
+ isExponentialSchedule(piped.source)
176
+ );
177
+ };
178
+
179
+ const report = (node: ESTree.Node, usage: string, polling: ESTree.Node | null) => {
180
+ if (timedSchedules === "banned") {
181
+ const operation = polling === null ? undefined : pollingOperation(polling);
182
+ const retries = operation !== undefined && RETRY_OPERATIONS.has(operation);
183
+ context.report({ node, messageId: retries ? "retrying" : "polling", data: { usage } });
184
+ return;
185
+ }
186
+ const scheduleAllowedByGrant =
187
+ SCHEDULES_A_GRANT_ALLOWS.has(usage) ||
188
+ (usage === JITTERED_SCHEDULE && isJitteredExponential(node));
189
+ if (!scheduleAllowedByGrant) {
190
+ context.report({ node, messageId: "scheduleEvenWhenGranted", data: { usage } });
191
+ return;
192
+ }
193
+ const lacksDeadline = polling === null || !hasDeadline(polling);
194
+ if (lacksDeadline) {
195
+ context.report({ node, messageId: "deadline", data: { usage } });
150
196
  }
151
- const [source, ...stages] = call.arguments;
152
- const isPipeFunctionCallWithSource = source !== undefined && isPipeFunction(call.callee);
153
- return isPipeFunctionCallWithSource ? { source, stages } : undefined;
154
197
  };
155
198
 
156
199
  const isCountOnlySchedule = (node: ESTree.Node | undefined): boolean => {
@@ -194,17 +237,62 @@ export const scheduleVisitors = ({
194
237
  );
195
238
  };
196
239
 
240
+ const isScheduleCall = (node: ESTree.Node) =>
241
+ node.type === "CallExpression" && scheduleUsageOf(node.callee) !== undefined;
242
+
243
+ const composingSchedule = (node: ESTree.Node): ESTree.Node | undefined => {
244
+ const parent = node.parent;
245
+ if (parent?.type === "ArrayExpression") {
246
+ const call = parent.parent;
247
+ const isScheduleList = call.type === "CallExpression" && isScheduleCall(call);
248
+ return isScheduleList ? call : undefined;
249
+ }
250
+ if (parent?.type === "MemberExpression") {
251
+ const call = parent.parent;
252
+ const isPipedSource =
253
+ parent.object === node &&
254
+ call.type === "CallExpression" &&
255
+ call.callee === parent &&
256
+ pipeOf(call) !== undefined;
257
+ return isPipedSource ? call : undefined;
258
+ }
259
+ if (parent?.type !== "CallExpression") {
260
+ return undefined;
261
+ }
262
+ if (parent.callee === node) {
263
+ return parent;
264
+ }
265
+ const isComposedArgument = isScheduleCall(parent) || pipeOf(parent) !== undefined;
266
+ return isComposedArgument ? parent : undefined;
267
+ };
268
+
269
+ const outermostSchedule = (node: ESTree.Node): ESTree.Node => {
270
+ const composing = composingSchedule(node);
271
+ return composing === undefined ? node : outermostSchedule(composing);
272
+ };
273
+
274
+ const usagesBySchedule = new Map<ESTree.Node, { node: ESTree.Node; usage: string }[]>();
275
+
197
276
  const scheduleReference = (node: ESTree.Node) => {
198
277
  const usage = scheduleUsageOf(node);
199
278
  const isUnboundedScheduleUsage = usage !== undefined && !keepsCountOnly(node);
200
279
  if (isUnboundedScheduleUsage) {
201
- report(node, usage, enclosingPolling(node));
280
+ const schedule = outermostSchedule(node);
281
+ usagesBySchedule.set(schedule, [...(usagesBySchedule.get(schedule) ?? []), { node, usage }]);
202
282
  }
203
283
  };
204
284
 
205
285
  return {
206
286
  Identifier: scheduleReference,
207
287
  MemberExpression: scheduleReference,
288
+ "Program:exit"() {
289
+ for (const usages of usagesBySchedule.values()) {
290
+ const timed = usages.filter(({ usage }) => !isUntimed(usage));
291
+ for (const { node, usage } of timed.length > 0 ? timed : usages.slice(0, 1)) {
292
+ report(node, usage, enclosingPolling(node));
293
+ }
294
+ }
295
+ },
208
296
  CallExpression(node: ESTree.CallExpression) {
209
297
  const operation = pollingOperation(node);
210
298
  if (operation === undefined) {
@@ -223,10 +311,6 @@ export const scheduleVisitors = ({
223
311
  export const noSchedules = defineRule({
224
312
  meta: {
225
313
  type: "problem",
226
- docs: {
227
- description:
228
- "Wait on signals instead of timers; Schedule.recurs(n) counts without a timer, and files allowed pollsWithADeadline may repeat with Schedule.spaced when the polling has a deadline, but no file retries on a timer.",
229
- },
230
314
  messages: SCHEDULE_MESSAGES,
231
315
  schema: ruleOptionsJsonSchema(SCHEDULE_OPTIONS),
232
316
  },
@@ -3,10 +3,6 @@ import { defineRule } from "@oxlint/plugins";
3
3
  export const noTypeDeclarations = defineRule({
4
4
  meta: {
5
5
  type: "suggestion",
6
- docs: {
7
- description:
8
- "Derive types inline from the Schema or function that produces them instead of declaring type aliases, interfaces, enums, namespaces or ambient modules.",
9
- },
10
6
  messages: {
11
7
  typeAlias: "type {{name}} is declared",
12
8
  interface: "interface {{name}} is declared",
@@ -50,10 +50,6 @@ const isSchemaDeclarationCallback = (predicate: ESTree.TSTypePredicate, sourceCo
50
50
  export const noTypePredicates = defineRule({
51
51
  meta: {
52
52
  type: "problem",
53
- docs: {
54
- description:
55
- "Let TypeScript narrow on checks it can see; a hand-written type predicate is a promise the compiler never checks, like a type assertion.",
56
- },
57
53
  messages: {
58
54
  typePredicate: "`{{predicate}}` is an unchecked type predicate",
59
55
  },