@sshkeda/check 1.0.2 → 2.0.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 (101) hide show
  1. package/README.md +19 -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 +10 -0
  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 +8 -3
  66. package/src/lib/tasks/lib/enforce-file-types/index.ts +6 -1
  67. package/src/lib/tasks/lib/format-with-bun-fmt/internal/declare-the-formatting-task.ts +3 -2
  68. package/src/lib/tasks/lib/judge-index-re-exports/internal/find-index-re-export-violations.ts +27 -21
  69. package/src/lib/tasks/lib/lint-with-oxlint/index.ts +2 -1
  70. package/src/lib/tasks/lib/lint-with-oxlint/internal/check-lint-rules-with-oxlint.ts +11 -8
  71. package/src/lib/tasks/lib/lint-with-oxlint/internal/read-oxlint-json-report.ts +18 -2
  72. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/export-placement-errors.ts +12 -1
  73. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-export-placement-violations.ts +75 -21
  74. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-package-entry-sources.ts +146 -0
  75. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-the-importer-graph.ts +13 -188
  76. package/src/lib/tasks/lib/refuse-disable-directives/index.ts +2 -1
  77. package/src/lib/tasks/lib/refuse-disable-directives/internal/find-disable-directives.ts +9 -3
  78. package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/internal/find-error-mappings-to-done.ts +108 -9
  79. package/src/lib/tasks/lib/require-separate-service-layers/internal/find-service-layer-violations.ts +1 -5
  80. package/src/lib/tasks/lib/require-type-checking-every-linted-file/index.ts +2 -1
  81. package/src/lib/tasks/lib/require-type-checking-every-linted-file/internal/find-linted-files-not-type-checked.ts +2 -4
  82. package/src/lib/tasks/shared/check-each-nested-package.ts +13 -1
  83. package/src/lib/tasks/shared/classify-files-by-role.ts +5 -0
  84. package/src/lib/tasks/shared/count-the-line-of-an-offset.ts +20 -2
  85. package/src/lib/tasks/shared/create-the-module-resolver.ts +39 -23
  86. package/src/lib/tasks/shared/declare-lint-config-task.ts +2 -1
  87. package/src/lib/tasks/shared/declare-typescript-config-task.ts +30 -2
  88. package/src/lib/tasks/shared/find-unmet-typescript-config-requirements.ts +24 -7
  89. package/src/lib/tasks/shared/list-ignored-files-imported-by-source.ts +25 -16
  90. package/src/lib/tasks/shared/load-the-lint-config.ts +14 -12
  91. package/src/lib/tasks/shared/parse-config-files-that-import-from-check.ts +6 -7
  92. package/src/lib/tasks/shared/project-config-and-tool-errors.ts +63 -4
  93. package/src/lib/tasks/shared/provide-project-query-caches.ts +11 -0
  94. package/src/lib/tasks/shared/query-the-typescript-project.ts +108 -43
  95. package/src/lib/tasks/shared/resolve-absolute-paths.ts +4 -1
  96. package/src/lib/tasks/shared/resolve-module-requests.ts +131 -90
  97. package/src/lib/tasks/shared/run-tools-and-decode-their-reports.ts +15 -11
  98. package/src/shared/bans-each-permission-lifts.ts +4 -21
  99. package/src/shared/find-retired-words.ts +1 -1
  100. package/src/shared/lint-rule-messages-and-patterns.ts +7 -12
  101. package/src/shared/record-and-grant-permissions-by-path.ts +41 -65
@@ -1,7 +1,7 @@
1
1
  import { defineRule } from "@oxlint/plugins";
2
2
  import type { ESTree } from "@oxlint/plugins";
3
3
 
4
- import { effectModuleOperation } from "./resolve-what-an-expression-imports.ts";
4
+ import { effectModuleOperation, staticKeyName } from "./resolve-what-an-expression-imports.ts";
5
5
 
6
6
  const isUnknownAnnotation = (annotation: ESTree.TSTypeAnnotation | null | undefined) =>
7
7
  annotation?.typeAnnotation.type === "TSUnknownKeyword";
@@ -29,10 +29,31 @@ const isCallback = (node: ESTree.ArrowFunctionExpression | ESTree.Function) =>
29
29
  node.parent.type === "CallExpression" ||
30
30
  (node.parent.type === "Property" && node.parent.value === node);
31
31
 
32
+ const CLASS_MEMBERS: ReadonlySet<string> = new Set(["MethodDefinition", "PropertyDefinition"]);
33
+
34
+ const memberOfFunction = (node: Parameters<typeof isCallback>[0]) => {
35
+ const parent = node.parent;
36
+ const isMemberValue =
37
+ (CLASS_MEMBERS.has(parent.type) || parent.type === "Property") &&
38
+ "value" in parent &&
39
+ parent.value === node;
40
+ return isMemberValue ? parent : undefined;
41
+ };
42
+
43
+ const memberKeyName = (member: NonNullable<ReturnType<typeof memberOfFunction>>) => {
44
+ const { computed, key } = member;
45
+ const isUncomputedIdentifierKey = !computed && key.type === "Identifier";
46
+ return isUncomputedIdentifierKey ? key.name : staticKeyName(key);
47
+ };
48
+
32
49
  const reportedFunctionName = (node: Parameters<typeof isCallback>[0]) => {
33
50
  if (node.id) {
34
51
  return node.id.name;
35
52
  }
53
+ const member = memberOfFunction(node);
54
+ if (member !== undefined) {
55
+ return memberKeyName(member) ?? "this function";
56
+ }
36
57
  const parent = node.parent;
37
58
  if (parent.type !== "VariableDeclarator") {
38
59
  return "this function";
@@ -41,31 +62,46 @@ const reportedFunctionName = (node: Parameters<typeof isCallback>[0]) => {
41
62
  return declared.type === "Identifier" ? declared.name : "this function";
42
63
  };
43
64
 
44
- const isDeclaredForExport = (node: Parameters<typeof isCallback>[0]) => {
45
- const isFunctionDeclaration =
46
- node.type === "FunctionDeclaration" || node.type === "TSDeclareFunction";
47
- if (isFunctionDeclaration) {
48
- return true;
49
- }
65
+ const isExportedValue = (node: ESTree.Node) => {
50
66
  const parent = node.parent;
51
- if (parent.type === "ExportDefaultDeclaration") {
67
+ if (parent?.type === "ExportDefaultDeclaration") {
52
68
  return true;
53
69
  }
54
70
  return (
55
- parent.type === "VariableDeclarator" &&
71
+ parent?.type === "VariableDeclarator" &&
56
72
  parent.init === node &&
57
73
  parent.parent.type === "VariableDeclaration" &&
58
74
  parent.parent.parent.type === "ExportNamedDeclaration"
59
75
  );
60
76
  };
61
77
 
78
+ const isMemberOfAnExport = (member: NonNullable<ReturnType<typeof memberOfFunction>>) => {
79
+ if (member.type === "Property") {
80
+ return member.parent.type === "ObjectExpression" && isExportedValue(member.parent);
81
+ }
82
+ const isPrivate = member.accessibility === "private" || member.key.type === "PrivateIdentifier";
83
+ if (isPrivate) {
84
+ return false;
85
+ }
86
+ const owner = member.parent.parent;
87
+ return (
88
+ owner?.parent?.type === "ExportNamedDeclaration" || (owner !== null && isExportedValue(owner))
89
+ );
90
+ };
91
+
92
+ const isDeclaredForExport = (node: Parameters<typeof isCallback>[0]) => {
93
+ const isFunctionDeclaration =
94
+ node.type === "FunctionDeclaration" || node.type === "TSDeclareFunction";
95
+ if (isFunctionDeclaration) {
96
+ return true;
97
+ }
98
+ const member = memberOfFunction(node);
99
+ return member === undefined ? isExportedValue(node) : isMemberOfAnExport(member);
100
+ };
101
+
62
102
  export const noUnknownBoundaries = defineRule({
63
103
  meta: {
64
104
  type: "problem",
65
- docs: {
66
- description:
67
- "Keep unknown out of function signatures; decode unknown input with Schema where it enters.",
68
- },
69
105
  messages: {
70
106
  parameter: "parameter {{name}} is typed unknown",
71
107
  returnType: "{{name}} returns unknown",
@@ -6,10 +6,6 @@ const VAGUE_ERROR_NAME = /^(?:Failure|Error|Invalid|Unknown|Refused)+$/u;
6
6
  export const noVagueErrorNames = defineRule({
7
7
  meta: {
8
8
  type: "suggestion",
9
- docs: {
10
- description:
11
- "Name a class that extends another after what went wrong, not only Failure, Error, Invalid, Unknown or Refused.",
12
- },
13
9
  messages: {
14
10
  vague: "error class {{name}} has a vague name",
15
11
  },
@@ -2,15 +2,14 @@ import { defineRule } from "@oxlint/plugins";
2
2
 
3
3
  import { fileNameOf, isEntryFileName } from "../shared/match-file-names-and-path-prefixes.ts";
4
4
 
5
- const VAGUE_SUFFIXES: ReadonlySet<string> = new Set(["logic", "impl"]);
6
-
7
5
  const VAGUE_FILE_NAMES: ReadonlySet<string> = new Set([
8
6
  "utils",
9
7
  "helpers",
10
8
  "common",
11
9
  "misc",
12
10
  "types",
13
- ...VAGUE_SUFFIXES,
11
+ "logic",
12
+ "impl",
14
13
  ]);
15
14
 
16
15
  const ENTRY_NAME = "index";
@@ -18,9 +17,6 @@ const ENTRY_NAME = "index";
18
17
  export const noVagueFileNames = defineRule({
19
18
  meta: {
20
19
  type: "suggestion",
21
- docs: {
22
- description: `Name files after what they do, not ${[...VAGUE_FILE_NAMES].join(", ")} or a ${[...VAGUE_SUFFIXES].map((suffix) => `.${suffix}`).join(" or ")} suffix, and keep ${ENTRY_NAME} for entries.`,
23
- },
24
20
  messages: {
25
21
  vague: "file name {{fileName}} is {{name}}, which does not say what it does",
26
22
  vagueSuffix: "file name {{fileName}} ends in .{{suffix}}, which does not say what it does",
@@ -36,7 +32,7 @@ export const noVagueFileNames = defineRule({
36
32
  context.report({ node, messageId: "vague", data: { fileName, name } });
37
33
  return;
38
34
  }
39
- const suffix = suffixes.find((part) => VAGUE_SUFFIXES.has(part));
35
+ const suffix = suffixes.find((part) => VAGUE_FILE_NAMES.has(part));
40
36
  if (suffix !== undefined) {
41
37
  context.report({ node, messageId: "vagueSuffix", data: { fileName, suffix } });
42
38
  return;
@@ -10,6 +10,7 @@ import {
10
10
  IMPORT_META,
11
11
  moduleSpecifiersOfGlobalObject,
12
12
  PERMISSION_BANS,
13
+ UNUSED_GRANT_REPORTED,
13
14
  } from "../shared/bans-each-permission-lifts.ts";
14
15
  import type { importsBan } from "../shared/bans-each-permission-lifts.ts";
15
16
  import { decodeRuleOptions, ruleOptionsJsonSchema } from "./describe-and-decode-rule-options.ts";
@@ -26,6 +27,7 @@ import {
26
27
  importedExpression,
27
28
  importsOnlyTypes,
28
29
  isGlobalReference,
30
+ isImportMeta,
29
31
  isMemberExpressionObject,
30
32
  isReferencePosition,
31
33
  isValueOperandOfTypeScriptOrExport,
@@ -72,9 +74,6 @@ const isWholeModuleImportOf = (
72
74
  return importsWholeModule && moduleSpecifiers.includes(definition.parent.source.value);
73
75
  };
74
76
 
75
- const isImportMeta = (node: ESTree.Node) =>
76
- node.type === "MetaProperty" && node.meta.name === "import" && node.property.name === "meta";
77
-
78
77
  const constantInitializer = (node: ESTree.Node, sourceCode: SourceCode) => {
79
78
  const definitions = resolvedVariable(node, sourceCode)?.defs ?? [];
80
79
  const declarator = definitions.length === 1 ? definitions[0]?.node : undefined;
@@ -186,6 +185,12 @@ const outermostTypeScriptWrapper = (node: ESTree.Node): ESTree.Node => {
186
185
  return isWrapped ? outermostTypeScriptWrapper(parent) : node;
187
186
  };
188
187
 
188
+ const isInATypeQuery = (node: ESTree.Node): boolean => {
189
+ const { parent } = node;
190
+ const isLeftOfAQualifiedName = parent?.type === "TSQualifiedName" && parent.left === node;
191
+ return parent?.type === "TSTypeQuery" || (isLeftOfAQualifiedName && isInATypeQuery(parent));
192
+ };
193
+
189
194
  const createdRequireVisitors = (
190
195
  context: Context,
191
196
  createdRequires: readonly {
@@ -239,6 +244,7 @@ const createdRequireVisitors = (
239
244
  const declared = sourceCode.getDeclaredVariables(declarator)[0];
240
245
  return (declared?.references ?? [])
241
246
  .filter((reference) => reference.identifier !== declarator.id)
247
+ .filter((reference) => !isInATypeQuery(reference.identifier))
242
248
  .every((reference) => isExceptedMemberOf(reference.identifier, ban));
243
249
  };
244
250
  const checkReference = (node: ESTree.Node) => {
@@ -333,12 +339,8 @@ const banVisitors = (
333
339
  isBannedEffectUsage,
334
340
  createdRequires,
335
341
  }: ReturnType<typeof compiledBans>,
336
- messageId: "banned" | "bannedEvenWhenGranted",
342
+ report: (node: ESTree.Node, usage: string) => void,
337
343
  ): Visitor => {
338
- const report = (node: ESTree.Node, usage: string) => {
339
- context.report({ node, messageId, data: { usage } });
340
- };
341
-
342
344
  const reportImport = (node: ESTree.Node, specifier: string, name: string) => {
343
345
  const ban = bannedImport(specifier, name);
344
346
  if (ban === undefined) {
@@ -473,11 +475,12 @@ const banVisitors = (
473
475
 
474
476
  const BAN_OPTIONS = Schema.Struct({
475
477
  bans: Schema.optionalKey(Schema.Literals(["all", BANS_NO_GRANT_LIFTS])),
478
+ unusedGrant: Schema.optionalKey(Schema.Literal(UNUSED_GRANT_REPORTED)),
476
479
  });
477
480
 
478
481
  export const permissionBanRule = (permission: keyof typeof PERMISSION_BANS) => {
479
482
  const entry = PERMISSION_BANS[permission];
480
- const { description, message } = entry;
483
+ const { message } = entry;
481
484
  const liftedByAGrant = compiledBans(entry.bans);
482
485
  const liftedByNoGrant = compiledBans(bansNoGrantLifts(permission));
483
486
  const liftedByNoGrantBeyondTheGrantableImports = compiledBans(
@@ -489,23 +492,43 @@ export const permissionBanRule = (permission: keyof typeof PERMISSION_BANS) => {
489
492
  return defineRule({
490
493
  meta: {
491
494
  type: "problem",
492
- docs: { description },
493
495
  messages: {
494
496
  banned: message,
495
497
  bannedEvenWhenGranted: messageEvenWhenGranted,
498
+ unusedGrant: `this file is granted ${permission}, but uses nothing ${permission} allows`,
496
499
  },
497
500
  schema: ruleOptionsJsonSchema(BAN_OPTIONS),
498
501
  },
499
502
  create(context) {
500
- const isGranted =
501
- decodeRuleOptions(BAN_OPTIONS, context.options)?.bans === BANS_NO_GRANT_LIFTS;
503
+ const options = decodeRuleOptions(BAN_OPTIONS, context.options);
504
+ const isGranted = options?.bans === BANS_NO_GRANT_LIFTS;
505
+ const reporterOf =
506
+ (messageId: "banned" | "bannedEvenWhenGranted") => (node: ESTree.Node, usage: string) => {
507
+ context.report({ node, messageId, data: { usage } });
508
+ };
509
+ const grantedUsages = new Set<ESTree.Node>();
510
+ const isUnusedGrantReported = isGranted && options.unusedGrant === UNUSED_GRANT_REPORTED;
502
511
  return mergedVisitors([
503
- ...(isGranted ? [] : [banVisitors(context, liftedByAGrant, "banned")]),
512
+ ...(isGranted ? [] : [banVisitors(context, liftedByAGrant, reporterOf("banned"))]),
504
513
  banVisitors(
505
514
  context,
506
515
  isGranted ? liftedByNoGrant : liftedByNoGrantBeyondTheGrantableImports,
507
- "bannedEvenWhenGranted",
516
+ reporterOf("bannedEvenWhenGranted"),
508
517
  ),
518
+ ...(isUnusedGrantReported
519
+ ? [
520
+ banVisitors(context, liftedByAGrant, (node) => {
521
+ grantedUsages.add(node);
522
+ }),
523
+ {
524
+ "Program:exit"(node) {
525
+ if (grantedUsages.size === 0) {
526
+ context.report({ node, messageId: "unusedGrant" });
527
+ }
528
+ },
529
+ } satisfies Visitor,
530
+ ]
531
+ : []),
509
532
  ]);
510
533
  },
511
534
  });
@@ -3,10 +3,6 @@ import { defineRule } from "@oxlint/plugins";
3
3
  export const noWhile = defineRule({
4
4
  meta: {
5
5
  type: "suggestion",
6
- docs: {
7
- description:
8
- "Write loops as for...of over a known sequence, so no loop runs on a condition or counter that outside state must change.",
9
- },
10
6
  messages: {
11
7
  conditionLoop: "{{loop}} loops on a condition or counter that outside state must change",
12
8
  },
@@ -0,0 +1,210 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import type { ESTree, SourceCode } from "@oxlint/plugins";
3
+
4
+ import {
5
+ constantValue,
6
+ effectModuleOperation,
7
+ isAssignmentTarget,
8
+ resolvedVariable,
9
+ unwrappedExpression,
10
+ } from "./resolve-what-an-expression-imports.ts";
11
+
12
+ const FUNCTION_TYPES: ReadonlySet<string> = new Set([
13
+ "ArrowFunctionExpression",
14
+ "FunctionDeclaration",
15
+ "FunctionExpression",
16
+ ]);
17
+
18
+ const EFFECT_MODULES_THAT_RUN_CALLBACKS = [
19
+ "Effect",
20
+ "Stream",
21
+ "Layer",
22
+ "Schedule",
23
+ "Sink",
24
+ "Channel",
25
+ ] as const;
26
+
27
+ const TRANSPARENT_WRAPPERS: ReadonlySet<string> = new Set([
28
+ "ChainExpression",
29
+ "TSAsExpression",
30
+ "TSNonNullExpression",
31
+ "TSSatisfiesExpression",
32
+ "TSInstantiationExpression",
33
+ "TSTypeAssertion",
34
+ ]);
35
+
36
+ const enclosingFunctionOf = (node: ESTree.Node): ESTree.Node | null => {
37
+ const { parent } = node;
38
+ if (parent === null) {
39
+ return null;
40
+ }
41
+ return FUNCTION_TYPES.has(parent.type) ? parent : enclosingFunctionOf(parent);
42
+ };
43
+
44
+ const outermostWrapperOf = (node: ESTree.Node): ESTree.Node => {
45
+ const { parent } = node;
46
+ const isWrapped =
47
+ parent !== null &&
48
+ TRANSPARENT_WRAPPERS.has(parent.type) &&
49
+ "expression" in parent &&
50
+ parent.expression === node;
51
+ return isWrapped ? outermostWrapperOf(parent) : node;
52
+ };
53
+
54
+ const rootObjectOf = (node: ESTree.Node): ESTree.Node => {
55
+ const unwrapped = unwrappedExpression(node);
56
+ const inner = unwrapped.type === "ChainExpression" ? unwrapped.expression : unwrapped;
57
+ return inner.type === "MemberExpression" ? rootObjectOf(inner.object) : inner;
58
+ };
59
+
60
+ const isWritten = (member: ESTree.MemberExpression) => {
61
+ const target = outermostWrapperOf(member);
62
+ const { parent } = target;
63
+ const isDeleted = parent?.type === "UnaryExpression" && parent.operator === "delete";
64
+ return isDeleted || parent?.type === "UpdateExpression" || isAssignmentTarget(target);
65
+ };
66
+
67
+ const CURRIED_EFFECT_FUNCTIONS: ReadonlySet<string> = new Set(["Effect.fn", "Effect.fnUntraced"]);
68
+
69
+ const LAYER_PREFIX = "Layer.";
70
+
71
+ const CONTEXT_RUNNER_SUFFIX = "With";
72
+
73
+ const PREDICATE = /^is[A-Z]/u;
74
+
75
+ const effectOperationNamed = (callee: ESTree.Node, sourceCode: SourceCode) =>
76
+ EFFECT_MODULES_THAT_RUN_CALLBACKS.flatMap((moduleName) => {
77
+ const operation = effectModuleOperation(callee, sourceCode, moduleName);
78
+ return operation === undefined ? [] : [`${moduleName}.${operation}`];
79
+ }).at(0);
80
+
81
+ const effectOperationOf = (callee: ESTree.Node, sourceCode: SourceCode): string | undefined => {
82
+ const called = unwrappedExpression(callee);
83
+ if (called.type === "CallExpression") {
84
+ const curried = effectOperationNamed(called.callee, sourceCode);
85
+ const isCurriedEffectFunction =
86
+ curried !== undefined &&
87
+ (CURRIED_EFFECT_FUNCTIONS.has(curried) ||
88
+ curried.startsWith(LAYER_PREFIX) ||
89
+ curried.endsWith(CONTEXT_RUNNER_SUFFIX));
90
+ return isCurriedEffectFunction ? curried : undefined;
91
+ }
92
+ const operation = effectOperationNamed(called, sourceCode);
93
+ const name = operation?.split(".").at(-1) ?? "";
94
+ const isPredicate = PREDICATE.test(name);
95
+ return isPredicate ? undefined : operation;
96
+ };
97
+
98
+ const callReceiving = (callback: ESTree.Node): ESTree.CallExpression | undefined => {
99
+ const argument = outermostWrapperOf(callback);
100
+ const { parent } = argument;
101
+ if (parent === null) {
102
+ return undefined;
103
+ }
104
+ const isArgument =
105
+ parent.type === "CallExpression" &&
106
+ parent.arguments.some((candidate) => candidate === argument);
107
+ if (isArgument) {
108
+ return parent;
109
+ }
110
+ const isOptionValue =
111
+ parent.type === "Property" &&
112
+ parent.value === argument &&
113
+ parent.parent.type === "ObjectExpression";
114
+ return isOptionValue ? callReceiving(parent.parent) : undefined;
115
+ };
116
+
117
+ const namedReferencesOf = (callback: ESTree.Node, sourceCode: SourceCode) => {
118
+ const declarator =
119
+ callback.type === "FunctionDeclaration" ? callback : outermostWrapperOf(callback).parent;
120
+ const isDeclaration =
121
+ declarator?.type === "FunctionDeclaration" || declarator?.type === "VariableDeclarator";
122
+ const binding = isDeclaration ? declarator.id : null;
123
+ const isNamedConst =
124
+ declarator?.type === "VariableDeclarator" &&
125
+ declarator.id.type === "Identifier" &&
126
+ declarator.parent.type === "VariableDeclaration" &&
127
+ declarator.parent.kind === "const";
128
+ const isNamedFunction = declarator?.type === "FunctionDeclaration";
129
+ const isNamedCallback = (isNamedConst || isNamedFunction) && binding?.type === "Identifier";
130
+ if (!isNamedCallback) {
131
+ return [];
132
+ }
133
+ const variable = sourceCode
134
+ .getDeclaredVariables(declarator)
135
+ .find((candidate) => candidate.identifiers.includes(binding));
136
+ return (variable?.references ?? [])
137
+ .map(({ identifier }) => identifier)
138
+ .filter(
139
+ (identifier) =>
140
+ identifier !== binding &&
141
+ (!isNamedFunction || constantValue(identifier, sourceCode) === callback),
142
+ );
143
+ };
144
+
145
+ const effectOperationRunning = (
146
+ write: ESTree.Node,
147
+ declarer: ESTree.Node | null,
148
+ sourceCode: SourceCode,
149
+ ): string | undefined => {
150
+ const writer = enclosingFunctionOf(write);
151
+ const isOutsideEveryCapturingFunction = writer === null || writer === declarer;
152
+ if (isOutsideEveryCapturingFunction) {
153
+ return undefined;
154
+ }
155
+ const operation = [writer, ...namedReferencesOf(writer, sourceCode)]
156
+ .map((callback) => callReceiving(callback))
157
+ .flatMap((call) => (call === undefined ? [] : [effectOperationOf(call.callee, sourceCode)]))
158
+ .find((found) => found !== undefined);
159
+ return operation ?? effectOperationRunning(writer, declarer, sourceCode);
160
+ };
161
+
162
+ export const noSharedHolders = defineRule({
163
+ meta: {
164
+ type: "problem",
165
+ messages: {
166
+ captured:
167
+ "{{name}} is written inside a function passed to {{operation}}, which can run it concurrently or later; hold that state in a Ref",
168
+ },
169
+ },
170
+ create(context) {
171
+ const reported = new Set<ESTree.Node>();
172
+ return {
173
+ MemberExpression(member) {
174
+ const isOutermostMember = outermostWrapperOf(member).parent?.type !== "MemberExpression";
175
+ const isWrittenMember = isOutermostMember && isWritten(member);
176
+ if (!isWrittenMember) {
177
+ return;
178
+ }
179
+ const root = rootObjectOf(member);
180
+ const declaration = resolvedVariable(root, context.sourceCode)?.defs[0];
181
+ const declarator = declaration?.node;
182
+ const isConstObjectLiteral =
183
+ declarator?.type === "VariableDeclarator" &&
184
+ declarator.id.type === "Identifier" &&
185
+ declarator.init !== null &&
186
+ unwrappedExpression(declarator.init).type === "ObjectExpression" &&
187
+ declarator.parent.type === "VariableDeclaration" &&
188
+ declarator.parent.kind === "const";
189
+ const isUnreportedObjectLiteral =
190
+ isConstObjectLiteral && declaration !== undefined && !reported.has(declaration.name);
191
+ if (!isUnreportedObjectLiteral) {
192
+ return;
193
+ }
194
+ const operation = effectOperationRunning(
195
+ member,
196
+ enclosingFunctionOf(declaration.name),
197
+ context.sourceCode,
198
+ );
199
+ if (operation !== undefined) {
200
+ reported.add(declaration.name);
201
+ context.report({
202
+ node: declaration.name,
203
+ messageId: "captured",
204
+ data: { name: root.type === "Identifier" ? root.name : "", operation },
205
+ });
206
+ }
207
+ },
208
+ };
209
+ },
210
+ });
@@ -44,10 +44,6 @@ const grantedServices = (options: readonly unknown[]) =>
44
44
  export const noServiceDeclarations = defineRule({
45
45
  meta: {
46
46
  type: "problem",
47
- docs: {
48
- description:
49
- "Declare Effect services only in files granted them by exact name with lintConfig({ [file]: { services: [names] } }).",
50
- },
51
47
  messages: {
52
48
  service: "Context.{{factory}} declares service {{name}} outside a services file",
53
49
  anonymousService: "Context.{{factory}} declares a service outside a services file",
@@ -83,9 +79,6 @@ export const noServiceDeclarations = defineRule({
83
79
  export const noUnusedServiceGrants = defineRule({
84
80
  meta: {
85
81
  type: "problem",
86
- docs: {
87
- description: "Declare every service a file is granted in lintConfig.",
88
- },
89
82
  messages: {
90
83
  missing: "granted service {{name}} is not declared here",
91
84
  },
@@ -5,9 +5,11 @@ import {
5
5
  effectOperation,
6
6
  importedExpression,
7
7
  isImportBinding,
8
+ isPipeFunction,
8
9
  isReferencePosition,
9
10
  memberName,
10
11
  propertyKeyName,
12
+ rootOf,
11
13
  } from "./resolve-what-an-expression-imports.ts";
12
14
 
13
15
  const MEMBERS_THAT_WRITE_NOTHING = new Set([
@@ -24,9 +26,6 @@ const PIPE = "pipe";
24
26
 
25
27
  const OPERATIONS_THAT_PASS_THE_SERVICE = new Set(["map", "flatMap", "andThen"]);
26
28
 
27
- const rootOf = (node: ESTree.Node): ESTree.Node =>
28
- node.type === "MemberExpression" ? rootOf(node.object) : node;
29
-
30
29
  const isStdioTag = (node: ESTree.Node, sourceCode: SourceCode) => {
31
30
  if (!isImportBinding(rootOf(node), sourceCode)) {
32
31
  return false;
@@ -108,15 +107,6 @@ const stepWritesNothing = (step: ESTree.Node | undefined, sourceCode: SourceCode
108
107
  return passesTheServiceOn && callbackWritesNothing(step.arguments[0], sourceCode);
109
108
  };
110
109
 
111
- const isPipeFunction = (callee: ESTree.Node, sourceCode: SourceCode) => {
112
- const imported = importedExpression(callee, sourceCode);
113
- const path = imported?.members.join(".");
114
- const isFromEffect =
115
- imported?.module === "effect" && (path === PIPE || path === `Function.${PIPE}`);
116
- const isFromFunction = imported?.module === "effect/Function" && path === PIPE;
117
- return isFromEffect || isFromFunction;
118
- };
119
-
120
110
  const tagUseWritesNothing = (tag: ESTree.Node, sourceCode: SourceCode) => {
121
111
  const { parent } = tag;
122
112
  const isYielded =
@@ -153,10 +143,6 @@ const tagUseWritesNothing = (tag: ESTree.Node, sourceCode: SourceCode) => {
153
143
  export const noStdioService = defineRule({
154
144
  meta: {
155
145
  type: "problem",
156
- docs: {
157
- description:
158
- "Use the Effect Stdio service only in files allowed writesToTheTerminal, except to read stdin, args or whether a stream is a terminal.",
159
- },
160
146
  messages: {
161
147
  stdioService:
162
148
  "{{usage}} is used for more than reading stdin, args or whether a stream is a terminal",
@@ -43,7 +43,9 @@ import {
43
43
  CLOCK_PROPERTIES,
44
44
  noClockReadsAndSleeps,
45
45
  } from "./ban-reading-the-clock-and-sleeping-outside-effect.ts";
46
+ import { noExistsRecovery } from "./ban-recovering-file-system-exists.ts";
46
47
  import { noRetiredWords } from "./ban-retired-words-in-names.ts";
48
+ import { noInstantRetries } from "./ban-retries-without-a-wait.ts";
47
49
  import { noGlobScan } from "./ban-scanning-directories-with-bun-glob.ts";
48
50
  import { noShebang } from "./ban-shebang-lines.ts";
49
51
  import { globalRule } from "./ban-timer-globals-and-the-global-object.ts";
@@ -55,6 +57,7 @@ import { noVagueErrorNames } from "./ban-vague-error-class-names.ts";
55
57
  import { noVagueFileNames } from "./ban-vague-file-names.ts";
56
58
  import { permissionBanRule } from "./ban-what-a-file-is-not-granted.ts";
57
59
  import { noWhile } from "./ban-while-do-while-and-for-loops.ts";
60
+ import { noSharedHolders } from "./ban-writing-captured-objects-inside-effect-callbacks.ts";
58
61
  import {
59
62
  noServiceDeclarations,
60
63
  noUnusedServiceGrants,
@@ -130,6 +133,7 @@ const NAMED_RULES = {
130
133
  "no-clock-reads-and-sleeps": noClockReadsAndSleeps,
131
134
  "no-unchecked-parsing": noUncheckedParsing,
132
135
  "no-schedules": noSchedules,
136
+ "no-instant-retries": noInstantRetries,
133
137
  "no-service-declarations": noServiceDeclarations,
134
138
  "no-unused-service-grants": noUnusedServiceGrants,
135
139
  "no-glob-scan": noGlobScan,
@@ -150,8 +154,10 @@ const NAMED_RULES = {
150
154
  "no-mutable-ref": noMutableRef,
151
155
  "no-while": noWhile,
152
156
  "no-shebang": noShebang,
157
+ "no-shared-holders": noSharedHolders,
153
158
  "no-uncalled-members": noUncalledMembers,
154
159
  "no-unnamed-fallbacks": noUnnamedFallbacks,
160
+ "no-exists-recovery": noExistsRecovery,
155
161
  "no-namespace-aliases": noNamespaceAliases(
156
162
  new Set([
157
163
  ...RESTRICTED_PROPERTIES.map(({ object }) => object),
@@ -50,9 +50,6 @@ const visitValueImports = (
50
50
  export const noAssertions = defineRule({
51
51
  meta: {
52
52
  type: "problem",
53
- docs: {
54
- description: `Keep assertions in ${TEST_HOME}; source code fails with named errors.`,
55
- },
56
53
  messages: {
57
54
  assertion: `asserts with {{assertion}} outside ${TEST_HOME}`,
58
55
  },
@@ -80,9 +77,6 @@ export const noAssertions = defineRule({
80
77
  export const noTestRunners = defineRule({
81
78
  meta: {
82
79
  type: "problem",
83
- docs: {
84
- description: `Write tests as tasks in ${TEST_HOME} instead of with a test runner; only the assertions of ${BUN_TEST} may be imported, and a side-effect import of a test runner module is refused too.`,
85
- },
86
80
  messages: {
87
81
  testRunner: "imports {{imported}} from {{source}}",
88
82
  },
@@ -6,6 +6,8 @@ import {
6
6
  importsEmptyBraces,
7
7
  memberName,
8
8
  moduleExportName,
9
+ typeNameParts,
10
+ typeNameRoot,
9
11
  } from "./resolve-what-an-expression-imports.ts";
10
12
 
11
13
  const EFFECT_PACKAGE = "effect";
@@ -58,16 +60,6 @@ const testingHelperAt = (effectPath: readonly string[]) =>
58
60
  })
59
61
  .at(0);
60
62
 
61
- const typeNameParts = (name: ESTree.Node): readonly string[] => {
62
- if (name.type === "TSQualifiedName") {
63
- return [...typeNameParts(name.left), name.right.name];
64
- }
65
- return name.type === "Identifier" ? [name.name] : [];
66
- };
67
-
68
- const typeNameRoot = (name: ESTree.Node): ESTree.Node =>
69
- name.type === "TSQualifiedName" ? typeNameRoot(name.left) : name;
70
-
71
63
  const effectPathOfImported = (imported: ReturnType<typeof importedExpression>) => {
72
64
  const modulePath = imported && effectPathOf(imported.module);
73
65
  return imported && modulePath && [...modulePath, ...imported.members];
@@ -76,10 +68,6 @@ const effectPathOfImported = (imported: ReturnType<typeof importedExpression>) =
76
68
  export const noTestingHelpers = defineRule({
77
69
  meta: {
78
70
  type: "problem",
79
- docs: {
80
- description:
81
- "Keep Effect's testing helpers, the effect/testing modules and exports effect and @effect packages tag @category testing, in test files.",
82
- },
83
71
  messages: {
84
72
  testingHelper: "uses Effect's testing helper {{helper}} outside a test file",
85
73
  },
@@ -11,10 +11,6 @@ const unwrappedNegation = (node: ESTree.Expression): ESTree.Expression => {
11
11
  export const noUnnamedCompoundConditions = defineRule({
12
12
  meta: {
13
13
  type: "suggestion",
14
- docs: {
15
- description:
16
- "Name an if or ternary condition that joins checks with && or || by declaring it as a const first.",
17
- },
18
14
  messages: {
19
15
  unnamed: "{{test}} condition joins checks with {{operator}} without a name",
20
16
  },
@@ -84,10 +84,6 @@ const isMutableCollectionType = (annotation: ESTree.TSType) =>
84
84
  export const noLooseLiteralConstants = defineRule({
85
85
  meta: {
86
86
  type: "suggestion",
87
- docs: {
88
- description:
89
- "Write module constants that hold fixed data in CAPITAL_SNAKE_CASE ending in as const, and check literal constants with satisfies instead of a type annotation, so their type is the exact data.",
90
- },
91
87
  messages: {
92
88
  constant: "const {{name}} {{violations}}",
93
89
  },