@shivaedev/quality 0.42.0 → 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.
- package/CHANGELOG.md +33 -0
- package/README.md +45 -1
- package/biome/declarations.json +5 -0
- package/biome/plugins/ambient-runtime.grit +3 -1
- package/biome/preset.json +2 -2
- package/dist/rules/builtIn.d.ts +1 -1
- package/dist/rules/builtIn.d.ts.map +1 -1
- package/dist/rules/builtIn.js +6 -0
- package/dist/rules/builtIn.js.map +1 -1
- package/dist/rules/effect/boundary.d.ts +2 -0
- package/dist/rules/effect/boundary.d.ts.map +1 -0
- package/dist/rules/effect/boundary.js +66 -0
- package/dist/rules/effect/boundary.js.map +1 -0
- package/dist/rules/effect/layerConstructor.d.ts +3 -0
- package/dist/rules/effect/layerConstructor.d.ts.map +1 -0
- package/dist/rules/effect/layerConstructor.js +35 -0
- package/dist/rules/effect/layerConstructor.js.map +1 -0
- package/dist/rules/effect/ownsParameters.d.ts +3 -0
- package/dist/rules/effect/ownsParameters.d.ts.map +1 -0
- package/dist/rules/effect/ownsParameters.js +45 -0
- package/dist/rules/effect/ownsParameters.js.map +1 -0
- package/dist/rules/effect/program.d.ts +4 -0
- package/dist/rules/effect/program.d.ts.map +1 -0
- package/dist/rules/effect/program.js +24 -0
- package/dist/rules/effect/program.js.map +1 -0
- package/dist/rules/effect/publicFunctions.d.ts +3 -0
- package/dist/rules/effect/publicFunctions.d.ts.map +1 -0
- package/dist/rules/effect/publicFunctions.js +68 -0
- package/dist/rules/effect/publicFunctions.js.map +1 -0
- package/dist/rules/effect/serviceCatalog.d.ts +8 -0
- package/dist/rules/effect/serviceCatalog.d.ts.map +1 -0
- package/dist/rules/effect/serviceCatalog.js +56 -0
- package/dist/rules/effect/serviceCatalog.js.map +1 -0
- package/dist/rules/effect/serviceFindings.d.ts +5 -0
- package/dist/rules/effect/serviceFindings.d.ts.map +1 -0
- package/dist/rules/effect/serviceFindings.js +88 -0
- package/dist/rules/effect/serviceFindings.js.map +1 -0
- package/dist/rules/effect/serviceOrigin.d.ts +3 -0
- package/dist/rules/effect/serviceOrigin.d.ts.map +1 -0
- package/dist/rules/effect/serviceOrigin.js +85 -0
- package/dist/rules/effect/serviceOrigin.js.map +1 -0
- package/dist/rules/effect/serviceParameters.d.ts +2 -0
- package/dist/rules/effect/serviceParameters.d.ts.map +1 -0
- package/dist/rules/effect/serviceParameters.js +33 -0
- package/dist/rules/effect/serviceParameters.js.map +1 -0
- package/dist/rules/effect/serviceTypes.d.ts +8 -0
- package/dist/rules/effect/serviceTypes.d.ts.map +1 -0
- package/dist/rules/effect/serviceTypes.js +94 -0
- package/dist/rules/effect/serviceTypes.js.map +1 -0
- package/dist/rules/react/jsxConditionals.d.ts +2 -0
- package/dist/rules/react/jsxConditionals.d.ts.map +1 -0
- package/dist/rules/react/jsxConditionals.js +54 -0
- package/dist/rules/react/jsxConditionals.js.map +1 -0
- package/dist/rules/stories/engineOf.d.ts.map +1 -1
- package/dist/rules/stories/engineOf.js +2 -1
- package/dist/rules/stories/engineOf.js.map +1 -1
- package/dist/rules/stories/namedValues.js +4 -4
- package/dist/rules/stories/namedValues.js.map +1 -1
- package/package.json +3 -3
- package/src/rules/builtIn.ts +6 -0
- package/src/rules/effect/boundary.ts +75 -0
- package/src/rules/effect/layerConstructor.ts +40 -0
- package/src/rules/effect/ownsParameters.ts +46 -0
- package/src/rules/effect/program.ts +26 -0
- package/src/rules/effect/publicFunctions.ts +70 -0
- package/src/rules/effect/serviceCatalog.ts +57 -0
- package/src/rules/effect/serviceFindings.ts +96 -0
- package/src/rules/effect/serviceOrigin.ts +94 -0
- package/src/rules/effect/serviceParameters.ts +35 -0
- package/src/rules/effect/serviceTypes.ts +101 -0
- package/src/rules/react/jsxConditionals.ts +59 -0
- package/src/rules/stories/engineOf.ts +2 -1
- package/src/rules/stories/namedValues.ts +4 -4
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { relative } from "node:path";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import type { Finding } from "#rule.ts";
|
|
4
|
+
import { layerConstructor } from "#rules/effect/layerConstructor.ts";
|
|
5
|
+
import { publicFunctions } from "#rules/effect/publicFunctions.ts";
|
|
6
|
+
import type { serviceTypes } from "#rules/effect/serviceTypes.ts";
|
|
7
|
+
|
|
8
|
+
function publicDeclaration(declaration: ts.Declaration, local: ReadonlySet<ts.SourceFile>): boolean {
|
|
9
|
+
if (!local.has(declaration.getSourceFile()) || ts.getCombinedModifierFlags(declaration) & (ts.ModifierFlags.Private | ts.ModifierFlags.Protected)) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return !((ts.isMethodDeclaration(declaration) || ts.isPropertyDeclaration(declaration)) && ts.isPrivateIdentifier(declaration.name));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function serviceFindings(
|
|
16
|
+
root: string,
|
|
17
|
+
sources: readonly ts.SourceFile[],
|
|
18
|
+
checker: ts.TypeChecker,
|
|
19
|
+
services: ReturnType<typeof serviceTypes>,
|
|
20
|
+
): readonly Finding[] {
|
|
21
|
+
const findings = new Map<ts.ParameterDeclaration, Finding>();
|
|
22
|
+
const visited = new Set<ts.Type>();
|
|
23
|
+
const activeSignatures = new Set<NonNullable<ts.Signature["declaration"]>>();
|
|
24
|
+
const local = new Set(sources);
|
|
25
|
+
function parameterFinding(symbol: ts.Symbol): void {
|
|
26
|
+
if (!services.carries(checker.getTypeOfSymbol(symbol))) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
for (const parameter of symbol.declarations ?? []) {
|
|
30
|
+
if (!(ts.isParameter(parameter) && local.has(parameter.getSourceFile()))) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const source = parameter.getSourceFile();
|
|
34
|
+
const file = relative(root, source.fileName).replaceAll("\\", "/");
|
|
35
|
+
findings.set(parameter, {
|
|
36
|
+
file,
|
|
37
|
+
line: source.getLineAndCharacterOfPosition(parameter.getStart(source)).line + 1,
|
|
38
|
+
message: `Parameter ${parameter.name.getText(source)} carries a service. Read services from Effect's environment instead of passing them; return an Effect or Stream with requirements. Service implementation methods and Layer constructors are accepted; only test-support/ folders are exempt, not adapters/.`,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function signatureFinding(signature: ts.Signature): void {
|
|
43
|
+
const declaration = signature.declaration;
|
|
44
|
+
if (declaration === undefined || !local.has(declaration.getSourceFile()) || layerConstructor(checker, declaration)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const result = signature.getReturnType();
|
|
48
|
+
if (result.getProperty("~effect/Layer") !== undefined) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
for (const parameter of signature.parameters) {
|
|
52
|
+
parameterFinding(parameter);
|
|
53
|
+
}
|
|
54
|
+
if (activeSignatures.has(declaration)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
activeSignatures.add(declaration);
|
|
58
|
+
surface(result);
|
|
59
|
+
activeSignatures.delete(declaration);
|
|
60
|
+
}
|
|
61
|
+
function members(type: ts.Type): void {
|
|
62
|
+
if (type.isUnionOrIntersection()) {
|
|
63
|
+
for (const part of type.types) {
|
|
64
|
+
surface(part);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
for (const index of checker.getIndexInfosOfType(type)) {
|
|
68
|
+
surface(index.type);
|
|
69
|
+
}
|
|
70
|
+
for (const signature of type.getConstructSignatures()) {
|
|
71
|
+
surface(signature.getReturnType());
|
|
72
|
+
}
|
|
73
|
+
for (const property of type
|
|
74
|
+
.getProperties()
|
|
75
|
+
.filter((candidate) => (candidate.declarations ?? []).some((declaration) => publicDeclaration(declaration, local)))) {
|
|
76
|
+
surface(checker.getTypeOfSymbol(property));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function surface(type: ts.Type): void {
|
|
80
|
+
if (visited.has(type) || !(type.flags & (ts.TypeFlags.Object | ts.TypeFlags.Intersection | ts.TypeFlags.Union))) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
visited.add(type);
|
|
84
|
+
const signatures = type.getCallSignatures();
|
|
85
|
+
const constructsLayer = signatures.some((signature) => signature.declaration !== undefined && layerConstructor(checker, signature.declaration));
|
|
86
|
+
if (services.computation(type) || (!constructsLayer && services.implementation(type))) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
members(type);
|
|
90
|
+
for (const signature of signatures) {
|
|
91
|
+
signatureFinding(signature);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
publicFunctions(sources, checker, surface);
|
|
95
|
+
return [...findings.values()];
|
|
96
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { returnValues } from "#rules/stories/returnValues.ts";
|
|
3
|
+
|
|
4
|
+
const SERVICE_NAMES = new Set(["Service", "ServiceClass", "Key", "Reference", "Tag", "TagClass", "GenericTag"]);
|
|
5
|
+
|
|
6
|
+
function annotation(declaration: ts.Declaration): ts.TypeNode | undefined {
|
|
7
|
+
if (
|
|
8
|
+
ts.isVariableDeclaration(declaration)
|
|
9
|
+
|| ts.isParameter(declaration)
|
|
10
|
+
|| ts.isTypeAliasDeclaration(declaration)
|
|
11
|
+
|| ts.isFunctionLike(declaration)
|
|
12
|
+
) {
|
|
13
|
+
return declaration.type;
|
|
14
|
+
}
|
|
15
|
+
return ts.isTypeParameterDeclaration(declaration) ? declaration.constraint : undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function declarationOrigin(checker: ts.TypeChecker, declaration: ts.Declaration, seen: Set<ts.Symbol>): boolean {
|
|
19
|
+
const type = annotation(declaration);
|
|
20
|
+
if (type !== undefined && serviceOrigin(checker, type, seen)) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
if (ts.isVariableDeclaration(declaration) && declaration.initializer !== undefined) {
|
|
24
|
+
return serviceOrigin(checker, declaration.initializer, seen);
|
|
25
|
+
}
|
|
26
|
+
if (ts.isClassDeclaration(declaration) || ts.isInterfaceDeclaration(declaration)) {
|
|
27
|
+
return declaration.heritageClauses?.some((clause) => clause.types.some((base) => serviceOrigin(checker, base.expression, seen))) ?? false;
|
|
28
|
+
}
|
|
29
|
+
return ts.isFunctionLike(declaration) && returnValues(declaration).some((value) => value !== undefined && serviceOrigin(checker, value, seen));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function symbolOrigin(checker: ts.TypeChecker, symbol: ts.Symbol, seen: Set<ts.Symbol>): boolean {
|
|
33
|
+
const target = symbol.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(symbol) : symbol;
|
|
34
|
+
if (seen.has(target)) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
seen.add(target);
|
|
38
|
+
return (target.declarations ?? []).some((declaration) => {
|
|
39
|
+
const file = declaration.getSourceFile().fileName.replaceAll("\\", "/");
|
|
40
|
+
if (
|
|
41
|
+
/\/effect\/(?:src|dist)\/(?:Context|Effect)(?:\.d)?\.ts$/u.test(file)
|
|
42
|
+
&& SERVICE_NAMES.has(target.name)
|
|
43
|
+
&& (ts.isVariableDeclaration(declaration)
|
|
44
|
+
|| ts.isFunctionDeclaration(declaration)
|
|
45
|
+
|| ts.isClassDeclaration(declaration)
|
|
46
|
+
|| ts.isInterfaceDeclaration(declaration)
|
|
47
|
+
|| ts.isTypeAliasDeclaration(declaration))
|
|
48
|
+
) {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
if (file.endsWith("/service/src/defineService.ts") && target.name === "defineService") {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
return declarationOrigin(checker, declaration, seen);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function reference(node: ts.Node): ts.Node | undefined {
|
|
59
|
+
if (ts.isCallExpression(node)) {
|
|
60
|
+
return node.expression;
|
|
61
|
+
}
|
|
62
|
+
if (ts.isTypeReferenceNode(node)) {
|
|
63
|
+
return node.typeName;
|
|
64
|
+
}
|
|
65
|
+
if (ts.isTypeQueryNode(node)) {
|
|
66
|
+
return node.exprName;
|
|
67
|
+
}
|
|
68
|
+
return ts.isParenthesizedTypeNode(node) ? node.type : undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function serviceOrigin(checker: ts.TypeChecker, node: ts.Node | ts.Symbol, seen = new Set<ts.Symbol>()): boolean {
|
|
72
|
+
if (!("kind" in node)) {
|
|
73
|
+
return symbolOrigin(checker, node, seen);
|
|
74
|
+
}
|
|
75
|
+
const referred = reference(node);
|
|
76
|
+
if (referred !== undefined) {
|
|
77
|
+
return serviceOrigin(checker, referred, seen);
|
|
78
|
+
}
|
|
79
|
+
if (ts.isUnionTypeNode(node) || ts.isIntersectionTypeNode(node)) {
|
|
80
|
+
return node.types.some((part) => serviceOrigin(checker, part, seen));
|
|
81
|
+
}
|
|
82
|
+
if (ts.isFunctionExpression(node) || ts.isArrowFunction(node)) {
|
|
83
|
+
return returnValues(node).some((value) => value !== undefined && serviceOrigin(checker, value, seen));
|
|
84
|
+
}
|
|
85
|
+
if (ts.isPropertyAccessExpression(node) && !SERVICE_NAMES.has(node.name.text)) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
const location = ts.isQualifiedName(node) ? node.right : node;
|
|
89
|
+
if (!(ts.isIdentifier(location) || ts.isPropertyAccessExpression(location))) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
const symbol = checker.getSymbolAtLocation(ts.isPropertyAccessExpression(location) ? location.name : location);
|
|
93
|
+
return symbol !== undefined && symbolOrigin(checker, symbol, seen);
|
|
94
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { defineRule, type Finding, type RuleInputs } from "#rule.ts";
|
|
3
|
+
import { effectProgram } from "#rules/effect/program.ts";
|
|
4
|
+
import { serviceCatalog } from "#rules/effect/serviceCatalog.ts";
|
|
5
|
+
import { serviceFindings } from "#rules/effect/serviceFindings.ts";
|
|
6
|
+
|
|
7
|
+
export const serviceParameters = defineRule({
|
|
8
|
+
check: (inputs) => {
|
|
9
|
+
const groups = new Map<string, RuleInputs["sources"][number][]>();
|
|
10
|
+
for (const source of inputs.sources) {
|
|
11
|
+
if (/(?:^|\/)test-support\//u.test(source.path)) {
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
const group = /^(?:packages|examples)\/[^/]+/u.exec(source.path)?.[0] ?? ".";
|
|
15
|
+
const files = groups.get(group) ?? [];
|
|
16
|
+
files.push(source);
|
|
17
|
+
groups.set(group, files);
|
|
18
|
+
}
|
|
19
|
+
return [...groups.values()].flatMap((sources) => checkServices({ ...inputs, sources }));
|
|
20
|
+
},
|
|
21
|
+
description:
|
|
22
|
+
"Exported functions and public methods take services from Effect's environment, including aliases, constraints, inferred/destructured parameters and factory returns. Service keys/classes, non-empty Contexts and implementation shapes are services; Effect/Stream requirements are computations. Service implementation methods and Layer constructors are accepted. Only test-support/ folders are exempt. The fixed adapters/ convention bridges outside libraries to Effect values; adapters still follow this rule and ambient runtime bans. No config exceptions or inline suppressions; baseline existing debt.",
|
|
23
|
+
id: "effect/service-parameters",
|
|
24
|
+
registrable: false,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
function checkServices(inputs: RuleInputs): readonly Finding[] {
|
|
28
|
+
const program = effectProgram(inputs);
|
|
29
|
+
const checker = program.getTypeChecker();
|
|
30
|
+
const sources = inputs.sources.flatMap((file) => {
|
|
31
|
+
const source = program.getSourceFile(join(inputs.root, file.path));
|
|
32
|
+
return source === undefined ? [] : [source];
|
|
33
|
+
});
|
|
34
|
+
return serviceFindings(inputs.root, sources, checker, serviceCatalog(program, checker));
|
|
35
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
const SERVICE = "~effect/Context/Service";
|
|
4
|
+
const CONTEXT = "~effect/Context";
|
|
5
|
+
|
|
6
|
+
export function serviceTypes(checker: ts.TypeChecker) {
|
|
7
|
+
const shapes = new Set<ts.Type>();
|
|
8
|
+
const carried = new Map<ts.Type, boolean>();
|
|
9
|
+
function propertyType(type: ts.Type, name: string): ts.Type | undefined {
|
|
10
|
+
const property = type.getProperty(name);
|
|
11
|
+
return property === undefined ? undefined : checker.getTypeOfSymbol(property);
|
|
12
|
+
}
|
|
13
|
+
function remember(type: ts.Type): void {
|
|
14
|
+
if (type.getProperty(SERVICE) === undefined) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const shape = propertyType(type, "Service");
|
|
18
|
+
if (
|
|
19
|
+
shape !== undefined
|
|
20
|
+
&& shape.flags & ts.TypeFlags.Object
|
|
21
|
+
&& (shape.getProperties().length > 0 || shape.getCallSignatures().length > 0)
|
|
22
|
+
&& checker.getIndexInfosOfType(shape).length === 0
|
|
23
|
+
&& (shape.getCallSignatures().length === 0 || type.getProperty("~effect/Context/Reference") === undefined)
|
|
24
|
+
) {
|
|
25
|
+
shapes.add(shape);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function computation(type: ts.Type): boolean {
|
|
29
|
+
return ["~effect/Effect", "~effect/Stream"].some((brand) => type.getProperty(brand) !== undefined);
|
|
30
|
+
}
|
|
31
|
+
function implementation(type: ts.Type): boolean {
|
|
32
|
+
if (shapes.has(type)) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
const declaration = type.getSymbol()?.valueDeclaration;
|
|
36
|
+
const contextual = declaration !== undefined && ts.isObjectLiteralExpression(declaration) ? checker.getContextualType(declaration) : undefined;
|
|
37
|
+
return contextual !== undefined && shapes.has(contextual);
|
|
38
|
+
}
|
|
39
|
+
function nonEmptyContext(context: ts.Type): boolean {
|
|
40
|
+
const variance = propertyType(context, "_Services");
|
|
41
|
+
const parameter = variance?.getCallSignatures()[0]?.parameters[0];
|
|
42
|
+
if (parameter === undefined) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
const required = checker.getTypeOfSymbol(parameter);
|
|
46
|
+
return !((checker.getBaseConstraintOfType(required) ?? required).flags & ts.TypeFlags.Never);
|
|
47
|
+
}
|
|
48
|
+
function carries(type: ts.Type, seen = new Set<ts.Type>()): boolean {
|
|
49
|
+
if (
|
|
50
|
+
seen.has(type)
|
|
51
|
+
|| type.flags
|
|
52
|
+
& (ts.TypeFlags.Any
|
|
53
|
+
| ts.TypeFlags.Unknown
|
|
54
|
+
| ts.TypeFlags.Never
|
|
55
|
+
| ts.TypeFlags.StringLike
|
|
56
|
+
| ts.TypeFlags.NumberLike
|
|
57
|
+
| ts.TypeFlags.BooleanLike
|
|
58
|
+
| ts.TypeFlags.ESSymbolLike
|
|
59
|
+
| ts.TypeFlags.BigIntLike)
|
|
60
|
+
) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
seen.add(type);
|
|
64
|
+
if (type.isUnionOrIntersection()) {
|
|
65
|
+
return type.types.some((part) => carries(part, seen));
|
|
66
|
+
}
|
|
67
|
+
if (type.getProperty(SERVICE) !== undefined) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
const context = propertyType(type, CONTEXT);
|
|
71
|
+
if (context !== undefined) {
|
|
72
|
+
return nonEmptyContext(context);
|
|
73
|
+
}
|
|
74
|
+
if (computation(type)) {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
const constraint = checker.getBaseConstraintOfType(type);
|
|
78
|
+
if (constraint !== undefined && carries(constraint, seen)) {
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
return (
|
|
82
|
+
implementation(type)
|
|
83
|
+
|| checker.getIndexInfosOfType(type).some((index) => carries(index.type, seen))
|
|
84
|
+
|| type.getProperties().some((property) => !(property.flags & ts.SymbolFlags.Method) && carries(checker.getTypeOfSymbol(property), seen))
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
carries: (type: ts.Type) => {
|
|
89
|
+
const known = carried.get(type);
|
|
90
|
+
if (known !== undefined) {
|
|
91
|
+
return known;
|
|
92
|
+
}
|
|
93
|
+
const result = carries(type);
|
|
94
|
+
carried.set(type, result);
|
|
95
|
+
return result;
|
|
96
|
+
},
|
|
97
|
+
computation,
|
|
98
|
+
implementation,
|
|
99
|
+
remember,
|
|
100
|
+
};
|
|
101
|
+
}
|