@sshkeda/check 2.2.0 → 3.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 (42) hide show
  1. package/README.md +52 -22
  2. package/docs/how-to-make-check-pass.md +1 -0
  3. package/package.json +7 -10
  4. package/scripts/check.ts +10 -0
  5. package/src/internal/ban-re-exports.ts +14 -1
  6. package/src/internal/define-the-lint-plugin-and-configs.ts +0 -2
  7. package/src/lib/project-directory/index.ts +5 -0
  8. package/src/lib/project-directory/internal/read-the-project-directory.ts +13 -0
  9. package/src/lib/tasks/internal/declare-the-check-task.ts +18 -10
  10. package/src/lib/tasks/internal/declare-the-checker-queries-task.ts +3 -1
  11. package/src/lib/tasks/internal/verify-where-check-was-started.ts +50 -0
  12. package/src/lib/tasks/lib/check-effect-diagnostics-with-effect-tsgo/index.ts +1 -1
  13. package/src/lib/tasks/lib/confine-layer-provides-to-program-starts/internal/find-layer-provides-outside-program-starts.ts +6 -36
  14. package/src/lib/tasks/lib/detect-duplicate-functions/internal/find-duplicate-functions.ts +2 -3
  15. package/src/lib/tasks/lib/enforce-file-layout/internal/check-the-layout-of-each-package.ts +1 -1
  16. package/src/lib/tasks/lib/enforce-file-layout/internal/file-layout-errors.ts +19 -2
  17. package/src/lib/tasks/lib/enforce-file-layout/internal/find-file-layout-violations.ts +53 -19
  18. package/src/lib/tasks/lib/enforce-file-types/internal/find-file-type-violations.ts +1 -1
  19. package/src/lib/tasks/lib/format-with-bun-fmt/internal/check-formatting-with-bun-fmt.ts +16 -15
  20. package/src/lib/tasks/lib/format-with-bun-fmt/internal/declare-the-formatting-task.ts +22 -14
  21. package/src/lib/tasks/lib/lint-with-oxlint/index.ts +1 -1
  22. package/src/lib/tasks/lib/lint-with-oxlint/internal/check-lint-rules-with-oxlint.ts +7 -0
  23. package/src/lib/tasks/lib/lint-with-oxlint/internal/find-shebangs-outside-bin.ts +38 -0
  24. package/src/lib/tasks/lib/lint-with-oxlint/internal/shebang-errors.ts +22 -0
  25. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-export-placement-violations.ts +9 -1
  26. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/find-the-importer-graph.ts +21 -0
  27. package/src/lib/tasks/lib/place-exports-in-the-narrowest-directory/internal/recognise-task-default-exports.ts +76 -0
  28. package/src/lib/tasks/lib/refuse-disable-directives/index.ts +1 -1
  29. package/src/lib/tasks/lib/refuse-forks-whose-failures-nothing-observes/index.ts +1 -0
  30. package/src/lib/tasks/lib/refuse-forks-whose-failures-nothing-observes/internal/find-unobserved-fork-failures.ts +402 -0
  31. package/src/lib/tasks/lib/refuse-forks-whose-failures-nothing-observes/internal/fork-failure-errors.ts +28 -0
  32. package/src/lib/tasks/lib/require-type-checking-every-linted-file/internal/find-linted-files-not-type-checked.ts +15 -9
  33. package/src/lib/tasks/lib/type-check-with-bun-check/index.ts +1 -1
  34. package/src/lib/tasks/shared/check-each-nested-package.ts +12 -4
  35. package/src/lib/tasks/shared/declare-lint-config-task.ts +1 -1
  36. package/src/lib/tasks/shared/declare-typescript-config-task.ts +1 -1
  37. package/src/lib/tasks/shared/find-installed-package-binaries.ts +10 -0
  38. package/src/lib/tasks/shared/read-piped-steps-through-wrappers.ts +41 -0
  39. package/src/lib/tasks/shared/report-paths-from-the-project.ts +56 -0
  40. package/src/shared/format-reported-failures.ts +8 -6
  41. package/src/shared/record-and-grant-permissions-by-path.ts +4 -1
  42. package/src/internal/ban-shebang-lines.ts +0 -22
package/README.md CHANGED
@@ -4,11 +4,11 @@ Check sends your AI slop code back to the kitchen!
4
4
 
5
5
  Check holds your TypeScript code to a strict standard built around Effect and Bun, so the code stays type-safe, observable and predictable.
6
6
 
7
- It runs about 310 lint rules, 67 of them its own, and about 80 checks on your project's layout, imports and config. It also runs oxfmt, Effect's diagnostics and `bun check`. Your agent keeps fixing what it reports until the slop is gone.
7
+ It runs about 310 lint rules, 66 of them its own, and about 80 checks on your project's layout, imports and config. It also runs oxfmt, Effect's diagnostics and `bun check`. Your agent keeps fixing what it reports until the slop is gone.
8
8
 
9
9
  No comment or setting turns a rule off everywhere. A small set of rules was picked to allow an override, and you grant each one to a single file in `oxlint.config.ts`. See [Grant a permission](docs/how-to-make-check-pass.md#grant-a-permission).
10
10
 
11
- Check is built on [Task](https://github.com/sshkeda/task), a task runner for Bun.
11
+ Check is a command you run in your repo. It runs its checks with [Task](https://github.com/sshkeda/task), a task runner for Bun.
12
12
 
13
13
  ## See it catch slop
14
14
 
@@ -37,13 +37,13 @@ Here's what Check prints when it runs on that code:
37
37
  [effect-tsgo] src/index.ts:1:25: error effect(asyncFunction): This code declares an async function, consider representing this async control flow with Effect values and `Effect.gen`.
38
38
  [effect-tsgo] src/index.ts:3:28: error effect(globalFetch): This code uses the global `fetch` function, HTTP requests are represented through `HttpClient` from `effect/http`.
39
39
  [effect-tsgo] error ToolExited: effect-tsgo exited with code 1
40
- [oxlint] src/index.ts:4:12: error typescript(consistent-type-assertions): Do not use any type assertions.
41
- [oxlint] src/index.ts:4:39: error typescript(no-explicit-any): Unexpected `any`. Specify a different type.
42
- [oxlint] src/index.ts:7:5: error eslint(no-console): Unexpected console statement.
43
40
  [oxlint] src/index.ts:1:25: error sshkeda(no-index-logic): index file holds an arrow function expression
41
+ [oxlint] src/index.ts:4:5: error typescript(no-unsafe-return): Unsafe return of a value of type `any`.
42
+ [oxlint] src/index.ts:4:12: error typescript(consistent-type-assertions): Do not use any type assertions.
44
43
  [oxlint] src/index.ts:4:19: error sshkeda(no-unchecked-parsing): json() parses JSON without a Schema
44
+ [oxlint] src/index.ts:4:39: error typescript(no-explicit-any): Unexpected `any`. Specify a different type.
45
45
  [oxlint] src/index.ts:6:5: error sshkeda(no-comments): comment in code
46
- [oxlint] src/index.ts:4:5: error typescript(no-unsafe-return): Unsafe return of a value of type `any`.
46
+ [oxlint] src/index.ts:7:5: error eslint(no-console): Unexpected console statement.
47
47
  [oxlint] error ToolExited: oxlint exited with code 1
48
48
  [typed-checks] error SettledTasksFailed: 2 of 6 tasks failed: oxlint, effect-tsgo
49
49
  [check] read node_modules/@sshkeda/check/docs/how-to-make-check-pass.md before you change the code
@@ -84,7 +84,7 @@ Check passes.
84
84
 
85
85
  - **No comments.** When code is hard to follow, the fix is better names and simpler code, which stay true as the code changes. Comments drift out of date, and agents fill them with noise. When you need more explanation, ask your agent to explain the code.
86
86
  - **No `type` or `interface` declarations.** Types are inferred from the code that produces each value, so every type has one source of truth and nothing to keep in sync.
87
- - **No `let` and no loops.** Values don't change after they're made, which leaves an agent fewer ways to get state wrong.
87
+ - **No `let`, and no `while` or counting `for` loops.** Values don't change after they're made, which leaves an agent fewer ways to get state wrong.
88
88
  - **Modules with `internal/`, `shared/` and `lib/`.** Each module exposes its public code in `index.ts`, keeps private code in `internal/`, which only that module may import, puts code its parts share in `shared/` and holds its child modules in `lib/`. Code stays easy to organize and hard to tangle.
89
89
  - **No abbreviations.** `imABigFanWhenVariablesAreLongBecauseThenItsEasierToUnderstandWhatTheCodeDoes`. Full words read the same to every person and every agent.
90
90
  - **No unchecked data.** JSON and other input pass through a Schema before the code trusts them, because a type assertion or a hand-written type guard proves nothing.
@@ -93,17 +93,17 @@ Check passes.
93
93
  - **Tests are tasks.** Every test is a Task task in `tests/`, never a Vitest, Jest or `bun:test` suite. Tests run in parallel and clean up after themselves.
94
94
  - **Each service in its own layer.** Services are declared only in files you grant, which keeps their wiring in one place.
95
95
  - **No polling.** Code waits for an event or a signal instead of checking on a timer, so it reacts right away. When there's nothing to listen to, such as a server that's still starting, polling needs a grant and a time limit.
96
- - **No dropped failures.** Every error is handled or passed on, so nothing fails silently.
96
+ - **No dropped failures.** Every error is handled or passed on, and a fork that can fail must keep its fiber, so nothing fails silently.
97
97
  - **Risky access only where you grant it.** Reading environment variables, starting commands and printing to the terminal each need a grant on the exact file that does it.
98
98
 
99
99
  ## Get started
100
100
 
101
101
  Check needs Bun 1.4.4 or newer and a Git repository.
102
102
 
103
- 1. Install Check, Task, Effect and Bun's types.
103
+ 1. Install Check, Effect and Bun's types.
104
104
 
105
105
  ```bash
106
- bun add @sshkeda/check @sshkeda/task effect
106
+ bun add @sshkeda/check effect
107
107
  bun add -d @types/bun
108
108
  ```
109
109
 
@@ -142,22 +142,52 @@ Check needs Bun 1.4.4 or newer and a Git repository.
142
142
  export default FORMAT_CONFIG;
143
143
  ```
144
144
 
145
- 5. Create `scripts/ci.ts` to run Check.
145
+ 5. Run Check, and have your agent fix what it reports. Expect the first run to fail, even on a new project from `bun init`.
146
146
 
147
- ```ts
148
- import { check } from "@sshkeda/check/tasks";
149
- import { Task } from "@sshkeda/task";
150
-
151
- if (import.meta.main) {
152
- Task.main(check);
153
- }
147
+ ```bash
148
+ bunx @sshkeda/check
154
149
  ```
155
150
 
156
- 6. Run it, and have your agent fix what it reports. Expect the first run to fail, even on a new project from `bun init`.
151
+ Check checks the package in the directory you start it in.
157
152
 
158
- ```bash
159
- bun scripts/ci.ts
160
- ```
153
+ ## Run it from a script
154
+
155
+ To run Check beside your own tasks, such as your tests, add Task and start the command from a task in `scripts/`.
156
+
157
+ ```bash
158
+ bun add @sshkeda/task
159
+ ```
160
+
161
+ `scripts/internal/start-check.ts` starts the command:
162
+
163
+ ```ts
164
+ import { Task } from "@sshkeda/task";
165
+
166
+ export const check = Task.make("check", Task.exec("bunx", ["@sshkeda/check"]));
167
+ ```
168
+
169
+ `scripts/ci.ts` runs it:
170
+
171
+ ```ts
172
+ import { Task } from "@sshkeda/task";
173
+
174
+ import { check } from "./internal/start-check.ts";
175
+
176
+ if (import.meta.main) {
177
+ Task.main(check);
178
+ }
179
+ ```
180
+
181
+ To run your tests too, make one task in `scripts/internal/run-checks-and-tests.ts` that runs both, and pass it to `Task.main` in `scripts/ci.ts`:
182
+
183
+ ```ts
184
+ import { Task } from "@sshkeda/task";
185
+
186
+ import { tests } from "../../tests/index.ts";
187
+ import { check } from "./start-check.ts";
188
+
189
+ export const ci = Task.make("ci", Task.allSettled([check, tests]));
190
+ ```
161
191
 
162
192
  ## Make it yours
163
193
 
@@ -9,6 +9,7 @@ Check may not catch every violation of these principles. Review your code agains
9
9
  - Use a grant only for an unavoidable exception.
10
10
  - Move the code that needs a grant into its own small file and grant that file. A grant covers the whole file, so Check stops reporting that ban for everything else in it.
11
11
  - Design for the current requirement as if you were starting fresh. Delete code the new design doesn't need.
12
+ - Handle a forked effect's failure before the fork, or keep the fiber and join it.
12
13
  - Grant `isGeneratedByATool` only to a file a tool generates.
13
14
 
14
15
  ## Grant a permission
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sshkeda/check",
3
- "version": "2.2.0",
3
+ "version": "3.0.0",
4
4
  "homepage": "https://github.com/sshkeda/check#readme",
5
5
  "bugs": {
6
6
  "url": "https://github.com/sshkeda/check/issues"
@@ -10,14 +10,17 @@
10
10
  "type": "git",
11
11
  "url": "git+https://github.com/sshkeda/check.git"
12
12
  },
13
+ "bin": {
14
+ "check": "./scripts/check.ts"
15
+ },
13
16
  "files": [
14
17
  "docs",
18
+ "scripts/check.ts",
15
19
  "src"
16
20
  ],
17
21
  "type": "module",
18
22
  "exports": {
19
- ".": "./src/index.ts",
20
- "./tasks": "./src/lib/tasks/index.ts"
23
+ ".": "./src/index.ts"
21
24
  },
22
25
  "publishConfig": {
23
26
  "access": "public",
@@ -27,6 +30,7 @@
27
30
  "@effect/platform-bun": "4.1.0",
28
31
  "@effect/tsgo": "0.67.4",
29
32
  "@oxlint/plugins": "1.88.0",
33
+ "@sshkeda/task": "1.0.2",
30
34
  "oxc-parser": "0.153.0",
31
35
  "oxc-resolver": "11.24.2",
32
36
  "oxlint": "1.88.0",
@@ -34,19 +38,12 @@
34
38
  "typescript": "7.0.2"
35
39
  },
36
40
  "devDependencies": {
37
- "@sshkeda/task": "^1.0.0",
38
41
  "@types/bun": "1.4.3",
39
42
  "effect": "4.1.0"
40
43
  },
41
44
  "peerDependencies": {
42
- "@sshkeda/task": "^1.0.0",
43
45
  "effect": "^4.1.0"
44
46
  },
45
- "peerDependenciesMeta": {
46
- "@sshkeda/task": {
47
- "optional": true
48
- }
49
- },
50
47
  "engines": {
51
48
  "bun": ">=1.4.4"
52
49
  }
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env bun
2
+ import { Task } from "@sshkeda/task";
3
+
4
+ import { PROJECT_DIRECTORY_VARIABLE } from "../src/lib/project-directory/index.ts";
5
+ import { check } from "../src/lib/tasks/index.ts";
6
+
7
+ if (import.meta.main) {
8
+ process.env[PROJECT_DIRECTORY_VARIABLE] = process.cwd();
9
+ Task.main(check);
10
+ }
@@ -1,8 +1,10 @@
1
1
  import { defineRule } from "@oxlint/plugins";
2
2
  import type { ESTree } from "@oxlint/plugins";
3
+ import { Schema } from "effect";
3
4
 
4
5
  import { FORMAT_CONFIG_BINDING, FORMAT_CONFIG_FILE } from "../shared/config-file-names.ts";
5
6
  import { fileNameOf, isEntryFileName } from "../shared/match-file-names-and-path-prefixes.ts";
7
+ import { decodeRuleOptions, ruleOptionsJsonSchema } from "./describe-and-decode-rule-options.ts";
6
8
  import {
7
9
  DEFAULT_EXPORT,
8
10
  importedBindingOf,
@@ -13,6 +15,12 @@ import {
13
15
  unwrappedExpression,
14
16
  } from "./resolve-what-an-expression-imports.ts";
15
17
 
18
+ const DEFAULT_EXPORTS_OF_IMPORTS = ["banned", "allowed"] as const;
19
+
20
+ const RE_EXPORTS_OPTIONS = Schema.Struct({
21
+ defaultExportOfAnImport: Schema.optionalKey(Schema.Literals(DEFAULT_EXPORTS_OF_IMPORTS)),
22
+ });
23
+
16
24
  export const noReExports = defineRule({
17
25
  meta: {
18
26
  type: "suggestion",
@@ -30,8 +38,11 @@ export const noReExports = defineRule({
30
38
  importEquals: "export import {{name}} re-exports another module",
31
39
  exportAssignment: "export = replaces the module's exports",
32
40
  },
41
+ schema: ruleOptionsJsonSchema(RE_EXPORTS_OPTIONS),
33
42
  },
34
43
  create(context) {
44
+ const defaultExportOfAnImport =
45
+ decodeRuleOptions(RE_EXPORTS_OPTIONS, context.options)?.defaultExportOfAnImport ?? "banned";
35
46
  const isIndexFile = isEntryFileName(fileNameOf(context.filename));
36
47
  const isFormatConfigFile = fileNameOf(context.filename) === FORMAT_CONFIG_FILE;
37
48
  const reportImport = (
@@ -195,7 +206,9 @@ export const noReExports = defineRule({
195
206
  statement.declaration.name === FORMAT_CONFIG_BINDING &&
196
207
  importedExpression(statement.declaration, context.sourceCode)?.members.join() ===
197
208
  FORMAT_CONFIG_BINDING;
198
- if (!exportsTheFormatConfig) {
209
+ const isAllowedDefaultExport =
210
+ exportsTheFormatConfig || defaultExportOfAnImport === "allowed";
211
+ if (!isAllowedDefaultExport) {
199
212
  reportImported(statement, DEFAULT_EXPORT, statement.declaration);
200
213
  }
201
214
  } else if (statement.type === "TSExportAssignment") {
@@ -50,7 +50,6 @@ import { noExistsRecovery } from "./ban-recovering-file-system-exists.ts";
50
50
  import { noRetiredWords } from "./ban-retired-words-in-names.ts";
51
51
  import { noInstantRetries } from "./ban-retries-without-a-wait.ts";
52
52
  import { noGlobScan } from "./ban-scanning-directories-with-bun-glob.ts";
53
- import { noShebang } from "./ban-shebang-lines.ts";
54
53
  import { globalRule } from "./ban-timer-globals-and-the-global-object.ts";
55
54
  import { noSchedules } from "./ban-timer-schedules-without-deadlines.ts";
56
55
  import { noTypeDeclarations } from "./ban-type-and-interface-declarations.ts";
@@ -158,7 +157,6 @@ const NAMED_RULES = {
158
157
  "no-let": noLet,
159
158
  "no-mutable-ref": noMutableRef,
160
159
  "no-while": noWhile,
161
- "no-shebang": noShebang,
162
160
  "no-shared-holders": noSharedHolders,
163
161
  "no-uncalled-members": noUncalledMembers,
164
162
  "no-unnamed-fallbacks": noUnnamedFallbacks,
@@ -0,0 +1,5 @@
1
+ export {
2
+ givenArguments,
3
+ PROJECT_DIRECTORY_VARIABLE,
4
+ projectDirectory,
5
+ } from "./internal/read-the-project-directory.ts";
@@ -0,0 +1,13 @@
1
+ import { Config, Effect } from "effect";
2
+
3
+ export const PROJECT_DIRECTORY_VARIABLE = "SSHKEDA_CHECK_PROJECT_DIRECTORY";
4
+
5
+ const THE_PACKAGE_THAT_RUNS_THE_TASK = ".";
6
+
7
+ const FIRST_SCRIPT_ARGUMENT = 2;
8
+
9
+ export const projectDirectory = Config.String(PROJECT_DIRECTORY_VARIABLE).pipe(
10
+ Config.withDefault(THE_PACKAGE_THAT_RUNS_THE_TASK),
11
+ );
12
+
13
+ export const givenArguments = Effect.sync(() => process.argv.slice(FIRST_SCRIPT_ARGUMENT));
@@ -1,6 +1,7 @@
1
1
  import { Task } from "@sshkeda/task";
2
2
  import { Effect, Path } from "effect";
3
3
 
4
+ import { projectDirectory } from "../../project-directory/index.ts";
4
5
  import { duplicateFunctions } from "../lib/detect-duplicate-functions/index.ts";
5
6
  import { layout } from "../lib/enforce-file-layout/index.ts";
6
7
  import { fileTypes } from "../lib/enforce-file-types/index.ts";
@@ -8,24 +9,31 @@ import { formatting } from "../lib/format-with-bun-fmt/index.ts";
8
9
  import { lintConfigFile } from "../shared/declare-lint-config-task.ts";
9
10
  import { typeScriptConfig } from "../shared/declare-typescript-config-task.ts";
10
11
  import { typedChecks } from "./declare-the-typed-checks-task.ts";
12
+ import { verifyWhereCheckWasStarted } from "./verify-where-check-was-started.ts";
11
13
 
12
14
  const GUIDE_FILE = new URL("../../../../docs/how-to-make-check-pass.md", import.meta.url);
13
15
 
14
16
  const pointAtTheGuide = Effect.gen(function* () {
15
17
  const path = yield* Path.Path;
16
- const guide = path.relative(path.resolve("."), yield* path.fromFileUrl(GUIDE_FILE));
18
+ const guide = path.relative(
19
+ path.resolve(yield* projectDirectory),
20
+ yield* path.fromFileUrl(GUIDE_FILE),
21
+ );
17
22
  yield* Effect.logError(`read ${guide} before you change the code`);
18
23
  });
19
24
 
20
25
  export const check: Task.Task<void> = Task.make(
21
26
  "check",
22
- Task.allSettled([
23
- typeScriptConfig,
24
- lintConfigFile,
25
- typedChecks,
26
- formatting,
27
- layout,
28
- fileTypes,
29
- duplicateFunctions,
30
- ]).pipe(Effect.tapError(() => pointAtTheGuide)),
27
+ Effect.andThen(
28
+ verifyWhereCheckWasStarted,
29
+ Task.allSettled([
30
+ typeScriptConfig,
31
+ lintConfigFile,
32
+ typedChecks,
33
+ formatting,
34
+ layout,
35
+ fileTypes,
36
+ duplicateFunctions,
37
+ ]).pipe(Effect.tapError(() => pointAtTheGuide)),
38
+ ),
31
39
  );
@@ -10,6 +10,7 @@ import { findRepeatedServiceYields } from "../lib/detect-repeated-service-yields
10
10
  import { findIndexReExportViolations } from "../lib/judge-index-re-exports/index.ts";
11
11
  import { findExportPlacementViolations } from "../lib/place-exports-in-the-narrowest-directory/index.ts";
12
12
  import { findErrorMappingsToDone } from "../lib/refuse-error-mappings-that-end-streams/index.ts";
13
+ import { findUnobservedForkFailures } from "../lib/refuse-forks-whose-failures-nothing-observes/index.ts";
13
14
  import { findServiceLayerViolations } from "../lib/require-separate-service-layers/index.ts";
14
15
  import { inEachPackage } from "../shared/check-each-nested-package.ts";
15
16
  import { lintConfigFile } from "../shared/declare-lint-config-task.ts";
@@ -22,7 +23,7 @@ export const checkerQueries = Task.make(
22
23
  "checker-queries",
23
24
  Effect.andThen(
24
25
  Task.all([typeScriptConfig, lintConfigFile]),
25
- inEachPackage("checker-queries", ".", (packageDirectory) =>
26
+ inEachPackage("checker-queries", (packageDirectory) =>
26
27
  Path.Path.use((path) =>
27
28
  runChecksOverOneTypeScriptProject(path.join(packageDirectory, TYPESCRIPT_PROJECT_FILE), {
28
29
  "service-yields": findRepeatedServiceYields,
@@ -34,6 +35,7 @@ export const checkerQueries = Task.make(
34
35
  "export-placement": findExportPlacementViolations,
35
36
  "layer-provides": findLayerProvidesOutsideProgramStarts,
36
37
  "error-mappings": findErrorMappingsToDone,
38
+ "fork-failures": findUnobservedForkFailures,
37
39
  "re-exports": findIndexReExportViolations,
38
40
  }),
39
41
  ),
@@ -0,0 +1,50 @@
1
+ import { Task } from "@sshkeda/task";
2
+ import { Effect, FileSystem, Path, References, Schema } from "effect";
3
+
4
+ import { givenArguments, projectDirectory } from "../../project-directory/index.ts";
5
+ import { quietViolations } from "../shared/print-failures-and-keep-detail-in-the-trace.ts";
6
+
7
+ const PACKAGE_MANIFEST_FILE = "package.json";
8
+
9
+ class ArgumentsNotAccepted extends Schema.TaggedError<ArgumentsNotAccepted>()(
10
+ "ArgumentsNotAccepted",
11
+ { givenArguments: Schema.Array(Schema.String) },
12
+ ) {
13
+ public override get message() {
14
+ return `check takes no arguments, and was given ${this.givenArguments.join(" ")}`;
15
+ }
16
+ }
17
+
18
+ class PackageManifestMissing extends Schema.TaggedError<PackageManifestMissing>()(
19
+ "PackageManifestMissing",
20
+ { directory: Schema.String },
21
+ ) {
22
+ public override get message() {
23
+ return `${this.directory} has no ${PACKAGE_MANIFEST_FILE}`;
24
+ }
25
+ }
26
+
27
+ class RepositoryNotFound extends Schema.TaggedError<RepositoryNotFound>()("RepositoryNotFound", {
28
+ directory: Schema.String,
29
+ }) {
30
+ public override get message() {
31
+ return `${this.directory} is not inside a git repository`;
32
+ }
33
+ }
34
+
35
+ export const verifyWhereCheckWasStarted = Effect.gen(function* () {
36
+ const fileSystem = yield* FileSystem.FileSystem;
37
+ const path = yield* Path.Path;
38
+ const given = yield* givenArguments;
39
+ if (given.length > 0) {
40
+ return yield* new ArgumentsNotAccepted({ givenArguments: given });
41
+ }
42
+ const directory = path.resolve(yield* projectDirectory);
43
+ if (!(yield* fileSystem.exists(path.join(directory, PACKAGE_MANIFEST_FILE)))) {
44
+ return yield* new PackageManifestMissing({ directory });
45
+ }
46
+ yield* Task.exec("git", ["rev-parse", "--show-toplevel"], { cwd: directory }).pipe(
47
+ Effect.provideService(References.MinimumLogLevel, "Warn"),
48
+ Effect.catchTag("CommandFailed", () => Effect.fail(new RepositoryNotFound({ directory }))),
49
+ );
50
+ }).pipe(quietViolations("check"));
@@ -11,6 +11,6 @@ export const effectStrict = Task.make(
11
11
  "effect-tsgo",
12
12
  Effect.andThen(
13
13
  Task.all([typeScriptConfig, lintConfigFile]),
14
- inEachPackage("effect-tsgo", ".", effectDiagnosticsIn),
14
+ inEachPackage("effect-tsgo", effectDiagnosticsIn),
15
15
  ).pipe(Effect.provide(ProjectQueryCachesLive)),
16
16
  );
@@ -4,7 +4,6 @@ import { NodeFlags } from "typescript/unstable/ast";
4
4
  import type { CallExpression, Node, SourceFile } from "typescript/unstable/ast";
5
5
  import {
6
6
  isArrowFunction,
7
- isAsExpression,
8
7
  isBlock,
9
8
  isCallExpression,
10
9
  isConditionalExpression,
@@ -12,13 +11,9 @@ import {
12
11
  isFunctionDeclaration,
13
12
  isFunctionExpression,
14
13
  isIdentifier,
15
- isNonNullExpression,
16
14
  isObjectLiteralExpression,
17
15
  isParameterDeclaration,
18
- isParenthesizedExpression,
19
- isPropertyAccessExpression,
20
16
  isReturnStatement,
21
- isSatisfiesExpression,
22
17
  isSpreadElement,
23
18
  isVariableDeclaration,
24
19
  isVariableStatement,
@@ -37,6 +32,12 @@ import {
37
32
  typeScriptRequest,
38
33
  } from "../../../shared/query-the-typescript-project.ts";
39
34
  import type { openTypeScriptProjects } from "../../../shared/query-the-typescript-project.ts";
35
+ import {
36
+ outermostWrapperOf,
37
+ pipedStepsOf,
38
+ referencedNameOf,
39
+ withoutWrappers,
40
+ } from "../../../shared/read-piped-steps-through-wrappers.ts";
40
41
  import { absolutePathOf } from "../../../shared/resolve-absolute-paths.ts";
41
42
  import {
42
43
  LayerProvidedOutsideProgramStart,
@@ -78,7 +79,6 @@ const EFFECT_OPERATIONS = [
78
79
  { module: STREAM_MODULE, names: PROVIDE_OPERATIONS },
79
80
  ];
80
81
  const PROVIDING_MODULES = [EFFECT_MODULE, STREAM_MODULE];
81
- const PIPE = "pipe";
82
82
  const PROVIDED_ARGUMENTS = 2;
83
83
 
84
84
  const anyOf = <Item, Failure>(
@@ -86,18 +86,6 @@ const anyOf = <Item, Failure>(
86
86
  predicate: (item: Item) => Effect.Effect<boolean, Failure>,
87
87
  ) => Effect.findFirst(items, predicate).pipe(Effect.map(Option.isSome));
88
88
 
89
- const isWrapper = (node: Node) =>
90
- isParenthesizedExpression(node) ||
91
- isAsExpression(node) ||
92
- isSatisfiesExpression(node) ||
93
- isNonNullExpression(node);
94
-
95
- const withoutWrappers = (node: Node): Node =>
96
- isWrapper(node) ? withoutWrappers(node.expression) : node;
97
-
98
- const outermostWrapperOf = (node: Node): Node =>
99
- isWrapper(node.parent) ? outermostWrapperOf(node.parent) : node;
100
-
101
89
  const isFunctionLike = (node: Node) =>
102
90
  isFunctionDeclaration(node) || isFunctionExpression(node) || isArrowFunction(node);
103
91
 
@@ -122,14 +110,6 @@ const returnedBy = (callable: Node) => {
122
110
  return returned;
123
111
  };
124
112
 
125
- const referencedNameOf = (node: Node) => {
126
- const reference = withoutWrappers(node);
127
- if (isPropertyAccessExpression(reference)) {
128
- return reference.name;
129
- }
130
- return isIdentifier(reference) ? reference : undefined;
131
- };
132
-
133
113
  const isExportOf = (
134
114
  apis: Effect.Success<ReturnType<typeof importedPackageApis>>,
135
115
  symbol: TypeScriptSymbol | undefined,
@@ -137,16 +117,6 @@ const isExportOf = (
137
117
  names: ReadonlySet<string>,
138
118
  ) => symbol !== undefined && names.has(symbol.name) && apis.effectExports.get(symbol.id) === module;
139
119
 
140
- const pipedStepsOf = (call: CallExpression) => {
141
- if (referencedNameOf(call.expression)?.text !== PIPE) {
142
- return;
143
- }
144
- const callee = withoutWrappers(call.expression);
145
- return isPropertyAccessExpression(callee)
146
- ? [callee.expression, ...call.arguments]
147
- : [...call.arguments];
148
- };
149
-
150
120
  const stepsBefore = (step: Node) => {
151
121
  const { parent } = step;
152
122
  if (!isCallExpression(parent)) {
@@ -142,9 +142,8 @@ export const findDuplicateFunctions = (directory: string, shownDirectory = ".")
142
142
 
143
143
  export const duplicateFunctionsOfEachPackage = inEachPackage(
144
144
  DUPLICATE_FUNCTIONS,
145
- ".",
146
- (packageDirectory) =>
147
- findDuplicateFunctions(packageDirectory, packageDirectory).pipe(
145
+ (packageDirectory, packageDirectoryFromTheProject) =>
146
+ findDuplicateFunctions(packageDirectory, packageDirectoryFromTheProject).pipe(
148
147
  quietViolations(DUPLICATE_FUNCTIONS),
149
148
  ),
150
149
  );
@@ -4,6 +4,6 @@ import { findFileLayoutViolations } from "./find-file-layout-violations.ts";
4
4
 
5
5
  const LAYOUT = "layout";
6
6
 
7
- export const fileLayoutViolationsOfEachPackage = inEachPackage(LAYOUT, ".", (packageDirectory) =>
7
+ export const fileLayoutViolationsOfEachPackage = inEachPackage(LAYOUT, (packageDirectory) =>
8
8
  findFileLayoutViolations(packageDirectory).pipe(quietViolations(LAYOUT)),
9
9
  );
@@ -73,16 +73,32 @@ export class TopLevelScriptWithoutMain extends Schema.TaggedError<TopLevelScript
73
73
  { location: Schema.String },
74
74
  ) {
75
75
  public override get message() {
76
- return `sits directly in ${SCRIPTS_DIRECTORY}/ and does not call ${TASK_NAMESPACE_EXPORT}.${TASK_MAIN_MEMBER} imported from ${TASK_PACKAGE}`;
76
+ return `sits directly in ${SCRIPTS_DIRECTORY}/ and neither calls ${TASK_NAMESPACE_EXPORT}.${TASK_MAIN_MEMBER} imported from ${TASK_PACKAGE} nor has a default export`;
77
77
  }
78
78
  }
79
79
 
80
+ const SCRIPT_ENTRY_POINTS = ["main-guard", "default-export"] as const;
81
+
82
+ const STATEMENTS_A_SCRIPT_MAY_HOLD = {
83
+ "main-guard": `an import and if (import.meta.main) { ${TASK_NAMESPACE_EXPORT}.${TASK_MAIN_MEMBER}(task); }`,
84
+ "default-export": "an import and export default task",
85
+ } satisfies Record<(typeof SCRIPT_ENTRY_POINTS)[number], string>;
86
+
80
87
  export class TopLevelScriptHoldsOtherStatement extends Schema.TaggedError<TopLevelScriptHoldsOtherStatement>()(
81
88
  "TopLevelScriptHoldsOtherStatement",
89
+ { location: Schema.String, entryPoint: Schema.Literals(SCRIPT_ENTRY_POINTS) },
90
+ ) {
91
+ public override get message() {
92
+ return `holds a statement other than ${STATEMENTS_A_SCRIPT_MAY_HOLD[this.entryPoint]}`;
93
+ }
94
+ }
95
+
96
+ export class TopLevelScriptWithMainAndDefaultExport extends Schema.TaggedError<TopLevelScriptWithMainAndDefaultExport>()(
97
+ "TopLevelScriptWithMainAndDefaultExport",
82
98
  { location: Schema.String },
83
99
  ) {
84
100
  public override get message() {
85
- return `holds a statement other than an import and if (import.meta.main) { ${TASK_NAMESPACE_EXPORT}.${TASK_MAIN_MEMBER}(task); }`;
101
+ return `calls ${TASK_NAMESPACE_EXPORT}.${TASK_MAIN_MEMBER} and also has a default export. Keep one of them`;
86
102
  }
87
103
  }
88
104
 
@@ -388,6 +404,7 @@ export class FileLayoutViolations extends Schema.TaggedError<FileLayoutViolation
388
404
  TopLevelNonScript,
389
405
  TopLevelScriptWithoutMain,
390
406
  TopLevelScriptHoldsOtherStatement,
407
+ TopLevelScriptWithMainAndDefaultExport,
391
408
  LibraryDirectory,
392
409
  DirectoryOutsideModuleLayout,
393
410
  ModuleWithoutIndex,