@noego/trust 0.5.0-dev.4 → 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
@@ -20,6 +20,8 @@ import {
20
20
  RESULT_CONDITION_MESSAGE,
21
21
  RESULT_CONDITION_ORIGIN,
22
22
  RESULT_CONDITION_RULE,
23
+ RESULT_LOOKALIKE_ORIGIN,
24
+ RESULT_LOOKALIKE_RULE,
23
25
  THROW_MESSAGES,
24
26
  THROW_ORIGIN,
25
27
  THROW_RULE,
@@ -45,9 +47,10 @@ import {
45
47
  isTrustPackageFile,
46
48
  nodeBelow,
47
49
  realpathOrSelf,
50
+ resultLookalikeMessage,
48
51
  svelteFor,
49
52
  trustDirectivesIn
50
- } from "./chunk-6YBPW4NP.js";
53
+ } from "./chunk-GJUWPZZJ.js";
51
54
 
52
55
  // src/core/guard.ts
53
56
  function unparen(ts, node) {
@@ -3096,6 +3099,43 @@ function untrustedValueMessage(value) {
3096
3099
  return fillMessage(UNTRUSTED_VALUE_MESSAGES[untrustedValueMessageId(value)], untrustedValueData(value));
3097
3100
  }
3098
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
+
3099
3139
  // src/core/check-file.ts
3100
3140
  var emptyConfigs = /* @__PURE__ */ new Map();
3101
3141
  function emptyTrustConfig(cwd) {
@@ -3172,6 +3212,11 @@ function checkSourceFileWithAccepted(options) {
3172
3212
  ts.forEachChild(node, visit);
3173
3213
  };
3174
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
+ }
3175
3220
  if (isRuleEnabled(config, DROPPED_RESULT_RULE)) {
3176
3221
  for (const dropped of droppedResultsIn(sourceFile, droppedResultContext(ts, checker, policy))) {
3177
3222
  reportAt(dropped.start, DROPPED_RESULT_RULE, dropped.origin, droppedResultMessage(dropped));
@@ -3183,7 +3228,6 @@ function checkSourceFileWithAccepted(options) {
3183
3228
  }
3184
3229
  }
3185
3230
  if (config.boundaries.length > 0) {
3186
- const strict = options.strict ?? isStrictByConfig(config, sourceFile.fileName);
3187
3231
  for (const found of boundaryFindingsIn(sourceFile, boundaryContext(ts, checker, options.program, policy, config), strict)) {
3188
3232
  const finding = findingAt(found.start, BOUNDARY_RULE, found.origin, boundaryMessage(found));
3189
3233
  findings.push({ ...finding, file: found.file ?? finding.file, ...found.severity === "warning" ? { severity: "warning" } : {} });
@@ -3243,4 +3287,4 @@ export {
3243
3287
  checkSourceFile,
3244
3288
  checkSourceFileWithAccepted
3245
3289
  };
3246
- //# sourceMappingURL=chunk-LJJDRFO7.js.map
3290
+ //# sourceMappingURL=chunk-EJ4O7BCA.js.map