@noego/trust 0.5.0-dev.6 → 0.5.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/README.md CHANGED
@@ -1014,8 +1014,9 @@ the value. Opt-in: `"rules": { "redundant-guard": "on" }`.
1014
1014
 
1015
1015
  The guarded code can fail when it holds, outside nested functions: a call or `new` whose origin is untrusted,
1016
1016
  unresolved or in an ignored file; a call into first-party code not declared to return a `Result` (it may still
1017
- throw); a `throw`; a dynamic `import()`; an `await` (or `for await`) of something that is not a call. Calls into
1018
- trusted origins and to functions declared to return a `Result` cannot fail. A guard around only those is reported at
1017
+ throw); a call to a function value typed to return a `Result` (below); a `throw`; a dynamic `import()`; an `await`
1018
+ (or `for await`) of something that is not a call. Calls into trusted origins and to functions declared to return a
1019
+ `Result` cannot fail. A guard around only those is reported at
1019
1020
  the guard (origin `redundant`):
1020
1021
 
1021
1022
  ```ts
@@ -1029,6 +1030,24 @@ async instance<T>(token: new () => T) {
1029
1030
  Fix it by calling directly and returning the value. If the call really can fail, the trust entry is wrong: remove the
1030
1031
  origin from `trust`, and the guard becomes required again.
1031
1032
 
1033
+ A `Result` return counts only for first-party code the linter can see: a function, method or arrow with a body. A
1034
+ call to a **function value** typed to return a `Result` (a callback parameter, an injected function field, an
1035
+ options property, an interface's call signature: any callee whose signature is a function type) can still fail. Its
1036
+ `Result` is a claim about whatever implementation the caller passes in, which may throw (a defect, or a driver error
1037
+ escaping a buggy callback), so a guard around it is not reported:
1038
+
1039
+ ```ts
1040
+ async function once<T, E>(db: Db, work: (db: Db) => Promise<Result<T, E>>) {
1041
+ // Not reported: work is the caller's code, and a throw inside it must still be rolled back.
1042
+ const outcome = await Trust.tryAsync(() => work(db), (cause) => ({ kind: 'query_failed', cause }));
1043
+ …
1044
+ }
1045
+ ```
1046
+
1047
+ A statically known function declared to return a `Result` keeps its contract, called directly or through an alias:
1048
+ `Trust.tryAsync(() => repo.find(id), …)` with `class Repo { async find(id): Promise<Result<…>> { … } }` is still
1049
+ reported. `//trust:accept` does not accept a redundant-guard finding.
1050
+
1032
1051
  ### `result-lookalike`: no hand-written Result
1033
1052
 
1034
1053
  Every rule recognises a `Result` by the declaration of its `ok` property: it is one only when `@noego/trust` declares
@@ -1745,6 +1745,11 @@ function isPromiseAdoption(node, context) {
1745
1745
  const owner = checker.getResolvedSignature(node)?.declaration?.parent;
1746
1746
  return owner !== void 0 && ts.isInterfaceDeclaration(owner) && owner.name.text === "PromiseConstructor";
1747
1747
  }
1748
+ function isFunctionValueSignature(ts, signature) {
1749
+ const declaration = signature.declaration;
1750
+ if (!declaration || ts.isJSDocSignature(declaration)) return false;
1751
+ return ts.isFunctionTypeNode(declaration) || ts.isConstructorTypeNode(declaration) || ts.isCallSignatureDeclaration(declaration) || ts.isConstructSignatureDeclaration(declaration);
1752
+ }
1748
1753
  function cannotFail(body, context) {
1749
1754
  const { ts, policy } = context;
1750
1755
  const origins = /* @__PURE__ */ new Set();
@@ -1784,7 +1789,7 @@ function cannotFail(body, context) {
1784
1789
  }
1785
1790
  if (verdict.kind === "first-party") {
1786
1791
  const signature = context.checker.getResolvedSignature(node);
1787
- if (!signature || !policy.returnsResult(signature)) {
1792
+ if (!signature || !policy.returnsResult(signature) || isFunctionValueSignature(ts, signature)) {
1788
1793
  fails = true;
1789
1794
  return;
1790
1795
  }
@@ -3290,4 +3295,4 @@ export {
3290
3295
  checkSourceFile,
3291
3296
  checkSourceFileWithAccepted
3292
3297
  };
3293
- //# sourceMappingURL=chunk-BHBYQYJJ.js.map
3298
+ //# sourceMappingURL=chunk-P4VFMA2A.js.map