@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,482 @@
1
+ import { Visitor } from "oxc-parser";
2
+ import type {
3
+ Argument,
4
+ Expression,
5
+ ObjectExpression,
6
+ ParseResult,
7
+ TSImportTypeQualifier,
8
+ } from "oxc-parser";
9
+
10
+ const REQUIRE = "require";
11
+ const CREATE_REQUIRE = "createRequire";
12
+ const RESOLVE = "resolve";
13
+ const BUN_GLOBAL = "Bun";
14
+ const BUILD = "build";
15
+ const ENTRYPOINTS = "entrypoints";
16
+ const WORKER_CONSTRUCTORS = new Set(["Worker", "SharedWorker"]);
17
+ const MODULE_URL_PROPERTY = "url";
18
+ const PRELOAD_OPTION = "preload";
19
+ const FILE_URL_TO_PATH = "fileURLToPath";
20
+ const URL_PATH_PROPERTIES = new Set(["pathname", "href"]);
21
+ const URL_CONSTRUCTOR = "URL";
22
+ const IMPORT = "import";
23
+ const MODULE_DIRECTORY_PROPERTIES = new Set(["dir", "dirname"]);
24
+ const ADDITION = "+";
25
+ const URL_QUERY_OR_FRAGMENT = /[?#][\s\S]*$/u;
26
+ const QUERY = /\?[\s\S]*$/u;
27
+
28
+ export const withoutQuery = (specifier: string) => specifier.replace(QUERY, "");
29
+
30
+ export const MODULE_REQUEST_KINDS = [
31
+ "static",
32
+ "dynamic",
33
+ "require",
34
+ "type",
35
+ "resolve",
36
+ "url",
37
+ "launch",
38
+ "build",
39
+ ] as const;
40
+
41
+ export const URL_REQUESTS = new Set<(typeof MODULE_REQUEST_KINDS)[number]>([
42
+ "resolve",
43
+ "url",
44
+ "launch",
45
+ ]);
46
+
47
+ export const literalSpecifierOf = (node: Expression | null | undefined): string | undefined => {
48
+ const literalValue = node?.type === "Literal" ? node.value : undefined;
49
+ if (typeof literalValue === "string") {
50
+ return literalValue;
51
+ }
52
+ const isTemplateWithoutExpressions =
53
+ node?.type === "TemplateLiteral" && node.expressions.length === 0;
54
+ const cooked = isTemplateWithoutExpressions ? node.quasis[0]?.value.cooked : undefined;
55
+ return typeof cooked === "string" ? cooked : undefined;
56
+ };
57
+
58
+ const importMetaPropertyOf = (node: Argument | Expression | null | undefined) => {
59
+ const readsImportMetaProperty =
60
+ node?.type === "MemberExpression" &&
61
+ !node.computed &&
62
+ node.object.type === "MetaProperty" &&
63
+ node.object.meta.name === IMPORT;
64
+ return readsImportMetaProperty ? node.property.name : undefined;
65
+ };
66
+
67
+ const isImportMetaUrl = (node: Argument | Expression | null | undefined) =>
68
+ importMetaPropertyOf(node) === MODULE_URL_PROPERTY;
69
+
70
+ const calleeName = (callee: Expression) => {
71
+ if (callee.type === "Identifier") {
72
+ return callee.name;
73
+ }
74
+ const readsNamedProperty =
75
+ callee.type === "MemberExpression" && !callee.computed && "name" in callee.property;
76
+ return readsNamedProperty ? callee.property.name : undefined;
77
+ };
78
+
79
+ const isModuleUrl = (node: Expression) =>
80
+ node.type === "NewExpression" &&
81
+ node.callee.type === "Identifier" &&
82
+ node.callee.name === URL_CONSTRUCTOR &&
83
+ isImportMetaUrl(node.arguments[1]);
84
+
85
+ const isBunBuild = (callee: Expression) =>
86
+ callee.type === "MemberExpression" &&
87
+ !callee.computed &&
88
+ callee.object.type === "Identifier" &&
89
+ callee.object.name === BUN_GLOBAL &&
90
+ callee.property.name === BUILD;
91
+
92
+ const isCreateRequireCall = (node: Expression | null | undefined) =>
93
+ node?.type === "CallExpression" && calleeName(node.callee) === CREATE_REQUIRE;
94
+
95
+ const urlConstructionOf = (node: Expression): number | undefined => {
96
+ if (isModuleUrl(node)) {
97
+ return node.start;
98
+ }
99
+ const isFileUrlToPathCall =
100
+ node.type === "CallExpression" && calleeName(node.callee) === FILE_URL_TO_PATH;
101
+ if (isFileUrlToPathCall) {
102
+ const [argument] = node.arguments;
103
+ const isMissingOrSpreadArgument = argument === undefined || argument.type === "SpreadElement";
104
+ return isMissingOrSpreadArgument ? undefined : urlConstructionOf(argument);
105
+ }
106
+ const readsUrlPathProperty =
107
+ node.type === "MemberExpression" &&
108
+ !node.computed &&
109
+ URL_PATH_PROPERTIES.has(node.property.name);
110
+ if (readsUrlPathProperty) {
111
+ return urlConstructionOf(node.object);
112
+ }
113
+ return undefined;
114
+ };
115
+
116
+ const firstNameOf = (qualifier: TSImportTypeQualifier): string =>
117
+ qualifier.type === "Identifier" ? qualifier.name : firstNameOf(qualifier.left);
118
+
119
+ const staticRequestsIn = (
120
+ parsed: ParseResult,
121
+ ): {
122
+ readonly specifier: string;
123
+ readonly start: number;
124
+ readonly kind: (typeof MODULE_REQUEST_KINDS)[number];
125
+ readonly namedExport?: string;
126
+ }[] => [
127
+ ...parsed.module.staticImports.map((declaration) => ({
128
+ specifier: withoutQuery(declaration.moduleRequest.value),
129
+ start: declaration.moduleRequest.start,
130
+ kind: "static" as const,
131
+ })),
132
+ ...parsed.module.staticExports.flatMap((declaration) =>
133
+ declaration.entries.flatMap((entry) =>
134
+ entry.moduleRequest === null
135
+ ? []
136
+ : [
137
+ {
138
+ specifier: withoutQuery(entry.moduleRequest.value),
139
+ start: entry.moduleRequest.start,
140
+ kind: "static" as const,
141
+ },
142
+ ],
143
+ ),
144
+ ),
145
+ ];
146
+
147
+ export const moduleRequestsIn = (parsed: ParseResult) => {
148
+ const requests = staticRequestsIn(parsed);
149
+ const reportedByNoRequireImports = new Set<number>();
150
+ const add = (
151
+ node: Parameters<typeof literalSpecifierOf>[0],
152
+ start: number,
153
+ kind: (typeof requests)[number]["kind"],
154
+ named: { readonly namedExport?: string } = {},
155
+ ) => {
156
+ const specifier = literalSpecifierOf(node);
157
+ if (specifier !== undefined) {
158
+ requests.push({
159
+ specifier: withoutQuery(specifier),
160
+ start: node?.start ?? start,
161
+ kind,
162
+ ...named,
163
+ });
164
+ }
165
+ };
166
+ const createdRequires = new Set<string>();
167
+ new Visitor({
168
+ VariableDeclarator(node) {
169
+ const declared = node.id;
170
+ const bindsCreateRequire = declared.type === "Identifier" && isCreateRequireCall(node.init);
171
+ if (bindsCreateRequire) {
172
+ createdRequires.add(declared.name);
173
+ }
174
+ },
175
+ }).visit(parsed.program);
176
+ const constantObjects = new Map<string, ObjectExpression>();
177
+ const launched = new Set<number>();
178
+ const launch = (node: Argument | null | undefined) => {
179
+ const isMissingOrSpread = node === null || node === undefined || node.type === "SpreadElement";
180
+ if (isMissingOrSpread) {
181
+ return;
182
+ }
183
+ if (node.type === "ArrayExpression") {
184
+ for (const element of node.elements) {
185
+ launch(element);
186
+ }
187
+ return;
188
+ }
189
+ const start = urlConstructionOf(node);
190
+ if (start !== undefined) {
191
+ launched.add(start);
192
+ }
193
+ };
194
+ const optionValue = (options: Argument | undefined, name: string): Expression[] => {
195
+ if (options?.type === "Identifier") {
196
+ const constant = constantObjects.get(options.name);
197
+ return constant === undefined ? [] : optionValue(constant, name);
198
+ }
199
+ if (options?.type === "SpreadElement") {
200
+ return optionValue(options.argument, name);
201
+ }
202
+ if (options?.type === "ParenthesizedExpression") {
203
+ return optionValue(options.expression, name);
204
+ }
205
+ if (options?.type === "ConditionalExpression") {
206
+ return [...optionValue(options.consequent, name), ...optionValue(options.alternate, name)];
207
+ }
208
+ if (options?.type !== "ObjectExpression") {
209
+ return [];
210
+ }
211
+ return options.properties.flatMap((property) => {
212
+ if (property.type === "SpreadElement") {
213
+ return optionValue(property, name);
214
+ }
215
+ const isPropertyForOption =
216
+ !property.computed && property.key.type === "Identifier" && property.key.name === name;
217
+ return isPropertyForOption ? [property.value] : [];
218
+ });
219
+ };
220
+ for (const statement of parsed.program.body) {
221
+ const declaration =
222
+ statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
223
+ const isConstDeclaration =
224
+ declaration?.type === "VariableDeclaration" && declaration.kind === "const";
225
+ if (!isConstDeclaration) {
226
+ continue;
227
+ }
228
+ for (const { id, init: value } of declaration.declarations) {
229
+ const bindsConstantObject = id.type === "Identifier" && value?.type === "ObjectExpression";
230
+ if (bindsConstantObject) {
231
+ constantObjects.set(id.name, value);
232
+ }
233
+ }
234
+ }
235
+ new Visitor({
236
+ NewExpression(node) {
237
+ const isWorkerConstructor =
238
+ node.callee.type === "Identifier" && WORKER_CONSTRUCTORS.has(node.callee.name);
239
+ if (isWorkerConstructor) {
240
+ const [entry, options] = node.arguments;
241
+ launch(entry);
242
+ for (const preload of optionValue(options, PRELOAD_OPTION)) {
243
+ launch(preload);
244
+ }
245
+ }
246
+ },
247
+ CallExpression(node) {
248
+ if (isBunBuild(node.callee)) {
249
+ const [options] = node.arguments;
250
+ for (const entrypoints of optionValue(options, ENTRYPOINTS)) {
251
+ launch(entrypoints);
252
+ }
253
+ }
254
+ },
255
+ }).visit(parsed.program);
256
+ new Visitor({
257
+ ImportExpression(node) {
258
+ add(node.source, node.start, "dynamic");
259
+ },
260
+ CallExpression(node) {
261
+ const [first] = node.arguments;
262
+ const isMissingOrSpread = first === undefined || first.type === "SpreadElement";
263
+ if (isMissingOrSpread) {
264
+ return;
265
+ }
266
+ const { callee } = node;
267
+ const isBunBuildWithOptionsObject = isBunBuild(callee) && first.type === "ObjectExpression";
268
+ if (isBunBuildWithOptionsObject) {
269
+ for (const property of first.properties) {
270
+ if (property.type !== "Property") {
271
+ continue;
272
+ }
273
+ const { key, value: entrypointsValue } = property;
274
+ const listsEntrypoints =
275
+ !property.computed &&
276
+ key.type === "Identifier" &&
277
+ key.name === ENTRYPOINTS &&
278
+ entrypointsValue.type === "ArrayExpression";
279
+ if (listsEntrypoints) {
280
+ for (const element of entrypointsValue.elements) {
281
+ const isEntrypointNotBuiltFromModuleUrl =
282
+ element !== null &&
283
+ element.type !== "SpreadElement" &&
284
+ urlConstructionOf(element) === undefined;
285
+ if (isEntrypointNotBuiltFromModuleUrl) {
286
+ add(element, element.start, "build");
287
+ }
288
+ }
289
+ }
290
+ }
291
+ return;
292
+ }
293
+ const callsRequire =
294
+ (callee.type === "Identifier" &&
295
+ (callee.name === REQUIRE || createdRequires.has(callee.name))) ||
296
+ isCreateRequireCall(callee);
297
+ const callsImportMetaResolve =
298
+ callee.type === "MemberExpression" &&
299
+ !callee.computed &&
300
+ callee.property.type === "Identifier" &&
301
+ callee.property.name === RESOLVE &&
302
+ callee.object.type === "MetaProperty";
303
+ const callsTheGlobalRequire =
304
+ callee.type === "Identifier" && callee.name === REQUIRE && !createdRequires.has(REQUIRE);
305
+ if (callsTheGlobalRequire) {
306
+ reportedByNoRequireImports.add(first.start);
307
+ }
308
+ if (callsRequire) {
309
+ add(first, node.start, "require");
310
+ } else if (callsImportMetaResolve) {
311
+ add(first, node.start, "resolve");
312
+ }
313
+ },
314
+ NewExpression(node) {
315
+ const [first, second] = node.arguments;
316
+ const isUrlRelativeToModule =
317
+ node.callee.type === "Identifier" &&
318
+ node.callee.name === URL_CONSTRUCTOR &&
319
+ first !== undefined &&
320
+ first.type !== "SpreadElement" &&
321
+ isImportMetaUrl(second);
322
+ if (isUrlRelativeToModule) {
323
+ const url = literalSpecifierOf(first);
324
+ if (url !== undefined) {
325
+ requests.push({
326
+ specifier: url.replace(URL_QUERY_OR_FRAGMENT, ""),
327
+ start: first.start,
328
+ kind: launched.has(node.start) ? "launch" : "url",
329
+ });
330
+ }
331
+ }
332
+ },
333
+ TSImportType(node) {
334
+ add(
335
+ node.source,
336
+ node.start,
337
+ "type",
338
+ node.qualifier === null ? {} : { namedExport: firstNameOf(node.qualifier) },
339
+ );
340
+ },
341
+ TSExternalModuleReference(node) {
342
+ reportedByNoRequireImports.add(node.expression.start);
343
+ add(node.expression, node.start, "require");
344
+ },
345
+ }).visit(parsed.program);
346
+ return { requests, reportedByNoRequireImports };
347
+ };
348
+
349
+ export const MODULE_URL_USAGES = ["stored", "computed", "base", "other"] as const;
350
+
351
+ const UNNAMED_FUNCTION = "a function";
352
+
353
+ const calleeTextOf = (callee: Expression) => {
354
+ if (callee.type === "Identifier") {
355
+ return callee.name;
356
+ }
357
+ const isStaticMemberCallee = callee.type === "MemberExpression" && !callee.computed;
358
+ if (isStaticMemberCallee) {
359
+ return callee.object.type === "Identifier"
360
+ ? `${callee.object.name}.${callee.property.name}`
361
+ : callee.property.name;
362
+ }
363
+ return UNNAMED_FUNCTION;
364
+ };
365
+
366
+ const stored = (start: number) => ({ start, usage: "stored" as const });
367
+
368
+ const passedTo = (callee: Expression) => (start: number) => ({
369
+ start,
370
+ callee: calleeTextOf(callee),
371
+ });
372
+
373
+ const within = (starts: readonly number[], range: { start: number; end: number }) =>
374
+ starts.some((start) => range.start <= start && start < range.end);
375
+
376
+ export const moduleUrlMisusesIn = (parsed: ParseResult) => {
377
+ const classified = new Map<
378
+ number,
379
+ | { readonly start: number; readonly callee: string }
380
+ | { readonly start: number; readonly usage: (typeof MODULE_URL_USAGES)[number] }
381
+ >();
382
+ const inline = new Set<number>();
383
+ const moduleUrls: number[] = [];
384
+ const importMetaStarts: number[] = [];
385
+ const derivedBases: {
386
+ readonly start: number;
387
+ readonly end: number;
388
+ readonly url: number;
389
+ }[] = [];
390
+ const directoryPaths: { readonly start: number; readonly property: string }[] = [];
391
+ const classify = (
392
+ node: Argument | Expression | null | undefined,
393
+ misuse: (start: number) => Parameters<typeof classified.set>[1],
394
+ ) => {
395
+ const isImportMetaUrlNode = node !== null && node !== undefined && isImportMetaUrl(node);
396
+ if (isImportMetaUrlNode) {
397
+ classified.set(node.start, misuse(node.start));
398
+ }
399
+ };
400
+ const directoryPath = (start: number, parts: readonly Expression[]) => {
401
+ const property = parts
402
+ .map((part) => importMetaPropertyOf(part))
403
+ .find((name) => name !== undefined && MODULE_DIRECTORY_PROPERTIES.has(name));
404
+ if (property !== undefined) {
405
+ directoryPaths.push({ start, property });
406
+ }
407
+ };
408
+ new Visitor({
409
+ MetaProperty(node) {
410
+ if (node.meta.name === IMPORT) {
411
+ importMetaStarts.push(node.start);
412
+ }
413
+ },
414
+ MemberExpression(node) {
415
+ if (isImportMetaUrl(node)) {
416
+ moduleUrls.push(node.start);
417
+ }
418
+ },
419
+ VariableDeclarator(node) {
420
+ classify(node.init, stored);
421
+ },
422
+ AssignmentExpression(node) {
423
+ classify(node.right, stored);
424
+ },
425
+ Property(node) {
426
+ if (node.value.type === "MemberExpression") {
427
+ classify(node.value, stored);
428
+ }
429
+ },
430
+ CallExpression(node) {
431
+ for (const argument of node.arguments) {
432
+ classify(argument, passedTo(node.callee));
433
+ }
434
+ },
435
+ NewExpression(node) {
436
+ const [path, base] = node.arguments;
437
+ const isUrl = node.callee.type === "Identifier" && node.callee.name === URL_CONSTRUCTOR;
438
+ for (const argument of node.arguments) {
439
+ classify(argument, passedTo(node.callee));
440
+ }
441
+ const isUrlWithBase = isUrl && base !== undefined;
442
+ if (isUrlWithBase) {
443
+ if (isImportMetaUrl(base)) {
444
+ const hasLiteralPath =
445
+ path !== undefined &&
446
+ path.type !== "SpreadElement" &&
447
+ literalSpecifierOf(path) !== undefined;
448
+ if (hasLiteralPath) {
449
+ inline.add(base.start);
450
+ } else {
451
+ classify(base, (start) => ({ start, usage: "computed" }));
452
+ }
453
+ } else {
454
+ derivedBases.push({ start: base.start, end: base.end, url: node.start });
455
+ }
456
+ }
457
+ },
458
+ TemplateLiteral(node) {
459
+ directoryPath(node.start, node.expressions);
460
+ },
461
+ BinaryExpression(node) {
462
+ if (node.operator === ADDITION) {
463
+ directoryPath(node.start, [node.left, node.right]);
464
+ }
465
+ },
466
+ }).visit(parsed.program);
467
+ const misuses = moduleUrls
468
+ .filter((start) => !inline.has(start))
469
+ .map(
470
+ (start): Parameters<typeof classified.set>[1] =>
471
+ classified.get(start) ?? { start, usage: "other" },
472
+ );
473
+ const reported = [...misuses, ...directoryPaths].map(({ start }) => start);
474
+ for (const base of derivedBases) {
475
+ const isUnreportedBaseFromImportMeta =
476
+ within(importMetaStarts, base) && !within(reported, base);
477
+ if (isUnreportedBaseFromImportMeta) {
478
+ misuses.push({ start: base.url, usage: "base" });
479
+ }
480
+ }
481
+ return { misuses, directoryPaths };
482
+ };
@@ -0,0 +1,94 @@
1
+ import { flow, Option, Result, Schema } from "effect";
2
+ import type { ResolveResult } from "oxc-resolver";
3
+
4
+ import { directoryHolding, isWithin } from "../../../shared/match-file-names-and-path-prefixes.ts";
5
+
6
+ export const PACKAGE_MANIFEST_FILE = "package.json";
7
+ const LINE_BREAKS = /\s*\n\s*/gu;
8
+ const RELATIVE_REQUEST = /^\.\.?(?:\/|$)/u;
9
+ const SKIPPED_SEGMENTS: ReadonlySet<string> = new Set(["", "."]);
10
+ const PARENT_SEGMENT = "..";
11
+ const ERRORS_OF_A_REQUEST_FOR_NO_FILE = [
12
+ /^Cannot find module '/u,
13
+ /^"[\s\S]*" is not exported under /u,
14
+ /^Package import specifier "[\s\S]*" is not defined in package /u,
15
+ ];
16
+
17
+ const PACKAGE_MANIFEST = Schema.fromJsonString(
18
+ Schema.Struct({
19
+ name: Schema.optionalKey(Schema.String),
20
+ exports: Schema.optionalKey(Schema.Json),
21
+ }),
22
+ );
23
+
24
+ const ERRNO_FAILURE = Schema.Struct({ code: Schema.String });
25
+ const CODES_OF_A_PATH_TO_NO_FILE: ReadonlySet<string> = new Set(["ENOENT", "ENOTDIR"]);
26
+ const SYMLINK_LOOP_CODE = "ELOOP";
27
+
28
+ export const enclosingPackageManifestsOf = (directory: string, projectDirectory: string) => {
29
+ const segments = directory.split("/");
30
+ const highestSegments = isWithin(directory, projectDirectory)
31
+ ? projectDirectory.split("/").length
32
+ : 1;
33
+ return Array.from(
34
+ { length: segments.length - highestSegments + 1 },
35
+ (_entry, above) =>
36
+ `${segments.slice(0, segments.length - above).join("/")}/${PACKAGE_MANIFEST_FILE}`,
37
+ );
38
+ };
39
+
40
+ export const namedPackageManifestIn = (bytes: Uint8Array) =>
41
+ Schema.decodeResult(PACKAGE_MANIFEST)(new TextDecoder().decode(bytes)).pipe(
42
+ Result.map(({ name, exports }) =>
43
+ name === undefined ? Option.none() : Option.some({ name, hasExports: exports !== undefined }),
44
+ ),
45
+ Result.mapError((issue) => issue.message.replaceAll(LINE_BREAKS, " ")),
46
+ );
47
+
48
+ export const unreadableManifestReason = flow(
49
+ Schema.decodeUnknownOption(ERRNO_FAILURE),
50
+ Option.match({
51
+ onNone: () => "reading it failed",
52
+ onSome: ({ code }) => `reading it failed with ${code}`,
53
+ }),
54
+ );
55
+
56
+ export const namesNoManifest = flow(
57
+ Schema.decodeUnknownOption(ERRNO_FAILURE),
58
+ Option.exists(({ code }) => CODES_OF_A_PATH_TO_NO_FILE.has(code)),
59
+ );
60
+
61
+ export const namesASymlinkLoop = flow(
62
+ Schema.decodeUnknownOption(ERRNO_FAILURE),
63
+ Option.exists(({ code }) => code === SYMLINK_LOOP_CODE),
64
+ );
65
+
66
+ const withoutDotSegments = (file: string) => {
67
+ const [root = "", ...segments] = file.split("/");
68
+ const kept: string[] = [];
69
+ for (const segment of segments) {
70
+ if (segment === PARENT_SEGMENT) {
71
+ kept.pop();
72
+ } else if (!SKIPPED_SEGMENTS.has(segment)) {
73
+ kept.push(segment);
74
+ }
75
+ }
76
+ return [root, ...kept].join("/");
77
+ };
78
+
79
+ export const directoriesAFailedResolutionReads = ({
80
+ importingFile,
81
+ request,
82
+ }: {
83
+ readonly importingFile: string;
84
+ readonly request: string;
85
+ }) => {
86
+ const importingDirectory = directoryHolding(importingFile);
87
+ return RELATIVE_REQUEST.test(request)
88
+ ? [importingDirectory, directoryHolding(withoutDotSegments(`${importingDirectory}/${request}`))]
89
+ : [importingDirectory];
90
+ };
91
+
92
+ export const resolutionNamesNoFile = (resolved: ResolveResult, reason: string) =>
93
+ resolved.builtin !== undefined ||
94
+ ERRORS_OF_A_REQUEST_FOR_NO_FILE.some((error) => error.test(reason));
@@ -0,0 +1,60 @@
1
+ import { Effect, Record } from "effect";
2
+
3
+ import {
4
+ LooseCompilerOption,
5
+ REQUIRED_VALUES_OF_OPTIONS_OFF_BY_DEFAULT,
6
+ STRICT_OPTIONS,
7
+ TypeScriptConfigViolations,
8
+ } from "./project-config-and-tool-errors.ts";
9
+ import {
10
+ compilesFiles,
11
+ effectiveCompilerOptions,
12
+ projectConfigFiles,
13
+ } from "./query-the-typescript-project.ts";
14
+
15
+ const optionViolationsOf = (configFile: string) =>
16
+ effectiveCompilerOptions(configFile).pipe(
17
+ Effect.map((options) =>
18
+ [
19
+ ...STRICT_OPTIONS.map((option) => ({
20
+ option,
21
+ required: true,
22
+ resolvedValue: options[option] ?? true,
23
+ })),
24
+ ...Record.toEntries(REQUIRED_VALUES_OF_OPTIONS_OFF_BY_DEFAULT).map(
25
+ ([option, required]) => ({
26
+ option,
27
+ required,
28
+ resolvedValue:
29
+ (option === "allowImportingTsExtensions" &&
30
+ options["rewriteRelativeImportExtensions"] === true) ||
31
+ (options[option] ?? false),
32
+ }),
33
+ ),
34
+ ].flatMap(({ option, required, resolvedValue }) => {
35
+ const isLoose = typeof resolvedValue === "boolean" && resolvedValue !== required;
36
+ return isLoose
37
+ ? [new LooseCompilerOption({ projectFile: configFile, option, resolvedValue })]
38
+ : [];
39
+ }),
40
+ ),
41
+ );
42
+
43
+ export const findTypeScriptConfigViolations = (projectFile: string) =>
44
+ Effect.gen(function* () {
45
+ const builtConfigFiles = yield* projectConfigFiles(projectFile).pipe(
46
+ Effect.catchTags({
47
+ TypeScriptProjectFileMissing: (missing) =>
48
+ Effect.fail(new TypeScriptConfigViolations({ violations: [missing] })),
49
+ TypeScriptConfigUnparsable: (unparsable) =>
50
+ Effect.fail(new TypeScriptConfigViolations({ violations: [unparsable] })),
51
+ ProjectReferencesUndecodable: (undecodable) =>
52
+ Effect.fail(new TypeScriptConfigViolations({ violations: [undecodable] })),
53
+ }),
54
+ );
55
+ const compilingConfigFiles = yield* Effect.filter(builtConfigFiles, compilesFiles);
56
+ const violations = (yield* Effect.forEach(compilingConfigFiles, optionViolationsOf)).flat();
57
+ if (violations.length > 0) {
58
+ return yield* new TypeScriptConfigViolations({ violations });
59
+ }
60
+ });