@shivaedev/quality 0.42.0 → 0.44.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 +51 -0
- package/README.md +98 -38
- package/biome/declarations.json +6 -1
- package/biome/plugins/ambient-runtime.grit +3 -1
- package/biome/preset.json +62 -62
- package/dist/adapters/knip/configuration.d.ts +29 -0
- package/dist/adapters/knip/configuration.d.ts.map +1 -0
- package/dist/adapters/knip/configuration.js +56 -0
- package/dist/adapters/knip/configuration.js.map +1 -0
- package/dist/adapters/knip/entryPointFindings.d.ts +3 -0
- package/dist/adapters/knip/entryPointFindings.d.ts.map +1 -0
- package/dist/adapters/knip/entryPointFindings.js +49 -0
- package/dist/adapters/knip/entryPointFindings.js.map +1 -0
- package/dist/adapters/knip/findings.d.ts +4 -0
- package/dist/adapters/knip/findings.d.ts.map +1 -0
- package/dist/adapters/knip/findings.js +40 -0
- package/dist/adapters/knip/findings.js.map +1 -0
- package/dist/adapters/knip/run.d.ts +3 -0
- package/dist/adapters/knip/run.d.ts.map +1 -0
- package/dist/adapters/knip/run.js +46 -0
- package/dist/adapters/knip/run.js.map +1 -0
- package/dist/adapters/knip/sourceConfig.d.ts +4 -0
- package/dist/adapters/knip/sourceConfig.d.ts.map +1 -0
- package/dist/adapters/knip/sourceConfig.js +15 -0
- package/dist/adapters/knip/sourceConfig.js.map +1 -0
- package/dist/biome/recognizeDiagnostics.js +2 -2
- package/dist/biome/recognizeDiagnostics.js.map +1 -1
- package/dist/docs/documentedImports.d.ts +12 -0
- package/dist/docs/documentedImports.d.ts.map +1 -0
- package/dist/docs/documentedImports.js +80 -0
- package/dist/docs/documentedImports.js.map +1 -0
- package/dist/docs/documentedModule.d.ts +11 -0
- package/dist/docs/documentedModule.d.ts.map +1 -0
- package/dist/docs/documentedModule.js +32 -0
- package/dist/docs/documentedModule.js.map +1 -0
- package/dist/docs/exportsName.d.ts +2 -0
- package/dist/docs/exportsName.d.ts.map +1 -0
- package/dist/docs/exportsName.js +25 -0
- package/dist/docs/exportsName.js.map +1 -0
- package/dist/docs/headingAnchors.d.ts +4 -0
- package/dist/docs/headingAnchors.d.ts.map +1 -0
- package/dist/docs/headingAnchors.js +43 -0
- package/dist/docs/headingAnchors.js.map +1 -0
- package/dist/docs/importFindings.d.ts +12 -0
- package/dist/docs/importFindings.d.ts.map +1 -0
- package/dist/docs/importFindings.js +36 -0
- package/dist/docs/importFindings.js.map +1 -0
- package/dist/docs/isDocument.d.ts +2 -0
- package/dist/docs/isDocument.d.ts.map +1 -0
- package/dist/docs/isDocument.js +6 -0
- package/dist/docs/isDocument.js.map +1 -0
- package/dist/docs/linkFindings.d.ts +12 -0
- package/dist/docs/linkFindings.d.ts.map +1 -0
- package/dist/docs/linkFindings.js +59 -0
- package/dist/docs/linkFindings.js.map +1 -0
- package/dist/docs/markdownLines.d.ts +11 -0
- package/dist/docs/markdownLines.d.ts.map +1 -0
- package/dist/docs/markdownLines.js +38 -0
- package/dist/docs/markdownLines.js.map +1 -0
- package/dist/docs/markdownLinks.d.ts +7 -0
- package/dist/docs/markdownLinks.d.ts.map +1 -0
- package/dist/docs/markdownLinks.js +23 -0
- package/dist/docs/markdownLinks.js.map +1 -0
- package/dist/docs/repositoryLink.d.ts +11 -0
- package/dist/docs/repositoryLink.d.ts.map +1 -0
- package/dist/docs/repositoryLink.js +28 -0
- package/dist/docs/repositoryLink.js.map +1 -0
- package/dist/imports/fences/compilePolicy.d.ts.map +1 -1
- package/dist/imports/fences/compilePolicy.js +1 -1
- package/dist/imports/fences/compilePolicy.js.map +1 -1
- package/dist/imports/fences/match.d.ts +1 -1
- package/dist/imports/fences/match.d.ts.map +1 -1
- package/dist/imports/fences/match.js +1 -1
- package/dist/imports/fences/match.js.map +1 -1
- package/dist/imports/packageExports.d.ts +1 -0
- package/dist/imports/packageExports.d.ts.map +1 -1
- package/dist/imports/packageExports.js +7 -5
- package/dist/imports/packageExports.js.map +1 -1
- package/dist/imports/packageName.d.ts +4 -0
- package/dist/imports/packageName.d.ts.map +1 -0
- package/dist/imports/packageName.js +25 -0
- package/dist/imports/packageName.js.map +1 -0
- package/dist/imports/resolve.d.ts +0 -2
- package/dist/imports/resolve.d.ts.map +1 -1
- package/dist/imports/resolve.js +1 -24
- package/dist/imports/resolve.js.map +1 -1
- package/dist/inventory/collect.d.ts +0 -1
- package/dist/inventory/collect.d.ts.map +1 -1
- package/dist/inventory/collect.js +1 -4
- package/dist/inventory/collect.js.map +1 -1
- package/dist/inventory/linesOf.d.ts +2 -0
- package/dist/inventory/linesOf.d.ts.map +1 -0
- package/dist/inventory/linesOf.js +5 -0
- package/dist/inventory/linesOf.js.map +1 -0
- package/dist/rules/builtIn.d.ts +2 -2
- package/dist/rules/builtIn.d.ts.map +1 -1
- package/dist/rules/builtIn.js +12 -0
- package/dist/rules/builtIn.js.map +1 -1
- package/dist/rules/docs/references.d.ts +2 -0
- package/dist/rules/docs/references.d.ts.map +1 -0
- package/dist/rules/docs/references.js +66 -0
- package/dist/rules/docs/references.js.map +1 -0
- package/dist/rules/docs/unversionedInstalls.d.ts +2 -0
- package/dist/rules/docs/unversionedInstalls.d.ts.map +1 -0
- package/dist/rules/docs/unversionedInstalls.js +36 -0
- package/dist/rules/docs/unversionedInstalls.js.map +1 -0
- 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/files/folderNames.js +2 -2
- package/dist/rules/files/folderNames.js.map +1 -1
- package/dist/rules/imports/manifest.js +1 -1
- package/dist/rules/imports/manifest.js.map +1 -1
- package/dist/rules/imports/runtime.js +1 -1
- package/dist/rules/imports/runtime.js.map +1 -1
- 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/dist/rules/stories/unitTests.js +1 -1
- package/dist/rules/stories/unitTests.js.map +1 -1
- package/dist/rules/unusedCode.d.ts +2 -0
- package/dist/rules/unusedCode.d.ts.map +1 -0
- package/dist/rules/unusedCode.js +9 -0
- package/dist/rules/unusedCode.js.map +1 -0
- package/docs/debt-and-hooks.md +0 -1
- package/docs/north-star.md +1 -1
- package/package.json +5 -4
- package/src/adapters/knip/configuration.ts +66 -0
- package/src/adapters/knip/entryPointFindings.ts +54 -0
- package/src/adapters/knip/findings.ts +46 -0
- package/src/adapters/knip/run.ts +60 -0
- package/src/adapters/knip/sourceConfig.ts +20 -0
- package/src/biome/recognizeDiagnostics.ts +2 -2
- package/src/docs/documentedImports.ts +104 -0
- package/src/docs/documentedModule.ts +44 -0
- package/src/docs/exportsName.ts +27 -0
- package/src/docs/headingAnchors.ts +52 -0
- package/src/docs/importFindings.ts +62 -0
- package/src/docs/isDocument.ts +6 -0
- package/src/docs/linkFindings.ts +90 -0
- package/src/docs/markdownLines.ts +56 -0
- package/src/docs/markdownLinks.ts +33 -0
- package/src/docs/repositoryLink.ts +44 -0
- package/src/imports/fences/compilePolicy.ts +2 -1
- package/src/imports/fences/match.ts +2 -1
- package/src/imports/packageExports.ts +8 -5
- package/src/imports/packageName.ts +31 -0
- package/src/imports/resolve.ts +1 -31
- package/src/inventory/collect.ts +1 -5
- package/src/inventory/linesOf.ts +4 -0
- package/src/rules/builtIn.ts +12 -0
- package/src/rules/docs/references.ts +83 -0
- package/src/rules/docs/unversionedInstalls.ts +53 -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/files/folderNames.ts +2 -2
- package/src/rules/imports/manifest.ts +1 -1
- package/src/rules/imports/runtime.ts +1 -1
- package/src/rules/react/jsxConditionals.ts +59 -0
- package/src/rules/stories/engineOf.ts +2 -1
- package/src/rules/stories/namedValues.ts +4 -4
- package/src/rules/stories/unitTests.ts +1 -1
- package/src/rules/unusedCode.ts +10 -0
|
@@ -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
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { posix } from "node:path";
|
|
2
|
-
import { Effect, Schema } from "effect";
|
|
2
|
+
import { Effect, Order, Schema } from "effect";
|
|
3
3
|
import ignore, { type Ignore } from "ignore";
|
|
4
4
|
import { nativeNames } from "#naming/nativeNames.ts";
|
|
5
5
|
import { packageLayout } from "#naming/packageLayout.ts";
|
|
@@ -22,7 +22,7 @@ function foldersIn(files: readonly string[]): readonly string[] {
|
|
|
22
22
|
folders.add(folder);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
return [...folders].sort();
|
|
25
|
+
return [...folders].sort(Order.String);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
function stemsIn(files: readonly string[]): ReadonlySet<string> {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { posix } from "node:path";
|
|
2
2
|
import { type ImportGraph, importGraph } from "#imports/graph.ts";
|
|
3
3
|
import { isTooling } from "#imports/isTooling.ts";
|
|
4
|
-
import { builtinName } from "#imports/
|
|
4
|
+
import { builtinName } from "#imports/packageName.ts";
|
|
5
5
|
import { packageOf, type WorkspacePackage } from "#imports/workspace.ts";
|
|
6
6
|
import { type EntryPoint, entryPointsOf, isApplication, provider, SHIPPED_FIELDS } from "#manifests/entryPoints.ts";
|
|
7
7
|
import { isTestCode } from "#naming/testName.ts";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { importGraph } from "#imports/graph.ts";
|
|
2
|
-
import { builtinName } from "#imports/
|
|
2
|
+
import { builtinName } from "#imports/packageName.ts";
|
|
3
3
|
import { type Runtime, runtimeDeclarationProblems, runtimeOf } from "#imports/runtime.ts";
|
|
4
4
|
import { isTestCode } from "#naming/testName.ts";
|
|
5
5
|
import { defineRule } from "#rule.ts";
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { defineRule, type Finding } from "#rule.ts";
|
|
3
|
+
import { parse } from "#rules/parse.ts";
|
|
4
|
+
|
|
5
|
+
const REPAIR =
|
|
6
|
+
"Decide before the JSX with an if and early return, or a variable computed above the return; or extract a component that receives the data and returns null when it has nothing to show.";
|
|
7
|
+
|
|
8
|
+
function mapCall(node: ts.Node): boolean {
|
|
9
|
+
return ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === "map";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function childContext(node: ts.Node): { readonly mapped: boolean } | undefined {
|
|
13
|
+
let mapped = false;
|
|
14
|
+
let child = false;
|
|
15
|
+
for (let parent = node.parent; parent !== undefined; parent = parent.parent) {
|
|
16
|
+
if (!child && ts.isJsxSpreadAttribute(parent)) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
if (ts.isJsxExpression(parent)) {
|
|
20
|
+
if (!(child || ts.isJsxElement(parent.parent) || ts.isJsxFragment(parent.parent))) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
child = true;
|
|
24
|
+
}
|
|
25
|
+
if (ts.isVariableDeclaration(parent) || (ts.isStatement(parent) && !ts.isReturnStatement(parent) && !ts.isBlock(parent))) {
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
mapped ||= mapCall(parent);
|
|
29
|
+
}
|
|
30
|
+
return child ? { mapped } : undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const jsxConditionals = defineRule({
|
|
34
|
+
check: ({ sources }) =>
|
|
35
|
+
sources.flatMap((file) => {
|
|
36
|
+
const source = parse(file);
|
|
37
|
+
const findings: Finding[] = [];
|
|
38
|
+
function visit(node: ts.Node): void {
|
|
39
|
+
if (ts.isConditionalExpression(node) || (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken)) {
|
|
40
|
+
const context = childContext(node);
|
|
41
|
+
if (context !== undefined && source !== undefined) {
|
|
42
|
+
findings.push({
|
|
43
|
+
file: file.path,
|
|
44
|
+
line: source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1,
|
|
45
|
+
message: `Conditional rendering inside a JSX child. ${REPAIR}${context.mapped ? " Inside .map, extract the item into its own component that makes its own decision." : ""}`,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
ts.forEachChild(node, visit);
|
|
50
|
+
}
|
|
51
|
+
if (source !== undefined) {
|
|
52
|
+
visit(source);
|
|
53
|
+
}
|
|
54
|
+
return findings;
|
|
55
|
+
}),
|
|
56
|
+
description: `Components decide what to render outside their JSX. ${REPAIR} Inside .map, extract an item component that makes its own decision. Attribute values are accepted.`,
|
|
57
|
+
id: "react/jsx-conditionals",
|
|
58
|
+
registrable: false,
|
|
59
|
+
});
|
|
@@ -58,7 +58,8 @@ function names(prefix: string, specifier: string): boolean {
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
export function engineOf(endpoint: Endpoint): string | undefined {
|
|
61
|
-
|
|
61
|
+
// HttpStatus only maps names to numbers; importing it acquires no network services.
|
|
62
|
+
if (endpoint.kind === "file" || endpoint.specifier === "effect/http/HttpStatus") {
|
|
62
63
|
return undefined;
|
|
63
64
|
}
|
|
64
65
|
return ENGINES.find(([, prefixes]) => prefixes.some((prefix) => names(prefix, endpoint.specifier) || names(prefix, endpoint.package)))?.[0];
|