@noego/trust 0.5.0-dev.5 → 0.5.0-dev.7

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
@@ -80,6 +80,12 @@ interface TrustConfig {
80
80
  }
81
81
  /** An opt-in rule's switch. */
82
82
  type RuleSwitch = 'on' | 'off';
83
+ /**
84
+ * `result-lookalike`'s setting: `strict` (default) reports only in strict files (`trust check --strict`, or the
85
+ * config's `strict` globs), as errors; `on` reports in every checked file (an error where the file is strict, a
86
+ * warning elsewhere); `off` reports nothing.
87
+ */
88
+ type ResultLookalikeSetting = 'off' | 'strict' | 'on';
83
89
  /** Which callback parameters are untrusted-value sources (see the file comment). */
84
90
  type UntrustedCallbacks = 'off' | 'inline' | 'all';
85
91
  /** The rules that are off unless a config turns them on. */
@@ -89,6 +95,7 @@ interface TrustRuleSettings {
89
95
  readonly 'untrusted-value': RuleSwitch;
90
96
  readonly 'dropped-result': RuleSwitch;
91
97
  readonly 'redundant-guard': RuleSwitch;
98
+ readonly 'result-lookalike': ResultLookalikeSetting;
92
99
  readonly untrustedCallbacks: UntrustedCallbacks;
93
100
  }
94
101
  /** What a config that sets nothing gets: the opt-in rules off, callbacks as today. */
@@ -47,10 +47,11 @@ import {
47
47
  isTrustPackageFile,
48
48
  nodeBelow,
49
49
  realpathOrSelf,
50
+ reportsResultLookalikes,
50
51
  resultLookalikeMessage,
51
52
  svelteFor,
52
53
  trustDirectivesIn
53
- } from "./chunk-GJUWPZZJ.js";
54
+ } from "./chunk-RVTPDLEW.js";
54
55
 
55
56
  // src/core/guard.ts
56
57
  function unparen(ts, node) {
@@ -1744,6 +1745,11 @@ function isPromiseAdoption(node, context) {
1744
1745
  const owner = checker.getResolvedSignature(node)?.declaration?.parent;
1745
1746
  return owner !== void 0 && ts.isInterfaceDeclaration(owner) && owner.name.text === "PromiseConstructor";
1746
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
+ }
1747
1753
  function cannotFail(body, context) {
1748
1754
  const { ts, policy } = context;
1749
1755
  const origins = /* @__PURE__ */ new Set();
@@ -1783,7 +1789,7 @@ function cannotFail(body, context) {
1783
1789
  }
1784
1790
  if (verdict.kind === "first-party") {
1785
1791
  const signature = context.checker.getResolvedSignature(node);
1786
- if (!signature || !policy.returnsResult(signature)) {
1792
+ if (!signature || !policy.returnsResult(signature) || isFunctionValueSignature(ts, signature)) {
1787
1793
  fails = true;
1788
1794
  return;
1789
1795
  }
@@ -3213,9 +3219,11 @@ function checkSourceFileWithAccepted(options) {
3213
3219
  };
3214
3220
  visit(sourceFile);
3215
3221
  const strict = options.strict ?? isStrictByConfig(config, sourceFile.fileName);
3216
- for (const lookalike of resultLookalikesIn(ts, sourceFile)) {
3217
- const finding = findingAt(lookalike.start, RESULT_LOOKALIKE_RULE, RESULT_LOOKALIKE_ORIGIN, resultLookalikeMessage(lookalike.name));
3218
- findings.push(strict ? finding : { ...finding, severity: "warning" });
3222
+ if (reportsResultLookalikes(config, strict)) {
3223
+ for (const lookalike of resultLookalikesIn(ts, sourceFile)) {
3224
+ const finding = findingAt(lookalike.start, RESULT_LOOKALIKE_RULE, RESULT_LOOKALIKE_ORIGIN, resultLookalikeMessage(lookalike.name));
3225
+ findings.push(strict ? finding : { ...finding, severity: "warning" });
3226
+ }
3219
3227
  }
3220
3228
  if (isRuleEnabled(config, DROPPED_RESULT_RULE)) {
3221
3229
  for (const dropped of droppedResultsIn(sourceFile, droppedResultContext(ts, checker, policy))) {
@@ -3287,4 +3295,4 @@ export {
3287
3295
  checkSourceFile,
3288
3296
  checkSourceFileWithAccepted
3289
3297
  };
3290
- //# sourceMappingURL=chunk-EJ4O7BCA.js.map
3298
+ //# sourceMappingURL=chunk-P4VFMA2A.js.map