@shivaedev/quality 0.48.2 → 0.50.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 (148) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/README.md +18 -10
  3. package/biome/plugins/ambient-runtime.grit +67 -1
  4. package/biome/plugins/type-re-exports.grit +33 -4
  5. package/dist/adapters/biome/run.js +4 -4
  6. package/dist/adapters/biome/run.js.map +1 -1
  7. package/dist/adapters/knip/configuration.d.ts +5 -2
  8. package/dist/adapters/knip/configuration.d.ts.map +1 -1
  9. package/dist/adapters/knip/configuration.js +12 -5
  10. package/dist/adapters/knip/configuration.js.map +1 -1
  11. package/dist/adapters/knip/run.d.ts +2 -3
  12. package/dist/adapters/knip/run.d.ts.map +1 -1
  13. package/dist/adapters/knip/run.js +25 -7
  14. package/dist/adapters/knip/run.js.map +1 -1
  15. package/dist/adapters/knip/sourceConfig.d.ts.map +1 -1
  16. package/dist/adapters/knip/sourceConfig.js +3 -0
  17. package/dist/adapters/knip/sourceConfig.js.map +1 -1
  18. package/dist/baseline/looseStagedEntries.d.ts +4 -0
  19. package/dist/baseline/looseStagedEntries.d.ts.map +1 -0
  20. package/dist/baseline/looseStagedEntries.js +31 -0
  21. package/dist/baseline/looseStagedEntries.js.map +1 -0
  22. package/dist/baseline/update.d.ts +1 -1
  23. package/dist/baseline/update.d.ts.map +1 -1
  24. package/dist/baseline/update.js +3 -3
  25. package/dist/baseline/update.js.map +1 -1
  26. package/dist/cli/args.d.ts +2 -1
  27. package/dist/cli/args.d.ts.map +1 -1
  28. package/dist/cli/args.js +9 -35
  29. package/dist/cli/args.js.map +1 -1
  30. package/dist/cli/baseline.d.ts +1 -1
  31. package/dist/cli/baseline.d.ts.map +1 -1
  32. package/dist/cli/baseline.js +25 -2
  33. package/dist/cli/baseline.js.map +1 -1
  34. package/dist/cli/lint.d.ts +1 -1
  35. package/dist/cli/lint.d.ts.map +1 -1
  36. package/dist/cli/lint.js +11 -2
  37. package/dist/cli/lint.js.map +1 -1
  38. package/dist/cli/messages.d.ts +1 -1
  39. package/dist/cli/messages.d.ts.map +1 -1
  40. package/dist/cli/messages.js +4 -3
  41. package/dist/cli/messages.js.map +1 -1
  42. package/dist/cli/options.d.ts +45 -0
  43. package/dist/cli/options.d.ts.map +1 -0
  44. package/dist/cli/options.js +35 -0
  45. package/dist/cli/options.js.map +1 -0
  46. package/dist/cli/program.d.ts.map +1 -1
  47. package/dist/cli/program.js +3 -2
  48. package/dist/cli/program.js.map +1 -1
  49. package/dist/hooks/preCommit.js +1 -1
  50. package/dist/hooks/preCommit.js.map +1 -1
  51. package/dist/imports/classifyRuntime.d.ts.map +1 -1
  52. package/dist/imports/classifyRuntime.js +4 -1
  53. package/dist/imports/classifyRuntime.js.map +1 -1
  54. package/dist/imports/runtime.d.ts +6 -3
  55. package/dist/imports/runtime.d.ts.map +1 -1
  56. package/dist/imports/runtime.js +15 -21
  57. package/dist/imports/runtime.js.map +1 -1
  58. package/dist/inventory/collect.js +3 -3
  59. package/dist/inventory/collect.js.map +1 -1
  60. package/dist/inventory/shadcnFiles.d.ts +4 -0
  61. package/dist/inventory/shadcnFiles.d.ts.map +1 -0
  62. package/dist/inventory/shadcnFiles.js +83 -0
  63. package/dist/inventory/shadcnFiles.js.map +1 -0
  64. package/dist/naming/migrationFile.d.ts.map +1 -1
  65. package/dist/naming/migrationFile.js +3 -1
  66. package/dist/naming/migrationFile.js.map +1 -1
  67. package/dist/naming/moduleRoot.d.ts +2 -0
  68. package/dist/naming/moduleRoot.d.ts.map +1 -0
  69. package/dist/naming/moduleRoot.js +13 -0
  70. package/dist/naming/moduleRoot.js.map +1 -0
  71. package/dist/report/render.d.ts.map +1 -1
  72. package/dist/report/render.js +3 -10
  73. package/dist/report/render.js.map +1 -1
  74. package/dist/report/staleSections.js +1 -1
  75. package/dist/report/staleSections.js.map +1 -1
  76. package/dist/rules/builtIn.d.ts +2 -2
  77. package/dist/rules/effect/boundary.d.ts.map +1 -1
  78. package/dist/rules/effect/boundary.js +30 -6
  79. package/dist/rules/effect/boundary.js.map +1 -1
  80. package/dist/rules/effect/boundaryCalls.d.ts +3 -0
  81. package/dist/rules/effect/boundaryCalls.d.ts.map +1 -0
  82. package/dist/rules/effect/boundaryCalls.js +121 -0
  83. package/dist/rules/effect/boundaryCalls.js.map +1 -0
  84. package/dist/rules/effect/boundaryReference.d.ts +4 -0
  85. package/dist/rules/effect/boundaryReference.d.ts.map +1 -0
  86. package/dist/rules/effect/boundaryReference.js +80 -0
  87. package/dist/rules/effect/boundaryReference.js.map +1 -0
  88. package/dist/rules/effect/program.d.ts +1 -1
  89. package/dist/rules/effect/program.d.ts.map +1 -1
  90. package/dist/rules/effect/program.js +4 -1
  91. package/dist/rules/effect/program.js.map +1 -1
  92. package/dist/rules/effect/promiseParameter.d.ts +3 -0
  93. package/dist/rules/effect/promiseParameter.d.ts.map +1 -0
  94. package/dist/rules/effect/promiseParameter.js +24 -0
  95. package/dist/rules/effect/promiseParameter.js.map +1 -0
  96. package/dist/rules/importOrigins.d.ts +4 -0
  97. package/dist/rules/importOrigins.d.ts.map +1 -0
  98. package/dist/rules/importOrigins.js +36 -0
  99. package/dist/rules/importOrigins.js.map +1 -0
  100. package/dist/rules/imports/runtime.d.ts +1 -1
  101. package/dist/rules/imports/runtime.d.ts.map +1 -1
  102. package/dist/rules/noBarrel.d.ts.map +1 -1
  103. package/dist/rules/noBarrel.js +19 -7
  104. package/dist/rules/noBarrel.js.map +1 -1
  105. package/dist/rules/secondNames.d.ts +7 -0
  106. package/dist/rules/secondNames.d.ts.map +1 -0
  107. package/dist/rules/secondNames.js +98 -0
  108. package/dist/rules/secondNames.js.map +1 -0
  109. package/dist/rules/stories/groupedOutcomes.js +1 -1
  110. package/dist/rules/stories/groupedOutcomes.js.map +1 -1
  111. package/dist/rules/stories/importBindings.d.ts.map +1 -1
  112. package/dist/rules/stories/importBindings.js +3 -0
  113. package/dist/rules/stories/importBindings.js.map +1 -1
  114. package/dist/rules/unusedCode.d.ts +1 -1
  115. package/dist/rules/unusedCode.d.ts.map +1 -1
  116. package/docs/debt-and-hooks.md +7 -5
  117. package/package.json +3 -3
  118. package/src/adapters/biome/run.ts +4 -4
  119. package/src/adapters/knip/configuration.ts +12 -5
  120. package/src/adapters/knip/run.ts +27 -7
  121. package/src/adapters/knip/sourceConfig.ts +3 -0
  122. package/src/baseline/looseStagedEntries.ts +32 -0
  123. package/src/baseline/update.ts +5 -2
  124. package/src/cli/args.ts +16 -45
  125. package/src/cli/baseline.ts +26 -1
  126. package/src/cli/lint.ts +11 -1
  127. package/src/cli/messages.ts +4 -3
  128. package/src/cli/options.ts +43 -0
  129. package/src/cli/program.ts +3 -2
  130. package/src/hooks/preCommit.ts +1 -1
  131. package/src/imports/classifyRuntime.ts +4 -1
  132. package/src/imports/runtime.ts +19 -21
  133. package/src/inventory/collect.ts +3 -3
  134. package/src/inventory/shadcnFiles.ts +87 -0
  135. package/src/naming/migrationFile.ts +3 -1
  136. package/src/naming/moduleRoot.ts +13 -0
  137. package/src/report/render.ts +3 -10
  138. package/src/report/staleSections.ts +1 -1
  139. package/src/rules/effect/boundary.ts +48 -24
  140. package/src/rules/effect/boundaryCalls.ts +130 -0
  141. package/src/rules/effect/boundaryReference.ts +85 -0
  142. package/src/rules/effect/program.ts +4 -1
  143. package/src/rules/effect/promiseParameter.ts +26 -0
  144. package/src/rules/importOrigins.ts +38 -0
  145. package/src/rules/noBarrel.ts +21 -8
  146. package/src/rules/secondNames.ts +121 -0
  147. package/src/rules/stories/groupedOutcomes.ts +1 -1
  148. package/src/rules/stories/importBindings.ts +3 -0
@@ -1,7 +1,7 @@
1
- import { statSync } from "node:fs";
2
1
  import { join, posix } from "node:path";
3
- import { Effect } from "effect";
2
+ import { Effect, FileSystem } from "effect";
4
3
  import { readPackageManifest } from "#adapters/manifestInventory.ts";
4
+ import { orWhenAbsent } from "#inventory/filesystem.ts";
5
5
  import type { RuleInputs } from "#rule.ts";
6
6
  import { classifyRuntime } from "./classifyRuntime.ts";
7
7
  import type { WorkspacePackage } from "./workspace.ts";
@@ -29,29 +29,26 @@ function declaredRuntime(manifest: object | undefined): DeclaredRuntime | undefi
29
29
  return value === "client" || value === "server" ? value : undefined;
30
30
  }
31
31
 
32
- function directoryExists(path: string): boolean {
33
- try {
34
- return statSync(path).isDirectory();
35
- } catch (cause) {
36
- if (cause instanceof Error && "code" in cause && (cause.code === "ENOENT" || cause.code === "ENOTDIR")) {
37
- return false;
38
- }
39
- throw cause;
40
- }
41
- }
42
-
43
32
  type Owner = Pick<WorkspacePackage, "directory">;
44
33
 
45
- function runtimeFoldersOf(root: string, owner: Owner): readonly string[] {
46
- return ["client", "server"].filter((folder) => directoryExists(join(root, owner.directory, "src", folder)));
47
- }
34
+ const runtimeFoldersOf = Effect.fn("Imports.runtimeFoldersOf")(function* (root: string, owner: Owner) {
35
+ const fs = yield* FileSystem.FileSystem;
36
+ return yield* Effect.filter(["client", "server"], (folder) => {
37
+ const path = join(root, owner.directory, "src", folder);
38
+ return orWhenAbsent(
39
+ path,
40
+ Effect.map(fs.stat(path), (info) => info.type === "Directory"),
41
+ false,
42
+ );
43
+ });
44
+ });
48
45
 
49
46
  export interface RuntimeDeclarationProblem {
50
47
  readonly directory: string;
51
48
  readonly message: string;
52
49
  }
53
50
 
54
- function problemsFor(root: string, owner: Owner, manifest: object | undefined): readonly RuntimeDeclarationProblem[] {
51
+ const problemsFor = Effect.fn("Imports.problemsFor")(function* (root: string, owner: Owner, manifest: object | undefined) {
55
52
  const value = declaredValue(manifest);
56
53
  if (value === undefined) {
57
54
  return [];
@@ -64,7 +61,7 @@ function problemsFor(root: string, owner: Owner, manifest: object | undefined):
64
61
  },
65
62
  ];
66
63
  }
67
- const folders = runtimeFoldersOf(root, owner);
64
+ const folders = yield* runtimeFoldersOf(root, owner);
68
65
  return folders.length === 0
69
66
  ? []
70
67
  : [
@@ -73,14 +70,14 @@ function problemsFor(root: string, owner: Owner, manifest: object | undefined):
73
70
  message: `"quality.runtime" declares the whole package ${value} code, but it also has ${folders.map((folder) => `src/${folder}/`).join(" and ")}. Keep one convention: remove the declaration and classify code by runtime folders, or move the code out of the runtime folders.`,
74
71
  },
75
72
  ];
76
- }
73
+ });
77
74
 
78
75
  export const runtimeDeclarationProblems = Effect.fn("Imports.runtimeDeclarationProblems")(function* (
79
76
  inputs: RuleInputs,
80
77
  packages: readonly WorkspacePackage[],
81
78
  ) {
82
79
  const problems = yield* Effect.forEach([...packages, { directory: ".", name: "." }], (owner) =>
83
- Effect.map(
80
+ Effect.flatMap(
84
81
  Effect.promise(() => readPackageManifest(inputs.readText, posix.join(owner.directory, "package.json"))),
85
82
  (manifest) => problemsFor(inputs.root, owner, manifest),
86
83
  ),
@@ -98,7 +95,8 @@ export const runtimeOf = Effect.fn("Imports.runtimeOf")(function* (inputs: RuleI
98
95
  ),
99
96
  ),
100
97
  );
101
- const adopted = new Set(owners.filter((owner) => runtimeFoldersOf(inputs.root, owner).length > 0).map((owner) => owner.directory));
98
+ const folders = yield* Effect.filter(owners, (owner) => Effect.map(runtimeFoldersOf(inputs.root, owner), (found) => found.length > 0));
99
+ const adopted = new Set(folders.map((owner) => owner.directory));
102
100
  const declared = new Map(
103
101
  owners.flatMap((owner) => {
104
102
  const runtime = declaredRuntime(manifests.get(owner.directory));
@@ -7,7 +7,7 @@ import type { RuleServices, SourceFile } from "#rule.ts";
7
7
  import { FilesystemFailure, orWhenAbsent, readRequiredText } from "./filesystem.ts";
8
8
  import { posix } from "./ignoreScope.ts";
9
9
  import { linesOf } from "./linesOf.ts";
10
- import { shadcnFolders } from "./shadcnFolders.ts";
10
+ import { shadcnFiles } from "./shadcnFiles.ts";
11
11
  import { scopeAbove, walk } from "./walk.ts";
12
12
 
13
13
  export interface InventoryScope {
@@ -49,12 +49,12 @@ export const collectInventory = Effect.fn("Inventory.collectInventory")(function
49
49
  scope: InventoryScope,
50
50
  ): Effect.fn.Return<Inventory, FilesystemFailure | SetupFailure, RuleServices> {
51
51
  const excluded = ignore().add([...scope.exclude]);
52
- const toolOwned = yield* shadcnFolders(root);
52
+ const toolOwned = new Set(yield* shadcnFiles(root));
53
53
  const zones = yield* Effect.forEach(scope.sources, (zone) => zoneFiles(root, zone), { concurrency: READ_CONCURRENCY });
54
54
  const relativePaths = zones.flat().map((absolute) => posix(relative(root, absolute)));
55
55
  const files = [...new Set(relativePaths)]
56
56
  .filter((path) => !excluded.ignores(path))
57
- .filter((path) => !toolOwned.some((folder) => path.startsWith(`${folder}/`)))
57
+ .filter((path) => !toolOwned.has(path))
58
58
  .sort((left, right) => {
59
59
  if (left === right) {
60
60
  return 0;
@@ -0,0 +1,87 @@
1
+ import { basename, join, matchesGlob, relative } from "node:path";
2
+ import { Effect } from "effect";
3
+ import { posix } from "./ignoreScope.ts";
4
+ import { shadcnFolders } from "./shadcnFolders.ts";
5
+ import { walk } from "./walk.ts";
6
+
7
+ // Registry item names are the CLI ownership convention; custom names stay authored.
8
+ const COMPONENTS = [
9
+ "accordion",
10
+ "alert",
11
+ "alert-dialog",
12
+ "aspect-ratio",
13
+ "attachment",
14
+ "avatar",
15
+ "badge",
16
+ "breadcrumb",
17
+ "bubble",
18
+ "button",
19
+ "button-group",
20
+ "calendar",
21
+ "card",
22
+ "carousel",
23
+ "chart",
24
+ "checkbox",
25
+ "collapsible",
26
+ "combobox",
27
+ "command",
28
+ "context-menu",
29
+ "dialog",
30
+ "direction",
31
+ "drawer",
32
+ "dropdown-menu",
33
+ "empty",
34
+ "field",
35
+ "form",
36
+ "hover-card",
37
+ "input",
38
+ "input-group",
39
+ "input-otp",
40
+ "item",
41
+ "kbd",
42
+ "label",
43
+ "marker",
44
+ "menubar",
45
+ "message",
46
+ "message-scroller",
47
+ "native-select",
48
+ "navigation-menu",
49
+ "pagination",
50
+ "popover",
51
+ "progress",
52
+ "radio-group",
53
+ "resizable",
54
+ "scroll-area",
55
+ "select",
56
+ "separator",
57
+ "sheet",
58
+ "sidebar",
59
+ "skeleton",
60
+ "slider",
61
+ "sonner",
62
+ "spinner",
63
+ "switch",
64
+ "table",
65
+ "tabs",
66
+ "textarea",
67
+ "toast",
68
+ "toaster",
69
+ "toggle",
70
+ "toggle-group",
71
+ "tooltip",
72
+ ];
73
+
74
+ export const SHADCN_FILE_GLOB = `{${COMPONENTS.join(",")}}.{ts,tsx,js,jsx}`;
75
+
76
+ export const shadcnFiles = Effect.fn("Inventory.shadcnFiles")(function* (root: string) {
77
+ const folders = yield* shadcnFolders(root);
78
+ const files = yield* Effect.forEach(folders, (folder) => walk(join(root, folder)));
79
+ return [
80
+ ...new Set(
81
+ files
82
+ .flat()
83
+ .filter((path) => matchesGlob(basename(path), SHADCN_FILE_GLOB))
84
+ .map((path) => posix(relative(root, path))),
85
+ ),
86
+ ];
87
+ });
@@ -1,4 +1,5 @@
1
1
  import { posix } from "node:path";
2
+ import { moduleRoot } from "./moduleRoot.ts";
2
3
 
3
4
  export const MIGRATIONS_DIRECTORY = "src/server/migrations";
4
5
  export const MIGRATION_FILE_PATTERN = /^[0-9]{4}_[a-z][a-z0-9]*(?:_[a-z0-9]+)*\.ts$/u;
@@ -6,6 +7,7 @@ export const MIGRATION_FILE_PATTERN = /^[0-9]{4}_[a-z][a-z0-9]*(?:_[a-z0-9]+)*\.
6
7
  export function isMigrationFile(path: string): boolean {
7
8
  const directory = posix.dirname(path);
8
9
  const conventional = directory === MIGRATIONS_DIRECTORY || directory.endsWith(`/${MIGRATIONS_DIRECTORY}`);
9
- const modular = /(?:^|\/)src\/modules\/[^/]+\/server\/migrations$/u.test(directory);
10
+ const root = moduleRoot(path);
11
+ const modular = root !== undefined && directory === `${root}/server/migrations`;
10
12
  return (conventional || modular) && MIGRATION_FILE_PATTERN.test(posix.basename(path));
11
13
  }
@@ -0,0 +1,13 @@
1
+ const RUNTIME = /^(?:client|server|test-support)\//u;
2
+
3
+ export function moduleRoot(path: string): string | undefined {
4
+ const match = /^(?<root>(?:.*\/)?src\/modules\/[^/]+)\/(?<local>.+)$/u.exec(path)?.groups;
5
+ if (match?.root === undefined || match.local === undefined) {
6
+ return undefined;
7
+ }
8
+ if (RUNTIME.test(match.local)) {
9
+ return match.root;
10
+ }
11
+ const separator = match.local.indexOf("/");
12
+ return separator > 0 && RUNTIME.test(match.local.slice(separator + 1)) ? `${match.root}/${match.local.slice(0, separator)}` : undefined;
13
+ }
@@ -60,15 +60,8 @@ function summary(outcome: Outcome, context: ReportContext): string {
60
60
  return `${verdict}. ${plural(context.checked, "source file")} checked${suppressed}.`;
61
61
  }
62
62
 
63
- function rerecord(outcome: Outcome): readonly string[] {
64
- const rules = [...new Set(outcome.errors.map((violation) => violation.rule))].sort((left, right) => left.localeCompare(right));
65
- if (rules.length === 0) {
66
- return [];
67
- }
68
- const command = ["quality baseline write", ...rules.map((rule) => `--rule ${rule}`)].join(" ");
69
- return [
70
- `If the baseline should keep these findings, such as the debt of a moved or renamed file, record them again with \`${command}\` and call out the baseline growth in the pull request description.`,
71
- ];
63
+ function movedDebt(outcome: Outcome): readonly string[] {
64
+ return passes(outcome) ? [] : ["For moved files, carry their existing debt with `quality baseline prune --against <base>`."];
72
65
  }
73
66
 
74
67
  export function render(outcome: Outcome, context: ReportContext): string {
@@ -77,6 +70,6 @@ export function render(outcome: Outcome, context: ReportContext): string {
77
70
  ...ruleSections(outcome.errors, context, regressions),
78
71
  ...staleSections(outcome, context),
79
72
  summary(outcome, context),
80
- ...rerecord(outcome),
73
+ ...movedDebt(outcome),
81
74
  ].join("\n\n");
82
75
  }
@@ -27,7 +27,7 @@ export function staleSections(outcome: Outcome, files: StaleFiles): readonly str
27
27
  ...section(
28
28
  "error",
29
29
  files.baseline,
30
- "An entry for an unknown rule covers nothing. Run `quality baseline prune` to drop it.",
30
+ "Drop unknown rules with `quality baseline prune`. Lower staged counts with `quality baseline tighten --staged`, then stage the baseline.",
31
31
  outcome.staleBaseline.map(baselineLine),
32
32
  ),
33
33
  ...section(
@@ -1,5 +1,10 @@
1
+ import { join } from "node:path";
1
2
  import ts from "typescript";
2
- import { defineRule, type Finding } from "#rule.ts";
3
+ import { defineRule, type Finding, type RuleInputs } from "#rule.ts";
4
+ import { boundaryCalls } from "#rules/effect/boundaryCalls.ts";
5
+ import { boundaryEntryPoint } from "#rules/effect/boundaryReference.ts";
6
+ import { effectProgram } from "#rules/effect/program.ts";
7
+ import { promiseParameter } from "#rules/effect/promiseParameter.ts";
3
8
  import { parse } from "#rules/parse.ts";
4
9
 
5
10
  function asyncConstruct(node: ts.Node): string | undefined {
@@ -44,32 +49,51 @@ function construct(node: ts.Node): string | undefined {
44
49
  return ts.isThrowStatement(node) ? "throw statement" : asyncConstruct(node);
45
50
  }
46
51
 
47
- export const effectBoundary = defineRule({
48
- check: ({ sources }) =>
49
- sources.flatMap((file) => {
50
- if (/(?:^|\/)(?:adapters|test-support)\//u.test(file.path)) {
51
- return [];
52
- }
53
- const source = parse(file);
54
- const findings: Finding[] = [];
55
- function visit(node: ts.Node): void {
56
- const name = construct(node);
57
- if (name !== undefined && source !== undefined) {
58
- findings.push({
59
- file: file.path,
60
- line: source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1,
61
- message: `Uses ${name}. Model the work with Effect; fail with a tagged error (or Effect.die for a defect). Move a bridge to an outside library into an adapters/ folder, returning Effect values.`,
62
- });
63
- }
64
- ts.forEachChild(node, visit);
52
+ function checkBoundary(inputs: RuleInputs): readonly Finding[] {
53
+ const program = effectProgram(inputs, false);
54
+ const checker = program.getTypeChecker();
55
+ const call = boundaryCalls(checker);
56
+ return inputs.sources.flatMap((file) => {
57
+ if (/(?:^|\/)(?:adapters|test-support)\//u.test(file.path)) {
58
+ return [];
59
+ }
60
+ const source = parse(file) === undefined ? undefined : program.getSourceFile(join(inputs.root, file.path));
61
+ const findings: Finding[] = [];
62
+ const entryPoint = source !== undefined && boundaryEntryPoint(file.path, source, checker);
63
+ function visit(node: ts.Node): void {
64
+ const name = construct(node) ?? call(node, entryPoint) ?? promiseParameter(checker, node);
65
+ if (name !== undefined && source !== undefined) {
66
+ findings.push({
67
+ file: file.path,
68
+ line: source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1,
69
+ message: `Uses ${name}. Model the work with Effect; fail with a tagged error (or Effect.die for a defect). Move a bridge to an outside library into an adapters/ folder, returning Effect values.`,
70
+ });
65
71
  }
66
- if (source !== undefined) {
67
- visit(source);
72
+ ts.forEachChild(node, visit);
73
+ }
74
+ if (source !== undefined) {
75
+ visit(source);
76
+ }
77
+ return findings;
78
+ });
79
+ }
80
+
81
+ export const effectBoundary = defineRule({
82
+ check: (inputs) => {
83
+ const groups = new Map<string, RuleInputs["sources"][number][]>();
84
+ for (const source of inputs.sources) {
85
+ if (/(?:^|\/)(?:adapters|test-support)\//u.test(source.path) || !/\.[cm]?[jt]sx?$/u.test(source.path)) {
86
+ continue;
68
87
  }
69
- return findings;
70
- }),
88
+ const group = /^(?:packages|examples)\/[^/]+/u.exec(source.path)?.[0] ?? ".";
89
+ const sources = groups.get(group) ?? [];
90
+ sources.push(source);
91
+ groups.set(group, sources);
92
+ }
93
+ return [...groups.values()].flatMap((sources) => checkBoundary({ ...inputs, sources }));
94
+ },
71
95
  description:
72
- "All checked source, tooling and specs use Effect instead of async, await, new Promise, .then, try or throw. Only adapters/ and test-support/ folders at any depth are exempt. adapters/ bridges outside libraries to Effect; it remains subject to service-parameter and ambient runtime bans. Use tagged errors or Effect.die for defects. Config exceptions and inline suppressions are refused; baseline existing debt.",
96
+ "Use Effect instead of Promise syntax and parameters, synchronous Node IO, throwing helper calls or Effect.sync with throwing APIs. Runners belong at entry points, tool configs and tests. Only adapters/ and test-support/ folders are exempt. Use Effect services and tagged errors; bridge outside libraries in adapters/. No config exceptions or inline suppressions; baseline existing debt.",
73
97
  id: "effect/boundary",
74
98
  registrable: false,
75
99
  });
@@ -0,0 +1,130 @@
1
+ import ts from "typescript";
2
+ import { boundaryReference } from "#rules/effect/boundaryReference.ts";
3
+
4
+ function synchronousIo(reference: string): boolean {
5
+ return /^(?:node:)?fs\.[^.]+Sync$/u.test(reference) || /^(?:node:)?child_process\.(?:execSync|execFileSync|spawnSync)$/u.test(reference);
6
+ }
7
+
8
+ function knownThrowing(reference: string): boolean {
9
+ return (
10
+ (synchronousIo(reference) && !/^(?:node:)?fs\.existsSync$/u.test(reference))
11
+ || /^(?:JSON\.parse|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|BigInt)$/u.test(reference)
12
+ || /^(?:effect\.Schema|effect\/Schema)\.(?:decodeUnknownSync|decodeSync|encodeUnknownSync|encodeSync)$/u.test(reference)
13
+ );
14
+ }
15
+
16
+ function immediateBody(node: ts.Node): ts.ConciseBody | undefined {
17
+ if (!(ts.isFunctionDeclaration(node) || ts.isMethodDeclaration(node) || ts.isArrowFunction(node) || ts.isFunctionExpression(node))) {
18
+ return undefined;
19
+ }
20
+ if (
21
+ node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword)
22
+ || ("asteriskToken" in node && node.asteriskToken !== undefined)
23
+ ) {
24
+ return undefined;
25
+ }
26
+ return node.body;
27
+ }
28
+
29
+ function functionBody(checker: ts.TypeChecker, expression: ts.Expression): ts.ConciseBody | undefined {
30
+ if (ts.isArrowFunction(expression) || ts.isFunctionExpression(expression)) {
31
+ return immediateBody(expression);
32
+ }
33
+ const symbol = checker.getSymbolAtLocation(expression);
34
+ const target = symbol !== undefined && symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
35
+ for (const declaration of target?.declarations ?? []) {
36
+ if (declaration.getSourceFile().isDeclarationFile || /(?:^|\/)node_modules\//u.test(declaration.getSourceFile().fileName)) {
37
+ continue;
38
+ }
39
+ if (ts.isVariableDeclaration(declaration) && declaration.initializer !== undefined) {
40
+ return functionBody(checker, declaration.initializer);
41
+ }
42
+ const body = immediateBody(declaration);
43
+ if (body !== undefined) {
44
+ return body;
45
+ }
46
+ }
47
+ return undefined;
48
+ }
49
+
50
+ function runnerReference(node: ts.Node): ts.Node | undefined {
51
+ if (ts.isCallExpression(node)) {
52
+ return ts.isIdentifier(node.expression) ? node.expression : undefined;
53
+ }
54
+ if (!ts.isIdentifier(node)) {
55
+ return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) ? node : undefined;
56
+ }
57
+ const parent = node.parent;
58
+ if (ts.isCallExpression(parent) && parent.expression === node) {
59
+ return undefined;
60
+ }
61
+ return "name" in parent && parent.name === node && !ts.isShorthandPropertyAssignment(parent) ? undefined : node;
62
+ }
63
+
64
+ export function boundaryCalls(checker: ts.TypeChecker) {
65
+ const throwing = new Map<ts.Node, boolean>();
66
+ function throws(body: ts.Node): boolean {
67
+ const previous = throwing.get(body);
68
+ if (previous !== undefined) {
69
+ return previous;
70
+ }
71
+ function visit(node: ts.Node): boolean {
72
+ return ts.isThrowStatement(node) || (!ts.isFunctionLike(node) && (ts.forEachChild(node, visit) ?? false));
73
+ }
74
+ const result = visit(body);
75
+ throwing.set(body, result);
76
+ return result;
77
+ }
78
+ function guard(call: ts.CallExpression): boolean {
79
+ const body = functionBody(checker, call.expression);
80
+ return body !== undefined && throws(body);
81
+ }
82
+ function unsafeSync(call: ts.CallExpression): boolean {
83
+ const callback = call.arguments[0];
84
+ const body = callback === undefined ? undefined : functionBody(checker, callback);
85
+ if (body === undefined) {
86
+ return callback !== undefined && knownThrowing(boundaryReference(checker, callback) ?? "");
87
+ }
88
+ function visit(node: ts.Node): boolean {
89
+ if (ts.isFunctionLike(node)) {
90
+ return false;
91
+ }
92
+ if (ts.isThrowStatement(node)) {
93
+ return true;
94
+ }
95
+ if (ts.isCallExpression(node) && (knownThrowing(boundaryReference(checker, node.expression) ?? "") || guard(node))) {
96
+ return true;
97
+ }
98
+ return ts.forEachChild(node, visit) ?? false;
99
+ }
100
+ return visit(body);
101
+ }
102
+ function runner(node: ts.Node, entryPoint: boolean): string | undefined {
103
+ if (entryPoint) {
104
+ return undefined;
105
+ }
106
+ const expression = runnerReference(node);
107
+ if (expression === undefined) {
108
+ return undefined;
109
+ }
110
+ const reference = boundaryReference(checker, expression);
111
+ return /^effect(?:\.Effect|\/Effect)\.run/u.test(reference ?? "") ? `${reference} outside an entry point` : undefined;
112
+ }
113
+ return (node: ts.Node, entryPoint: boolean): string | undefined => {
114
+ const runtime = runner(node, entryPoint);
115
+ if (runtime !== undefined) {
116
+ return runtime;
117
+ }
118
+ if (!ts.isCallExpression(node)) {
119
+ return undefined;
120
+ }
121
+ const reference = boundaryReference(checker, node.expression) ?? "";
122
+ if (synchronousIo(reference)) {
123
+ return `synchronous IO (${reference})`;
124
+ }
125
+ if (/^effect(?:\.Effect|\/Effect)\.sync$/u.test(reference) && unsafeSync(node)) {
126
+ return "Effect.sync with a throwing body";
127
+ }
128
+ return guard(node) ? `throwing helper call (${node.expression.getText()})` : undefined;
129
+ };
130
+ }
@@ -0,0 +1,85 @@
1
+ import ts from "typescript";
2
+
3
+ function importedReference(declaration: ts.Declaration): string | undefined {
4
+ if (ts.isImportSpecifier(declaration)) {
5
+ const module = declaration.parent.parent.parent.moduleSpecifier;
6
+ return ts.isStringLiteral(module) ? `${module.text}.${(declaration.propertyName ?? declaration.name).text}` : undefined;
7
+ }
8
+ if (ts.isNamespaceImport(declaration) || ts.isImportClause(declaration)) {
9
+ const module = ts.isNamespaceImport(declaration) ? declaration.parent.parent.moduleSpecifier : declaration.parent.moduleSpecifier;
10
+ return ts.isStringLiteral(module) ? module.text : undefined;
11
+ }
12
+ return undefined;
13
+ }
14
+
15
+ function memberName(node: ts.PropertyAccessExpression | ts.ElementAccessExpression): string | undefined {
16
+ if (ts.isPropertyAccessExpression(node)) {
17
+ return node.name.text;
18
+ }
19
+ return ts.isStringLiteralLike(node.argumentExpression) ? node.argumentExpression.text : undefined;
20
+ }
21
+
22
+ function bindingReference(checker: ts.TypeChecker, declaration: ts.Declaration, seen: Set<ts.Symbol>): string | undefined {
23
+ if (!(ts.isBindingElement(declaration) && ts.isObjectBindingPattern(declaration.parent))) {
24
+ return undefined;
25
+ }
26
+ const variable = declaration.parent.parent;
27
+ const name = declaration.propertyName ?? declaration.name;
28
+ if (!ts.isVariableDeclaration(variable) || variable.initializer === undefined || !(ts.isIdentifier(name) || ts.isStringLiteral(name))) {
29
+ return undefined;
30
+ }
31
+ const owner = boundaryReference(checker, variable.initializer, seen);
32
+ return owner === undefined ? undefined : `${owner}.${name.text}`;
33
+ }
34
+
35
+ function symbolReference(checker: ts.TypeChecker, node: ts.Identifier, seen: Set<ts.Symbol>): string | undefined {
36
+ const symbol = checker.getSymbolAtLocation(node);
37
+ if (symbol === undefined) {
38
+ return node.text;
39
+ }
40
+ if (seen.has(symbol)) {
41
+ return undefined;
42
+ }
43
+ seen.add(symbol);
44
+ for (const declaration of symbol.declarations ?? []) {
45
+ if (ts.isVariableDeclaration(declaration) && declaration.initializer !== undefined) {
46
+ return boundaryReference(checker, declaration.initializer, seen);
47
+ }
48
+ const imported = importedReference(declaration) ?? bindingReference(checker, declaration, seen);
49
+ if (imported !== undefined) {
50
+ return imported;
51
+ }
52
+ }
53
+ return symbol.declarations?.some((declaration) => !declaration.getSourceFile().isDeclarationFile) ? undefined : node.text;
54
+ }
55
+
56
+ export function boundaryReference(checker: ts.TypeChecker, node: ts.Node, seen = new Set<ts.Symbol>()): string | undefined {
57
+ if (ts.isParenthesizedExpression(node)) {
58
+ return boundaryReference(checker, node.expression, seen);
59
+ }
60
+ if (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) {
61
+ const member = memberName(node);
62
+ const owner = boundaryReference(checker, node.expression, seen);
63
+ return owner === undefined || member === undefined ? undefined : `${owner}.${member}`;
64
+ }
65
+ return ts.isIdentifier(node) ? symbolReference(checker, node, seen) : undefined;
66
+ }
67
+
68
+ export function boundaryEntryPoint(path: string, source: ts.SourceFile, checker: ts.TypeChecker): boolean {
69
+ if (/(?:^|\/)(?:cli|main|[^/]+-cli)\.[cm]?[jt]sx?$|\.(?:config|test|spec)\.[cm]?[jt]sx?$/u.test(path)) {
70
+ return true;
71
+ }
72
+ function visit(node: ts.Node): boolean {
73
+ if (ts.isCallExpression(node)) {
74
+ const reference = boundaryReference(checker, node.expression);
75
+ if (reference?.endsWith(".runMain") || (ts.isIdentifier(node.expression) && node.expression.text === "runMain")) {
76
+ return true;
77
+ }
78
+ }
79
+ if (ts.isBinaryExpression(node) && node.getText(source).replace(/\s/gu, "") === "fileURLToPath(import.meta.url)===process.argv[1]") {
80
+ return true;
81
+ }
82
+ return ts.forEachChild(node, visit) ?? false;
83
+ }
84
+ return visit(source);
85
+ }
@@ -3,7 +3,7 @@ import ts from "typescript";
3
3
  import { projectsFor } from "#imports/projects.ts";
4
4
  import type { RuleInputs } from "#rule.ts";
5
5
 
6
- export function effectProgram(inputs: RuleInputs): ts.Program {
6
+ export function effectProgram(inputs: RuleInputs, externalTypes = true): ts.Program {
7
7
  const projectOf = projectsFor(inputs.root);
8
8
  const options: ts.CompilerOptions = {
9
9
  allowJs: true,
@@ -19,6 +19,9 @@ export function effectProgram(inputs: RuleInputs): ts.Program {
19
19
  host.readFile = (path) => texts.get(path) ?? read(path);
20
20
  host.resolveModuleNames = (names, from) =>
21
21
  names.map((name) => {
22
+ if (!(externalTypes || name.startsWith(".") || name.startsWith("#") || name.startsWith("@shivaedev/"))) {
23
+ return undefined;
24
+ }
22
25
  const project = projectOf(from);
23
26
  return ts.resolveModuleName(name, from, project.typesOptions, host, project.typesCache).resolvedModule;
24
27
  });
@@ -0,0 +1,26 @@
1
+ import ts from "typescript";
2
+
3
+ export function promiseParameter(checker: ts.TypeChecker, node: ts.Node): string | undefined {
4
+ if (!ts.isParameter(node)) {
5
+ return undefined;
6
+ }
7
+ const seen = new Set<ts.Type>();
8
+ function carries(type: ts.Type): boolean {
9
+ if (seen.has(type)) {
10
+ return false;
11
+ }
12
+ seen.add(type);
13
+ if (["Promise", "PromiseLike"].includes(type.getSymbol()?.name ?? "")) {
14
+ return true;
15
+ }
16
+ if (type.isUnionOrIntersection()) {
17
+ return type.types.some(carries);
18
+ }
19
+ const constraint = checker.getBaseConstraintOfType(type);
20
+ return (
21
+ (constraint !== undefined && carries(constraint))
22
+ || type.getCallSignatures().some((signature) => carries(checker.getReturnTypeOfSignature(signature)))
23
+ );
24
+ }
25
+ return carries(checker.getTypeAtLocation(node)) ? `Promise-typed parameter (${node.name.getText()})` : undefined;
26
+ }
@@ -0,0 +1,38 @@
1
+ import ts from "typescript";
2
+ import { importBindings } from "#rules/stories/importBindings.ts";
3
+
4
+ function constantOrigins(source: ts.SourceFile, origins: Map<string, string>): void {
5
+ for (const statement of source.statements) {
6
+ if (!ts.isVariableStatement(statement) || (statement.declarationList.flags & ts.NodeFlags.Const) === 0) {
7
+ continue;
8
+ }
9
+ for (const declaration of statement.declarationList.declarations) {
10
+ if (!ts.isIdentifier(declaration.name) || declaration.initializer === undefined) {
11
+ continue;
12
+ }
13
+ const origin = originOf(declaration.initializer, origins);
14
+ if (origin !== undefined) {
15
+ origins.set(declaration.name.text, origin);
16
+ }
17
+ }
18
+ }
19
+ }
20
+
21
+ export function importOrigins(source: ts.SourceFile): ReadonlyMap<string, string> {
22
+ const origins = new Map([...importBindings(source)].map(([name, binding]) => [name, binding.module]));
23
+ constantOrigins(source, origins);
24
+ return origins;
25
+ }
26
+
27
+ export function originOf(node: ts.Node, origins: ReadonlyMap<string, string>): string | undefined {
28
+ if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node)) {
29
+ return originOf(node.expression, origins);
30
+ }
31
+ if (ts.isIdentifier(node)) {
32
+ return origins.get(node.text);
33
+ }
34
+ if (ts.isPropertyAccessExpression(node)) {
35
+ return originOf(node.expression, origins);
36
+ }
37
+ return undefined;
38
+ }