@sshkeda/check 0.0.0-stage → 1.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 (167) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +156 -2
  3. package/docs/how-to-make-check-pass.md +71 -0
  4. package/package.json +51 -4
  5. package/src/index.ts +8 -0
  6. package/src/internal/ban-abbreviated-words-in-declared-names.ts +165 -0
  7. package/src/internal/ban-checked-members-outside-calls.ts +151 -0
  8. package/src/internal/ban-comments.ts +23 -0
  9. package/src/internal/ban-comparing-or-switching-on-tag.ts +114 -0
  10. package/src/internal/ban-deadlines-inside-tests.ts +41 -0
  11. package/src/internal/ban-declared-values.ts +70 -0
  12. package/src/internal/ban-dropped-failures-detached-fibers-sleeps-and-untraced-functions.ts +67 -0
  13. package/src/internal/ban-failures-collected-as-results.ts +57 -0
  14. package/src/internal/ban-fallbacks-of-named-recoveries.ts +118 -0
  15. package/src/internal/ban-function-overloads.ts +107 -0
  16. package/src/internal/ban-global-error-in-failure-types.ts +146 -0
  17. package/src/internal/ban-instanceof-in-effect-recovery-callbacks.ts +63 -0
  18. package/src/internal/ban-let-and-effect-mutable-ref.ts +79 -0
  19. package/src/internal/ban-logic-in-index-files.ts +178 -0
  20. package/src/internal/ban-namespaces-used-without-a-member.ts +176 -0
  21. package/src/internal/ban-node-builtins-that-effect-replaces.ts +42 -0
  22. package/src/internal/ban-parsing-outside-data-without-a-schema.ts +299 -0
  23. package/src/internal/ban-re-exports.ts +172 -0
  24. package/src/internal/ban-reading-the-clock-and-sleeping-outside-effect.ts +239 -0
  25. package/src/internal/ban-retired-words-in-names.ts +49 -0
  26. package/src/internal/ban-scanning-directories-with-bun-glob.ts +79 -0
  27. package/src/internal/ban-shebang-lines.ts +26 -0
  28. package/src/internal/ban-timer-globals-and-the-global-object.ts +34 -0
  29. package/src/internal/ban-timer-schedules-without-deadlines.ts +240 -0
  30. package/src/internal/ban-type-and-interface-declarations.ts +48 -0
  31. package/src/internal/ban-type-predicates.ts +82 -0
  32. package/src/internal/ban-unknown-in-function-signatures.ts +143 -0
  33. package/src/internal/ban-vague-error-class-names.ts +28 -0
  34. package/src/internal/ban-vague-file-names.ts +51 -0
  35. package/src/internal/ban-what-a-file-is-not-granted.ts +512 -0
  36. package/src/internal/ban-while-do-while-and-for-loops.ts +27 -0
  37. package/src/internal/confine-service-declarations-to-granted-files.ts +116 -0
  38. package/src/internal/confine-the-stdio-service-to-terminal-output.ts +190 -0
  39. package/src/internal/define-the-lint-plugin-and-configs.ts +361 -0
  40. package/src/internal/describe-and-decode-rule-options.ts +22 -0
  41. package/src/internal/keep-assertions-in-the-test-home-and-ban-test-runners.ts +128 -0
  42. package/src/internal/keep-effect-testing-helpers-in-test-files.ts +157 -0
  43. package/src/internal/match-banned-modules-and-globals.ts +71 -0
  44. package/src/internal/match-effect-service-factories.ts +20 -0
  45. package/src/internal/require-a-named-const-for-compound-conditions.ts +44 -0
  46. package/src/internal/require-capital-snake-case-and-as-const-on-literal-constants.ts +141 -0
  47. package/src/internal/require-service-keys-to-end-with-their-class-name.ts +64 -0
  48. package/src/internal/resolve-what-an-expression-imports.ts +295 -0
  49. package/src/internal/sort-type-constituents.ts +82 -0
  50. package/src/internal/split-names-into-words.ts +12 -0
  51. package/src/internal/visit-every-declared-name.ts +264 -0
  52. package/src/lib/tasks/index.ts +1 -0
  53. package/src/lib/tasks/internal/declare-the-check-task.ts +29 -0
  54. package/src/lib/tasks/internal/declare-the-checker-queries-task.ts +41 -0
  55. package/src/lib/tasks/internal/declare-the-typed-checks-task.ts +25 -0
  56. package/src/lib/tasks/internal/run-checks-over-one-typescript-project.ts +68 -0
  57. package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/index.ts +15 -0
  58. package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/internal/check-effect-diagnostics-with-effect-tsgo.ts +211 -0
  59. package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/internal/read-effect-tsgo-json-report.ts +39 -0
  60. package/src/lib/tasks/lib/check-import-boundaries/index.ts +1 -0
  61. package/src/lib/tasks/lib/check-import-boundaries/internal/find-import-boundary-violations.ts +415 -0
  62. package/src/lib/tasks/lib/check-import-boundaries/internal/find-subpath-import-targets.ts +117 -0
  63. package/src/lib/tasks/lib/check-import-boundaries/internal/find-what-hides-an-import.ts +65 -0
  64. package/src/lib/tasks/lib/check-import-boundaries/internal/import-boundary-errors.ts +176 -0
  65. package/src/lib/tasks/lib/check-uses-by-their-declarations/index.ts +1 -0
  66. package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/declared-use-errors.ts +32 -0
  67. package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-declared-use-violations.ts +38 -0
  68. package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-sync-calls.ts +23 -0
  69. package/src/lib/tasks/lib/check-uses-by-their-declarations/internal/find-type-only-schemas.ts +375 -0
  70. package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/index.ts +1 -0
  71. package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/find-layer-provides-outside-program-starts.ts +648 -0
  72. package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/layer-provide-errors.ts +27 -0
  73. package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/resolve-imported-package-apis.ts +262 -0
  74. package/src/lib/tasks/lib/confine-stdio-to-granted-files/index.ts +1 -0
  75. package/src/lib/tasks/lib/confine-stdio-to-granted-files/internal/find-ungranted-stdio-uses.ts +219 -0
  76. package/src/lib/tasks/lib/confine-stdio-to-granted-files/internal/stdio-use-errors.ts +31 -0
  77. package/src/lib/tasks/lib/detect-inferrable-annotations/index.ts +1 -0
  78. package/src/lib/tasks/lib/detect-inferrable-annotations/internal/find-inferrable-annotations.ts +357 -0
  79. package/src/lib/tasks/lib/detect-inferrable-annotations/internal/inferrable-annotation-errors.ts +33 -0
  80. package/src/lib/tasks/lib/detect-repeated-service-yields/index.ts +1 -0
  81. package/src/lib/tasks/lib/detect-repeated-service-yields/internal/find-repeated-service-yields.ts +162 -0
  82. package/src/lib/tasks/lib/detect-repeated-service-yields/internal/repeated-service-yield-errors.ts +25 -0
  83. package/src/lib/tasks/lib/enforce-file-layout/index.ts +5 -0
  84. package/src/lib/tasks/lib/enforce-file-layout/internal/check-the-layout-of-each-package.ts +9 -0
  85. package/src/lib/tasks/lib/enforce-file-layout/internal/file-layout-errors.ts +417 -0
  86. package/src/lib/tasks/lib/enforce-file-layout/internal/find-export-path-mismatches.ts +83 -0
  87. package/src/lib/tasks/lib/enforce-file-layout/internal/find-file-layout-violations.ts +827 -0
  88. package/src/lib/tasks/lib/enforce-file-layout/internal/find-module-layout-violations.ts +82 -0
  89. package/src/lib/tasks/lib/enforce-file-layout/internal/resolve-task-imports.ts +178 -0
  90. package/src/lib/tasks/lib/enforce-file-types/index.ts +5 -0
  91. package/src/lib/tasks/lib/enforce-file-types/internal/file-type-errors.ts +21 -0
  92. package/src/lib/tasks/lib/enforce-file-types/internal/find-file-type-violations.ts +78 -0
  93. package/src/lib/tasks/lib/format-with-bun-fmt/index.ts +1 -0
  94. package/src/lib/tasks/lib/format-with-bun-fmt/internal/check-formatting-with-bun-fmt.ts +157 -0
  95. package/src/lib/tasks/lib/format-with-bun-fmt/internal/declare-the-formatting-task.ts +66 -0
  96. package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-format-config-violations.ts +16 -0
  97. package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-format-input-violations.ts +74 -0
  98. package/src/lib/tasks/lib/format-with-bun-fmt/internal/find-symbolic-links.ts +14 -0
  99. package/src/lib/tasks/lib/format-with-bun-fmt/internal/unformatted-file-errors.ts +12 -0
  100. package/src/lib/tasks/lib/judge-index-re-exports/index.ts +1 -0
  101. package/src/lib/tasks/lib/judge-index-re-exports/internal/find-index-re-export-violations.ts +353 -0
  102. package/src/lib/tasks/lib/judge-index-re-exports/internal/index-re-export-errors.ts +21 -0
  103. package/src/lib/tasks/lib/lint-with-oxlint/index.ts +15 -0
  104. package/src/lib/tasks/lib/lint-with-oxlint/internal/check-lint-rules-with-oxlint.ts +123 -0
  105. package/src/lib/tasks/lib/lint-with-oxlint/internal/check-unsafe-enum-assignments.ts +152 -0
  106. package/src/lib/tasks/lib/lint-with-oxlint/internal/find-tsgolint-executable.ts +21 -0
  107. package/src/lib/tasks/lib/lint-with-oxlint/internal/read-oxlint-json-report.ts +82 -0
  108. package/src/lib/tasks/lib/lint-with-oxlint/internal/read-tsgolint-rule-messages.ts +68 -0
  109. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/index.ts +1 -0
  110. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/export-placement-errors.ts +135 -0
  111. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-export-placement-violations.ts +507 -0
  112. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-misplaced-files.ts +116 -0
  113. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-subdivided-directories.ts +29 -0
  114. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-the-importer-graph.ts +451 -0
  115. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/list-binding-names.ts +49 -0
  116. package/src/lib/tasks/lib/refuse-disable-directives/index.ts +15 -0
  117. package/src/lib/tasks/lib/refuse-disable-directives/internal/disable-directive-errors.ts +45 -0
  118. package/src/lib/tasks/lib/refuse-disable-directives/internal/find-disable-directives.ts +66 -0
  119. package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/index.ts +1 -0
  120. package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/internal/error-mapping-errors.ts +21 -0
  121. package/src/lib/tasks/lib/refuse-error-mappings-that-end-streams/internal/find-error-mappings-to-done.ts +135 -0
  122. package/src/lib/tasks/lib/require-separate-service-layers/index.ts +1 -0
  123. package/src/lib/tasks/lib/require-separate-service-layers/internal/find-service-layer-violations.ts +170 -0
  124. package/src/lib/tasks/lib/require-separate-service-layers/internal/service-layer-errors.ts +21 -0
  125. package/src/lib/tasks/lib/require-type-checking-every-linted-file/index.ts +14 -0
  126. package/src/lib/tasks/lib/require-type-checking-every-linted-file/internal/find-linted-files-not-type-checked.ts +117 -0
  127. package/src/lib/tasks/lib/require-type-checking-every-linted-file/internal/linted-file-errors.ts +51 -0
  128. package/src/lib/tasks/lib/type-check-with-bun-check/index.ts +11 -0
  129. package/src/lib/tasks/lib/type-check-with-bun-check/internal/check-types-with-bun-check.ts +54 -0
  130. package/src/lib/tasks/lib/type-check-with-bun-check/internal/read-bun-check-output.ts +73 -0
  131. package/src/lib/tasks/lib/type-check-with-bun-check/internal/type-check-errors.ts +10 -0
  132. package/src/lib/tasks/shared/check-each-nested-package.ts +79 -0
  133. package/src/lib/tasks/shared/classify-files-by-role.ts +115 -0
  134. package/src/lib/tasks/shared/count-the-line-of-an-offset.ts +2 -0
  135. package/src/lib/tasks/shared/create-the-module-resolver.ts +38 -0
  136. package/src/lib/tasks/shared/declare-lint-config-task.ts +13 -0
  137. package/src/lib/tasks/shared/declare-typescript-config-task.ts +18 -0
  138. package/src/lib/tasks/shared/encode-compact-json.ts +5 -0
  139. package/src/lib/tasks/shared/find-installed-package-binaries.ts +62 -0
  140. package/src/lib/tasks/shared/find-module-requests.ts +482 -0
  141. package/src/lib/tasks/shared/find-named-package-manifests.ts +94 -0
  142. package/src/lib/tasks/shared/find-unmet-typescript-config-requirements.ts +60 -0
  143. package/src/lib/tasks/shared/list-ignored-files-imported-by-source.ts +79 -0
  144. package/src/lib/tasks/shared/list-linted-files.ts +28 -0
  145. package/src/lib/tasks/shared/list-owned-files.ts +13 -0
  146. package/src/lib/tasks/shared/list-package-files.ts +66 -0
  147. package/src/lib/tasks/shared/load-the-lint-config.ts +176 -0
  148. package/src/lib/tasks/shared/match-granted-patch-files.ts +88 -0
  149. package/src/lib/tasks/shared/parse-config-files-that-import-from-check.ts +310 -0
  150. package/src/lib/tasks/shared/print-failures-and-keep-detail-in-the-trace.ts +99 -0
  151. package/src/lib/tasks/shared/project-config-and-tool-errors.ts +485 -0
  152. package/src/lib/tasks/shared/query-the-typescript-project.ts +635 -0
  153. package/src/lib/tasks/shared/read-named-package-manifests.ts +53 -0
  154. package/src/lib/tasks/shared/read-package-manifest-object.ts +28 -0
  155. package/src/lib/tasks/shared/resolve-absolute-paths.ts +4 -0
  156. package/src/lib/tasks/shared/resolve-module-requests.ts +121 -0
  157. package/src/lib/tasks/shared/run-tools-and-decode-their-reports.ts +98 -0
  158. package/src/lib/tasks/shared/tool-file-locations.ts +38 -0
  159. package/src/shared/bans-each-permission-lifts.ts +454 -0
  160. package/src/shared/config-file-names.ts +3 -0
  161. package/src/shared/find-retired-words.ts +17 -0
  162. package/src/shared/format-reported-failures.ts +31 -0
  163. package/src/shared/lint-rule-messages-and-patterns.ts +104 -0
  164. package/src/shared/match-file-names-and-path-prefixes.ts +18 -0
  165. package/src/shared/permission-grant-errors.ts +42 -0
  166. package/src/shared/record-and-grant-permissions-by-path.ts +419 -0
  167. package/src/shared/test-home-directories.ts +4 -0
@@ -0,0 +1,128 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import type { ESTree, SourceCode } from "@oxlint/plugins";
3
+
4
+ import {
5
+ ASSERTION_MODULES,
6
+ BUN_TEST,
7
+ BUN_TEST_ASSERTIONS,
8
+ isTestRunnerModule,
9
+ } from "../shared/lint-rule-messages-and-patterns.ts";
10
+ import { TEST_HOME } from "../shared/test-home-directories.ts";
11
+ import { importsOnlyTypes, moduleExportName } from "./resolve-what-an-expression-imports.ts";
12
+
13
+ const SIDE_EFFECT_IMPORT_KEYWORD = "import";
14
+ const A_TEST_RUNNER = "a test runner";
15
+
16
+ const imported = (node: ESTree.Node, name: string, isNamed: boolean) => ({
17
+ node,
18
+ name,
19
+ isAssertion: isNamed && BUN_TEST_ASSERTIONS.has(name),
20
+ });
21
+
22
+ const visitValueImports = (
23
+ sourceCode: SourceCode,
24
+ checkModule: (
25
+ source: ESTree.StringLiteral,
26
+ specifiers: readonly {
27
+ readonly node: ESTree.Node;
28
+ readonly name: string;
29
+ readonly isAssertion: boolean;
30
+ }[],
31
+ ) => void,
32
+ ) => ({
33
+ ImportDeclaration(declaration: ESTree.ImportDeclaration) {
34
+ const valueSpecifiers = declaration.specifiers.filter(
35
+ (specifier) => specifier.type !== "ImportSpecifier" || specifier.importKind !== "type",
36
+ );
37
+ if (!importsOnlyTypes(declaration, sourceCode)) {
38
+ checkModule(
39
+ declaration.source,
40
+ valueSpecifiers.map((specifier) =>
41
+ specifier.type === "ImportSpecifier"
42
+ ? imported(specifier, moduleExportName(specifier.imported), true)
43
+ : imported(specifier, specifier.local.name, false),
44
+ ),
45
+ );
46
+ }
47
+ },
48
+ });
49
+
50
+ export const noAssertions = defineRule({
51
+ meta: {
52
+ type: "problem",
53
+ docs: {
54
+ description: `Keep assertions in ${TEST_HOME}; source code fails with named errors.`,
55
+ },
56
+ messages: {
57
+ assertion: `asserts with {{assertion}} outside ${TEST_HOME}`,
58
+ },
59
+ },
60
+ create(context) {
61
+ return visitValueImports(context.sourceCode, (source, specifiers) => {
62
+ if (ASSERTION_MODULES.has(source.value)) {
63
+ context.report({ node: source, messageId: "assertion", data: { assertion: source.value } });
64
+ return;
65
+ }
66
+ if (source.value !== BUN_TEST) {
67
+ return;
68
+ }
69
+ for (const specifier of specifiers.filter(({ isAssertion }) => isAssertion)) {
70
+ context.report({
71
+ node: specifier.node,
72
+ messageId: "assertion",
73
+ data: { assertion: `${specifier.name} from ${BUN_TEST}` },
74
+ });
75
+ }
76
+ });
77
+ },
78
+ });
79
+
80
+ export const noTestRunners = defineRule({
81
+ meta: {
82
+ 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
+ messages: {
87
+ testRunner: "imports {{imported}} from {{source}}",
88
+ },
89
+ },
90
+ create(context) {
91
+ const reportTestRunnerModule = (node: ESTree.Node, source: string) => {
92
+ context.report({
93
+ node,
94
+ messageId: "testRunner",
95
+ data: { imported: A_TEST_RUNNER, source },
96
+ });
97
+ };
98
+ const valueImports = visitValueImports(context.sourceCode, (source, specifiers) => {
99
+ if (isTestRunnerModule(source.value)) {
100
+ reportTestRunnerModule(source, source.value);
101
+ return;
102
+ }
103
+ const runnerNames = specifiers
104
+ .filter(({ isAssertion }) => !isAssertion)
105
+ .map(({ name }) => name);
106
+ const importsTestRunnerFromBunTest = source.value === BUN_TEST && runnerNames.length > 0;
107
+ if (importsTestRunnerFromBunTest) {
108
+ context.report({
109
+ node: source,
110
+ messageId: "testRunner",
111
+ data: { imported: runnerNames.join(", "), source: BUN_TEST },
112
+ });
113
+ }
114
+ });
115
+ return {
116
+ ImportDeclaration(declaration) {
117
+ valueImports.ImportDeclaration(declaration);
118
+ const loadsBunTestForSideEffects =
119
+ declaration.source.value === BUN_TEST &&
120
+ context.sourceCode.getTokenBefore(declaration.source)?.value ===
121
+ SIDE_EFFECT_IMPORT_KEYWORD;
122
+ if (loadsBunTestForSideEffects) {
123
+ reportTestRunnerModule(declaration.source, BUN_TEST);
124
+ }
125
+ },
126
+ };
127
+ },
128
+ });
@@ -0,0 +1,157 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import type { ESTree } from "@oxlint/plugins";
3
+
4
+ import {
5
+ importedExpression,
6
+ importsEmptyBraces,
7
+ memberName,
8
+ moduleExportName,
9
+ } from "./resolve-what-an-expression-imports.ts";
10
+
11
+ const EFFECT_PACKAGE = "effect";
12
+ const EFFECT_SCOPE = "@effect";
13
+ const PATH_SEPARATOR = "/";
14
+
15
+ const effectPathOf = (source: string) => {
16
+ const segments = source.split(PATH_SEPARATOR);
17
+ const [first, second, ...rest] = segments;
18
+ if (first === EFFECT_PACKAGE) {
19
+ return segments;
20
+ }
21
+ const isScopedEffectPackage = first === EFFECT_SCOPE && second !== undefined;
22
+ return isScopedEffectPackage ? [`${first}${PATH_SEPARATOR}${second}`, ...rest] : undefined;
23
+ };
24
+
25
+ export const EFFECT_TESTING_MODULES: ReadonlySet<string> = new Set([
26
+ "effect/ai/McpTest",
27
+ "effect/cluster/TestRunner",
28
+ "effect/http-api/HttpApiTest",
29
+ "effect/rpc/RpcTest",
30
+ "effect/testing",
31
+ "effect/testing/TestClock",
32
+ "effect/testing/TestConsole",
33
+ "effect/testing/TestSchema",
34
+ ]);
35
+
36
+ const EFFECT_TESTING_EXPORTS: ReadonlyMap<string, ReadonlySet<string>> = new Map([
37
+ ["@effect/platform-bun/BunHttpServer", new Set(["layerTest"])],
38
+ ["effect/Layer", new Set(["PartialEffectful", "mock"])],
39
+ ["effect/cluster/Entity", new Set(["makeTestClient"])],
40
+ [
41
+ "effect/graphql/GraphQLClient",
42
+ new Set(["TestCallOptions", "TestHandler", "TestHandlers", "layerTest", "makeTest"]),
43
+ ],
44
+ ["effect/http/HttpServer", new Set(["layerServices", "layerTestClient", "makeTestClient"])],
45
+ ]);
46
+
47
+ const testingHelperAt = (effectPath: readonly string[]) =>
48
+ effectPath
49
+ .flatMap((segment, index) => {
50
+ const modulePath = effectPath.slice(0, index + 1).join(PATH_SEPARATOR);
51
+ if (EFFECT_TESTING_MODULES.has(modulePath)) {
52
+ return [modulePath];
53
+ }
54
+ const member = effectPath[index + 1];
55
+ const isTestingExportOfModule =
56
+ member !== undefined && EFFECT_TESTING_EXPORTS.get(modulePath)?.has(member);
57
+ return isTestingExportOfModule === true ? [`${segment}.${member}`] : [];
58
+ })
59
+ .at(0);
60
+
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
+ const effectPathOfImported = (imported: ReturnType<typeof importedExpression>) => {
72
+ const modulePath = imported && effectPathOf(imported.module);
73
+ return imported && modulePath && [...modulePath, ...imported.members];
74
+ };
75
+
76
+ export const noTestingHelpers = defineRule({
77
+ meta: {
78
+ 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
+ messages: {
84
+ testingHelper: "uses Effect's testing helper {{helper}} outside a test file",
85
+ },
86
+ },
87
+ create(context) {
88
+ const report = (node: ESTree.Node, helper: string) => {
89
+ context.report({ node, messageId: "testingHelper", data: { helper } });
90
+ };
91
+
92
+ const effectPathOfExpression = (node: ESTree.Node) =>
93
+ effectPathOfImported(importedExpression(node, context.sourceCode));
94
+
95
+ const reportNewHelper = (node: ESTree.Node, effectPath: readonly string[] | undefined) => {
96
+ if (effectPath === undefined) {
97
+ return;
98
+ }
99
+ if (testingHelperAt(effectPath.slice(0, -1)) !== undefined) {
100
+ return;
101
+ }
102
+ const helper = testingHelperAt(effectPath);
103
+ if (helper !== undefined) {
104
+ report(node, helper);
105
+ }
106
+ };
107
+
108
+ const checkModule = (
109
+ source: ESTree.StringLiteral,
110
+ specifiers: readonly { readonly node: ESTree.Node; readonly name: string }[],
111
+ ) => {
112
+ const modulePath = effectPathOf(source.value);
113
+ if (modulePath === undefined) {
114
+ return;
115
+ }
116
+ const helper = testingHelperAt(modulePath);
117
+ if (helper !== undefined) {
118
+ report(source, helper);
119
+ return;
120
+ }
121
+ for (const specifier of specifiers) {
122
+ reportNewHelper(specifier.node, [...modulePath, specifier.name]);
123
+ }
124
+ };
125
+
126
+ const qualifiedEffectPath = (name: ESTree.Node) => {
127
+ const [, ...members] = typeNameParts(name);
128
+ const rootPath = effectPathOfExpression(typeNameRoot(name));
129
+ return rootPath && [...rootPath, ...members];
130
+ };
131
+
132
+ return {
133
+ ImportDeclaration(declaration) {
134
+ if (importsEmptyBraces(declaration, context.sourceCode)) {
135
+ return;
136
+ }
137
+ checkModule(
138
+ declaration.source,
139
+ declaration.specifiers.flatMap((specifier) =>
140
+ specifier.type === "ImportSpecifier"
141
+ ? [{ node: specifier, name: moduleExportName(specifier.imported) }]
142
+ : [],
143
+ ),
144
+ );
145
+ },
146
+ MemberExpression(expression) {
147
+ const object = effectPathOfExpression(expression.object);
148
+ const member = memberName(expression);
149
+ const isNamedMemberOfEffectPath = object !== undefined && member !== undefined;
150
+ reportNewHelper(expression, isNamedMemberOfEffectPath ? [...object, member] : undefined);
151
+ },
152
+ TSQualifiedName(name) {
153
+ reportNewHelper(name, qualifiedEffectPath(name));
154
+ },
155
+ };
156
+ },
157
+ });
@@ -0,0 +1,71 @@
1
+ import {
2
+ equivalentModuleSpecifiers,
3
+ MODULE_PATTERN_WILDCARD,
4
+ moduleOfGlobalObject,
5
+ importsBan,
6
+ } from "../shared/bans-each-permission-lifts.ts";
7
+ import type { everyBanOf, moduleBan } from "../shared/bans-each-permission-lifts.ts";
8
+ import {
9
+ NODE_BUILTIN_RULES,
10
+ nodeBuiltinNameOf,
11
+ } from "../shared/lint-rule-messages-and-patterns.ts";
12
+
13
+ export const bansByKind = (bans: readonly ReturnType<typeof everyBanOf>[number][]) => ({
14
+ module: bans.flatMap((ban) => (ban.kind === "module" ? [ban] : [])),
15
+ imports: bans.flatMap((ban) => (ban.kind === "imports" ? [ban] : [])),
16
+ globalMembers: bans.flatMap((ban) => (ban.kind === "global-members" ? [ban] : [])),
17
+ effectMembers: bans.flatMap((ban) => (ban.kind === "effect-members" ? [ban] : [])),
18
+ memberNames: bans.flatMap((ban) => (ban.kind === "member-names" ? [ban] : [])),
19
+ dynamicImport: bans.flatMap((ban) => (ban.kind === "dynamic-import" ? [ban] : [])),
20
+ createdRequire: bans.flatMap((ban) => (ban.kind === "created-require" ? [ban] : [])),
21
+ });
22
+
23
+ const escapedForRegularExpression = (text: string) =>
24
+ text.replaceAll(/[.+?^${}()|[\]\\/]/gu, String.raw`\$&`);
25
+
26
+ export const modulePatternMatcher = (modulePattern: string) => {
27
+ const expressions = equivalentModuleSpecifiers(modulePattern).map(
28
+ (pattern) =>
29
+ new RegExp(
30
+ `^${pattern
31
+ .split(MODULE_PATTERN_WILDCARD)
32
+ .map((part) => escapedForRegularExpression(part))
33
+ .join(".*")}$`,
34
+ "u",
35
+ ),
36
+ );
37
+ return (specifier: string) => expressions.some((expression) => expression.test(specifier));
38
+ };
39
+
40
+ export const importBansEquivalentToGlobalMembers = (
41
+ bans: readonly ReturnType<typeof everyBanOf>[number][],
42
+ ) =>
43
+ bansByKind(bans).globalMembers.flatMap((ban) => {
44
+ const module = moduleOfGlobalObject(ban.globalObject);
45
+ return module === undefined ? [] : [importsBan(module, ban.memberNames)];
46
+ });
47
+
48
+ export const moduleBansMatcher = (moduleBans: readonly ReturnType<typeof moduleBan>[]) => {
49
+ const entries = moduleBans.flatMap((ban) =>
50
+ ban.modulePatterns.map((modulePattern) => ({
51
+ matchesModule: modulePatternMatcher(modulePattern),
52
+ exceptImportNames: ban.exceptImportNames,
53
+ })),
54
+ );
55
+ return (specifier: string, importNames: readonly (string | undefined)[]) =>
56
+ entries.some(
57
+ (entry) =>
58
+ entry.matchesModule(specifier) &&
59
+ !(
60
+ importNames.length > 0 &&
61
+ importNames.every((name) => name !== undefined && entry.exceptImportNames.includes(name))
62
+ ),
63
+ );
64
+ };
65
+
66
+ const MODULES_NODE_BUILTIN_RULES_BAN: ReadonlySet<string> = new Set(
67
+ Object.values(NODE_BUILTIN_RULES).flatMap(({ modules }) => modules),
68
+ );
69
+
70
+ export const isBannedByANodeBuiltinRule = (specifier: string) =>
71
+ MODULES_NODE_BUILTIN_RULES_BAN.has(nodeBuiltinNameOf(specifier));
@@ -0,0 +1,20 @@
1
+ import type { ESTree, SourceCode } from "@oxlint/plugins";
2
+
3
+ import { effectModuleOperation, resolvedVariable } from "./resolve-what-an-expression-imports.ts";
4
+
5
+ export const SERVICE_FACTORIES = new Set(["Service", "Reference"]);
6
+
7
+ const isVariableReference = (node: ESTree.Node, sourceCode: SourceCode) =>
8
+ resolvedVariable(node, sourceCode)?.references.some(({ identifier }) => identifier === node) ===
9
+ true;
10
+
11
+ export const serviceFactoryOf = (node: ESTree.Node, sourceCode: SourceCode) => {
12
+ const isMemberOrVariableReference =
13
+ node.type === "MemberExpression" || isVariableReference(node, sourceCode);
14
+ if (!isMemberOrVariableReference) {
15
+ return;
16
+ }
17
+ const factory = effectModuleOperation(node, sourceCode, "Context");
18
+ const isServiceFactory = factory !== undefined && SERVICE_FACTORIES.has(factory);
19
+ return isServiceFactory ? factory : undefined;
20
+ };
@@ -0,0 +1,44 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import type { ESTree } from "@oxlint/plugins";
3
+
4
+ const JOINING_OPERATORS: ReadonlySet<string> = new Set(["&&", "||"]);
5
+
6
+ const unwrappedNegation = (node: ESTree.Expression): ESTree.Expression => {
7
+ const isNegation = node.type === "UnaryExpression" && node.operator === "!";
8
+ return isNegation ? unwrappedNegation(node.argument) : node;
9
+ };
10
+
11
+ export const noUnnamedCompoundConditions = defineRule({
12
+ meta: {
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
+ messages: {
19
+ unnamed: "{{test}} condition joins checks with {{operator}} without a name",
20
+ },
21
+ },
22
+ create(context) {
23
+ const reportUnnamed = (test: ESTree.Expression, testKind: "if" | "ternary") => {
24
+ const condition = unwrappedNegation(test);
25
+ const joinsChecks =
26
+ condition.type === "LogicalExpression" && JOINING_OPERATORS.has(condition.operator);
27
+ if (joinsChecks) {
28
+ context.report({
29
+ node: test,
30
+ messageId: "unnamed",
31
+ data: { test: testKind, operator: condition.operator },
32
+ });
33
+ }
34
+ };
35
+ return {
36
+ IfStatement(node) {
37
+ reportUnnamed(node.test, "if");
38
+ },
39
+ ConditionalExpression(node) {
40
+ reportUnnamed(node.test, "ternary");
41
+ },
42
+ };
43
+ },
44
+ });
@@ -0,0 +1,141 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import type { ESTree } from "@oxlint/plugins";
3
+
4
+ import { inEnglish } from "./ban-abbreviated-words-in-declared-names.ts";
5
+
6
+ const CONSTANT_CASE = /^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$/u;
7
+
8
+ const isConstAssertion = (node: ESTree.Node) =>
9
+ node.type === "TSAsExpression" &&
10
+ node.typeAnnotation.type === "TSTypeReference" &&
11
+ node.typeAnnotation.typeName.type === "Identifier" &&
12
+ node.typeAnnotation.typeName.name === "const";
13
+
14
+ const isScalarLiteral = (node: ESTree.Node): boolean => {
15
+ if (node.type === "Literal") {
16
+ return !("regex" in node);
17
+ }
18
+ if (node.type === "Identifier") {
19
+ return node.name === "undefined";
20
+ }
21
+ if (node.type === "TemplateLiteral") {
22
+ return node.expressions.length === 0;
23
+ }
24
+ return (
25
+ node.type === "UnaryExpression" &&
26
+ node.operator === "-" &&
27
+ node.argument.type === "Literal" &&
28
+ typeof node.argument.value === "number"
29
+ );
30
+ };
31
+
32
+ const isFixedLiteral = (node: ESTree.Node): boolean => {
33
+ if (isScalarLiteral(node)) {
34
+ return true;
35
+ }
36
+ if (node.type === "ArrayExpression") {
37
+ return node.elements.every(
38
+ (element) => element !== null && element.type !== "SpreadElement" && isFixedLiteral(element),
39
+ );
40
+ }
41
+ if (node.type === "ObjectExpression") {
42
+ return node.properties.every(
43
+ (property) =>
44
+ property.type === "Property" &&
45
+ property.kind === "init" &&
46
+ !property.computed &&
47
+ !property.method &&
48
+ !property.shorthand &&
49
+ isFixedLiteral(property.value),
50
+ );
51
+ }
52
+ return false;
53
+ };
54
+
55
+ const literalUnder = (initializer: ESTree.Node): ESTree.Node => {
56
+ const isSatisfiesOrConstAssertion =
57
+ initializer.type === "TSSatisfiesExpression" ||
58
+ (initializer.type === "TSAsExpression" && isConstAssertion(initializer));
59
+ return isSatisfiesOrConstAssertion ? literalUnder(initializer.expression) : initializer;
60
+ };
61
+
62
+ const endsInAsConst = (initializer: ESTree.Node): boolean =>
63
+ isConstAssertion(initializer) ||
64
+ (initializer.type === "TSSatisfiesExpression" && endsInAsConst(initializer.expression));
65
+
66
+ const isModuleScope = (declaration: ESTree.VariableDeclaration) =>
67
+ declaration.parent.type === "Program" ||
68
+ (declaration.parent.type === "ExportNamedDeclaration" &&
69
+ declaration.parent.parent.type === "Program");
70
+
71
+ const isEmptyCollection = (node: ESTree.Node) =>
72
+ (node.type === "ObjectExpression" && node.properties.length === 0) ||
73
+ (node.type === "ArrayExpression" && node.elements.length === 0);
74
+
75
+ const MUTABLE_COLLECTION_TYPES = new Set(["Array", "Record"]);
76
+
77
+ const isMutableCollectionType = (annotation: ESTree.TSType) =>
78
+ annotation.type === "TSArrayType" ||
79
+ annotation.type === "TSTypeLiteral" ||
80
+ (annotation.type === "TSTypeReference" &&
81
+ annotation.typeName.type === "Identifier" &&
82
+ MUTABLE_COLLECTION_TYPES.has(annotation.typeName.name));
83
+
84
+ export const noLooseLiteralConstants = defineRule({
85
+ meta: {
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
+ messages: {
92
+ constant: "const {{name}} {{violations}}",
93
+ },
94
+ },
95
+ create(context) {
96
+ return {
97
+ VariableDeclarator(declarator) {
98
+ const declaration = declarator.parent;
99
+ const id = declarator.id;
100
+ const initializer = declarator.init;
101
+ if (declaration.type !== "VariableDeclaration") {
102
+ return;
103
+ }
104
+ const isInitializedConstName =
105
+ declaration.kind === "const" && id.type === "Identifier" && initializer !== null;
106
+ if (!isInitializedConstName) {
107
+ return;
108
+ }
109
+ const name = id.name;
110
+ const literal = literalUnder(initializer);
111
+ const isCollection =
112
+ literal.type === "ArrayExpression" || literal.type === "ObjectExpression";
113
+ const annotation = id.typeAnnotation?.typeAnnotation;
114
+ const isMutableAccumulator =
115
+ annotation !== undefined &&
116
+ isMutableCollectionType(annotation) &&
117
+ isEmptyCollection(literal) &&
118
+ !endsInAsConst(initializer);
119
+ const isFixed =
120
+ isModuleScope(declaration) && isFixedLiteral(literal) && !isMutableAccumulator;
121
+ const isAnnotatedNonAccumulatorCollection =
122
+ annotation !== undefined && isCollection && !isMutableAccumulator;
123
+ const isFixedButNotConstantCase = isFixed && !CONSTANT_CASE.test(name);
124
+ const isFixedCollectionWithoutAsConst =
125
+ isFixed && isCollection && !endsInAsConst(initializer);
126
+ const violations = [
127
+ ...(isAnnotatedNonAccumulatorCollection ? ["has a type annotation"] : []),
128
+ ...(isFixedButNotConstantCase ? ["is not in CAPITAL_SNAKE_CASE"] : []),
129
+ ...(isFixedCollectionWithoutAsConst ? ["does not end in as const"] : []),
130
+ ];
131
+ if (violations.length > 0) {
132
+ context.report({
133
+ node: id,
134
+ messageId: "constant",
135
+ data: { name, violations: inEnglish(violations) },
136
+ });
137
+ }
138
+ },
139
+ };
140
+ },
141
+ });
@@ -0,0 +1,64 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import type { ESTree, SourceCode } from "@oxlint/plugins";
3
+
4
+ import { serviceFactoryOf } from "./match-effect-service-factories.ts";
5
+
6
+ const isServiceFactoryCall = (expression: ESTree.Node, sourceCode: SourceCode): boolean =>
7
+ expression.type === "CallExpression"
8
+ ? isServiceFactoryCall(expression.callee, sourceCode)
9
+ : serviceFactoryOf(expression, sourceCode) !== undefined;
10
+
11
+ const staticKeySuffix = (expression: ESTree.Node | undefined): string | undefined => {
12
+ if (expression?.type === "Literal") {
13
+ const value = expression.value;
14
+ if (typeof value === "string") {
15
+ return value.split("/").at(-1);
16
+ }
17
+ }
18
+ if (expression?.type === "TemplateLiteral") {
19
+ const ending = expression.quasis.at(-1)?.value.cooked;
20
+ const hasStaticLastSegment = expression.expressions.length === 0 || ending?.includes("/");
21
+ if (hasStaticLastSegment === true) {
22
+ return ending?.split("/").at(-1);
23
+ }
24
+ }
25
+ return undefined;
26
+ };
27
+
28
+ export const noMismatchedServiceKeys = defineRule({
29
+ meta: {
30
+ type: "problem",
31
+ docs: { description: "Require Effect service keys to end with their class name." },
32
+ messages: {
33
+ key: "the service key ends with {{suffix}}, not {{className}}",
34
+ unknownKey: "the service key of {{className}} has no static last segment",
35
+ },
36
+ },
37
+ create(context) {
38
+ const checkClass = (declaration: ESTree.Node) => {
39
+ const isClassDeclarationOrExpression =
40
+ declaration.type === "ClassDeclaration" || declaration.type === "ClassExpression";
41
+ if (!isClassDeclarationOrExpression) {
42
+ return;
43
+ }
44
+ const parent = declaration.superClass;
45
+ if (parent?.type !== "CallExpression") {
46
+ return;
47
+ }
48
+ if (!isServiceFactoryCall(parent.callee, context.sourceCode)) {
49
+ return;
50
+ }
51
+ if (declaration.id === null) {
52
+ return;
53
+ }
54
+ const suffix = staticKeySuffix(parent.arguments[0]);
55
+ const className = declaration.id.name;
56
+ if (suffix === undefined) {
57
+ context.report({ node: parent, messageId: "unknownKey", data: { className } });
58
+ } else if (suffix !== className) {
59
+ context.report({ node: parent, messageId: "key", data: { suffix, className } });
60
+ }
61
+ };
62
+ return { ClassDeclaration: checkClass, ClassExpression: checkClass };
63
+ },
64
+ });