@shivaedev/quality 0.41.1 → 0.43.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 (120) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/README.md +56 -8
  3. package/biome/declarations.json +5 -0
  4. package/biome/plugins/ambient-runtime.grit +3 -1
  5. package/biome/plugins/effect-test-bodies.grit +1 -1
  6. package/biome/preset.json +2 -2
  7. package/dist/appTestConfig.js +1 -1
  8. package/dist/appTestConfig.js.map +1 -1
  9. package/dist/biome/withoutEntryPointImports.d.ts +4 -0
  10. package/dist/biome/withoutEntryPointImports.d.ts.map +1 -0
  11. package/dist/biome/withoutEntryPointImports.js +46 -0
  12. package/dist/biome/withoutEntryPointImports.js.map +1 -0
  13. package/dist/imports/pnpmWorkspacePatterns.d.ts +1 -0
  14. package/dist/imports/pnpmWorkspacePatterns.d.ts.map +1 -1
  15. package/dist/imports/pnpmWorkspacePatterns.js +28 -22
  16. package/dist/imports/pnpmWorkspacePatterns.js.map +1 -1
  17. package/dist/imports/runtime.d.ts +6 -0
  18. package/dist/imports/runtime.d.ts.map +1 -1
  19. package/dist/imports/runtime.js +54 -8
  20. package/dist/imports/runtime.js.map +1 -1
  21. package/dist/manifests/entryPoints.d.ts +15 -0
  22. package/dist/manifests/entryPoints.d.ts.map +1 -0
  23. package/dist/manifests/entryPoints.js +60 -0
  24. package/dist/manifests/entryPoints.js.map +1 -0
  25. package/dist/rules/biome.d.ts.map +1 -1
  26. package/dist/rules/biome.js +4 -2
  27. package/dist/rules/biome.js.map +1 -1
  28. package/dist/rules/builtIn.d.ts +2 -2
  29. package/dist/rules/builtIn.d.ts.map +1 -1
  30. package/dist/rules/builtIn.js +8 -0
  31. package/dist/rules/builtIn.js.map +1 -1
  32. package/dist/rules/effect/boundary.d.ts +2 -0
  33. package/dist/rules/effect/boundary.d.ts.map +1 -0
  34. package/dist/rules/effect/boundary.js +66 -0
  35. package/dist/rules/effect/boundary.js.map +1 -0
  36. package/dist/rules/effect/layerConstructor.d.ts +3 -0
  37. package/dist/rules/effect/layerConstructor.d.ts.map +1 -0
  38. package/dist/rules/effect/layerConstructor.js +35 -0
  39. package/dist/rules/effect/layerConstructor.js.map +1 -0
  40. package/dist/rules/effect/ownsParameters.d.ts +3 -0
  41. package/dist/rules/effect/ownsParameters.d.ts.map +1 -0
  42. package/dist/rules/effect/ownsParameters.js +45 -0
  43. package/dist/rules/effect/ownsParameters.js.map +1 -0
  44. package/dist/rules/effect/program.d.ts +4 -0
  45. package/dist/rules/effect/program.d.ts.map +1 -0
  46. package/dist/rules/effect/program.js +24 -0
  47. package/dist/rules/effect/program.js.map +1 -0
  48. package/dist/rules/effect/publicFunctions.d.ts +3 -0
  49. package/dist/rules/effect/publicFunctions.d.ts.map +1 -0
  50. package/dist/rules/effect/publicFunctions.js +68 -0
  51. package/dist/rules/effect/publicFunctions.js.map +1 -0
  52. package/dist/rules/effect/serviceCatalog.d.ts +8 -0
  53. package/dist/rules/effect/serviceCatalog.d.ts.map +1 -0
  54. package/dist/rules/effect/serviceCatalog.js +56 -0
  55. package/dist/rules/effect/serviceCatalog.js.map +1 -0
  56. package/dist/rules/effect/serviceFindings.d.ts +5 -0
  57. package/dist/rules/effect/serviceFindings.d.ts.map +1 -0
  58. package/dist/rules/effect/serviceFindings.js +88 -0
  59. package/dist/rules/effect/serviceFindings.js.map +1 -0
  60. package/dist/rules/effect/serviceOrigin.d.ts +3 -0
  61. package/dist/rules/effect/serviceOrigin.d.ts.map +1 -0
  62. package/dist/rules/effect/serviceOrigin.js +85 -0
  63. package/dist/rules/effect/serviceOrigin.js.map +1 -0
  64. package/dist/rules/effect/serviceParameters.d.ts +2 -0
  65. package/dist/rules/effect/serviceParameters.d.ts.map +1 -0
  66. package/dist/rules/effect/serviceParameters.js +33 -0
  67. package/dist/rules/effect/serviceParameters.js.map +1 -0
  68. package/dist/rules/effect/serviceTypes.d.ts +8 -0
  69. package/dist/rules/effect/serviceTypes.d.ts.map +1 -0
  70. package/dist/rules/effect/serviceTypes.js +94 -0
  71. package/dist/rules/effect/serviceTypes.js.map +1 -0
  72. package/dist/rules/imports/fences.d.ts +1 -1
  73. package/dist/rules/imports/manifest.d.ts.map +1 -1
  74. package/dist/rules/imports/manifest.js +27 -14
  75. package/dist/rules/imports/manifest.js.map +1 -1
  76. package/dist/rules/imports/runtime.d.ts.map +1 -1
  77. package/dist/rules/imports/runtime.js +10 -4
  78. package/dist/rules/imports/runtime.js.map +1 -1
  79. package/dist/rules/manifestsApplication.d.ts +2 -0
  80. package/dist/rules/manifestsApplication.d.ts.map +1 -0
  81. package/dist/rules/manifestsApplication.js +99 -0
  82. package/dist/rules/manifestsApplication.js.map +1 -0
  83. package/dist/rules/manifestsExportsShape.d.ts.map +1 -1
  84. package/dist/rules/manifestsExportsShape.js +5 -2
  85. package/dist/rules/manifestsExportsShape.js.map +1 -1
  86. package/dist/rules/react/jsxConditionals.d.ts +2 -0
  87. package/dist/rules/react/jsxConditionals.d.ts.map +1 -0
  88. package/dist/rules/react/jsxConditionals.js +54 -0
  89. package/dist/rules/react/jsxConditionals.js.map +1 -0
  90. package/dist/rules/stories/engineOf.d.ts.map +1 -1
  91. package/dist/rules/stories/engineOf.js +3 -2
  92. package/dist/rules/stories/engineOf.js.map +1 -1
  93. package/dist/rules/stories/namedValues.js +4 -4
  94. package/dist/rules/stories/namedValues.js.map +1 -1
  95. package/package.json +3 -3
  96. package/src/appTestConfig.ts +1 -1
  97. package/src/biome/withoutEntryPointImports.ts +52 -0
  98. package/src/imports/pnpmWorkspacePatterns.ts +29 -22
  99. package/src/imports/runtime.ts +69 -9
  100. package/src/manifests/entryPoints.ts +87 -0
  101. package/src/rules/biome.ts +4 -2
  102. package/src/rules/builtIn.ts +8 -0
  103. package/src/rules/effect/boundary.ts +75 -0
  104. package/src/rules/effect/layerConstructor.ts +40 -0
  105. package/src/rules/effect/ownsParameters.ts +46 -0
  106. package/src/rules/effect/program.ts +26 -0
  107. package/src/rules/effect/publicFunctions.ts +70 -0
  108. package/src/rules/effect/serviceCatalog.ts +57 -0
  109. package/src/rules/effect/serviceFindings.ts +96 -0
  110. package/src/rules/effect/serviceOrigin.ts +94 -0
  111. package/src/rules/effect/serviceParameters.ts +35 -0
  112. package/src/rules/effect/serviceTypes.ts +101 -0
  113. package/src/rules/imports/manifest.ts +42 -14
  114. package/src/rules/imports/runtime.ts +10 -4
  115. package/src/rules/manifestsApplication.ts +117 -0
  116. package/src/rules/manifestsExportsShape.ts +6 -2
  117. package/src/rules/react/jsxConditionals.ts +59 -0
  118. package/src/rules/stories/engineOf.ts +3 -2
  119. package/src/rules/stories/namedValues.ts +4 -4
  120. package/tsconfig/package.json +2 -0
@@ -6,27 +6,43 @@ import { packageOf, type WorkspacePackage } from "./workspace.ts";
6
6
 
7
7
  export type Runtime = "client" | "server" | "shared";
8
8
 
9
+ export type DeclaredRuntime = "client" | "server";
10
+
9
11
  function executablePath(value: unknown): boolean {
10
12
  return typeof value === "string" && value.trim().length > 0;
11
13
  }
12
14
 
13
- function hasExecutable(root: string, directory: string): boolean {
15
+ function readManifest(root: string, directory: string): object | undefined {
14
16
  const path = join(root, directory, "package.json");
15
17
  try {
16
18
  const manifest: unknown = JSON.parse(readFileSync(path, "utf8"));
17
19
  if (typeof manifest !== "object" || manifest === null || Array.isArray(manifest)) {
18
20
  throw new Error("the manifest must be an object");
19
21
  }
20
- const bin: unknown = Reflect.get(manifest, "bin");
21
- return executablePath(bin) || (typeof bin === "object" && bin !== null && !Array.isArray(bin) && Object.values(bin).some(executablePath));
22
+ return manifest;
22
23
  } catch (cause) {
23
24
  if (cause instanceof Error && "code" in cause && cause.code === "ENOENT") {
24
- return false;
25
+ return undefined;
25
26
  }
26
27
  throw new Error(`cannot read runtime policy at ${path}`, { cause });
27
28
  }
28
29
  }
29
30
 
31
+ function hasExecutable(manifest: object | undefined): boolean {
32
+ const bin: unknown = manifest === undefined ? undefined : Reflect.get(manifest, "bin");
33
+ return executablePath(bin) || (typeof bin === "object" && bin !== null && !Array.isArray(bin) && Object.values(bin).some(executablePath));
34
+ }
35
+
36
+ function declaredValue(manifest: object | undefined): unknown {
37
+ const quality: unknown = manifest === undefined ? undefined : Reflect.get(manifest, "quality");
38
+ return typeof quality === "object" && quality !== null ? Reflect.get(quality, "runtime") : undefined;
39
+ }
40
+
41
+ function declaredRuntime(manifest: object | undefined): DeclaredRuntime | undefined {
42
+ const value = declaredValue(manifest);
43
+ return value === "client" || value === "server" ? value : undefined;
44
+ }
45
+
30
46
  function directoryExists(path: string): boolean {
31
47
  try {
32
48
  return statSync(path).isDirectory();
@@ -38,19 +54,63 @@ function directoryExists(path: string): boolean {
38
54
  }
39
55
  }
40
56
 
57
+ type Owner = Pick<WorkspacePackage, "directory">;
58
+
59
+ function runtimeFoldersOf(root: string, owner: Owner): readonly string[] {
60
+ return ["client", "server"].filter((folder) => directoryExists(join(root, owner.directory, "src", folder)));
61
+ }
62
+
63
+ export interface RuntimeDeclarationProblem {
64
+ readonly directory: string;
65
+ readonly message: string;
66
+ }
67
+
68
+ export function runtimeDeclarationProblems(inputs: RuleInputs, packages: readonly WorkspacePackage[]): readonly RuntimeDeclarationProblem[] {
69
+ return [...packages, { directory: ".", name: "." }].flatMap((owner) => {
70
+ const value = declaredValue(readManifest(inputs.root, owner.directory));
71
+ if (value === undefined) {
72
+ return [];
73
+ }
74
+ if (value !== "client" && value !== "server") {
75
+ return [
76
+ {
77
+ directory: owner.directory,
78
+ message: `"quality.runtime" is ${JSON.stringify(value)}. Declare "client" or "server" for a package whose code runs in one runtime, or remove the field and use src/client/ and src/server/.`,
79
+ },
80
+ ];
81
+ }
82
+ const folders = runtimeFoldersOf(inputs.root, owner);
83
+ return folders.length === 0
84
+ ? []
85
+ : [
86
+ {
87
+ directory: owner.directory,
88
+ 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.`,
89
+ },
90
+ ];
91
+ });
92
+ }
93
+
41
94
  export function runtimeOf(inputs: RuleInputs, packages: readonly WorkspacePackage[]): (path: string) => Runtime | undefined {
42
95
  const owners = [...packages, { directory: ".", name: "." }];
43
- const adopted = new Set(
44
- owners
45
- .filter((owner) => ["client", "server"].some((folder) => directoryExists(join(inputs.root, owner.directory, "src", folder))))
46
- .map((owner) => owner.directory),
96
+ const manifests = new Map(owners.map((owner) => [owner.directory, readManifest(inputs.root, owner.directory)]));
97
+ const adopted = new Set(owners.filter((owner) => runtimeFoldersOf(inputs.root, owner).length > 0).map((owner) => owner.directory));
98
+ const declared = new Map(
99
+ owners.flatMap((owner) => {
100
+ const runtime = declaredRuntime(manifests.get(owner.directory));
101
+ return runtime === undefined || adopted.has(owner.directory) ? [] : [[owner.directory, runtime] as const];
102
+ }),
47
103
  );
48
- const executable = new Set(owners.filter((owner) => hasExecutable(inputs.root, owner.directory)).map((owner) => owner.directory));
104
+ const executable = new Set(owners.filter((owner) => hasExecutable(manifests.get(owner.directory))).map((owner) => owner.directory));
49
105
  return (path) => {
50
106
  const owner = packageOf(packages, path)?.directory ?? ".";
51
107
  if (isTooling(owner, path)) {
52
108
  return "server";
53
109
  }
110
+ const whole = declared.get(owner);
111
+ if (whole !== undefined) {
112
+ return whole;
113
+ }
54
114
  if (!adopted.has(owner)) {
55
115
  return executable.has(owner) ? "server" : undefined;
56
116
  }
@@ -0,0 +1,87 @@
1
+ import { posix } from "node:path";
2
+ import type { RuleInputs } from "#rule.ts";
3
+
4
+ export const SHIPPED_FIELDS = ["dependencies", "peerDependencies", "optionalDependencies"] as const;
5
+
6
+ export const DEPENDENCY_FIELDS = [...SHIPPED_FIELDS, "devDependencies"] as const;
7
+
8
+ export const PLATFORM = "@shivaedev/platform";
9
+
10
+ const ENTRY_POINTS = [
11
+ { name: PLATFORM, testOnly: false },
12
+ { name: "@shivaedev/test-story", testOnly: true },
13
+ ] as const;
14
+
15
+ export interface EntryPoint {
16
+ readonly declared: "development" | "shipped" | undefined;
17
+ readonly name: string;
18
+ readonly provides: ReadonlySet<string>;
19
+ readonly testOnly: boolean;
20
+ }
21
+
22
+ export function dependencyNames(manifest: unknown, field: string): readonly string[] {
23
+ const value: unknown = typeof manifest === "object" && manifest !== null ? Reflect.get(manifest, field) : undefined;
24
+ return typeof value === "object" && value !== null && !Array.isArray(value) ? Object.keys(value) : [];
25
+ }
26
+
27
+ function parsed(text: string | undefined): unknown {
28
+ if (text === undefined) {
29
+ return undefined;
30
+ }
31
+ try {
32
+ return JSON.parse(text);
33
+ } catch {
34
+ return undefined;
35
+ }
36
+ }
37
+
38
+ async function installedManifest(inputs: Pick<RuleInputs, "readText">, directory: string, name: string, workspace: ReadonlyMap<string, string>) {
39
+ const candidates = [
40
+ ...(workspace.has(name) ? [posix.join(workspace.get(name) ?? ".", "package.json")] : []),
41
+ posix.join(directory, "node_modules", name, "package.json"),
42
+ posix.join("node_modules", name, "package.json"),
43
+ ];
44
+ for (const candidate of candidates) {
45
+ const manifest = parsed(await inputs.readText(candidate));
46
+ if (manifest !== undefined) {
47
+ return manifest;
48
+ }
49
+ }
50
+ return undefined;
51
+ }
52
+
53
+ function declaration(manifest: unknown, name: string): EntryPoint["declared"] {
54
+ if (SHIPPED_FIELDS.some((field) => dependencyNames(manifest, field).includes(name))) {
55
+ return "shipped";
56
+ }
57
+ return dependencyNames(manifest, "devDependencies").includes(name) ? "development" : undefined;
58
+ }
59
+
60
+ export function entryPointsOf(
61
+ inputs: Pick<RuleInputs, "readText">,
62
+ directory: string,
63
+ manifest: unknown,
64
+ workspace: ReadonlyMap<string, string>,
65
+ ): Promise<readonly EntryPoint[]> {
66
+ return Promise.all(
67
+ ENTRY_POINTS.map(async ({ name, testOnly }) => ({
68
+ declared: declaration(manifest, name),
69
+ name,
70
+ provides: new Set(dependencyNames(await installedManifest(inputs, directory, name, workspace), "dependencies")),
71
+ testOnly,
72
+ })),
73
+ );
74
+ }
75
+
76
+ export function isApplication(manifest: unknown): boolean {
77
+ return DEPENDENCY_FIELDS.some((field) => dependencyNames(manifest, field).includes(PLATFORM));
78
+ }
79
+
80
+ export function provider(entryPoints: readonly EntryPoint[], target: string, development: boolean, declaredOnly: boolean) {
81
+ return entryPoints.find(
82
+ (entry) =>
83
+ entry.provides.has(target)
84
+ && (development || !entry.testOnly)
85
+ && (declaredOnly ? entry.declared === "shipped" || (entry.declared === "development" && development) : entry.declared === undefined),
86
+ );
87
+ }
@@ -1,4 +1,5 @@
1
1
  import { biomeReport, type Diagnostic, FAILING } from "#biome/report.ts";
2
+ import { withoutEntryPointImports } from "#biome/withoutEntryPointImports.ts";
2
3
  import { defineRule, type Finding, type Rule, type RuleInputs } from "#rule.ts";
3
4
  import { itemsOf, member, parseJsonc, textOf } from "#rules/suppressions/biome/json.ts";
4
5
 
@@ -43,10 +44,11 @@ async function presetFinding(readText: RuleInputs["readText"]): Promise<readonly
43
44
  const bridge = defineRule({
44
45
  check: async ({ exceptions, readText, root }) => {
45
46
  const [preset, report] = await Promise.all([presetFinding(readText), biomeReport(root, ["check"], exceptions)]);
46
- return [...preset, ...report.diagnostics.filter((diagnostic) => FAILING.has(diagnostic.severity)).map(findingOf)];
47
+ const failing = report.diagnostics.filter((diagnostic) => FAILING.has(diagnostic.severity));
48
+ return [...preset, ...(await withoutEntryPointImports(readText, failing)).map(findingOf)];
47
49
  },
48
50
  description:
49
- "Biome's lint, format and assist findings under the shared preset. `quality fix` applies Biome's lint fixes, unsafe ones included, its assist actions and formatting.",
51
+ "Biome's lint, format and assist findings under the shared preset, except undeclared-dependency findings for a piece that an application's entry point brings. `quality fix` applies Biome's lint fixes, unsafe ones included, its assist actions and formatting.",
50
52
  id: "biome",
51
53
  registrable: false,
52
54
  });
@@ -5,6 +5,8 @@ import { noJsdoc } from "#rules/comments/noJsdoc.ts";
5
5
  import { noLineReference } from "#rules/comments/noLineReference.ts";
6
6
  import { noPrReference } from "#rules/comments/noPrReference.ts";
7
7
  import { noTodo } from "#rules/comments/noTodo.ts";
8
+ import { effectBoundary } from "#rules/effect/boundary.ts";
9
+ import { serviceParameters } from "#rules/effect/serviceParameters.ts";
8
10
  import { distinctCase } from "#rules/files/distinctCase.ts";
9
11
  import { folderNames } from "#rules/files/folderNames.ts";
10
12
  import { namedAfterExport } from "#rules/files/namedAfterExport.ts";
@@ -16,6 +18,7 @@ import { importsManifest } from "#rules/imports/manifest.ts";
16
18
  import { noTestCode } from "#rules/imports/noTestCode.ts";
17
19
  import { importsResolvable } from "#rules/imports/resolvable.ts";
18
20
  import { importsRuntime } from "#rules/imports/runtime.ts";
21
+ import { jsxConditionals } from "#rules/react/jsxConditionals.ts";
19
22
  import { authSql } from "#rules/stories/authSql.ts";
20
23
  import { storyEngine } from "#rules/stories/engine.ts";
21
24
  import { httpStatus } from "#rules/stories/httpStatus.ts";
@@ -34,6 +37,7 @@ import { noInline } from "#rules/suppressions/noInline.ts";
34
37
  import { testsColocated } from "#rules/test-names/colocated.ts";
35
38
  import { testsFollow } from "#rules/test-names/follow.ts";
36
39
  import { biome } from "./biome.ts";
40
+ import { manifestsApplication } from "./manifestsApplication.ts";
37
41
  import { manifestsExportsShape } from "./manifestsExportsShape.ts";
38
42
  import { manifestsSorted } from "./manifestsSorted.ts";
39
43
  import { manifestsSourceFirst } from "./manifestsSourceFirst.ts";
@@ -42,6 +46,9 @@ import { maxLines } from "./maxLines.ts";
42
46
  import { noBarrel } from "./noBarrel.ts";
43
47
 
44
48
  export const builtInRules = [
49
+ effectBoundary,
50
+ serviceParameters,
51
+ jsxConditionals,
45
52
  maxLines,
46
53
  maxDepth,
47
54
  noBarrel,
@@ -68,6 +75,7 @@ export const builtInRules = [
68
75
  manifestsSorted,
69
76
  manifestsSourceFirst,
70
77
  manifestsExportsShape,
78
+ manifestsApplication,
71
79
  namedAfterExport,
72
80
  folderNames,
73
81
  distinctCase,
@@ -0,0 +1,75 @@
1
+ import ts from "typescript";
2
+ import { defineRule, type Finding } from "#rule.ts";
3
+ import { parse } from "#rules/parse.ts";
4
+
5
+ function asyncConstruct(node: ts.Node): string | undefined {
6
+ if (
7
+ !(ts.isFunctionLike(node) && ts.canHaveModifiers(node) && ts.getModifiers(node)?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword))
8
+ ) {
9
+ return undefined;
10
+ }
11
+ if (ts.isArrowFunction(node)) {
12
+ return "async arrow";
13
+ }
14
+ return ts.isMethodDeclaration(node) ? "async method" : "async function";
15
+ }
16
+
17
+ function thenCall(node: ts.Node): boolean {
18
+ if (!ts.isCallExpression(node)) {
19
+ return false;
20
+ }
21
+ const callee = node.expression;
22
+ return (
23
+ (ts.isPropertyAccessExpression(callee) && callee.name.text === "then")
24
+ || (ts.isElementAccessExpression(callee) && ts.isStringLiteral(callee.argumentExpression) && callee.argumentExpression.text === "then")
25
+ );
26
+ }
27
+
28
+ function construct(node: ts.Node): string | undefined {
29
+ if (ts.isVariableDeclarationList(node) && (node.flags & ts.NodeFlags.AwaitUsing) === ts.NodeFlags.AwaitUsing) {
30
+ return "await";
31
+ }
32
+ if (ts.isAwaitExpression(node) || (ts.isForOfStatement(node) && node.awaitModifier !== undefined)) {
33
+ return "await";
34
+ }
35
+ if (ts.isNewExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === "Promise") {
36
+ return "new Promise(...)";
37
+ }
38
+ if (thenCall(node)) {
39
+ return ".then(...)";
40
+ }
41
+ if (ts.isTryStatement(node)) {
42
+ return "try statement";
43
+ }
44
+ return ts.isThrowStatement(node) ? "throw statement" : asyncConstruct(node);
45
+ }
46
+
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);
65
+ }
66
+ if (source !== undefined) {
67
+ visit(source);
68
+ }
69
+ return findings;
70
+ }),
71
+ 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.",
73
+ id: "effect/boundary",
74
+ registrable: false,
75
+ });
@@ -0,0 +1,40 @@
1
+ import ts from "typescript";
2
+ import { returnValues } from "#rules/stories/returnValues.ts";
3
+
4
+ function declaredLayer(checker: ts.TypeChecker, declaration: ts.Signature["declaration"]): boolean {
5
+ const type = declaration?.type;
6
+ if (type === undefined || !ts.isTypeReferenceNode(type)) {
7
+ return false;
8
+ }
9
+ const symbol = checker.getSymbolAtLocation(type.typeName);
10
+ if (symbol === undefined) {
11
+ return false;
12
+ }
13
+ const target = symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
14
+ return checker.getDeclaredTypeOfSymbol(target).getProperty("~effect/Layer") !== undefined;
15
+ }
16
+
17
+ export function layerConstructor(checker: ts.TypeChecker, declaration: NonNullable<ts.Signature["declaration"]>): boolean {
18
+ if (declaredLayer(checker, declaration)) {
19
+ return true;
20
+ }
21
+ if (
22
+ ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Async
23
+ || ("asteriskToken" in declaration && declaration.asteriskToken !== undefined)
24
+ ) {
25
+ return false;
26
+ }
27
+ const returns = returnValues(declaration);
28
+ // Native Layer constructors declare the result independently of their inputs;
29
+ // resolving their instantiated result can expand an entire application graph.
30
+ return (
31
+ returns.length > 0
32
+ && returns.every((value) => {
33
+ if (value === undefined || !ts.isCallExpression(value)) {
34
+ return false;
35
+ }
36
+ const signatures = checker.getTypeAtLocation(value.expression).getCallSignatures();
37
+ return signatures.length > 0 && signatures.every((signature) => declaredLayer(checker, signature.declaration));
38
+ })
39
+ );
40
+ }
@@ -0,0 +1,46 @@
1
+ import ts from "typescript";
2
+
3
+ function declaredName(node: ts.Identifier): boolean {
4
+ const parent = node.parent;
5
+ if (ts.isBindingElement(parent)) {
6
+ return parent.name === node || parent.propertyName === node;
7
+ }
8
+ return "name" in parent && parent.name === node && !ts.isPropertyAccessExpression(parent) && !ts.isShorthandPropertyAssignment(parent);
9
+ }
10
+
11
+ export function ownsParameters(checker: ts.TypeChecker, local: ReadonlySet<ts.SourceFile>) {
12
+ const known = new Map<ts.Node, boolean>();
13
+ return (root: ts.Node): boolean => {
14
+ const previous = known.get(root);
15
+ if (previous !== undefined) {
16
+ return previous;
17
+ }
18
+ const seen = new Set<ts.Node>();
19
+ function reference(node: ts.Identifier): boolean {
20
+ const symbol = ts.isShorthandPropertyAssignment(node.parent)
21
+ ? checker.getShorthandAssignmentValueSymbol(node.parent)
22
+ : checker.getSymbolAtLocation(node);
23
+ if (symbol === undefined) {
24
+ return false;
25
+ }
26
+ const target = symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
27
+ return (target.declarations ?? []).some((declaration) => local.has(declaration.getSourceFile()) && visit(declaration));
28
+ }
29
+ function visit(node: ts.Node): boolean {
30
+ if (seen.has(node)) {
31
+ return false;
32
+ }
33
+ seen.add(node);
34
+ if (ts.isParameter(node)) {
35
+ return local.has(node.getSourceFile());
36
+ }
37
+ if (ts.isIdentifier(node) && !declaredName(node) && reference(node)) {
38
+ return true;
39
+ }
40
+ return ts.forEachChild(node, visit) ?? false;
41
+ }
42
+ const result = visit(root);
43
+ known.set(root, result);
44
+ return result;
45
+ };
46
+ }
@@ -0,0 +1,26 @@
1
+ import { join } from "node:path";
2
+ import ts from "typescript";
3
+ import { projectsFor } from "#imports/projects.ts";
4
+ import type { RuleInputs } from "#rule.ts";
5
+
6
+ export function effectProgram(inputs: RuleInputs): ts.Program {
7
+ const projectOf = projectsFor(inputs.root);
8
+ const options: ts.CompilerOptions = {
9
+ allowJs: true,
10
+ jsx: ts.JsxEmit.Preserve,
11
+ skipLibCheck: true,
12
+ strict: true,
13
+ target: ts.ScriptTarget.ESNext,
14
+ types: [],
15
+ };
16
+ const host = ts.createCompilerHost(options);
17
+ const texts = new Map(inputs.sources.map((source) => [join(inputs.root, source.path), source.text]));
18
+ const read = host.readFile;
19
+ host.readFile = (path) => texts.get(path) ?? read(path);
20
+ host.resolveModuleNames = (names, from) =>
21
+ names.map((name) => {
22
+ const project = projectOf(from);
23
+ return ts.resolveModuleName(name, from, project.typesOptions, host, project.typesCache).resolvedModule;
24
+ });
25
+ return ts.createProgram([...texts.keys()], options, host);
26
+ }
@@ -0,0 +1,70 @@
1
+ import ts from "typescript";
2
+ import { ownsParameters } from "#rules/effect/ownsParameters.ts";
3
+
4
+ export function publicFunctions(sources: readonly ts.SourceFile[], checker: ts.TypeChecker, surface: (type: ts.Type) => void): void {
5
+ const local = new Set(sources);
6
+ const owned = ownsParameters(checker, local);
7
+ const visited = new Set<ts.Symbol>();
8
+ function wrapped(value: ts.CallExpression): void {
9
+ if (checker.getTypeAtLocation(value).getCallSignatures().length === 0) {
10
+ return;
11
+ }
12
+ for (const argument of value.arguments) {
13
+ surface(checker.getTypeAtLocation(argument));
14
+ }
15
+ }
16
+
17
+ function variable(declaration: ts.VariableDeclaration, symbol: ts.Symbol): void {
18
+ surface(checker.getTypeOfSymbol(symbol));
19
+ if (declaration.initializer !== undefined && ts.isCallExpression(declaration.initializer)) {
20
+ wrapped(declaration.initializer);
21
+ }
22
+ }
23
+
24
+ function declarationSurface(declaration: ts.Declaration, target: ts.Symbol): void {
25
+ if (!(local.has(declaration.getSourceFile()) && owned(declaration))) {
26
+ return;
27
+ }
28
+ if (ts.isBindingElement(declaration)) {
29
+ surface(checker.getTypeOfSymbol(target));
30
+ }
31
+ if (ts.isExportAssignment(declaration)) {
32
+ surface(checker.getTypeAtLocation(declaration.expression));
33
+ }
34
+ if (ts.isInterfaceDeclaration(declaration)) {
35
+ surface(checker.getDeclaredTypeOfSymbol(target));
36
+ }
37
+ if (ts.isFunctionDeclaration(declaration) || ts.isClassDeclaration(declaration)) {
38
+ surface(checker.getTypeOfSymbol(target));
39
+ }
40
+ if (ts.isVariableDeclaration(declaration)) {
41
+ variable(declaration, target);
42
+ }
43
+ }
44
+ function exported(symbol: ts.Symbol | undefined): void {
45
+ if (symbol === undefined) {
46
+ return;
47
+ }
48
+ const target = symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
49
+ if (visited.has(target)) {
50
+ return;
51
+ }
52
+ visited.add(target);
53
+ for (const declaration of target.declarations ?? []) {
54
+ declarationSurface(declaration, target);
55
+ }
56
+ }
57
+ function publicClasses(node: ts.Node): void {
58
+ if (ts.isClassDeclaration(node) && node.name !== undefined && owned(node)) {
59
+ surface(checker.getTypeAtLocation(node.name));
60
+ }
61
+ ts.forEachChild(node, publicClasses);
62
+ }
63
+ for (const source of sources) {
64
+ publicClasses(source);
65
+ const module = checker.getSymbolAtLocation(source);
66
+ for (const symbol of module === undefined ? [] : checker.getExportsOfModule(module)) {
67
+ exported(symbol);
68
+ }
69
+ }
70
+ }
@@ -0,0 +1,57 @@
1
+ import ts from "typescript";
2
+ import { serviceOrigin } from "#rules/effect/serviceOrigin.ts";
3
+ import { serviceTypes } from "#rules/effect/serviceTypes.ts";
4
+
5
+ export function serviceCatalog(program: ts.Program, checker: ts.TypeChecker) {
6
+ const services = serviceTypes(checker);
7
+ function imported(node: ts.ImportSpecifier | ts.NamespaceImport): void {
8
+ const symbol = checker.getSymbolAtLocation(node.name);
9
+ if (symbol === undefined) {
10
+ return;
11
+ }
12
+ const target = symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
13
+ const candidates = target.flags & ts.SymbolFlags.Module ? checker.getExportsOfModule(target) : [target];
14
+ for (const candidate of candidates) {
15
+ const resolved = candidate.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(candidate) : candidate;
16
+ if (serviceOrigin(checker, resolved)) {
17
+ services.remember(checker.getTypeOfSymbol(resolved));
18
+ }
19
+ }
20
+ }
21
+ function referenced(node: ts.TypeReferenceNode): void {
22
+ if (!serviceOrigin(checker, node.typeName)) {
23
+ return;
24
+ }
25
+ const symbol = checker.getSymbolAtLocation(node.typeName);
26
+ if (symbol === undefined) {
27
+ return;
28
+ }
29
+ const target = symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
30
+ services.remember(checker.getTypeOfSymbol(target));
31
+ }
32
+
33
+ function discover(node: ts.Node): void {
34
+ if (ts.isCallExpression(node) && serviceOrigin(checker, node.expression)) {
35
+ services.remember(checker.getTypeAtLocation(node));
36
+ }
37
+ if (ts.isImportSpecifier(node) || ts.isNamespaceImport(node)) {
38
+ imported(node);
39
+ }
40
+ if (ts.isClassDeclaration(node) && node.name !== undefined && serviceOrigin(checker, node.name)) {
41
+ services.remember(checker.getTypeAtLocation(node.name));
42
+ }
43
+ if (ts.isVariableDeclaration(node) && serviceOrigin(checker, node.name)) {
44
+ services.remember(checker.getTypeAtLocation(node.name));
45
+ }
46
+ if (ts.isTypeReferenceNode(node)) {
47
+ referenced(node);
48
+ }
49
+ ts.forEachChild(node, discover);
50
+ }
51
+ for (const source of program.getSourceFiles()) {
52
+ if (!source.fileName.includes("/node_modules/") || source.fileName.includes("/@shivaedev/")) {
53
+ discover(source);
54
+ }
55
+ }
56
+ return services;
57
+ }