@noego/trust 0.5.0-dev.4 → 0.5.0-dev.6

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
@@ -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. */
@@ -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,11 @@ import {
45
47
  isTrustPackageFile,
46
48
  nodeBelow,
47
49
  realpathOrSelf,
50
+ reportsResultLookalikes,
51
+ resultLookalikeMessage,
48
52
  svelteFor,
49
53
  trustDirectivesIn
50
- } from "./chunk-6YBPW4NP.js";
54
+ } from "./chunk-RVTPDLEW.js";
51
55
 
52
56
  // src/core/guard.ts
53
57
  function unparen(ts, node) {
@@ -3096,6 +3100,43 @@ function untrustedValueMessage(value) {
3096
3100
  return fillMessage(UNTRUSTED_VALUE_MESSAGES[untrustedValueMessageId(value)], untrustedValueData(value));
3097
3101
  }
3098
3102
 
3103
+ // src/core/result-lookalikes.ts
3104
+ var INLINE_LOOKALIKE_NAME = "(inline type)";
3105
+ function isOkDiscriminant(ts, node) {
3106
+ if (!ts.isPropertySignature(node) || !node.type) return false;
3107
+ const name = node.name;
3108
+ if (!(ts.isIdentifier(name) || ts.isStringLiteral(name)) || name.text !== "ok") return false;
3109
+ if (!ts.isLiteralTypeNode(node.type)) return false;
3110
+ const literal = node.type.literal.kind;
3111
+ if (literal !== ts.SyntaxKind.TrueKeyword && literal !== ts.SyntaxKind.FalseKeyword) return false;
3112
+ const owner = node.parent;
3113
+ return (ts.isTypeLiteralNode(owner) || ts.isInterfaceDeclaration(owner)) && owner.members.length > 1;
3114
+ }
3115
+ function anchorOf(ts, sourceFile, member) {
3116
+ let current = member.parent;
3117
+ if (ts.isInterfaceDeclaration(current)) return { start: current.name.getStart(sourceFile), name: current.name.text };
3118
+ while (ts.isTypeNode(current.parent)) current = current.parent;
3119
+ const owner = current.parent;
3120
+ if (ts.isTypeAliasDeclaration(owner)) return { start: owner.name.getStart(sourceFile), name: owner.name.text };
3121
+ return { start: current.getStart(sourceFile), name: INLINE_LOOKALIKE_NAME };
3122
+ }
3123
+ function resultLookalikesIn(ts, sourceFile) {
3124
+ const found = [];
3125
+ const seen = /* @__PURE__ */ new Set();
3126
+ const visit = (node) => {
3127
+ if (isOkDiscriminant(ts, node)) {
3128
+ const anchor = anchorOf(ts, sourceFile, node);
3129
+ if (!seen.has(anchor.start)) {
3130
+ seen.add(anchor.start);
3131
+ found.push(anchor);
3132
+ }
3133
+ }
3134
+ ts.forEachChild(node, visit);
3135
+ };
3136
+ visit(sourceFile);
3137
+ return found;
3138
+ }
3139
+
3099
3140
  // src/core/check-file.ts
3100
3141
  var emptyConfigs = /* @__PURE__ */ new Map();
3101
3142
  function emptyTrustConfig(cwd) {
@@ -3172,6 +3213,13 @@ function checkSourceFileWithAccepted(options) {
3172
3213
  ts.forEachChild(node, visit);
3173
3214
  };
3174
3215
  visit(sourceFile);
3216
+ const strict = options.strict ?? isStrictByConfig(config, sourceFile.fileName);
3217
+ if (reportsResultLookalikes(config, strict)) {
3218
+ for (const lookalike of resultLookalikesIn(ts, sourceFile)) {
3219
+ const finding = findingAt(lookalike.start, RESULT_LOOKALIKE_RULE, RESULT_LOOKALIKE_ORIGIN, resultLookalikeMessage(lookalike.name));
3220
+ findings.push(strict ? finding : { ...finding, severity: "warning" });
3221
+ }
3222
+ }
3175
3223
  if (isRuleEnabled(config, DROPPED_RESULT_RULE)) {
3176
3224
  for (const dropped of droppedResultsIn(sourceFile, droppedResultContext(ts, checker, policy))) {
3177
3225
  reportAt(dropped.start, DROPPED_RESULT_RULE, dropped.origin, droppedResultMessage(dropped));
@@ -3183,7 +3231,6 @@ function checkSourceFileWithAccepted(options) {
3183
3231
  }
3184
3232
  }
3185
3233
  if (config.boundaries.length > 0) {
3186
- const strict = options.strict ?? isStrictByConfig(config, sourceFile.fileName);
3187
3234
  for (const found of boundaryFindingsIn(sourceFile, boundaryContext(ts, checker, options.program, policy, config), strict)) {
3188
3235
  const finding = findingAt(found.start, BOUNDARY_RULE, found.origin, boundaryMessage(found));
3189
3236
  findings.push({ ...finding, file: found.file ?? finding.file, ...found.severity === "warning" ? { severity: "warning" } : {} });
@@ -3243,4 +3290,4 @@ export {
3243
3290
  checkSourceFile,
3244
3291
  checkSourceFileWithAccepted
3245
3292
  };
3246
- //# sourceMappingURL=chunk-LJJDRFO7.js.map
3293
+ //# sourceMappingURL=chunk-BHBYQYJJ.js.map