@shivaedev/quality 0.51.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.
Files changed (51) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +8 -5
  3. package/biome/plugins/ambient-runtime.grit +1 -1
  4. package/dist/baseline/format.d.ts +1 -0
  5. package/dist/baseline/format.d.ts.map +1 -1
  6. package/dist/baseline/format.js +3 -2
  7. package/dist/baseline/format.js.map +1 -1
  8. package/dist/baseline/update.js +1 -1
  9. package/dist/baseline/update.js.map +1 -1
  10. package/dist/imports/fences/compilePolicy.d.ts.map +1 -1
  11. package/dist/imports/fences/compilePolicy.js +6 -2
  12. package/dist/imports/fences/compilePolicy.js.map +1 -1
  13. package/dist/inventory/shadcnAlias.js.map +1 -1
  14. package/dist/rules/effect/boundary.js +1 -1
  15. package/dist/rules/effect/boundary.js.map +1 -1
  16. package/dist/rules/effect/boundaryCalls.js +1 -1
  17. package/dist/rules/effect/boundaryCalls.js.map +1 -1
  18. package/dist/rules/effect/boundaryReference.js +1 -1
  19. package/dist/rules/effect/boundaryReference.js.map +1 -1
  20. package/dist/rules/noBarrel.js +1 -1
  21. package/dist/rules/noBarrel.js.map +1 -1
  22. package/dist/rules/secondNames.d.ts.map +1 -1
  23. package/dist/rules/secondNames.js +5 -1
  24. package/dist/rules/secondNames.js.map +1 -1
  25. package/dist/rules/suppressions/hiddenCast.d.ts +4 -0
  26. package/dist/rules/suppressions/hiddenCast.d.ts.map +1 -0
  27. package/dist/rules/suppressions/hiddenCast.js +80 -0
  28. package/dist/rules/suppressions/hiddenCast.js.map +1 -0
  29. package/dist/rules/suppressions/noDoubleCast.d.ts.map +1 -1
  30. package/dist/rules/suppressions/noDoubleCast.js +55 -4
  31. package/dist/rules/suppressions/noDoubleCast.js.map +1 -1
  32. package/dist/rules/suppressions/readonlyWrite.d.ts +4 -0
  33. package/dist/rules/suppressions/readonlyWrite.d.ts.map +1 -0
  34. package/dist/rules/suppressions/readonlyWrite.js +113 -0
  35. package/dist/rules/suppressions/readonlyWrite.js.map +1 -0
  36. package/dist/unitIoGuard.config.js +2 -2
  37. package/dist/unitIoGuard.config.js.map +1 -1
  38. package/package.json +2 -2
  39. package/src/baseline/format.ts +4 -2
  40. package/src/baseline/update.ts +1 -1
  41. package/src/imports/fences/compilePolicy.ts +10 -2
  42. package/src/inventory/shadcnAlias.ts +1 -1
  43. package/src/rules/effect/boundary.ts +1 -1
  44. package/src/rules/effect/boundaryCalls.ts +1 -1
  45. package/src/rules/effect/boundaryReference.ts +1 -1
  46. package/src/rules/noBarrel.ts +1 -1
  47. package/src/rules/secondNames.ts +7 -1
  48. package/src/rules/suppressions/hiddenCast.ts +95 -0
  49. package/src/rules/suppressions/noDoubleCast.ts +61 -5
  50. package/src/rules/suppressions/readonlyWrite.ts +140 -0
  51. 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: { readonly filepath?: string } | undefined = Reflect.get(globalThis, "__vitest_worker__");
11
- const file = worker?.filepath;
12
- if (loading > 0 || file === undefined || !/\.unit\.(?:test|spec)\.[cm]?[jt]sx?$/u.test(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.`);