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

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
@@ -606,6 +606,10 @@ Besides the two trust rules it counts two promise rules:
606
606
  an async callback passed where a void-returning function is expected (`void-callback`, e.g. `forEach`,
607
607
  `setTimeout`).
608
608
 
609
+ and two Result-shape rules that are always on: `result-condition` (origin `condition`: a `Result` tested for
610
+ truthiness instead of `.ok`) and `result-lookalike` (origin `declaration`: a hand-written `ok: true` / `ok: false`
611
+ type, see [`result-lookalike`](#result-lookalike-no-hand-written-result); a warning unless the file is strict).
612
+
609
613
  Baseline entries under the old `@typescript-eslint/no-floating-promises` / `@typescript-eslint/no-misused-promises`
610
614
  ids become stale (nothing reports those ids anymore); one `trust baseline --update` clears them and records the new
611
615
  rule ids.
@@ -1025,6 +1029,32 @@ async instance<T>(token: new () => T) {
1025
1029
  Fix it by calling directly and returning the value. If the call really can fail, the trust entry is wrong: remove the
1026
1030
  origin from `trust`, and the guard becomes required again.
1027
1031
 
1032
+ ### `result-lookalike`: no hand-written Result
1033
+
1034
+ Every rule recognises a `Result` by the declaration of its `ok` property: it is one only when `@noego/trust` declares
1035
+ it. A type that copies the shape by hand, `{ ok: true; … } | { ok: false; … }`, is plain data to the rules, so
1036
+ `dropped-result`, `boundary-result` and `result-condition` never follow its failure, and at runtime nothing tells it
1037
+ from a real `Result` or from an `{ ok, result }` envelope either (a layer that unwraps one unwraps the other).
1038
+ `result-lookalike` is always on and reports every such declaration once, at the alias or interface name (origin
1039
+ `declaration`), or at the type when it is written inline (a return type, a type argument). It is a **warning**, and
1040
+ an **error** under `--strict` or in a file the `strict` globs match. `//trust:accept` does not accept it.
1041
+
1042
+ ```ts
1043
+ export type CodeSandboxResult =
1044
+ // result-lookalike: Type "CodeSandboxResult" is a hand-written Result: it is told apart by `ok: true` / `ok: false`
1045
+ // but is not @noego/trust's Result, so no rule follows its failure…
1046
+ | { ok: true; result: unknown; logs: string[] }
1047
+ | { ok: false; error: string; logs: string[] };
1048
+ ```
1049
+
1050
+ Not reported: an alias of the real thing (`type R = Result<number, E>`), a plain boolean field (`ok: boolean`), a
1051
+ union told apart by another name (`status: 'completed' | 'failed'`), an `ok()` method, a class property, and a
1052
+ one-key narrowing probe on a real `Result` (`Extract<R, { ok: true }>`: nothing to tell apart). Fix it by
1053
+ returning `Result<T, E>` from `@noego/trust` inside the program; at an edge, decide the `Result` and return plain
1054
+ data with its own discriminant. Upgrading adds these findings to `trust baseline`; run `trust baseline --update` once
1055
+ to record them, then nothing new can be added.
1056
+
1057
+
1028
1058
  ### `boundary-result`: a Result does not cross the edge
1029
1059
 
1030
1060
  `Result` is an internal contract; the edge of the program decides every kind that is left and returns plain data
@@ -17,6 +17,11 @@ import {
17
17
  REDUNDANT_GUARD_MESSAGES,
18
18
  REDUNDANT_GUARD_ORIGIN,
19
19
  REDUNDANT_GUARD_RULE,
20
+ RESULT_CONDITION_MESSAGE,
21
+ RESULT_CONDITION_ORIGIN,
22
+ RESULT_CONDITION_RULE,
23
+ RESULT_LOOKALIKE_ORIGIN,
24
+ RESULT_LOOKALIKE_RULE,
20
25
  THROW_MESSAGES,
21
26
  THROW_ORIGIN,
22
27
  THROW_RULE,
@@ -42,9 +47,10 @@ import {
42
47
  isTrustPackageFile,
43
48
  nodeBelow,
44
49
  realpathOrSelf,
50
+ resultLookalikeMessage,
45
51
  svelteFor,
46
52
  trustDirectivesIn
47
- } from "./chunk-PMIYTMMA.js";
53
+ } from "./chunk-GJUWPZZJ.js";
48
54
 
49
55
  // src/core/guard.ts
50
56
  function unparen(ts, node) {
@@ -1530,6 +1536,9 @@ function conditionOperands(ts, node) {
1530
1536
  function isPromiseCondition(checker, operand) {
1531
1537
  return isAlwaysThenable(checker, checker.getTypeAtLocation(operand), operand);
1532
1538
  }
1539
+ function isResultCondition(checker, policy, operand) {
1540
+ return policy.isResultType(checker.getTypeAtLocation(operand));
1541
+ }
1533
1542
  function returnsThenable(checker, type, node) {
1534
1543
  return unionParts(type).some(
1535
1544
  (part) => part.getCallSignatures().some((signature) => isSometimesThenable(checker, signature.getReturnType(), node))
@@ -3090,6 +3099,43 @@ function untrustedValueMessage(value) {
3090
3099
  return fillMessage(UNTRUSTED_VALUE_MESSAGES[untrustedValueMessageId(value)], untrustedValueData(value));
3091
3100
  }
3092
3101
 
3102
+ // src/core/result-lookalikes.ts
3103
+ var INLINE_LOOKALIKE_NAME = "(inline type)";
3104
+ function isOkDiscriminant(ts, node) {
3105
+ if (!ts.isPropertySignature(node) || !node.type) return false;
3106
+ const name = node.name;
3107
+ if (!(ts.isIdentifier(name) || ts.isStringLiteral(name)) || name.text !== "ok") return false;
3108
+ if (!ts.isLiteralTypeNode(node.type)) return false;
3109
+ const literal = node.type.literal.kind;
3110
+ if (literal !== ts.SyntaxKind.TrueKeyword && literal !== ts.SyntaxKind.FalseKeyword) return false;
3111
+ const owner = node.parent;
3112
+ return (ts.isTypeLiteralNode(owner) || ts.isInterfaceDeclaration(owner)) && owner.members.length > 1;
3113
+ }
3114
+ function anchorOf(ts, sourceFile, member) {
3115
+ let current = member.parent;
3116
+ if (ts.isInterfaceDeclaration(current)) return { start: current.name.getStart(sourceFile), name: current.name.text };
3117
+ while (ts.isTypeNode(current.parent)) current = current.parent;
3118
+ const owner = current.parent;
3119
+ if (ts.isTypeAliasDeclaration(owner)) return { start: owner.name.getStart(sourceFile), name: owner.name.text };
3120
+ return { start: current.getStart(sourceFile), name: INLINE_LOOKALIKE_NAME };
3121
+ }
3122
+ function resultLookalikesIn(ts, sourceFile) {
3123
+ const found = [];
3124
+ const seen = /* @__PURE__ */ new Set();
3125
+ const visit = (node) => {
3126
+ if (isOkDiscriminant(ts, node)) {
3127
+ const anchor = anchorOf(ts, sourceFile, node);
3128
+ if (!seen.has(anchor.start)) {
3129
+ seen.add(anchor.start);
3130
+ found.push(anchor);
3131
+ }
3132
+ }
3133
+ ts.forEachChild(node, visit);
3134
+ };
3135
+ visit(sourceFile);
3136
+ return found;
3137
+ }
3138
+
3093
3139
  // src/core/check-file.ts
3094
3140
  var emptyConfigs = /* @__PURE__ */ new Map();
3095
3141
  function emptyTrustConfig(cwd) {
@@ -3154,6 +3200,9 @@ function checkSourceFileWithAccepted(options) {
3154
3200
  if (!acceptance) report(found.node, found.rule, found.origin, found.message);
3155
3201
  else accepted.push({ finding: findingAt(found.node.getStart(sourceFile), found.rule, found.origin, found.message), text: found.node.getText(sourceFile), acceptance });
3156
3202
  }
3203
+ for (const operand of conditionOperands(ts, node)) {
3204
+ if (isResultCondition(checker, policy, operand)) report(operand, RESULT_CONDITION_RULE, RESULT_CONDITION_ORIGIN, RESULT_CONDITION_MESSAGE);
3205
+ }
3157
3206
  if (claimContext) for (const claim2 of typeClaimsAt(node, claimContext)) reportClaim(claim2);
3158
3207
  const value = valueContext ? untrustedValueAt(node, valueContext) : null;
3159
3208
  if (value?.outcome === "finding") report(node, UNTRUSTED_VALUE_RULE, value.origin, untrustedValueMessage(value));
@@ -3163,6 +3212,11 @@ function checkSourceFileWithAccepted(options) {
3163
3212
  ts.forEachChild(node, visit);
3164
3213
  };
3165
3214
  visit(sourceFile);
3215
+ 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" });
3219
+ }
3166
3220
  if (isRuleEnabled(config, DROPPED_RESULT_RULE)) {
3167
3221
  for (const dropped of droppedResultsIn(sourceFile, droppedResultContext(ts, checker, policy))) {
3168
3222
  reportAt(dropped.start, DROPPED_RESULT_RULE, dropped.origin, droppedResultMessage(dropped));
@@ -3174,7 +3228,6 @@ function checkSourceFileWithAccepted(options) {
3174
3228
  }
3175
3229
  }
3176
3230
  if (config.boundaries.length > 0) {
3177
- const strict = options.strict ?? isStrictByConfig(config, sourceFile.fileName);
3178
3231
  for (const found of boundaryFindingsIn(sourceFile, boundaryContext(ts, checker, options.program, policy, config), strict)) {
3179
3232
  const finding = findingAt(found.start, BOUNDARY_RULE, found.origin, boundaryMessage(found));
3180
3233
  findings.push({ ...finding, file: found.file ?? finding.file, ...found.severity === "warning" ? { severity: "warning" } : {} });
@@ -3234,4 +3287,4 @@ export {
3234
3287
  checkSourceFile,
3235
3288
  checkSourceFileWithAccepted
3236
3289
  };
3237
- //# sourceMappingURL=chunk-EPWU2Q4U.js.map
3290
+ //# sourceMappingURL=chunk-EJ4O7BCA.js.map