@shivaedev/quality 0.50.0 → 0.52.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 +28 -0
- package/README.md +20 -5
- package/biome/plugins/ambient-runtime.grit +1 -1
- package/dist/baseline/format.d.ts +1 -0
- package/dist/baseline/format.d.ts.map +1 -1
- package/dist/baseline/format.js +3 -2
- package/dist/baseline/format.js.map +1 -1
- package/dist/baseline/update.js +1 -1
- package/dist/baseline/update.js.map +1 -1
- package/dist/config/resolve.d.ts.map +1 -1
- package/dist/config/resolve.js +9 -6
- package/dist/config/resolve.js.map +1 -1
- package/dist/imports/fences/compilePolicy.d.ts.map +1 -1
- package/dist/imports/fences/compilePolicy.js +6 -2
- package/dist/imports/fences/compilePolicy.js.map +1 -1
- package/dist/inventory/shadcnAlias.js.map +1 -1
- package/dist/rules/builtIn.d.ts +1 -1
- package/dist/rules/builtIn.d.ts.map +1 -1
- package/dist/rules/builtIn.js +2 -0
- package/dist/rules/builtIn.js.map +1 -1
- package/dist/rules/effect/boundary.js +1 -1
- package/dist/rules/effect/boundary.js.map +1 -1
- package/dist/rules/effect/boundaryCalls.js +1 -1
- package/dist/rules/effect/boundaryCalls.js.map +1 -1
- package/dist/rules/effect/boundaryReference.js +1 -1
- package/dist/rules/effect/boundaryReference.js.map +1 -1
- package/dist/rules/effect/failureFindings.d.ts +8 -0
- package/dist/rules/effect/failureFindings.d.ts.map +1 -0
- package/dist/rules/effect/failureFindings.js +47 -0
- package/dist/rules/effect/failureFindings.js.map +1 -0
- package/dist/rules/effect/failureHonesty.d.ts +2 -0
- package/dist/rules/effect/failureHonesty.d.ts.map +1 -0
- package/dist/rules/effect/failureHonesty.js +53 -0
- package/dist/rules/effect/failureHonesty.js.map +1 -0
- package/dist/rules/effect/failureOperations.d.ts +9 -0
- package/dist/rules/effect/failureOperations.d.ts.map +1 -0
- package/dist/rules/effect/failureOperations.js +55 -0
- package/dist/rules/effect/failureOperations.js.map +1 -0
- package/dist/rules/effect/successRecovery.d.ts +3 -0
- package/dist/rules/effect/successRecovery.d.ts.map +1 -0
- package/dist/rules/effect/successRecovery.js +43 -0
- package/dist/rules/effect/successRecovery.js.map +1 -0
- package/dist/rules/noBarrel.js +1 -1
- package/dist/rules/noBarrel.js.map +1 -1
- package/dist/rules/secondNames.d.ts.map +1 -1
- package/dist/rules/secondNames.js +5 -1
- package/dist/rules/secondNames.js.map +1 -1
- package/dist/rules/suppressions/hiddenCast.d.ts +4 -0
- package/dist/rules/suppressions/hiddenCast.d.ts.map +1 -0
- package/dist/rules/suppressions/hiddenCast.js +80 -0
- package/dist/rules/suppressions/hiddenCast.js.map +1 -0
- package/dist/rules/suppressions/noDoubleCast.d.ts.map +1 -1
- package/dist/rules/suppressions/noDoubleCast.js +55 -4
- package/dist/rules/suppressions/noDoubleCast.js.map +1 -1
- package/dist/rules/suppressions/readonlyWrite.d.ts +4 -0
- package/dist/rules/suppressions/readonlyWrite.d.ts.map +1 -0
- package/dist/rules/suppressions/readonlyWrite.js +113 -0
- package/dist/rules/suppressions/readonlyWrite.js.map +1 -0
- package/dist/unitIoGuard.config.js +2 -2
- package/dist/unitIoGuard.config.js.map +1 -1
- package/package.json +2 -2
- package/src/baseline/format.ts +4 -2
- package/src/baseline/update.ts +1 -1
- package/src/config/resolve.ts +4 -6
- package/src/imports/fences/compilePolicy.ts +10 -2
- package/src/inventory/shadcnAlias.ts +1 -1
- package/src/rules/builtIn.ts +2 -0
- package/src/rules/effect/boundary.ts +1 -1
- package/src/rules/effect/boundaryCalls.ts +1 -1
- package/src/rules/effect/boundaryReference.ts +1 -1
- package/src/rules/effect/failureFindings.ts +55 -0
- package/src/rules/effect/failureHonesty.ts +59 -0
- package/src/rules/effect/failureOperations.ts +66 -0
- package/src/rules/effect/successRecovery.ts +44 -0
- package/src/rules/noBarrel.ts +1 -1
- package/src/rules/secondNames.ts +7 -1
- package/src/rules/suppressions/hiddenCast.ts +95 -0
- package/src/rules/suppressions/noDoubleCast.ts +61 -5
- package/src/rules/suppressions/readonlyWrite.ts +140 -0
- package/src/unitIoGuard.config.ts +3 -3
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
function propertyName(node: ts.Expression, checker: ts.TypeChecker): string | undefined {
|
|
4
|
+
if (ts.isStringLiteralLike(node) || ts.isNumericLiteral(node)) {
|
|
5
|
+
return node.text;
|
|
6
|
+
}
|
|
7
|
+
const type = checker.getTypeAtLocation(node);
|
|
8
|
+
return type.isStringLiteral() || type.isNumberLiteral() ? String(type.value) : undefined;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function readonlyProperty(checker: ts.TypeChecker, target: ts.Expression, key: string): boolean {
|
|
12
|
+
const type = checker.getTypeAtLocation(target);
|
|
13
|
+
if (type.isUnionOrIntersection()) {
|
|
14
|
+
return type.types.some((part) => readonlyIn(part, key));
|
|
15
|
+
}
|
|
16
|
+
return readonlyIn(type, key);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function readonlyIn(type: ts.Type, key: string): boolean {
|
|
20
|
+
const symbol = type.getProperty(key);
|
|
21
|
+
const mapped = type.getSymbol()?.getDeclarations()?.find(ts.isMappedTypeNode);
|
|
22
|
+
if (symbol !== undefined && mapped?.readonlyToken !== undefined) {
|
|
23
|
+
return mapped.readonlyToken.kind !== ts.SyntaxKind.MinusToken;
|
|
24
|
+
}
|
|
25
|
+
return (
|
|
26
|
+
symbol
|
|
27
|
+
?.getDeclarations()
|
|
28
|
+
?.some(
|
|
29
|
+
(declaration) =>
|
|
30
|
+
(ts.canHaveModifiers(declaration) && ts.getModifiers(declaration)?.some((modifier) => modifier.kind === ts.SyntaxKind.ReadonlyKeyword))
|
|
31
|
+
|| (ts.isGetAccessorDeclaration(declaration) && !symbol.getDeclarations()?.some(ts.isSetAccessorDeclaration)),
|
|
32
|
+
) === true
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function scopeState(checker: ts.TypeChecker, target: ts.Expression, key: string): boolean {
|
|
37
|
+
if (key !== "state") {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return (
|
|
41
|
+
checker
|
|
42
|
+
.getTypeAtLocation(target)
|
|
43
|
+
.getProperty("state")
|
|
44
|
+
?.getDeclarations()
|
|
45
|
+
?.some((declaration) => /(?:^|\/)effect\/(?:src|dist(?:\/[^/]+)?)\/Scope\.(?:d\.)?ts$/u.test(declaration.getSourceFile().fileName)) === true
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function overwritten(checker: ts.TypeChecker, target: ts.Expression, keys: readonly string[]): string | undefined {
|
|
50
|
+
const key = keys.find((name) => readonlyProperty(checker, target, name) || scopeState(checker, target, name));
|
|
51
|
+
return key === undefined
|
|
52
|
+
? undefined
|
|
53
|
+
: `Writes ${key} on ${checker.typeToString(checker.getTypeAtLocation(target))}. Use the native constructor or copy API and preserve the declared readonly type; use Scope.close for Scope state.`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function assignedKeys(checker: ts.TypeChecker, source: ts.Expression): readonly string[] {
|
|
57
|
+
return checker
|
|
58
|
+
.getTypeAtLocation(source)
|
|
59
|
+
.getProperties()
|
|
60
|
+
.map((property) => property.name);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function writeTarget(node: ts.Node): ts.Expression | undefined {
|
|
64
|
+
if (
|
|
65
|
+
ts.isBinaryExpression(node)
|
|
66
|
+
&& node.operatorToken.kind >= ts.SyntaxKind.FirstAssignment
|
|
67
|
+
&& node.operatorToken.kind <= ts.SyntaxKind.LastAssignment
|
|
68
|
+
) {
|
|
69
|
+
return node.left;
|
|
70
|
+
}
|
|
71
|
+
if (
|
|
72
|
+
(ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node))
|
|
73
|
+
&& (node.operator === ts.SyntaxKind.PlusPlusToken || node.operator === ts.SyntaxKind.MinusMinusToken)
|
|
74
|
+
) {
|
|
75
|
+
return node.operand;
|
|
76
|
+
}
|
|
77
|
+
return ts.isDeleteExpression(node) ? node.expression : undefined;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function writeCandidate(node: ts.Node): boolean {
|
|
81
|
+
const target = writeTarget(node);
|
|
82
|
+
return (
|
|
83
|
+
(target !== undefined
|
|
84
|
+
&& (ts.isPropertyAccessExpression(target) || ts.isElementAccessExpression(target))
|
|
85
|
+
&& !constructorInitialization(node, target.expression))
|
|
86
|
+
|| (ts.isCallExpression(node)
|
|
87
|
+
&& ts.isPropertyAccessExpression(node.expression)
|
|
88
|
+
&& ts.isIdentifier(node.expression.expression)
|
|
89
|
+
&& ((node.expression.expression.text === "Object" && ["assign", "defineProperty"].includes(node.expression.name.text))
|
|
90
|
+
|| (node.expression.expression.text === "Reflect" && node.expression.name.text === "set")))
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function constructorInitialization(node: ts.Node, target: ts.Expression | undefined): boolean {
|
|
95
|
+
if (target?.kind !== ts.SyntaxKind.ThisKeyword) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
for (let parent = node.parent; parent !== undefined; parent = parent.parent) {
|
|
99
|
+
if (ts.isConstructorDeclaration(parent)) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
if (ts.isFunctionLike(parent)) {
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function callWrite(node: ts.CallExpression, checker: ts.TypeChecker): string | undefined {
|
|
110
|
+
const target = node.arguments[0];
|
|
111
|
+
if (target === undefined || constructorInitialization(node, target)) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
const method = ts.isPropertyAccessExpression(node.expression) ? node.expression.name.text : undefined;
|
|
115
|
+
if (method === "assign") {
|
|
116
|
+
return overwritten(
|
|
117
|
+
checker,
|
|
118
|
+
target,
|
|
119
|
+
node.arguments.slice(1).flatMap((source) => assignedKeys(checker, source)),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
const argument = node.arguments[1];
|
|
123
|
+
const key = argument === undefined ? undefined : propertyName(argument, checker);
|
|
124
|
+
return key === undefined ? undefined : overwritten(checker, target, [key]);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function readonlyWrite(node: ts.Node, checker: ts.TypeChecker): string | undefined {
|
|
128
|
+
if (ts.isCallExpression(node)) {
|
|
129
|
+
return callWrite(node, checker);
|
|
130
|
+
}
|
|
131
|
+
const target = writeTarget(node);
|
|
132
|
+
if (target === undefined || !(ts.isPropertyAccessExpression(target) || ts.isElementAccessExpression(target))) {
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
if (constructorInitialization(node, target.expression)) {
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
const key = ts.isPropertyAccessExpression(target) ? target.name.text : propertyName(target.argumentExpression, checker);
|
|
139
|
+
return key === undefined ? undefined : overwritten(checker, target.expression, [key]);
|
|
140
|
+
}
|
|
@@ -7,9 +7,9 @@ const installed = Symbol.for("@shivaedev/quality/unitIoGuard");
|
|
|
7
7
|
let loading = 0;
|
|
8
8
|
|
|
9
9
|
function assertUnitIoAllowed(call: string, boundary: (...args: never[]) => unknown): void {
|
|
10
|
-
const worker:
|
|
11
|
-
const file = worker
|
|
12
|
-
if (loading > 0 || file
|
|
10
|
+
const worker: unknown = Reflect.get(globalThis, "__vitest_worker__");
|
|
11
|
+
const file = typeof worker === "object" && worker !== null && "filepath" in worker ? worker.filepath : undefined;
|
|
12
|
+
if (loading > 0 || typeof file !== "string" || !/\.unit\.(?:test|spec)\.[cm]?[jt]sx?$/u.test(file)) {
|
|
13
13
|
return;
|
|
14
14
|
}
|
|
15
15
|
const error = new Error(`Unit test ${file} cannot call ${call}; use a story through the package's kit for I/O.`);
|