@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,262 @@
1
+ import { Array as Arrays, Effect } from "effect";
2
+ import type { Node, SourceFile } from "typescript/unstable/ast";
3
+ import {
4
+ isExportDeclaration,
5
+ isImportDeclaration,
6
+ isStringLiteralLikeNode,
7
+ } from "typescript/unstable/ast/is";
8
+ import { SymbolFlags } from "typescript/unstable/async";
9
+ import type { Checker, Symbol as TypeScriptSymbol } from "typescript/unstable/async";
10
+
11
+ import {
12
+ TASK_NAMESPACE_EXPORT,
13
+ TASK_PACKAGE,
14
+ TASK_MAIN_MEMBER,
15
+ } from "../../../../../shared/bans-each-permission-lifts.ts";
16
+ import {
17
+ CONCURRENT_TYPESCRIPT_REQUESTS,
18
+ followedImportAliases,
19
+ packageFileOf,
20
+ typeScriptRequest,
21
+ } from "../../../shared/query-the-typescript-project.ts";
22
+
23
+ const EFFECT_PACKAGE = "effect";
24
+ const PLATFORM_PACKAGES = "@effect/platform-";
25
+ const SCOPE_PREFIX = "@";
26
+ const SCOPED_NAME_SEGMENTS = 2;
27
+ const NAME_SEGMENTS = 1;
28
+ const NO_MEMBERS = [] as const;
29
+ const PACKAGE_ROOT = "";
30
+ export const EFFECT_MODULE = "Effect";
31
+ export const LAYER_MODULE = "Layer";
32
+ const MANAGED_RUNTIME = "ManagedRuntime";
33
+ const RUNTIME_MODULE = "Runtime";
34
+ const MAKE_RUN_MAIN = "makeRunMain";
35
+ const RUN_MAIN = "runMain";
36
+ const RUNNERS = [
37
+ "runFork",
38
+ "runCallback",
39
+ "runPromise",
40
+ "runPromiseExit",
41
+ "runSync",
42
+ "runSyncExit",
43
+ ] as const;
44
+ const RUNNERS_WITH_SERVICES = RUNNERS.map((runner) => `${runner}With`);
45
+ const DIRECT_CALLS: ReadonlySet<boolean> = new Set([false]);
46
+ const RETURNED_CALLS: ReadonlySet<boolean> = new Set([true]);
47
+ const EVERY_CALL: ReadonlySet<boolean> = new Set([false, true]);
48
+
49
+ const isEffectPackage = (packageName: string) => packageName === EFFECT_PACKAGE;
50
+
51
+ const isPlatformPackage = (packageName: string) => packageName.startsWith(PLATFORM_PACKAGES);
52
+
53
+ const isTaskPackage = (packageName: string) => packageName === TASK_PACKAGE;
54
+
55
+ const PROGRAM_STARTS = [
56
+ {
57
+ isPackage: isEffectPackage,
58
+ module: EFFECT_MODULE,
59
+ exports: RUNNERS,
60
+ members: NO_MEMBERS,
61
+ returnedBySignature: DIRECT_CALLS,
62
+ },
63
+ {
64
+ isPackage: isEffectPackage,
65
+ module: EFFECT_MODULE,
66
+ exports: RUNNERS_WITH_SERVICES,
67
+ members: NO_MEMBERS,
68
+ returnedBySignature: RETURNED_CALLS,
69
+ },
70
+ {
71
+ isPackage: isEffectPackage,
72
+ module: MANAGED_RUNTIME,
73
+ exports: [MANAGED_RUNTIME],
74
+ members: RUNNERS,
75
+ returnedBySignature: EVERY_CALL,
76
+ },
77
+ {
78
+ isPackage: isEffectPackage,
79
+ module: RUNTIME_MODULE,
80
+ exports: [MAKE_RUN_MAIN],
81
+ members: NO_MEMBERS,
82
+ returnedBySignature: RETURNED_CALLS,
83
+ },
84
+ {
85
+ isPackage: isPlatformPackage,
86
+ module: undefined,
87
+ exports: [RUN_MAIN],
88
+ members: NO_MEMBERS,
89
+ returnedBySignature: EVERY_CALL,
90
+ },
91
+ {
92
+ isPackage: isTaskPackage,
93
+ module: PACKAGE_ROOT,
94
+ exports: [TASK_NAMESPACE_EXPORT],
95
+ members: [TASK_MAIN_MEMBER],
96
+ returnedBySignature: DIRECT_CALLS,
97
+ },
98
+ ] as const;
99
+
100
+ const packageModuleOf = (specifier: string) => {
101
+ const segments = specifier.split("/");
102
+ const nameLength = specifier.startsWith(SCOPE_PREFIX) ? SCOPED_NAME_SEGMENTS : NAME_SEGMENTS;
103
+ return {
104
+ packageName: segments.slice(0, nameLength).join("/"),
105
+ module: segments.slice(nameLength).join("/"),
106
+ };
107
+ };
108
+
109
+ const isWantedPackage = (packageName: string) =>
110
+ isEffectPackage(packageName) || PROGRAM_STARTS.some(({ isPackage }) => isPackage(packageName));
111
+
112
+ const wantedModulesOf = (
113
+ packageName: string,
114
+ operations: readonly { readonly module: string }[],
115
+ ) => {
116
+ const starts = PROGRAM_STARTS.filter(({ isPackage }) => isPackage(packageName));
117
+ const modules = [
118
+ ...starts.map(({ module }) => module),
119
+ ...(isEffectPackage(packageName) ? operations.map(({ module }) => module) : []),
120
+ ];
121
+ return modules.includes(undefined) ? undefined : new Set(modules);
122
+ };
123
+
124
+ const packageImportsIn = (sourceFiles: readonly { readonly sourceFile: SourceFile }[]) =>
125
+ sourceFiles.flatMap(({ sourceFile }) =>
126
+ sourceFile.statements.flatMap((statement) => {
127
+ const isModuleRequest = isImportDeclaration(statement) || isExportDeclaration(statement);
128
+ const specifier = isModuleRequest ? statement.moduleSpecifier : undefined;
129
+ const isPackageRequest = specifier !== undefined && isStringLiteralLikeNode(specifier);
130
+ return isPackageRequest ? [{ specifier, ...packageModuleOf(specifier.text) }] : [];
131
+ }),
132
+ );
133
+
134
+ const definedSymbols = (symbols: readonly (TypeScriptSymbol | undefined)[]) =>
135
+ symbols.filter((symbol) => symbol !== undefined);
136
+
137
+ const publicModulesOf = (
138
+ checker: Checker,
139
+ imported: {
140
+ readonly packageName: string;
141
+ readonly module: string;
142
+ readonly symbol: TypeScriptSymbol;
143
+ },
144
+ wanted: ReadonlySet<string | undefined> | undefined,
145
+ ) =>
146
+ Effect.gen(function* () {
147
+ if (imported.module !== PACKAGE_ROOT) {
148
+ return [imported];
149
+ }
150
+ const exported = yield* typeScriptRequest("list a package's exports", () =>
151
+ checker.getExportsOfModule(imported.symbol),
152
+ );
153
+ const named = exported.filter(({ name }) => wanted === undefined || wanted.has(name));
154
+ const targets = yield* followedImportAliases(checker, named);
155
+ const namespaces = Arrays.zip(named, targets).flatMap(([alias, target]) => {
156
+ const isNamespace = target !== undefined && (target.flags & SymbolFlags.ValueModule) !== 0;
157
+ return isNamespace
158
+ ? [{ packageName: imported.packageName, module: alias.name, symbol: target }]
159
+ : [];
160
+ });
161
+ return [imported, ...namespaces];
162
+ });
163
+
164
+ const exportsOf = (checker: Checker, module: TypeScriptSymbol, names: readonly string[]) =>
165
+ Effect.gen(function* () {
166
+ const exported = yield* typeScriptRequest("list a module's exports", () =>
167
+ checker.getExportsOfModule(module),
168
+ );
169
+ return definedSymbols(
170
+ yield* followedImportAliases(
171
+ checker,
172
+ exported.filter(({ name }) => names.includes(name)),
173
+ ),
174
+ );
175
+ });
176
+
177
+ const membersOf = (checker: Checker, symbol: TypeScriptSymbol, names: readonly string[]) =>
178
+ Effect.gen(function* () {
179
+ const valueType = yield* typeScriptRequest("read an export's type", () =>
180
+ checker.getTypeOfSymbol(symbol),
181
+ );
182
+ const declaredType = yield* typeScriptRequest("read an export's declared type", () =>
183
+ checker.getDeclaredTypeOfSymbol(symbol),
184
+ );
185
+ const types = [valueType, declaredType].filter((type) => type !== undefined);
186
+ const members = yield* Effect.forEach(types, (type) =>
187
+ Effect.forEach(names, (name) =>
188
+ typeScriptRequest("read an export's member", () => checker.getPropertyOfType(type, name)),
189
+ ),
190
+ );
191
+ return definedSymbols(yield* followedImportAliases(checker, members.flat()));
192
+ });
193
+
194
+ export const importedPackageApis = (
195
+ checker: Checker,
196
+ projectDirectory: string,
197
+ sourceFiles: readonly { readonly sourceFile: SourceFile }[],
198
+ operations: readonly { readonly module: string; readonly names: ReadonlySet<string> }[],
199
+ ) =>
200
+ Effect.gen(function* () {
201
+ const imports = packageImportsIn(sourceFiles).filter(({ packageName }) =>
202
+ isWantedPackage(packageName),
203
+ );
204
+ const symbols = yield* typeScriptRequest("resolve imported packages", () =>
205
+ checker.getSymbolAtLocation(imports.map(({ specifier }) => specifier)),
206
+ );
207
+ const resolved = new Map(
208
+ imports.flatMap(({ packageName, module }, index) => {
209
+ const symbol = symbols[index];
210
+ return symbol === undefined
211
+ ? []
212
+ : [[`${packageName}\0${module}\0${symbol.id}`, { packageName, module, symbol }] as const];
213
+ }),
214
+ );
215
+ const modules = (yield* Effect.forEach(
216
+ resolved.values(),
217
+ (imported) =>
218
+ publicModulesOf(checker, imported, wantedModulesOf(imported.packageName, operations)),
219
+ { concurrency: CONCURRENT_TYPESCRIPT_REQUESTS },
220
+ )).flat();
221
+ const effectExports = new Map<number, string>();
222
+ for (const { module, names } of operations) {
223
+ const effectModules = modules.filter(
224
+ (candidate) => isEffectPackage(candidate.packageName) && candidate.module === module,
225
+ );
226
+ for (const { symbol } of effectModules) {
227
+ for (const exported of yield* exportsOf(checker, symbol, [...names])) {
228
+ effectExports.set(exported.id, module);
229
+ }
230
+ }
231
+ }
232
+ const programStarts = new Map<Node, ReadonlySet<boolean>>();
233
+ const programStartFiles = new Set<string>();
234
+ for (const start of PROGRAM_STARTS) {
235
+ const startModules = modules.filter(
236
+ ({ packageName, module }) =>
237
+ start.isPackage(packageName) && (start.module === undefined || start.module === module),
238
+ );
239
+ for (const { symbol } of startModules) {
240
+ const exported = yield* exportsOf(checker, symbol, start.exports);
241
+ const { members } = start;
242
+ const callees =
243
+ members.length === 0
244
+ ? exported
245
+ : (yield* Effect.forEach(exported, (owner) =>
246
+ membersOf(checker, owner, members),
247
+ )).flat();
248
+ for (const declaration of callees.flatMap(({ declarations }) => declarations)) {
249
+ const file = packageFileOf(projectDirectory, declaration.path);
250
+ const node = yield* typeScriptRequest("read a program start's declaration", () =>
251
+ declaration.resolve(),
252
+ );
253
+ const isDeclaredInAPackage = file !== undefined && node !== undefined;
254
+ if (isDeclaredInAPackage) {
255
+ programStartFiles.add(file);
256
+ programStarts.set(node, start.returnedBySignature);
257
+ }
258
+ }
259
+ }
260
+ }
261
+ return { projectDirectory, effectExports, programStarts, programStartFiles };
262
+ });
@@ -0,0 +1 @@
1
+ export { findUngrantedStdioUses } from "./internal/find-ungranted-stdio-uses.ts";
@@ -0,0 +1,219 @@
1
+ import { Effect, Path } from "effect";
2
+ import type { Node, SourceFile } from "typescript/unstable/ast";
3
+ import {
4
+ isBindingElement,
5
+ isComputedPropertyName,
6
+ isElementAccessExpression,
7
+ isIdentifier,
8
+ isNoSubstitutionTemplateLiteral,
9
+ isNumericLiteral,
10
+ isObjectBindingPattern,
11
+ isPropertyAccessExpression,
12
+ isStringLiteral,
13
+ } from "typescript/unstable/ast/is";
14
+ import { isStringLiteralType, isTypeParameter, isUnionType } from "typescript/unstable/async";
15
+ import type { Checker, Type } from "typescript/unstable/async";
16
+
17
+ import { loadedLintConfigIn } from "../../../shared/load-the-lint-config.ts";
18
+ import {
19
+ CONCURRENT_TYPESCRIPT_REQUESTS,
20
+ nodePosition,
21
+ parentOf,
22
+ requiredTypeScriptRequest,
23
+ typeScriptRequest,
24
+ } from "../../../shared/query-the-typescript-project.ts";
25
+ import type { openTypeScriptProjects } from "../../../shared/query-the-typescript-project.ts";
26
+ import {
27
+ StdioArgumentsReadWithoutGrant,
28
+ StdioTerminalWriteWithoutGrant,
29
+ StdioUsesRefused,
30
+ } from "./stdio-use-errors.ts";
31
+
32
+ const STDIO_TYPE_ID = "~effect/Stdio";
33
+
34
+ const PERMISSION_OF_STDIO_MEMBER = new Map<string, "readsConfig" | "writesToTheTerminal">([
35
+ ["args", "readsConfig"],
36
+ ["stdout", "writesToTheTerminal"],
37
+ ["stderr", "writesToTheTerminal"],
38
+ ]);
39
+
40
+ const literalKeyText = (key: Node) => {
41
+ const isNamedOrLiteralKey =
42
+ isIdentifier(key) ||
43
+ isStringLiteral(key) ||
44
+ isNoSubstitutionTemplateLiteral(key) ||
45
+ isNumericLiteral(key);
46
+ return isNamedOrLiteralKey ? key.text : undefined;
47
+ };
48
+
49
+ const literalRead = (receiver: Node, text: string | undefined) => {
50
+ const namesAGrantedMember = text !== undefined && PERMISSION_OF_STDIO_MEMBER.has(text);
51
+ return namesAGrantedMember ? { receiver, member: text, key: undefined } : undefined;
52
+ };
53
+
54
+ const keyExpressionRead = (receiver: Node, key: Node) => {
55
+ const text = isIdentifier(key) ? undefined : literalKeyText(key);
56
+ if (text !== undefined) {
57
+ return literalRead(receiver, text);
58
+ }
59
+ return { receiver, member: undefined, key };
60
+ };
61
+
62
+ const possibleStdioRead = (node: Node) => {
63
+ if (isPropertyAccessExpression(node)) {
64
+ return literalRead(node.expression, node.name.text);
65
+ }
66
+ if (isElementAccessExpression(node)) {
67
+ return keyExpressionRead(node.expression, node.argumentExpression);
68
+ }
69
+ if (!isBindingElement(node)) {
70
+ return;
71
+ }
72
+ const pattern = parentOf(node);
73
+ const isInObjectPattern = pattern !== undefined && isObjectBindingPattern(pattern);
74
+ if (!isInObjectPattern) {
75
+ return;
76
+ }
77
+ const key = node.propertyName ?? node.name;
78
+ if (key === undefined) {
79
+ return;
80
+ }
81
+ if (isComputedPropertyName(key)) {
82
+ return keyExpressionRead(pattern, key.expression);
83
+ }
84
+ return literalRead(pattern, literalKeyText(key));
85
+ };
86
+
87
+ const possibleStdioReadsIn = (sourceFile: SourceFile) => {
88
+ const reads: NonNullable<ReturnType<typeof possibleStdioRead>>[] = [];
89
+ const visit = (node: Node): void => {
90
+ const read = possibleStdioRead(node);
91
+ if (read !== undefined) {
92
+ reads.push(read);
93
+ }
94
+ node.forEachChild(visit);
95
+ };
96
+ sourceFile.forEachChild(visit);
97
+ return reads;
98
+ };
99
+
100
+ const unionMembersOf = (type: Type) =>
101
+ isUnionType(type)
102
+ ? typeScriptRequest("read the members of a union", () => type.getTypes())
103
+ : Effect.succeed([type]);
104
+
105
+ const stdioMembersNamedBy = (checker: Checker, key: Node) =>
106
+ Effect.gen(function* () {
107
+ const type = yield* typeScriptRequest("read the type of a key", () =>
108
+ checker.getTypeAtLocation(key),
109
+ );
110
+ if (type === undefined) {
111
+ return [];
112
+ }
113
+ const constraint = isTypeParameter(type)
114
+ ? yield* typeScriptRequest("read the constraint of a key", () =>
115
+ checker.getBaseConstraintOfType(type),
116
+ )
117
+ : type;
118
+ if (constraint === undefined) {
119
+ return [];
120
+ }
121
+ return (yield* unionMembersOf(constraint)).flatMap((member) => {
122
+ const namesAGrantedMember =
123
+ isStringLiteralType(member) && PERMISSION_OF_STDIO_MEMBER.has(member.value);
124
+ return namesAGrantedMember ? [member.value] : [];
125
+ });
126
+ });
127
+
128
+ const hasStdioTypeId = (checker: Checker, type: Type) =>
129
+ Effect.gen(function* () {
130
+ const apparent = yield* requiredTypeScriptRequest("read an apparent type", () =>
131
+ checker.getApparentType(type),
132
+ );
133
+ const typeId = yield* typeScriptRequest("read the Stdio type id", () =>
134
+ checker.getPropertyOfType(apparent, STDIO_TYPE_ID),
135
+ );
136
+ return typeId !== undefined;
137
+ });
138
+
139
+ const isStdio = (checker: Checker, receiver: Node) =>
140
+ Effect.gen(function* () {
141
+ const type = yield* requiredTypeScriptRequest("read the type a Stdio member is read on", () =>
142
+ checker.getTypeAtLocation(receiver),
143
+ );
144
+ const present = yield* requiredTypeScriptRequest("read a non-nullable type", () =>
145
+ checker.getNonNullableType(type),
146
+ );
147
+ const members = yield* unionMembersOf(present);
148
+ const stdioMembers = yield* Effect.filter(members, (member) => hasStdioTypeId(checker, member));
149
+ return stdioMembers.length > 0;
150
+ });
151
+
152
+ const stdioMembersRead = (
153
+ checker: Checker,
154
+ read: NonNullable<ReturnType<typeof possibleStdioRead>>,
155
+ ) =>
156
+ Effect.gen(function* () {
157
+ const members =
158
+ read.member === undefined ? yield* stdioMembersNamedBy(checker, read.key) : [read.member];
159
+ if (members.length === 0) {
160
+ return [];
161
+ }
162
+ return (yield* isStdio(checker, read.receiver)) ? members : [];
163
+ });
164
+
165
+ const violationOf = (member: string, location: string) => {
166
+ const writesToTheTerminal = member === "stdout" || member === "stderr";
167
+ return writesToTheTerminal
168
+ ? new StdioTerminalWriteWithoutGrant({ location, member })
169
+ : new StdioArgumentsReadWithoutGrant({ location });
170
+ };
171
+
172
+ export const findUngrantedStdioUses = <OpeningError, OpeningRequirement>(
173
+ projectFile: string,
174
+ openedProjects: Effect.Effect<
175
+ Effect.Success<ReturnType<typeof openTypeScriptProjects>>,
176
+ OpeningError,
177
+ OpeningRequirement
178
+ >,
179
+ ) =>
180
+ Effect.gen(function* () {
181
+ const path = yield* Path.Path;
182
+ const grants = yield* loadedLintConfigIn(path.dirname(projectFile));
183
+ const isGranted = (displayName: string, member: string) => {
184
+ const permission = PERMISSION_OF_STDIO_MEMBER.get(member);
185
+ return grants.some(
186
+ (grant) =>
187
+ grant.path === displayName &&
188
+ permission !== undefined &&
189
+ grant.allow.some((allowed) => allowed === permission),
190
+ );
191
+ };
192
+ const violations: StdioUsesRefused["violations"][number][] = [];
193
+ for (const { checker, sourceFiles } of yield* openedProjects) {
194
+ const reads = sourceFiles.flatMap(({ sourceFile, displayName }) =>
195
+ possibleStdioReadsIn(sourceFile).map((read) => {
196
+ const location = nodePosition(sourceFile, read.receiver);
197
+ return {
198
+ read,
199
+ displayName,
200
+ location: `${displayName}:${location.line}:${location.column}`,
201
+ };
202
+ }),
203
+ );
204
+ const found = yield* Effect.forEach(
205
+ reads,
206
+ ({ read, displayName, location }) =>
207
+ Effect.map(stdioMembersRead(checker, read), (members) =>
208
+ members
209
+ .filter((member) => !isGranted(displayName, member))
210
+ .map((member) => violationOf(member, location)),
211
+ ),
212
+ { concurrency: CONCURRENT_TYPESCRIPT_REQUESTS },
213
+ );
214
+ violations.push(...found.flat());
215
+ }
216
+ if (violations.length > 0) {
217
+ return yield* new StdioUsesRefused({ violations });
218
+ }
219
+ });
@@ -0,0 +1,31 @@
1
+ import { Schema } from "effect";
2
+
3
+ import { listedViolations } from "../../../../../shared/format-reported-failures.ts";
4
+
5
+ export class StdioArgumentsReadWithoutGrant extends Schema.TaggedError<StdioArgumentsReadWithoutGrant>()(
6
+ "StdioArgumentsReadWithoutGrant",
7
+ { location: Schema.String },
8
+ ) {
9
+ public override get message() {
10
+ return "Stdio.args reads configuration";
11
+ }
12
+ }
13
+
14
+ export class StdioTerminalWriteWithoutGrant extends Schema.TaggedError<StdioTerminalWriteWithoutGrant>()(
15
+ "StdioTerminalWriteWithoutGrant",
16
+ { location: Schema.String, member: Schema.Literals(["stdout", "stderr"]) },
17
+ ) {
18
+ public override get message() {
19
+ return `Stdio.${this.member} writes to the terminal`;
20
+ }
21
+ }
22
+
23
+ export class StdioUsesRefused extends Schema.TaggedError<StdioUsesRefused>()("StdioUsesRefused", {
24
+ violations: Schema.Array(
25
+ Schema.Union([StdioArgumentsReadWithoutGrant, StdioTerminalWriteWithoutGrant]),
26
+ ),
27
+ }) {
28
+ public override get message() {
29
+ return listedViolations(this.violations);
30
+ }
31
+ }
@@ -0,0 +1 @@
1
+ export { findInferrableAnnotations } from "./internal/find-inferrable-annotations.ts";