@shivaedev/quality 0.39.2 → 0.40.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.40.0 - 2026-10-25
4
+
5
+ ### Changed
6
+
7
+ - `tests/named-values` now reports a spec-local numeric const that only feeds `toBe` or `toHaveLength`. Consumers should assert the observed items or derive the value from the story's inputs.
8
+
3
9
  ## 0.39.2 - 2026-10-25
4
10
 
5
11
  ### Changed
package/README.md CHANGED
@@ -154,7 +154,7 @@ Every package tests its real engine through its own story kit, so setup and acti
154
154
 
155
155
  Quality's `tests/story-setup` rule reports top-level test functions named with the words `seed`, `make`, `build` or `setup`, and fixture-writing calls imported from Node's `fs` or `fs/promises`. It leaves source and `test-support/` files alone. This is a check for those syntax patterns; a passing test file still needs review of what it tests.
156
156
 
157
- Specs keep expectations visible. `tests/spec-helpers` moves all module-level helper functions out of specs and into the kit; `tests/no-expect-in-support` moves `expect` calls into the spec, except in test-support files that register tests at module top level and are not imported by any module, so nested Vitest runs can execute them as test files with real assertions. `tests/named-values` rejects numeric literals passed to `toBe` or `toHaveLength` in specs, except zero and a status or exit code asserted on a `status`, `code` or `exitCode` subject: assert the observed items or give the value a domain name. `tests/no-limit-imports` rejects importing `MAX_*` constants from a `limits` module into specs, so a story proves behavior instead of copying an implementation cap.
157
+ Specs keep expectations visible. `tests/spec-helpers` moves all module-level helper functions out of specs and into the kit; `tests/no-expect-in-support` moves `expect` calls into the spec, except in test-support files that register tests at module top level and are not imported by any module, so nested Vitest runs can execute them as test files with real assertions. `tests/named-values` rejects numeric literals passed to `toBe` or `toHaveLength` in specs, except zero and a status or exit code asserted on a `status`, `code` or `exitCode` subject: assert the observed items or use a domain name that comes from the story's inputs or the product. A spec-local numeric const that only feeds these matchers is reported, including aliases for zero and codes. `tests/no-limit-imports` rejects importing `MAX_*` constants from a `limits` module into specs, so a story proves behavior instead of copying an implementation cap.
158
158
 
159
159
  `boundaries/queue-sql` rejects SQL mentioning the private `pgboss.` schema. Apps use their queue definitions and let workers process jobs; there is no queue-purge API by design. `boundaries/auth-sql` rejects SQL accessing Better Auth's `user`, `account`, `session` and `verification` tables. Apps use platform's auth API; `options.owners` names platform's own auth implementation and its migrations. These rules inspect SQL templates, literal query calls and query variables, and inventoried `.sql` files. They do not evaluate arbitrary runtime SQL construction.
160
160
 
@@ -1 +1 @@
1
- {"version":3,"file":"namedValues.d.ts","sourceRoot":"","sources":["../../../src/rules/stories/namedValues.ts"],"names":[],"mappings":"AA0CA,eAAO,MAAM,WAAW,0DAKtB,CAAC"}
1
+ {"version":3,"file":"namedValues.d.ts","sourceRoot":"","sources":["../../../src/rules/stories/namedValues.ts"],"names":[],"mappings":"AAwFA,eAAO,MAAM,WAAW,0DAgBtB,CAAC"}
@@ -1,7 +1,46 @@
1
1
  import ts from "typescript";
2
2
  import { defineRule } from "#rule.ts";
3
3
  import { isSpec, memberName, nodesWhere, numericValue, syntaxFindings, unwrapped } from "./syntax.js";
4
- const MESSAGE = "Passes a numeric literal to toBe or toHaveLength. Assert the observed items, or give the expected value a domain name before asserting it.";
4
+ const MESSAGE = "Passes a numeric literal or a matcher-only spec-local numeric const to toBe or toHaveLength. Assert the observed items, write the literal where accepted (zero, or a status or exit code), or derive the value from the story's inputs.";
5
+ function matcherCall(node) {
6
+ return ts.isCallExpression(node) && ["toBe", "toHaveLength"].includes(memberName(node.expression) ?? "");
7
+ }
8
+ function matcherAliases(source) {
9
+ const declarations = nodesWhere(source, (node) => ts.isVariableDeclaration(node)
10
+ && ts.isIdentifier(node.name)
11
+ && ts.isVariableDeclarationList(node.parent)
12
+ && (node.parent.flags & ts.NodeFlags.Const) !== 0
13
+ && node.initializer !== undefined
14
+ && numericValue(node.initializer) !== undefined).filter(ts.isVariableDeclaration);
15
+ if (declarations.length === 0) {
16
+ return new Set();
17
+ }
18
+ const options = { allowJs: true, noLib: true, noResolve: true, types: [] };
19
+ const host = ts.createCompilerHost(options);
20
+ host.getSourceFile = (path) => (path === source.fileName ? source : undefined);
21
+ const checker = ts.createProgram([source.fileName], options, host).getTypeChecker();
22
+ const aliases = new Map();
23
+ for (const declaration of declarations) {
24
+ const symbol = checker.getSymbolAtLocation(declaration.name);
25
+ if (symbol !== undefined) {
26
+ aliases.set(symbol, []);
27
+ }
28
+ }
29
+ for (const node of nodesWhere(source, ts.isIdentifier)) {
30
+ if (!ts.isIdentifier(node) || (ts.isVariableDeclaration(node.parent) && node.parent.name === node)) {
31
+ continue;
32
+ }
33
+ const symbol = ts.isShorthandPropertyAssignment(node.parent)
34
+ ? checker.getShorthandAssignmentValueSymbol(node.parent)
35
+ : checker.getSymbolAtLocation(node);
36
+ if (symbol !== undefined) {
37
+ aliases.get(symbol)?.push(node);
38
+ }
39
+ }
40
+ return new Set([...aliases.values()]
41
+ .filter((references) => references.every((reference) => matcherCall(reference.parent) && reference.parent.arguments.includes(reference)))
42
+ .flatMap((references) => references.map((reference) => reference.parent)));
43
+ }
5
44
  function codeSubject(expression) {
6
45
  const value = unwrapped(expression);
7
46
  if (!(ts.isPropertyAccessExpression(value) || ts.isElementAccessExpression(value))) {
@@ -25,8 +64,7 @@ function codeSubject(expression) {
25
64
  return ["status", "code", "exitCode"].includes(ts.isIdentifier(subject) ? subject.text : (memberName(subject) ?? ""));
26
65
  }
27
66
  function numericAssertion(node) {
28
- return (ts.isCallExpression(node)
29
- && ["toBe", "toHaveLength"].includes(memberName(node.expression) ?? "")
67
+ return (matcherCall(node)
30
68
  && !codeSubject(node.expression)
31
69
  && node.arguments.some((argument) => {
32
70
  const value = numericValue(argument);
@@ -34,7 +72,12 @@ function numericAssertion(node) {
34
72
  }));
35
73
  }
36
74
  export const namedValues = defineRule({
37
- check: ({ sources }) => sources.filter((file) => isSpec(file.path)).flatMap((file) => syntaxFindings(file, (source) => nodesWhere(source, numericAssertion), MESSAGE)),
75
+ check: ({ sources }) => sources
76
+ .filter((file) => isSpec(file.path))
77
+ .flatMap((file) => syntaxFindings(file, (source) => {
78
+ const aliases = matcherAliases(source);
79
+ return nodesWhere(source, (node) => numericAssertion(node) || aliases.has(node));
80
+ }, MESSAGE)),
38
81
  description: "Stories assert observed items or named values instead of bare counts and numbers.",
39
82
  id: "tests/named-values",
40
83
  });
@@ -1 +1 @@
1
- {"version":3,"file":"namedValues.js","sourceRoot":"","sources":["../../../src/rules/stories/namedValues.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEtG,MAAM,OAAO,GACZ,4IAA4I,CAAC;AAE9I,SAAS,WAAW,CAAC,UAAyB;IAC7C,MAAM,KAAK,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC;IACpC,IAAI,CAAC,CAAC,EAAE,CAAC,0BAA0B,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,yBAAyB,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;QACpF,OAAO,KAAK,CAAC;IACd,CAAC;IACD,IAAI,IAAI,GAAG,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IACvC,OAAO,CAAC,EAAE,CAAC,0BAA0B,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,yBAAyB,CAAC,IAAI,CAAC,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,KAAK,KAAK,EAAE,CAAC;QAClH,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACnC,CAAC;IACD,IAAI,CAAC,EAAE,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;QAChC,OAAO,KAAK,CAAC;IACd,CAAC;IACD,MAAM,MAAM,GAAG,SAAS,CAAC,IAAI,CAAC,UAAU,C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AC,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;AACvH,CAAC;AAED,SAAS,gBAAgB,CAAC,IAAa;IACtC,OAAO,CACN,WAAW,CAAC,IAAI,CAAC;WACd,CAAC,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC;WAC7B,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;YACnC,MAAM,KAAK,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;YACrC,OAAO,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,CAAC,CAAC;QAC3C,CAAC,CAAC,CACF,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,WAAW,GAAG,UAAU,CAAC;IACrC,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CACtB,OAAO;SACL,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACnC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CACjB,cAAc,CACb,IAAI,EACJ,CAAC,MAAM,EAAE,EAAE;QACV,MAAM,OAAO,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;QACvC,OAAO,UAAU,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IAClF,CAAC,EACD,OAAO,CACP,CACD;IACH,WAAW,EAAE,mFAAmF;IAChG,EAAE,EAAE,oBAAoB;CACxB,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shivaedev/quality",
3
- "version": "0.39.2",
3
+ "version": "0.40.0",
4
4
  "description": "Repository quality gate with typed rules, a baseline of existing debt and a registry of reasoned exceptions",
5
5
  "homepage": "https://github.com/ShivaeDev/platform/tree/main/packages/quality#readme",
6
6
  "bugs": {
@@ -3,7 +3,54 @@ import { defineRule } from "#rule.ts";
3
3
  import { isSpec, memberName, nodesWhere, numericValue, syntaxFindings, unwrapped } from "./syntax.ts";
4
4
 
5
5
  const MESSAGE =
6
- "Passes a numeric literal to toBe or toHaveLength. Assert the observed items, or give the expected value a domain name before asserting it.";
6
+ "Passes a numeric literal or a matcher-only spec-local numeric const to toBe or toHaveLength. Assert the observed items, write the literal where accepted (zero, or a status or exit code), or derive the value from the story's inputs.";
7
+
8
+ function matcherCall(node: ts.Node): node is ts.CallExpression {
9
+ return ts.isCallExpression(node) && ["toBe", "toHaveLength"].includes(memberName(node.expression) ?? "");
10
+ }
11
+
12
+ function matcherAliases(source: ts.SourceFile): ReadonlySet<ts.Node> {
13
+ const declarations = nodesWhere(
14
+ source,
15
+ (node) =>
16
+ ts.isVariableDeclaration(node)
17
+ && ts.isIdentifier(node.name)
18
+ && ts.isVariableDeclarationList(node.parent)
19
+ && (node.parent.flags & ts.NodeFlags.Const) !== 0
20
+ && node.initializer !== undefined
21
+ && numericValue(node.initializer) !== undefined,
22
+ ).filter(ts.isVariableDeclaration);
23
+ if (declarations.length === 0) {
24
+ return new Set();
25
+ }
26
+ const options: ts.CompilerOptions = { allowJs: true, noLib: true, noResolve: true, types: [] };
27
+ const host = ts.createCompilerHost(options);
28
+ host.getSourceFile = (path) => (path === source.fileName ? source : undefined);
29
+ const checker = ts.createProgram([source.fileName], options, host).getTypeChecker();
30
+ const aliases = new Map<ts.Symbol, ts.Identifier[]>();
31
+ for (const declaration of declarations) {
32
+ const symbol = checker.getSymbolAtLocation(declaration.name);
33
+ if (symbol !== undefined) {
34
+ aliases.set(symbol, []);
35
+ }
36
+ }
37
+ for (const node of nodesWhere(source, ts.isIdentifier)) {
38
+ if (!ts.isIdentifier(node) || (ts.isVariableDeclaration(node.parent) && node.parent.name === node)) {
39
+ continue;
40
+ }
41
+ const symbol = ts.isShorthandPropertyAssignment(node.parent)
42
+ ? checker.getShorthandAssignmentValueSymbol(node.parent)
43
+ : checker.getSymbolAtLocation(node);
44
+ if (symbol !== undefined) {
45
+ aliases.get(symbol)?.push(node);
46
+ }
47
+ }
48
+ return new Set(
49
+ [...aliases.values()]
50
+ .filter((references) => references.every((reference) => matcherCall(reference.parent) && reference.parent.arguments.includes(reference)))
51
+ .flatMap((references) => references.map((reference) => reference.parent)),
52
+ );
53
+ }
7
54
 
8
55
  function codeSubject(expression: ts.Expression): boolean {
9
56
  const value = unwrapped(expression);
@@ -30,8 +77,7 @@ function codeSubject(expression: ts.Expression): boolean {
30
77
 
31
78
  function numericAssertion(node: ts.Node): boolean {
32
79
  return (
33
- ts.isCallExpression(node)
34
- && ["toBe", "toHaveLength"].includes(memberName(node.expression) ?? "")
80
+ matcherCall(node)
35
81
  && !codeSubject(node.expression)
36
82
  && node.arguments.some((argument) => {
37
83
  const value = numericValue(argument);
@@ -42,7 +88,18 @@ function numericAssertion(node: ts.Node): boolean {
42
88
 
43
89
  export const namedValues = defineRule({
44
90
  check: ({ sources }) =>
45
- sources.filter((file) => isSpec(file.path)).flatMap((file) => syntaxFindings(file, (source) => nodesWhere(source, numericAssertion), MESSAGE)),
91
+ sources
92
+ .filter((file) => isSpec(file.path))
93
+ .flatMap((file) =>
94
+ syntaxFindings(
95
+ file,
96
+ (source) => {
97
+ const aliases = matcherAliases(source);
98
+ return nodesWhere(source, (node) => numericAssertion(node) || aliases.has(node));
99
+ },
100
+ MESSAGE,
101
+ ),
102
+ ),
46
103
  description: "Stories assert observed items or named values instead of bare counts and numbers.",
47
104
  id: "tests/named-values",
48
105
  });