@noego/trust 0.5.1-dev.1 → 0.5.1-dev.3
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 +155 -0
- package/dist/{baseline-D0Ok-NGU.d.ts → baseline-DMNO7IAl.d.ts} +20 -2
- package/dist/{chunk-V4FYZG5Q.js → chunk-2MSSO67W.js} +2 -2
- package/dist/{chunk-P4VFMA2A.js → chunk-3IIEQHH2.js} +382 -12
- package/dist/chunk-3IIEQHH2.js.map +1 -0
- package/dist/{chunk-RVTPDLEW.js → chunk-VJGGEY4G.js} +3481 -3456
- package/dist/chunk-VJGGEY4G.js.map +1 -0
- package/dist/cli/index.js +82 -85
- package/dist/cli/index.js.map +1 -1
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +2 -2
- package/dist/eslint/index.d.ts +5 -2
- package/dist/eslint/index.js +49 -12
- package/dist/eslint/index.js.map +1 -1
- package/dist/index.cjs +33 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +66 -5
- package/dist/index.d.ts +66 -5
- package/dist/index.js +31 -5
- package/dist/index.js.map +1 -1
- package/dist/native/trust_native_wasm.wasm +0 -0
- package/package.json +4 -4
- package/dist/chunk-P4VFMA2A.js.map +0 -1
- package/dist/chunk-RVTPDLEW.js.map +0 -1
- /package/dist/{chunk-V4FYZG5Q.js.map → chunk-2MSSO67W.js.map} +0 -0
|
@@ -25,6 +25,8 @@ import {
|
|
|
25
25
|
THROW_MESSAGES,
|
|
26
26
|
THROW_ORIGIN,
|
|
27
27
|
THROW_RULE,
|
|
28
|
+
TRUSTED_STATE_MESSAGES,
|
|
29
|
+
TRUSTED_STATE_RULE,
|
|
28
30
|
TYPE_CLAIM_MESSAGES,
|
|
29
31
|
TYPE_CLAIM_RULE,
|
|
30
32
|
UNTRUSTED_CALL_MESSAGES,
|
|
@@ -51,7 +53,7 @@ import {
|
|
|
51
53
|
resultLookalikeMessage,
|
|
52
54
|
svelteFor,
|
|
53
55
|
trustDirectivesIn
|
|
54
|
-
} from "./chunk-
|
|
56
|
+
} from "./chunk-VJGGEY4G.js";
|
|
55
57
|
|
|
56
58
|
// src/core/guard.ts
|
|
57
59
|
function unparen(ts, node) {
|
|
@@ -326,6 +328,17 @@ var TrustPolicy = class {
|
|
|
326
328
|
if (discriminant === "false") return "err";
|
|
327
329
|
return null;
|
|
328
330
|
}
|
|
331
|
+
/** null: stateless (or not Ok); undefined: non-literal state; array: literal states. */
|
|
332
|
+
okStates(type) {
|
|
333
|
+
if (this.resultVariant(type) !== "ok") return null;
|
|
334
|
+
const property = type.getProperty("state");
|
|
335
|
+
const declaration = this.symbol_declaration(property);
|
|
336
|
+
if (!property || !declaration || !isTrustPackageFile(declaration.getSourceFile().fileName)) return null;
|
|
337
|
+
const state = this.checker.getTypeOfSymbol(property);
|
|
338
|
+
const members2 = state.isUnion() ? state.types : [state];
|
|
339
|
+
if (!members2.every((member) => member.isStringLiteral())) return void 0;
|
|
340
|
+
return members2.map((member) => member.value);
|
|
341
|
+
}
|
|
329
342
|
/** Name of the @noego/trust function a call resolves to (`err`, `tryResult`, …), else null. */
|
|
330
343
|
trustFunctionName(node) {
|
|
331
344
|
const resolved = this.calleeDeclaration(node);
|
|
@@ -471,10 +484,11 @@ function booleanLiteral(ts, node) {
|
|
|
471
484
|
return void 0;
|
|
472
485
|
}
|
|
473
486
|
var DropWalk = class {
|
|
474
|
-
constructor(context, owner, found) {
|
|
487
|
+
constructor(context, owner, found, stateFound) {
|
|
475
488
|
this.context = context;
|
|
476
489
|
this.owner = owner;
|
|
477
490
|
this.found = found;
|
|
491
|
+
this.stateFound = stateFound;
|
|
478
492
|
this.ts = context.ts;
|
|
479
493
|
this.checker = context.checker;
|
|
480
494
|
this.sourceFile = owner.getSourceFile();
|
|
@@ -483,6 +497,7 @@ var DropWalk = class {
|
|
|
483
497
|
context;
|
|
484
498
|
owner;
|
|
485
499
|
found;
|
|
500
|
+
stateFound;
|
|
486
501
|
ts;
|
|
487
502
|
checker;
|
|
488
503
|
sourceFile;
|
|
@@ -527,7 +542,9 @@ var DropWalk = class {
|
|
|
527
542
|
// ---- reporting ----
|
|
528
543
|
report(origin, at, subject, kinds) {
|
|
529
544
|
const start = typeof at === "number" ? at : at.getStart(this.sourceFile);
|
|
530
|
-
this.
|
|
545
|
+
if (this.stateFound) {
|
|
546
|
+
this.stateFound.push({ origin: origin === "stored" ? "stored-state" : "unchecked-state", start, subject, states: [...kinds].sort() });
|
|
547
|
+
} else this.found.push({ origin, start, subject, kinds: [...kinds].sort() });
|
|
531
548
|
}
|
|
532
549
|
reportVariable(origin, at, symbol, kinds) {
|
|
533
550
|
this.report(origin, at, this.tracked.get(symbol)?.name ?? symbol.name, kinds);
|
|
@@ -547,15 +564,21 @@ var DropWalk = class {
|
|
|
547
564
|
}
|
|
548
565
|
// ---- types ----
|
|
549
566
|
kindsOf(node) {
|
|
550
|
-
return
|
|
567
|
+
return this.typeKinds(this.checker.getTypeAtLocation(node));
|
|
551
568
|
}
|
|
552
569
|
typeKinds(type) {
|
|
553
|
-
|
|
570
|
+
if (!type) return null;
|
|
571
|
+
if (!this.stateFound) return failureKinds(this.context, type);
|
|
572
|
+
const states = /* @__PURE__ */ new Set();
|
|
573
|
+
for (const member of members(this.checker.getAwaitedType(type) ?? type)) {
|
|
574
|
+
for (const name of this.context.policy.okStates(member) ?? []) states.add(name);
|
|
575
|
+
}
|
|
576
|
+
return states.size ? states : null;
|
|
554
577
|
}
|
|
555
578
|
trackedSymbol(node, state) {
|
|
556
579
|
const inner = unparen(this.ts, node);
|
|
557
580
|
if (!this.ts.isIdentifier(inner)) return null;
|
|
558
|
-
const symbol = this.checker.getSymbolAtLocation(inner);
|
|
581
|
+
const symbol = this.stateFound && this.ts.isShorthandPropertyAssignment(inner.parent) ? this.checker.getShorthandAssignmentValueSymbol(inner.parent) : this.checker.getSymbolAtLocation(inner);
|
|
559
582
|
return symbol && state.has(symbol) ? symbol : null;
|
|
560
583
|
}
|
|
561
584
|
isDecideOrTranslate(call) {
|
|
@@ -636,7 +659,7 @@ var DropWalk = class {
|
|
|
636
659
|
if (ts.isSpreadElement(parent) && ts.isArrayLiteralExpression(parent.parent)) return parent;
|
|
637
660
|
if ((ts.isCallExpression(parent) || ts.isNewExpression(parent)) && argumentsOf(parent).includes(child)) {
|
|
638
661
|
if (this.isDecideOrTranslate(parent)) return null;
|
|
639
|
-
return this.kindsOf(parent) !== null ? parent : null;
|
|
662
|
+
return !this.stateFound && this.kindsOf(parent) !== null ? parent : null;
|
|
640
663
|
}
|
|
641
664
|
return null;
|
|
642
665
|
}
|
|
@@ -702,6 +725,84 @@ var DropWalk = class {
|
|
|
702
725
|
}
|
|
703
726
|
return "discarded";
|
|
704
727
|
}
|
|
728
|
+
/** The state pass reuses the control-flow engine, but not failure handoff rules. */
|
|
729
|
+
stateUse(node, states, temporary = true) {
|
|
730
|
+
if (!states.size) return false;
|
|
731
|
+
const ts = this.ts;
|
|
732
|
+
const parent = node.parent;
|
|
733
|
+
let origin;
|
|
734
|
+
let callee;
|
|
735
|
+
if (ts.isPropertyDeclaration(parent) || ts.isPropertyAssignment(parent) || ts.isShorthandPropertyAssignment(parent)) origin = "stored-state";
|
|
736
|
+
else if (ts.isVariableDeclaration(parent)) {
|
|
737
|
+
if (ts.isSourceFile(this.owner)) origin = "stored-state";
|
|
738
|
+
else return true;
|
|
739
|
+
} else if (ts.isBinaryExpression(parent) && isPlainOrLogicalAssignment(ts, parent.operatorToken.kind) && parent.right === node) {
|
|
740
|
+
if (!ts.isIdentifier(unparen(ts, parent.left)) || this.moduleHeld(parent.left)) origin = "stored-state";
|
|
741
|
+
else return true;
|
|
742
|
+
} else if (ts.isReturnStatement(parent) || ts.isYieldExpression(parent) || ts.isArrowFunction(parent) && parent.body === node) return true;
|
|
743
|
+
else if ((ts.isCallExpression(parent) || ts.isNewExpression(parent)) && argumentsOf(parent).includes(node)) {
|
|
744
|
+
const index = argumentsOf(parent).indexOf(node);
|
|
745
|
+
if (ts.isCallExpression(parent) && index === 0 && (this.isDecideOrTranslate(parent) || this.context.policy.trustFunctionName(parent) === "match")) return true;
|
|
746
|
+
const expression = unparen(ts, parent.expression);
|
|
747
|
+
const receiver = ts.isPropertyAccessExpression(expression) ? expression.expression : void 0;
|
|
748
|
+
const resolved = this.context.policy.calleeDeclaration(parent);
|
|
749
|
+
const external = resolved ? this.context.policy.origins.originOf(resolved.declaration) : void 0;
|
|
750
|
+
const lib = external?.kind === "external" && external.origin.startsWith("js:");
|
|
751
|
+
if (lib && receiver && this.moduleHeld(receiver) && ts.isPropertyAccessExpression(expression) && ["push", "unshift", "set", "add"].includes(expression.name.text)) origin = "stored-state";
|
|
752
|
+
else if (external && !(external.kind === "local" && external.zone === "checked") && !(lib && this.typeKinds(this.instantiatedParameterType(parent, index)))) {
|
|
753
|
+
origin = "stated-leaves";
|
|
754
|
+
callee = parent.expression.getText(this.sourceFile);
|
|
755
|
+
} else if (this.acceptsArgument(parent, node)) return true;
|
|
756
|
+
else return false;
|
|
757
|
+
} else if (ts.isExpressionStatement(parent) || ts.isVoidExpression(parent) || (ts.isPropertyAccessExpression(parent) || ts.isElementAccessExpression(parent)) && parent.expression === node) origin = "unchecked-state";
|
|
758
|
+
if (!origin || origin === "unchecked-state" && !temporary) return false;
|
|
759
|
+
const key = node;
|
|
760
|
+
if (!this.reported.has(key)) {
|
|
761
|
+
this.reported.add(key);
|
|
762
|
+
this.stateFound?.push({ origin, start: node.getStart(this.sourceFile), subject: subjectOf(node, this.sourceFile), states: [...states].sort(), ...callee ? { callee } : {} });
|
|
763
|
+
}
|
|
764
|
+
return true;
|
|
765
|
+
}
|
|
766
|
+
/** Fields and module variables are not followed; containers local to a function are. */
|
|
767
|
+
moduleHeld(node) {
|
|
768
|
+
const ts = this.ts;
|
|
769
|
+
const inner = unparen(ts, node);
|
|
770
|
+
if (inner.kind === ts.SyntaxKind.ThisKeyword) return true;
|
|
771
|
+
if (ts.isPropertyAccessExpression(inner) || ts.isElementAccessExpression(inner)) return this.moduleHeld(inner.expression);
|
|
772
|
+
if (!ts.isIdentifier(inner)) return false;
|
|
773
|
+
const declaration = this.checker.getSymbolAtLocation(inner)?.valueDeclaration;
|
|
774
|
+
if (!declaration) return false;
|
|
775
|
+
for (let parent = declaration.parent; parent; parent = parent.parent) {
|
|
776
|
+
if (isOwner(ts, parent)) return false;
|
|
777
|
+
if (ts.isSourceFile(parent)) return true;
|
|
778
|
+
}
|
|
779
|
+
return false;
|
|
780
|
+
}
|
|
781
|
+
stateReference(node, symbol, state) {
|
|
782
|
+
const ts = this.ts;
|
|
783
|
+
const current = state.get(symbol);
|
|
784
|
+
if (!current) return;
|
|
785
|
+
if (!current.kinds.size) {
|
|
786
|
+
this.stateUse(this.carrier(node), this.typeKinds(this.checker.getTypeOfSymbol(symbol)) ?? NOTHING, false);
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
let wrapped = node;
|
|
790
|
+
while (ts.isParenthesizedExpression(wrapped.parent) || ts.isAsExpression(wrapped.parent) || ts.isNonNullExpression(wrapped.parent) || ts.isSatisfiesExpression(wrapped.parent) || ts.isTypeAssertionExpression(wrapped.parent)) wrapped = wrapped.parent;
|
|
791
|
+
const parent = wrapped.parent;
|
|
792
|
+
const property = ts.isPropertyAccessExpression(parent) && parent.expression === wrapped ? parent.name.text : ts.isElementAccessExpression(parent) && parent.expression === wrapped && parent.argumentExpression ? stringLiteral(ts, parent.argumentExpression) : void 0;
|
|
793
|
+
const destructured = ts.isVariableDeclaration(parent) && parent.initializer === wrapped && ts.isObjectBindingPattern(parent.name) && parent.name.elements.some((element) => (element.propertyName ?? element.name).getText(this.sourceFile) === "value");
|
|
794
|
+
if (property === "value" || destructured) {
|
|
795
|
+
if (current.kinds.size === 1 && current.err) state.set(symbol, { kinds: NOTHING, err: false });
|
|
796
|
+
else if (this.typeKinds(this.checker.getTypeOfSymbol(symbol))?.size === 1) {
|
|
797
|
+
this.stateFound?.push({ origin: "unnarrowed-value", start: parent.getStart(this.sourceFile), subject: node.text, states: [...current.kinds].sort() });
|
|
798
|
+
state.set(symbol, { kinds: NOTHING, err: false });
|
|
799
|
+
}
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
if (property !== void 0) return;
|
|
803
|
+
if (ts.isBinaryExpression(parent) && parent.left === node && isPlainOrLogicalAssignment(ts, parent.operatorToken.kind)) return;
|
|
804
|
+
if (this.stateUse(this.carrier(node), current.kinds, false)) state.set(symbol, { kinds: NOTHING, err: current.err });
|
|
805
|
+
}
|
|
705
806
|
// ---- expressions ----
|
|
706
807
|
/** Visits the expressions under `node` (not into nested functions) in source order, deciding the fate of each value that can fail. */
|
|
707
808
|
visit(node, state) {
|
|
@@ -736,7 +837,16 @@ var DropWalk = class {
|
|
|
736
837
|
/** A call's value: reported where it is dropped; followed when it is kept in a variable. */
|
|
737
838
|
temporary(node) {
|
|
738
839
|
if (!this.kindsOf(node) || this.derivedFromReceiver(node)) return;
|
|
840
|
+
if (this.stateFound && this.ts.isCallExpression(node)) {
|
|
841
|
+
const resolved = this.context.policy.calleeDeclaration(node);
|
|
842
|
+
const origin = resolved && this.context.policy.origins.originOf(resolved.declaration);
|
|
843
|
+
if (origin?.kind === "external" && origin.origin.startsWith("js:") && node.arguments.some((arg) => this.kindsOf(arg))) return;
|
|
844
|
+
}
|
|
739
845
|
const carrier = this.carrier(node);
|
|
846
|
+
if (this.stateFound) {
|
|
847
|
+
this.stateUse(carrier, this.kindsOf(node) ?? NOTHING);
|
|
848
|
+
return;
|
|
849
|
+
}
|
|
740
850
|
const fate = this.fate(carrier);
|
|
741
851
|
if (fate !== "discarded" && fate !== "argument" && fate !== "stored") return;
|
|
742
852
|
if (fate !== "discarded" && this.returnedWithin(carrier)) return;
|
|
@@ -767,12 +877,17 @@ var DropWalk = class {
|
|
|
767
877
|
reference(node, state) {
|
|
768
878
|
const symbol = this.trackedSymbol(node, state);
|
|
769
879
|
if (!symbol) return;
|
|
880
|
+
if (this.stateFound) {
|
|
881
|
+
this.stateReference(node, symbol, state);
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
770
884
|
const fate = this.fate(this.carrier(node));
|
|
771
885
|
if (fate === "track" || fate === "consume" || fate === "return") state.set(symbol, { kinds: NOTHING, err: state.get(symbol)?.err ?? false });
|
|
772
886
|
}
|
|
773
887
|
/** A function written inline as an argument: its failure is dropped when the callee takes no Result back. */
|
|
774
888
|
handedFunction(node) {
|
|
775
889
|
const ts = this.ts;
|
|
890
|
+
if (this.stateFound) return;
|
|
776
891
|
if (!ts.isArrowFunction(node) && !ts.isFunctionExpression(node)) return;
|
|
777
892
|
const call = node.parent;
|
|
778
893
|
if (!(ts.isCallExpression(call) || ts.isNewExpression(call)) || !argumentsOf(call).includes(node)) return;
|
|
@@ -836,7 +951,7 @@ var DropWalk = class {
|
|
|
836
951
|
if (!initializer || !ts.isIdentifier(declaration.name)) return;
|
|
837
952
|
const alias = this.checker.getSymbolAtLocation(declaration.name);
|
|
838
953
|
if (!alias) return;
|
|
839
|
-
if (ts.isPropertyAccessExpression(initializer) && initializer.name.text === "error") {
|
|
954
|
+
if (ts.isPropertyAccessExpression(initializer) && initializer.name.text === (this.stateFound ? "state" : "error")) {
|
|
840
955
|
const owner = this.trackedSymbol(initializer.expression, state);
|
|
841
956
|
if (owner) this.errorAliases.set(alias, owner);
|
|
842
957
|
}
|
|
@@ -865,6 +980,14 @@ var DropWalk = class {
|
|
|
865
980
|
kindCheck(node, state) {
|
|
866
981
|
const ts = this.ts;
|
|
867
982
|
const inner = unparen(ts, node);
|
|
983
|
+
if (this.stateFound) {
|
|
984
|
+
if (ts.isIdentifier(inner)) {
|
|
985
|
+
const alias = this.checker.getSymbolAtLocation(inner);
|
|
986
|
+
const owner = alias && this.errorAliases.get(alias);
|
|
987
|
+
return owner && state.has(owner) ? owner : null;
|
|
988
|
+
}
|
|
989
|
+
return ts.isPropertyAccessExpression(inner) && inner.name.text === "state" ? this.trackedSymbol(inner.expression, state) : null;
|
|
990
|
+
}
|
|
868
991
|
if (!ts.isPropertyAccessExpression(inner) || inner.name.text !== "kind") return null;
|
|
869
992
|
const error = unparen(ts, inner.expression);
|
|
870
993
|
if (ts.isIdentifier(error)) {
|
|
@@ -878,6 +1001,10 @@ var DropWalk = class {
|
|
|
878
1001
|
withOk(state, symbol, isOk) {
|
|
879
1002
|
const narrowed = new Map(state);
|
|
880
1003
|
const current = state.get(symbol);
|
|
1004
|
+
if (this.stateFound) {
|
|
1005
|
+
if (current && !isOk) narrowed.set(symbol, { kinds: NOTHING, err: false });
|
|
1006
|
+
return narrowed;
|
|
1007
|
+
}
|
|
881
1008
|
if (current) narrowed.set(symbol, isOk ? { kinds: NOTHING, err: false } : { kinds: current.kinds, err: true });
|
|
882
1009
|
return narrowed;
|
|
883
1010
|
}
|
|
@@ -888,7 +1015,7 @@ var DropWalk = class {
|
|
|
888
1015
|
if (current) {
|
|
889
1016
|
const rest = new Set(current.kinds);
|
|
890
1017
|
rest.delete(kind);
|
|
891
|
-
narrowed.set(symbol, isKind ? { kinds: NOTHING, err: true } : { kinds: rest, err: true });
|
|
1018
|
+
narrowed.set(symbol, isKind ? { kinds: NOTHING, err: true } : { kinds: rest, err: this.stateFound ? current.err || rest.size < current.kinds.size : true });
|
|
892
1019
|
}
|
|
893
1020
|
return narrowed;
|
|
894
1021
|
}
|
|
@@ -1010,6 +1137,10 @@ var DropWalk = class {
|
|
|
1010
1137
|
* (`if (!a.ok) return a;` also covers a `b` not checked yet: the first failure goes up).
|
|
1011
1138
|
*/
|
|
1012
1139
|
checkReturn(at, expression, state) {
|
|
1140
|
+
if (this.stateFound) {
|
|
1141
|
+
for (const [symbol, value] of state) if (value.kinds.size) this.reportVariable("return", at, symbol, value.kinds);
|
|
1142
|
+
return;
|
|
1143
|
+
}
|
|
1013
1144
|
const ts = this.ts;
|
|
1014
1145
|
const passes = expression !== void 0 && this.kindsOf(expression) !== null;
|
|
1015
1146
|
const referenced = /* @__PURE__ */ new Set();
|
|
@@ -1174,6 +1305,16 @@ function droppedResultsIn(sourceFile, context) {
|
|
|
1174
1305
|
ts.forEachChild(sourceFile, visit);
|
|
1175
1306
|
return found.sort((a, b) => a.start - b.start);
|
|
1176
1307
|
}
|
|
1308
|
+
function stateFindingsIn(sourceFile, context) {
|
|
1309
|
+
const found = [];
|
|
1310
|
+
new DropWalk(context, sourceFile, [], found).run();
|
|
1311
|
+
const visit = (node) => {
|
|
1312
|
+
if (isOwner(context.ts, node)) new DropWalk(context, node, [], found).run();
|
|
1313
|
+
context.ts.forEachChild(node, visit);
|
|
1314
|
+
};
|
|
1315
|
+
context.ts.forEachChild(sourceFile, visit);
|
|
1316
|
+
return found.sort((a, b) => a.start - b.start);
|
|
1317
|
+
}
|
|
1177
1318
|
|
|
1178
1319
|
// src/core/boundaries.ts
|
|
1179
1320
|
function boundaryContext(ts, checker, program, policy, config) {
|
|
@@ -1218,7 +1359,19 @@ function verdictOfType(context, type) {
|
|
|
1218
1359
|
if (awaited.flags & ts.TypeFlags.Any) return { kind: "unproven", type: "any" };
|
|
1219
1360
|
if (awaited.flags & ts.TypeFlags.Unknown) return { kind: "unproven", type: "unknown" };
|
|
1220
1361
|
const kinds = failureKinds(context.kinds, type);
|
|
1221
|
-
|
|
1362
|
+
if (kinds) return { kind: "result", kinds: [...kinds].sort() };
|
|
1363
|
+
if (isRuleEnabled(context.config, "trusted-state")) {
|
|
1364
|
+
const members2 = (value) => value.isUnion() ? value.types : [value];
|
|
1365
|
+
for (const member of members2(awaited)) {
|
|
1366
|
+
if (context.policy.resultVariant(member) !== null) return { kind: "result", kinds: [] };
|
|
1367
|
+
if (member.flags & (ts.TypeFlags.StringLike | ts.TypeFlags.Any)) continue;
|
|
1368
|
+
const element = checker.getIndexTypeOfType(member, ts.IndexKind.Number);
|
|
1369
|
+
if (element && members2(element).some((value) => context.policy.resultVariant(value) !== null)) {
|
|
1370
|
+
return { kind: "result", kinds: [] };
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
return { kind: "plain" };
|
|
1222
1375
|
}
|
|
1223
1376
|
function verdictOfSignatures(context, signatures) {
|
|
1224
1377
|
for (const signature of signatures) {
|
|
@@ -1366,7 +1519,7 @@ function boundaryFindingsIn(sourceFile, context, strict) {
|
|
|
1366
1519
|
if (boundary.verdict.kind === "plain" || item.exclude.some((entry) => excludeMatches(entry, boundary))) continue;
|
|
1367
1520
|
const member = boundaryName(boundary);
|
|
1368
1521
|
if (boundary.verdict.kind === "result") {
|
|
1369
|
-
findings.push({ origin: "returns-result", start: boundary.start, file: null, severity: "error", data: { member, kinds: formatFailureKinds(boundary.verdict.kinds, ANY_KIND), item: label } });
|
|
1522
|
+
findings.push({ origin: boundary.verdict.kinds.length === 0 ? "returns-ok-result" : "returns-result", start: boundary.start, file: null, severity: "error", data: { member, kinds: formatFailureKinds(boundary.verdict.kinds, ANY_KIND), item: label } });
|
|
1370
1523
|
} else {
|
|
1371
1524
|
findings.push({ origin: "unproven-return", start: boundary.start, file: null, severity: strict ? "error" : "warning", data: { member, type: boundary.verdict.type, item: label } });
|
|
1372
1525
|
}
|
|
@@ -1832,6 +1985,215 @@ function redundantGuardsIn(sourceFile, context) {
|
|
|
1832
1985
|
return found;
|
|
1833
1986
|
}
|
|
1834
1987
|
|
|
1988
|
+
// src/core/trusted-state.ts
|
|
1989
|
+
function trustedStateContext(ts, checker, policy) {
|
|
1990
|
+
return { ts, checker, policy };
|
|
1991
|
+
}
|
|
1992
|
+
function trustedStateMessage(finding) {
|
|
1993
|
+
return fillMessage(TRUSTED_STATE_MESSAGES[finding.origin], finding.data);
|
|
1994
|
+
}
|
|
1995
|
+
function oneType(type, ts) {
|
|
1996
|
+
if (!type.isUnion()) return true;
|
|
1997
|
+
const members2 = type.types;
|
|
1998
|
+
if (members2.some((m) => m.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined))) return false;
|
|
1999
|
+
if (members2.every((m) => m.flags & ts.TypeFlags.BooleanLiteral)) return true;
|
|
2000
|
+
const enums = members2.map((m) => m.getSymbol()?.declarations?.find(ts.isEnumMember)?.parent);
|
|
2001
|
+
if (enums[0] && enums.every((e) => e === enums[0])) return true;
|
|
2002
|
+
if (members2.some((m) => m.flags & ts.TypeFlags.EnumLiteral)) return false;
|
|
2003
|
+
return members2.every((m) => m.isStringLiteral()) || members2.every((m) => m.isNumberLiteral());
|
|
2004
|
+
}
|
|
2005
|
+
function trustedStatesIn(sourceFile, context) {
|
|
2006
|
+
const { ts, checker, policy } = context;
|
|
2007
|
+
const findings = [];
|
|
2008
|
+
const covered = /* @__PURE__ */ new Set();
|
|
2009
|
+
const erasedArguments = /* @__PURE__ */ new Map();
|
|
2010
|
+
if (isTrustPackageFile(sourceFile.fileName)) return findings;
|
|
2011
|
+
const report = (node, origin, data = {}, warning = false) => {
|
|
2012
|
+
findings.push({ start: node.getStart(sourceFile), origin, data, ...warning ? { severity: "warning" } : {} });
|
|
2013
|
+
};
|
|
2014
|
+
const inspect = (type, node, extra = []) => {
|
|
2015
|
+
const members2 = [...new Set([type, ...extra].flatMap((t) => t.isUnion() ? t.types : [t]))];
|
|
2016
|
+
const oks = members2.filter((m) => policy.resultVariant(m) === "ok");
|
|
2017
|
+
const stateless = oks.filter((m) => policy.okStates(m) === null);
|
|
2018
|
+
const stated = oks.filter((m) => policy.okStates(m) !== null);
|
|
2019
|
+
if (stateless.length && stated.length) report(node, "mixed-success");
|
|
2020
|
+
if (!stated.length && (stateless.length > 1 || stateless.some((m) => {
|
|
2021
|
+
const value = m.getProperty("value");
|
|
2022
|
+
return value && !oneType(checker.getTypeOfSymbol(value), ts);
|
|
2023
|
+
}))) report(node, "stateless-union", { type: stateless.map((m) => {
|
|
2024
|
+
const value = m.getProperty("value");
|
|
2025
|
+
return checker.typeToString(checker.getTypeOfSymbol(value));
|
|
2026
|
+
}).join(" | ") });
|
|
2027
|
+
const states = /* @__PURE__ */ new Set();
|
|
2028
|
+
for (const member of stated) {
|
|
2029
|
+
const names = policy.okStates(member);
|
|
2030
|
+
if (names === void 0) report(node, "non-literal-state", { type: checker.typeToString(checker.getTypeOfSymbol(member.getProperty("state"))) });
|
|
2031
|
+
else if (names) {
|
|
2032
|
+
for (const name of names) states.add(name);
|
|
2033
|
+
const value = member.getProperty("value");
|
|
2034
|
+
const valueType = value && checker.getTypeOfSymbol(value);
|
|
2035
|
+
if (valueType && !oneType(valueType, ts)) for (const state of names) {
|
|
2036
|
+
report(node, "state-union", { state, type: checker.typeToString(valueType) });
|
|
2037
|
+
}
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
return { stated: stated.length > 0, states };
|
|
2041
|
+
};
|
|
2042
|
+
const awaited = (type) => checker.getAwaitedType(type) ?? type;
|
|
2043
|
+
const stateInfo = (type) => {
|
|
2044
|
+
const members2 = type.isUnion() ? type.types : [type];
|
|
2045
|
+
const stated = members2.filter((m) => policy.resultVariant(m) === "ok" && policy.okStates(m) !== null);
|
|
2046
|
+
return { stated: stated.length > 0, states: [...new Set(stated.flatMap((m) => policy.okStates(m) ?? []))].sort() };
|
|
2047
|
+
};
|
|
2048
|
+
const subjectOf2 = (node) => {
|
|
2049
|
+
const text = node.getText(sourceFile).replace(/\s+/g, " ").trim();
|
|
2050
|
+
return text.length > 60 ? `${text.slice(0, 59)}\u2026` : text;
|
|
2051
|
+
};
|
|
2052
|
+
const formatStates = (states) => {
|
|
2053
|
+
const quoted = states.map((s) => `'${s}'`);
|
|
2054
|
+
return quoted.length < 2 ? quoted.join("") : `${quoted.slice(0, -1).join(", ")} or ${quoted[quoted.length - 1]}`;
|
|
2055
|
+
};
|
|
2056
|
+
const valueRead = (receiver, name, type = checker.getTypeAtLocation(receiver)) => {
|
|
2057
|
+
const access = name.parent;
|
|
2058
|
+
if ((ts.isPropertyAccessExpression(access) || ts.isElementAccessExpression(access)) && (ts.isBinaryExpression(access.parent) && access.parent.left === access && access.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken || ts.isDeleteExpression(access.parent))) return;
|
|
2059
|
+
const info = stateInfo(type);
|
|
2060
|
+
if (info.states.length > 1) {
|
|
2061
|
+
covered.add(receiver.getStart(sourceFile));
|
|
2062
|
+
report(name, "unnarrowed-value", { subject: subjectOf2(receiver), states: formatStates(info.states) });
|
|
2063
|
+
}
|
|
2064
|
+
};
|
|
2065
|
+
const erasure = (expr, target) => {
|
|
2066
|
+
target = awaited(target);
|
|
2067
|
+
if (!policy.isResultType(target) || stateInfo(target).stated) return;
|
|
2068
|
+
const info = stateInfo(awaited(checker.getTypeAtLocation(expr)));
|
|
2069
|
+
if (info.stated) {
|
|
2070
|
+
report(expr, "erased-state", { subject: subjectOf2(expr), states: formatStates(info.states) });
|
|
2071
|
+
const argument = unparen(ts, expr);
|
|
2072
|
+
if (ts.isCallExpression(expr.parent) && expr.parent.arguments.includes(expr) && ts.isIdentifier(argument)) {
|
|
2073
|
+
const symbol = checker.getSymbolAtLocation(argument);
|
|
2074
|
+
const statement = expr.parent.parent;
|
|
2075
|
+
if (symbol && ts.isExpressionStatement(statement) && ts.isBlock(statement.parent)) {
|
|
2076
|
+
const entries = erasedArguments.get(symbol) ?? [];
|
|
2077
|
+
entries.push({ after: statement.end, exit: statement.parent.end - 1 });
|
|
2078
|
+
erasedArguments.set(symbol, entries);
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
}
|
|
2082
|
+
};
|
|
2083
|
+
const visit = (node) => {
|
|
2084
|
+
if (ts.isPropertyAccessExpression(node) && node.name.text === "value") valueRead(node.expression, node.name);
|
|
2085
|
+
if (ts.isElementAccessExpression(node) && ts.isStringLiteral(node.argumentExpression) && node.argumentExpression.text === "value") valueRead(node.expression, node.argumentExpression);
|
|
2086
|
+
if ((ts.isVariableDeclaration(node) || ts.isParameter(node)) && ts.isObjectBindingPattern(node.name)) {
|
|
2087
|
+
const receiver = node.initializer ?? node.name;
|
|
2088
|
+
for (const element of node.name.elements) {
|
|
2089
|
+
const name = element.propertyName ?? element.name;
|
|
2090
|
+
if (!element.dotDotDotToken && (ts.isIdentifier(name) || ts.isStringLiteral(name)) && name.text === "value") valueRead(receiver, name);
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
if ((ts.isVariableDeclaration(node) || ts.isPropertyDeclaration(node)) && node.type && node.initializer) erasure(node.initializer, checker.getTypeFromTypeNode(node.type));
|
|
2094
|
+
if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken) erasure(node.right, checker.getTypeAtLocation(node.left));
|
|
2095
|
+
if (ts.isReturnStatement(node) && node.expression) {
|
|
2096
|
+
let parent = node.parent;
|
|
2097
|
+
while (parent && !ts.isFunctionLike(parent)) parent = parent.parent;
|
|
2098
|
+
if (parent && ts.isFunctionLike(parent) && parent.type) erasure(node.expression, checker.getTypeFromTypeNode(parent.type));
|
|
2099
|
+
}
|
|
2100
|
+
if (ts.isArrowFunction(node) && node.type && !ts.isBlock(node.body)) erasure(node.body, checker.getTypeFromTypeNode(node.type));
|
|
2101
|
+
if (ts.isCallExpression(node)) {
|
|
2102
|
+
const resolved = policy.calleeDeclaration(node);
|
|
2103
|
+
const file = resolved?.declaration.getSourceFile().fileName;
|
|
2104
|
+
const signature = checker.getResolvedSignature(node);
|
|
2105
|
+
if (file && !isTrustPackageFile(file) && policy.zones.zoneOf(file) === "checked" && signature) {
|
|
2106
|
+
node.arguments.forEach((arg, i) => {
|
|
2107
|
+
const parameter = signature.parameters[i] ?? signature.parameters[signature.parameters.length - 1];
|
|
2108
|
+
if (!parameter) return;
|
|
2109
|
+
let type = checker.getTypeOfSymbolAtLocation(parameter, node);
|
|
2110
|
+
const declaration = parameter.valueDeclaration;
|
|
2111
|
+
if (declaration && ts.isParameter(declaration) && declaration.dotDotDotToken) type = checker.getIndexTypeOfType(type, ts.IndexKind.Number) ?? type;
|
|
2112
|
+
erasure(arg, type);
|
|
2113
|
+
});
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
if (ts.isFunctionLike(node) && "body" in node && node.body) {
|
|
2117
|
+
const fn = node;
|
|
2118
|
+
const signature = checker.getSignatureFromDeclaration(fn);
|
|
2119
|
+
if (signature) {
|
|
2120
|
+
const returned = checker.getReturnTypeOfSignature(signature);
|
|
2121
|
+
const position = fn.type ?? fn.name ?? fn;
|
|
2122
|
+
const extra = [];
|
|
2123
|
+
const returns = (child) => {
|
|
2124
|
+
if (ts.isFunctionLike(child)) return;
|
|
2125
|
+
if (ts.isReturnStatement(child) && child.expression) collect(child.expression);
|
|
2126
|
+
ts.forEachChild(child, returns);
|
|
2127
|
+
};
|
|
2128
|
+
const collect = (expr) => {
|
|
2129
|
+
if (ts.isConditionalExpression(expr)) {
|
|
2130
|
+
collect(expr.whenTrue);
|
|
2131
|
+
collect(expr.whenFalse);
|
|
2132
|
+
} else {
|
|
2133
|
+
const t = checker.getTypeAtLocation(expr);
|
|
2134
|
+
extra.push(checker.getAwaitedType(t) ?? t);
|
|
2135
|
+
}
|
|
2136
|
+
};
|
|
2137
|
+
if (!fn.type) {
|
|
2138
|
+
if (ts.isBlock(fn.body)) returns(fn.body);
|
|
2139
|
+
else collect(fn.body);
|
|
2140
|
+
}
|
|
2141
|
+
const awaited2 = checker.getAwaitedType(returned) ?? returned;
|
|
2142
|
+
const signatureMembers = awaited2.isUnion() ? awaited2.types : [awaited2];
|
|
2143
|
+
const lostState = !signatureMembers.some((m) => policy.resultVariant(m) === "ok" && policy.okStates(m) !== null) && extra.some((t) => (t.isUnion() ? t.types : [t]).some((m) => policy.resultVariant(m) === "ok" && policy.okStates(m) !== null));
|
|
2144
|
+
const result = inspect(awaited2, position, lostState ? extra : []);
|
|
2145
|
+
let callsState = false;
|
|
2146
|
+
const calls = (child) => {
|
|
2147
|
+
if (ts.isFunctionLike(child)) return;
|
|
2148
|
+
if (ts.isCallExpression(child)) {
|
|
2149
|
+
const name = policy.trustFunctionName(child);
|
|
2150
|
+
if (name === "state") callsState = true;
|
|
2151
|
+
if (name === "ok" && result.stated) report(child, "mixed-success");
|
|
2152
|
+
}
|
|
2153
|
+
ts.forEachChild(child, calls);
|
|
2154
|
+
};
|
|
2155
|
+
calls(fn.body);
|
|
2156
|
+
if (result.states.size === 1 && callsState) report(position, "single-state", { state: [...result.states][0] }, true);
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
if ((ts.isVariableDeclaration(node) || ts.isParameter(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node)) && node.type) {
|
|
2160
|
+
inspect(checker.getTypeFromTypeNode(node.type), node.type);
|
|
2161
|
+
}
|
|
2162
|
+
ts.forEachChild(node, visit);
|
|
2163
|
+
};
|
|
2164
|
+
visit(sourceFile);
|
|
2165
|
+
for (const flow of stateFindingsIn(sourceFile, droppedResultContext(ts, checker, policy))) {
|
|
2166
|
+
if (covered.has(flow.start) && (flow.origin === "unchecked-state" || flow.origin === "unnarrowed-value")) continue;
|
|
2167
|
+
if (flow.origin === "unchecked-state" && erasedArguments.size) {
|
|
2168
|
+
let at = sourceFile;
|
|
2169
|
+
const locate = (node) => {
|
|
2170
|
+
if (node.getStart(sourceFile) <= flow.start && flow.start < node.end) {
|
|
2171
|
+
at = node;
|
|
2172
|
+
ts.forEachChild(node, locate);
|
|
2173
|
+
}
|
|
2174
|
+
};
|
|
2175
|
+
locate(sourceFile);
|
|
2176
|
+
const symbol = checker.getSymbolsInScope(at, ts.SymbolFlags.Variable).find((s) => s.name === flow.subject);
|
|
2177
|
+
if (symbol && erasedArguments.get(symbol)?.some(({ after, exit }) => {
|
|
2178
|
+
if (flow.start < after || flow.start > exit) return false;
|
|
2179
|
+
let reassigned = false;
|
|
2180
|
+
const writes = (node) => {
|
|
2181
|
+
if (node.getStart(sourceFile) >= after && node.getStart(sourceFile) < flow.start && ts.isBinaryExpression(node) && node.operatorToken.kind >= ts.SyntaxKind.FirstAssignment && node.operatorToken.kind <= ts.SyntaxKind.LastAssignment && checker.getSymbolAtLocation(unparen(ts, node.left)) === symbol) reassigned = true;
|
|
2182
|
+
ts.forEachChild(node, writes);
|
|
2183
|
+
};
|
|
2184
|
+
writes(sourceFile);
|
|
2185
|
+
return !reassigned;
|
|
2186
|
+
})) continue;
|
|
2187
|
+
}
|
|
2188
|
+
findings.push({ start: flow.start, origin: flow.origin, data: {
|
|
2189
|
+
subject: flow.subject,
|
|
2190
|
+
states: formatStates([...flow.states].sort()),
|
|
2191
|
+
callee: flow.callee ?? ""
|
|
2192
|
+
} });
|
|
2193
|
+
}
|
|
2194
|
+
return findings.sort((a, b) => a.start - b.start);
|
|
2195
|
+
}
|
|
2196
|
+
|
|
1835
2197
|
// src/core/type-text.ts
|
|
1836
2198
|
var OPEN = /* @__PURE__ */ new Set(["(", "[", "{", "<"]);
|
|
1837
2199
|
var CLOSE = { ")": "(", "]": "[", "}": "{", ">": "<" };
|
|
@@ -3225,6 +3587,12 @@ function checkSourceFileWithAccepted(options) {
|
|
|
3225
3587
|
findings.push(strict ? finding : { ...finding, severity: "warning" });
|
|
3226
3588
|
}
|
|
3227
3589
|
}
|
|
3590
|
+
if (isRuleEnabled(config, TRUSTED_STATE_RULE)) {
|
|
3591
|
+
for (const found of trustedStatesIn(sourceFile, trustedStateContext(ts, checker, policy))) {
|
|
3592
|
+
const finding = findingAt(found.start, TRUSTED_STATE_RULE, found.origin, trustedStateMessage(found));
|
|
3593
|
+
findings.push(found.severity ? { ...finding, severity: found.severity } : finding);
|
|
3594
|
+
}
|
|
3595
|
+
}
|
|
3228
3596
|
if (isRuleEnabled(config, DROPPED_RESULT_RULE)) {
|
|
3229
3597
|
for (const dropped of droppedResultsIn(sourceFile, droppedResultContext(ts, checker, policy))) {
|
|
3230
3598
|
reportAt(dropped.start, DROPPED_RESULT_RULE, dropped.origin, droppedResultMessage(dropped));
|
|
@@ -3274,6 +3642,8 @@ export {
|
|
|
3274
3642
|
acceptDirectiveFindings,
|
|
3275
3643
|
redundantGuardData,
|
|
3276
3644
|
redundantGuardsIn,
|
|
3645
|
+
trustedStateContext,
|
|
3646
|
+
trustedStatesIn,
|
|
3277
3647
|
claimSpan,
|
|
3278
3648
|
typeClaimContext,
|
|
3279
3649
|
typeClaimMessage,
|
|
@@ -3295,4 +3665,4 @@ export {
|
|
|
3295
3665
|
checkSourceFile,
|
|
3296
3666
|
checkSourceFileWithAccepted
|
|
3297
3667
|
};
|
|
3298
|
-
//# sourceMappingURL=chunk-
|
|
3668
|
+
//# sourceMappingURL=chunk-3IIEQHH2.js.map
|