@sshkeda/check 0.0.0-stage → 1.0.1

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 +848 -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,310 @@
1
+ import { Effect, FileSystem, Option, Path, Result } from "effect";
2
+ import { parseSync } from "oxc-parser";
3
+ import type { ExportDefaultDeclaration, ImportDeclaration, Program } from "oxc-parser";
4
+
5
+ import {
6
+ FORMAT_CONFIG_BINDING,
7
+ FORMAT_CONFIG_FILE,
8
+ LINT_CONFIG_FILE,
9
+ } from "../../../shared/config-file-names.ts";
10
+ import { lineNumberAt } from "./count-the-line-of-an-offset.ts";
11
+ import { runtimeModuleResolver } from "./create-the-module-resolver.ts";
12
+ import type { CONFIG_FILE_VIOLATION } from "./project-config-and-tool-errors.ts";
13
+ import {
14
+ CHECK_PACKAGE,
15
+ ConfigFileExportsSomethingElse,
16
+ ConfigFileHoldsOtherStatement,
17
+ ConfigFileHoldsSomethingElse,
18
+ ConfigFileImportsFromOutsideCheck,
19
+ ConfigFileMissing,
20
+ ConfigFileMissingDefaultExport,
21
+ ConfigFileMissingImport,
22
+ ConfigFileUnparsable,
23
+ ModuleRequestUnresolvable,
24
+ } from "./project-config-and-tool-errors.ts";
25
+ import { namedPackageManifestAt } from "./read-named-package-manifests.ts";
26
+
27
+ export const LINT_CONFIG = {
28
+ file: LINT_CONFIG_FILE,
29
+ binding: "lintConfig",
30
+ argumentShapes: ["{ ... }"],
31
+ } as const;
32
+
33
+ export const FORMAT_CONFIG = {
34
+ file: FORMAT_CONFIG_FILE,
35
+ binding: FORMAT_CONFIG_BINDING,
36
+ argumentShapes: undefined,
37
+ } as const;
38
+
39
+ const INSTALLED_PACKAGES_DIRECTORY = "node_modules";
40
+
41
+ const realPathOf = (file: string) =>
42
+ FileSystem.FileSystem.use((fileSystem) => fileSystem.realPath(file));
43
+
44
+ const defaultExportOf = (config: typeof FORMAT_CONFIG | typeof LINT_CONFIG) =>
45
+ config.argumentShapes === undefined
46
+ ? config.binding
47
+ : `${config.binding}(${config.argumentShapes.join(", ")})`;
48
+
49
+ const importOf = (config: Parameters<typeof defaultExportOf>[0]) =>
50
+ `import { ${config.binding} } from "${CHECK_PACKAGE}"`;
51
+
52
+ const configCallOf = (
53
+ config: Parameters<typeof defaultExportOf>[0],
54
+ exported: ExportDefaultDeclaration,
55
+ ) => {
56
+ const call = exported.declaration;
57
+ const isCallOfTheBinding =
58
+ config.argumentShapes !== undefined &&
59
+ call.type === "CallExpression" &&
60
+ call.callee.type === "Identifier" &&
61
+ call.callee.name === config.binding &&
62
+ call.arguments.length === config.argumentShapes.length;
63
+ return isCallOfTheBinding ? call : undefined;
64
+ };
65
+
66
+ const exportsTheConfig = (
67
+ config: Parameters<typeof defaultExportOf>[0],
68
+ exported: ExportDefaultDeclaration,
69
+ call: ReturnType<typeof configCallOf>,
70
+ ) =>
71
+ config.argumentShapes === undefined
72
+ ? exported.declaration.type === "Identifier" && exported.declaration.name === config.binding
73
+ : call !== undefined;
74
+
75
+ const exportViolationsOf = (
76
+ config: Parameters<typeof defaultExportOf>[0],
77
+ file: string,
78
+ exported: ExportDefaultDeclaration | undefined,
79
+ call: ReturnType<typeof configCallOf>,
80
+ lineOf: (offset: number) => string,
81
+ ) => {
82
+ if (exported === undefined) {
83
+ return [
84
+ new ConfigFileMissingDefaultExport({
85
+ file,
86
+ defaultExport: defaultExportOf(config),
87
+ }),
88
+ ];
89
+ }
90
+ if (!exportsTheConfig(config, exported, call)) {
91
+ return [
92
+ new ConfigFileExportsSomethingElse({
93
+ location: lineOf(exported.declaration.start),
94
+ defaultExport: defaultExportOf(config),
95
+ }),
96
+ ];
97
+ }
98
+ return [];
99
+ };
100
+
101
+ const importsOnlyTheBinding = (
102
+ config: Parameters<typeof defaultExportOf>[0],
103
+ statement: ImportDeclaration,
104
+ ) => {
105
+ const [specifier, ...otherSpecifiers] = statement.specifiers;
106
+ const importsOneValue =
107
+ statement.importKind !== "type" &&
108
+ otherSpecifiers.length === 0 &&
109
+ specifier?.type === "ImportSpecifier" &&
110
+ specifier.importKind !== "type";
111
+ return (
112
+ importsOneValue &&
113
+ specifier.imported.type === "Identifier" &&
114
+ specifier.imported.name === config.binding &&
115
+ specifier.local.name === config.binding
116
+ );
117
+ };
118
+
119
+ const statementViolationsOf = (
120
+ config: Parameters<typeof defaultExportOf>[0],
121
+ file: string,
122
+ body: Program["body"],
123
+ exported: ExportDefaultDeclaration | undefined,
124
+ bindingImport: ImportDeclaration | undefined,
125
+ lineOf: (offset: number) => string,
126
+ ) => {
127
+ const otherStatements = body.filter(
128
+ (statement) => statement !== exported && statement !== bindingImport,
129
+ );
130
+ const lacksOnlyTheImport = bindingImport === undefined && otherStatements.length === 0;
131
+ return lacksOnlyTheImport
132
+ ? [new ConfigFileMissingImport({ file, configImport: importOf(config) })]
133
+ : otherStatements.map(
134
+ (statement) =>
135
+ new ConfigFileHoldsOtherStatement({
136
+ location: lineOf(statement.start),
137
+ configImport: importOf(config),
138
+ defaultExport: defaultExportOf(config),
139
+ }),
140
+ );
141
+ };
142
+
143
+ const STATEMENTS_OF_A_CONFIG_WITHOUT_ARGUMENTS = 2;
144
+
145
+ const shapeViolationsOf = (
146
+ config: Parameters<typeof defaultExportOf>[0],
147
+ file: string,
148
+ body: Program["body"],
149
+ exported: ExportDefaultDeclaration | undefined,
150
+ call: ReturnType<typeof configCallOf>,
151
+ bindingImport: ImportDeclaration | undefined,
152
+ lineOf: (offset: number) => string,
153
+ ) => {
154
+ if (config.argumentShapes !== undefined) {
155
+ return [
156
+ ...exportViolationsOf(config, file, exported, call, lineOf),
157
+ ...statementViolationsOf(config, file, body, exported, bindingImport, lineOf),
158
+ ];
159
+ }
160
+ const holdsOnlyTheImportAndTheExport =
161
+ body.length === STATEMENTS_OF_A_CONFIG_WITHOUT_ARGUMENTS &&
162
+ bindingImport !== undefined &&
163
+ exported !== undefined &&
164
+ exportsTheConfig(config, exported, call);
165
+ return holdsOnlyTheImportAndTheExport
166
+ ? []
167
+ : [
168
+ new ConfigFileHoldsSomethingElse({
169
+ file,
170
+ configImport: importOf(config),
171
+ defaultExport: defaultExportOf(config),
172
+ }),
173
+ ];
174
+ };
175
+
176
+ export const parsedConfigFile = (
177
+ config: Parameters<typeof defaultExportOf>[0],
178
+ file: string,
179
+ text: string,
180
+ ) => {
181
+ const parsed = parseSync(file, text);
182
+ if (parsed.errors.length > 0) {
183
+ return Result.fail<readonly (typeof CONFIG_FILE_VIOLATION.Type)[]>(
184
+ parsed.errors.map((error) => new ConfigFileUnparsable({ file, reason: error.message })),
185
+ );
186
+ }
187
+ const lineOf = (offset: number) => `${file}:${lineNumberAt(text, offset)}`;
188
+ const { body } = parsed.program;
189
+ const exported = body.find((statement) => statement.type === "ExportDefaultDeclaration");
190
+ const bindingImport = body
191
+ .filter((statement) => statement.type === "ImportDeclaration")
192
+ .find((statement) => importsOnlyTheBinding(config, statement));
193
+ const call = exported === undefined ? undefined : configCallOf(config, exported);
194
+ return Result.succeed({
195
+ call,
196
+ bindingImport,
197
+ violations: shapeViolationsOf(config, file, body, exported, call, bindingImport, lineOf),
198
+ lineOf,
199
+ });
200
+ };
201
+
202
+ export const isFormatConfigTextRefusedIn = (directory: string) =>
203
+ Effect.gen(function* () {
204
+ const fileSystem = yield* FileSystem.FileSystem;
205
+ const path = yield* Path.Path;
206
+ const configFile = path.join(directory, FORMAT_CONFIG.file);
207
+ if (!(yield* fileSystem.exists(configFile))) {
208
+ return false;
209
+ }
210
+ const parsed = parsedConfigFile(
211
+ FORMAT_CONFIG,
212
+ FORMAT_CONFIG.file,
213
+ yield* fileSystem.readFileString(configFile),
214
+ );
215
+ return Result.isFailure(parsed) || parsed.success.violations.length > 0;
216
+ });
217
+
218
+ const isCheckPackage = (packageManifest: string | undefined, projectDirectory: string) =>
219
+ Effect.gen(function* () {
220
+ const path = yield* Path.Path;
221
+ if (packageManifest === undefined) {
222
+ return false;
223
+ }
224
+ const packageRoot = path.dirname(packageManifest);
225
+ const isInstalledOrTheProject =
226
+ packageRoot === projectDirectory ||
227
+ packageRoot.split(path.sep).includes(INSTALLED_PACKAGES_DIRECTORY);
228
+ if (!isInstalledOrTheProject) {
229
+ return false;
230
+ }
231
+ return Option.exists(
232
+ yield* namedPackageManifestAt(packageManifest),
233
+ ({ name }) => name === CHECK_PACKAGE,
234
+ );
235
+ });
236
+
237
+ const importViolationsOf = (
238
+ config: Parameters<typeof defaultExportOf>[0],
239
+ directory: string,
240
+ bindingImport: ImportDeclaration,
241
+ lineOf: (offset: number) => string,
242
+ ) =>
243
+ Effect.gen(function* () {
244
+ const path = yield* Path.Path;
245
+ const projectDirectory = yield* realPathOf(directory);
246
+ const request = bindingImport.source.value;
247
+ const location = lineOf(bindingImport.start);
248
+ const resolved = yield* Effect.tryPromise({
249
+ try: () =>
250
+ runtimeModuleResolver().resolveFileAsync(path.join(projectDirectory, config.file), request),
251
+ catch: (cause) =>
252
+ new ModuleRequestUnresolvable({ request, importingFile: location, reason: String(cause) }),
253
+ });
254
+ if (resolved.path === undefined) {
255
+ return [
256
+ new ModuleRequestUnresolvable({
257
+ request,
258
+ importingFile: location,
259
+ reason: resolved.error ?? "the resolver returned no file and no error",
260
+ }),
261
+ ];
262
+ }
263
+ if (yield* isCheckPackage(resolved.packageJsonPath, projectDirectory)) {
264
+ return [];
265
+ }
266
+ return [
267
+ new ConfigFileImportsFromOutsideCheck({
268
+ location,
269
+ binding: config.binding,
270
+ request,
271
+ resolvedFile: path.relative(projectDirectory, resolved.path),
272
+ }),
273
+ ];
274
+ });
275
+
276
+ export const parsedConfigFileIn = (
277
+ directory: string,
278
+ config: Parameters<typeof defaultExportOf>[0],
279
+ ) =>
280
+ Effect.gen(function* () {
281
+ const fileSystem = yield* FileSystem.FileSystem;
282
+ const path = yield* Path.Path;
283
+ const configFile = path.join(directory, config.file);
284
+ if (!(yield* fileSystem.exists(configFile))) {
285
+ return Result.fail<readonly (typeof CONFIG_FILE_VIOLATION.Type)[]>([
286
+ new ConfigFileMissing({ file: config.file }),
287
+ ]);
288
+ }
289
+ const parsed = parsedConfigFile(
290
+ config,
291
+ config.file,
292
+ yield* fileSystem.readFileString(configFile),
293
+ );
294
+ if (Result.isFailure(parsed)) {
295
+ return parsed;
296
+ }
297
+ const { bindingImport, violations, lineOf } = parsed.success;
298
+ const namesTheImportToWrite = config.argumentShapes === undefined && violations.length > 0;
299
+ const hasNoImportToResolve = bindingImport === undefined || namesTheImportToWrite;
300
+ if (hasNoImportToResolve) {
301
+ return parsed;
302
+ }
303
+ return Result.succeed({
304
+ ...parsed.success,
305
+ violations: [
306
+ ...violations,
307
+ ...(yield* importViolationsOf(config, directory, bindingImport, lineOf)),
308
+ ],
309
+ });
310
+ });
@@ -0,0 +1,99 @@
1
+ import { ReportedErrors } from "@sshkeda/task/errors";
2
+ import type { PrintableError } from "@sshkeda/task/errors";
3
+ import { Array as Arrays, Cause, Effect, Predicate, Schema } from "effect";
4
+
5
+ import { printableError } from "../../../shared/format-reported-failures.ts";
6
+ import { ToolExited } from "./project-config-and-tool-errors.ts";
7
+ import type { ToolReportUndecodable } from "./project-config-and-tool-errors.ts";
8
+
9
+ const isToolExited = Schema.is(ToolExited);
10
+
11
+ const annotateFailure = (tool: string, failure: Parameters<typeof printableError>[0]) => {
12
+ const hasCommand =
13
+ Predicate.hasProperty(failure, "command") && Predicate.isString(failure.command);
14
+ return Effect.annotateCurrentSpan({
15
+ "check.tool": tool,
16
+ "check.failure": Cause.pretty(Cause.fail(failure)),
17
+ ...(hasCommand ? { "check.command": failure.command } : {}),
18
+ });
19
+ };
20
+
21
+ export const failQuietly = (
22
+ tool: string,
23
+ failure: Parameters<typeof printableError>[0],
24
+ reportedErrors: Arrays.NonEmptyReadonlyArray<PrintableError>,
25
+ ) =>
26
+ Effect.gen(function* () {
27
+ yield* annotateFailure(tool, failure);
28
+ return yield* new ReportedErrors({ reportedErrors: [...reportedErrors] });
29
+ });
30
+
31
+ export const couldNotRun = (tool: string, failure: Parameters<typeof printableError>[0]) =>
32
+ failQuietly(tool, failure, [printableError(failure)]);
33
+
34
+ const eachViolationOf = (
35
+ failure: Parameters<typeof printableError>[0] & {
36
+ readonly violations?: readonly Parameters<typeof printableError>[0][];
37
+ },
38
+ ) => {
39
+ const hasViolations =
40
+ failure.violations !== undefined && Arrays.isReadonlyArrayNonEmpty(failure.violations);
41
+ return new ReportedErrors({
42
+ reportedErrors: hasViolations
43
+ ? Arrays.map(failure.violations, printableError)
44
+ : [printableError(failure)],
45
+ });
46
+ };
47
+
48
+ const exitError = (exited: ToolExited, notes: readonly string[]): PrintableError => ({
49
+ tag: exited._tag,
50
+ message: [`${exited.program} exited with code ${exited.code}`, ...notes, exited.standardError]
51
+ .filter((line) => line !== "")
52
+ .join("\n"),
53
+ });
54
+
55
+ export const quietToolFailure =
56
+ <ReportRequirements = never>(
57
+ tool: string,
58
+ reportOf: (exited: ToolExited) => Effect.Effect<
59
+ {
60
+ readonly violations: readonly PrintableError[];
61
+ readonly notes: readonly string[];
62
+ },
63
+ ToolReportUndecodable,
64
+ ReportRequirements
65
+ >,
66
+ ) =>
67
+ <Success, Failure extends Parameters<typeof eachViolationOf>[0], Requirements>(
68
+ effect: Effect.Effect<Success, Failure | ToolExited, Requirements>,
69
+ ): Effect.Effect<Success, ReportedErrors, ReportRequirements | Requirements> =>
70
+ effect.pipe(
71
+ Effect.tapError((error) =>
72
+ isToolExited(error) ? Effect.void : annotateFailure(tool, error),
73
+ ),
74
+ Effect.mapError((error) => (isToolExited(error) ? error : eachViolationOf(error))),
75
+ Effect.catchTag("ToolExited", (exited) =>
76
+ reportOf(exited).pipe(
77
+ Effect.flatMap((report) =>
78
+ failQuietly(
79
+ tool,
80
+ exited,
81
+ Arrays.append(report.violations, exitError(exited, report.notes)),
82
+ ),
83
+ ),
84
+ Effect.catchTag("ToolReportUndecodable", (unreadableReport) =>
85
+ failQuietly(tool, exited, [printableError(unreadableReport), exitError(exited, [])]),
86
+ ),
87
+ ),
88
+ ),
89
+ );
90
+
91
+ export const quietViolations =
92
+ (tool: string) =>
93
+ <Success, Requirements>(
94
+ effect: Effect.Effect<Success, Parameters<typeof eachViolationOf>[0], Requirements>,
95
+ ): Effect.Effect<Success, ReportedErrors, Requirements> =>
96
+ effect.pipe(
97
+ Effect.tapError((failure) => annotateFailure(tool, failure)),
98
+ Effect.mapError(eachViolationOf),
99
+ );