@noego/trust 0.5.1-dev.2 → 0.5.1-dev.4
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-DsqHPLAg.d.ts} +27 -2
- package/dist/{chunk-P4VFMA2A.js → chunk-35ETSKEC.js} +414 -12
- package/dist/chunk-35ETSKEC.js.map +1 -0
- package/dist/{chunk-RVTPDLEW.js → chunk-4XGOH7JE.js} +3491 -3456
- package/dist/chunk-4XGOH7JE.js.map +1 -0
- package/dist/{chunk-V4FYZG5Q.js → chunk-6K5I3ZRI.js} +2 -2
- package/dist/cli/index.js +6 -6
- 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 +6 -2
- package/dist/eslint/index.js +84 -12
- package/dist/eslint/index.js.map +1 -1
- package/dist/index.cjs +253 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +112 -6
- package/dist/index.d.ts +112 -6
- package/dist/index.js +248 -6
- 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-6K5I3ZRI.js.map} +0 -0
|
@@ -8,6 +8,9 @@ import {
|
|
|
8
8
|
DIRECTIVE_ORIGIN,
|
|
9
9
|
DROPPED_RESULT_MESSAGES,
|
|
10
10
|
DROPPED_RESULT_RULE,
|
|
11
|
+
FINALLY_RETURN_MESSAGES,
|
|
12
|
+
FINALLY_RETURN_ORIGIN,
|
|
13
|
+
FINALLY_RETURN_RULE,
|
|
11
14
|
FLOATING_PROMISE_MESSAGE,
|
|
12
15
|
FLOATING_PROMISE_ORIGIN,
|
|
13
16
|
FLOATING_PROMISE_RULE,
|
|
@@ -25,6 +28,8 @@ import {
|
|
|
25
28
|
THROW_MESSAGES,
|
|
26
29
|
THROW_ORIGIN,
|
|
27
30
|
THROW_RULE,
|
|
31
|
+
TRUSTED_STATE_MESSAGES,
|
|
32
|
+
TRUSTED_STATE_RULE,
|
|
28
33
|
TYPE_CLAIM_MESSAGES,
|
|
29
34
|
TYPE_CLAIM_RULE,
|
|
30
35
|
UNTRUSTED_CALL_MESSAGES,
|
|
@@ -51,7 +56,7 @@ import {
|
|
|
51
56
|
resultLookalikeMessage,
|
|
52
57
|
svelteFor,
|
|
53
58
|
trustDirectivesIn
|
|
54
|
-
} from "./chunk-
|
|
59
|
+
} from "./chunk-4XGOH7JE.js";
|
|
55
60
|
|
|
56
61
|
// src/core/guard.ts
|
|
57
62
|
function unparen(ts, node) {
|
|
@@ -326,6 +331,17 @@ var TrustPolicy = class {
|
|
|
326
331
|
if (discriminant === "false") return "err";
|
|
327
332
|
return null;
|
|
328
333
|
}
|
|
334
|
+
/** null: stateless (or not Ok); undefined: non-literal state; array: literal states. */
|
|
335
|
+
okStates(type) {
|
|
336
|
+
if (this.resultVariant(type) !== "ok") return null;
|
|
337
|
+
const property = type.getProperty("state");
|
|
338
|
+
const declaration = this.symbol_declaration(property);
|
|
339
|
+
if (!property || !declaration || !isTrustPackageFile(declaration.getSourceFile().fileName)) return null;
|
|
340
|
+
const state = this.checker.getTypeOfSymbol(property);
|
|
341
|
+
const members2 = state.isUnion() ? state.types : [state];
|
|
342
|
+
if (!members2.every((member) => member.isStringLiteral())) return void 0;
|
|
343
|
+
return members2.map((member) => member.value);
|
|
344
|
+
}
|
|
329
345
|
/** Name of the @noego/trust function a call resolves to (`err`, `tryResult`, …), else null. */
|
|
330
346
|
trustFunctionName(node) {
|
|
331
347
|
const resolved = this.calleeDeclaration(node);
|
|
@@ -471,10 +487,11 @@ function booleanLiteral(ts, node) {
|
|
|
471
487
|
return void 0;
|
|
472
488
|
}
|
|
473
489
|
var DropWalk = class {
|
|
474
|
-
constructor(context, owner, found) {
|
|
490
|
+
constructor(context, owner, found, stateFound) {
|
|
475
491
|
this.context = context;
|
|
476
492
|
this.owner = owner;
|
|
477
493
|
this.found = found;
|
|
494
|
+
this.stateFound = stateFound;
|
|
478
495
|
this.ts = context.ts;
|
|
479
496
|
this.checker = context.checker;
|
|
480
497
|
this.sourceFile = owner.getSourceFile();
|
|
@@ -483,6 +500,7 @@ var DropWalk = class {
|
|
|
483
500
|
context;
|
|
484
501
|
owner;
|
|
485
502
|
found;
|
|
503
|
+
stateFound;
|
|
486
504
|
ts;
|
|
487
505
|
checker;
|
|
488
506
|
sourceFile;
|
|
@@ -527,7 +545,9 @@ var DropWalk = class {
|
|
|
527
545
|
// ---- reporting ----
|
|
528
546
|
report(origin, at, subject, kinds) {
|
|
529
547
|
const start = typeof at === "number" ? at : at.getStart(this.sourceFile);
|
|
530
|
-
this.
|
|
548
|
+
if (this.stateFound) {
|
|
549
|
+
this.stateFound.push({ origin: origin === "stored" ? "stored-state" : "unchecked-state", start, subject, states: [...kinds].sort() });
|
|
550
|
+
} else this.found.push({ origin, start, subject, kinds: [...kinds].sort() });
|
|
531
551
|
}
|
|
532
552
|
reportVariable(origin, at, symbol, kinds) {
|
|
533
553
|
this.report(origin, at, this.tracked.get(symbol)?.name ?? symbol.name, kinds);
|
|
@@ -547,15 +567,21 @@ var DropWalk = class {
|
|
|
547
567
|
}
|
|
548
568
|
// ---- types ----
|
|
549
569
|
kindsOf(node) {
|
|
550
|
-
return
|
|
570
|
+
return this.typeKinds(this.checker.getTypeAtLocation(node));
|
|
551
571
|
}
|
|
552
572
|
typeKinds(type) {
|
|
553
|
-
|
|
573
|
+
if (!type) return null;
|
|
574
|
+
if (!this.stateFound) return failureKinds(this.context, type);
|
|
575
|
+
const states = /* @__PURE__ */ new Set();
|
|
576
|
+
for (const member of members(this.checker.getAwaitedType(type) ?? type)) {
|
|
577
|
+
for (const name of this.context.policy.okStates(member) ?? []) states.add(name);
|
|
578
|
+
}
|
|
579
|
+
return states.size ? states : null;
|
|
554
580
|
}
|
|
555
581
|
trackedSymbol(node, state) {
|
|
556
582
|
const inner = unparen(this.ts, node);
|
|
557
583
|
if (!this.ts.isIdentifier(inner)) return null;
|
|
558
|
-
const symbol = this.checker.getSymbolAtLocation(inner);
|
|
584
|
+
const symbol = this.stateFound && this.ts.isShorthandPropertyAssignment(inner.parent) ? this.checker.getShorthandAssignmentValueSymbol(inner.parent) : this.checker.getSymbolAtLocation(inner);
|
|
559
585
|
return symbol && state.has(symbol) ? symbol : null;
|
|
560
586
|
}
|
|
561
587
|
isDecideOrTranslate(call) {
|
|
@@ -636,7 +662,7 @@ var DropWalk = class {
|
|
|
636
662
|
if (ts.isSpreadElement(parent) && ts.isArrayLiteralExpression(parent.parent)) return parent;
|
|
637
663
|
if ((ts.isCallExpression(parent) || ts.isNewExpression(parent)) && argumentsOf(parent).includes(child)) {
|
|
638
664
|
if (this.isDecideOrTranslate(parent)) return null;
|
|
639
|
-
return this.kindsOf(parent) !== null ? parent : null;
|
|
665
|
+
return !this.stateFound && this.kindsOf(parent) !== null ? parent : null;
|
|
640
666
|
}
|
|
641
667
|
return null;
|
|
642
668
|
}
|
|
@@ -702,6 +728,84 @@ var DropWalk = class {
|
|
|
702
728
|
}
|
|
703
729
|
return "discarded";
|
|
704
730
|
}
|
|
731
|
+
/** The state pass reuses the control-flow engine, but not failure handoff rules. */
|
|
732
|
+
stateUse(node, states, temporary = true) {
|
|
733
|
+
if (!states.size) return false;
|
|
734
|
+
const ts = this.ts;
|
|
735
|
+
const parent = node.parent;
|
|
736
|
+
let origin;
|
|
737
|
+
let callee;
|
|
738
|
+
if (ts.isPropertyDeclaration(parent) || ts.isPropertyAssignment(parent) || ts.isShorthandPropertyAssignment(parent)) origin = "stored-state";
|
|
739
|
+
else if (ts.isVariableDeclaration(parent)) {
|
|
740
|
+
if (ts.isSourceFile(this.owner)) origin = "stored-state";
|
|
741
|
+
else return true;
|
|
742
|
+
} else if (ts.isBinaryExpression(parent) && isPlainOrLogicalAssignment(ts, parent.operatorToken.kind) && parent.right === node) {
|
|
743
|
+
if (!ts.isIdentifier(unparen(ts, parent.left)) || this.moduleHeld(parent.left)) origin = "stored-state";
|
|
744
|
+
else return true;
|
|
745
|
+
} else if (ts.isReturnStatement(parent) || ts.isYieldExpression(parent) || ts.isArrowFunction(parent) && parent.body === node) return true;
|
|
746
|
+
else if ((ts.isCallExpression(parent) || ts.isNewExpression(parent)) && argumentsOf(parent).includes(node)) {
|
|
747
|
+
const index = argumentsOf(parent).indexOf(node);
|
|
748
|
+
if (ts.isCallExpression(parent) && index === 0 && (this.isDecideOrTranslate(parent) || this.context.policy.trustFunctionName(parent) === "match")) return true;
|
|
749
|
+
const expression = unparen(ts, parent.expression);
|
|
750
|
+
const receiver = ts.isPropertyAccessExpression(expression) ? expression.expression : void 0;
|
|
751
|
+
const resolved = this.context.policy.calleeDeclaration(parent);
|
|
752
|
+
const external = resolved ? this.context.policy.origins.originOf(resolved.declaration) : void 0;
|
|
753
|
+
const lib = external?.kind === "external" && external.origin.startsWith("js:");
|
|
754
|
+
if (lib && receiver && this.moduleHeld(receiver) && ts.isPropertyAccessExpression(expression) && ["push", "unshift", "set", "add"].includes(expression.name.text)) origin = "stored-state";
|
|
755
|
+
else if (external && !(external.kind === "local" && external.zone === "checked") && !(lib && this.typeKinds(this.instantiatedParameterType(parent, index)))) {
|
|
756
|
+
origin = "stated-leaves";
|
|
757
|
+
callee = parent.expression.getText(this.sourceFile);
|
|
758
|
+
} else if (this.acceptsArgument(parent, node)) return true;
|
|
759
|
+
else return false;
|
|
760
|
+
} else if (ts.isExpressionStatement(parent) || ts.isVoidExpression(parent) || (ts.isPropertyAccessExpression(parent) || ts.isElementAccessExpression(parent)) && parent.expression === node) origin = "unchecked-state";
|
|
761
|
+
if (!origin || origin === "unchecked-state" && !temporary) return false;
|
|
762
|
+
const key = node;
|
|
763
|
+
if (!this.reported.has(key)) {
|
|
764
|
+
this.reported.add(key);
|
|
765
|
+
this.stateFound?.push({ origin, start: node.getStart(this.sourceFile), subject: subjectOf(node, this.sourceFile), states: [...states].sort(), ...callee ? { callee } : {} });
|
|
766
|
+
}
|
|
767
|
+
return true;
|
|
768
|
+
}
|
|
769
|
+
/** Fields and module variables are not followed; containers local to a function are. */
|
|
770
|
+
moduleHeld(node) {
|
|
771
|
+
const ts = this.ts;
|
|
772
|
+
const inner = unparen(ts, node);
|
|
773
|
+
if (inner.kind === ts.SyntaxKind.ThisKeyword) return true;
|
|
774
|
+
if (ts.isPropertyAccessExpression(inner) || ts.isElementAccessExpression(inner)) return this.moduleHeld(inner.expression);
|
|
775
|
+
if (!ts.isIdentifier(inner)) return false;
|
|
776
|
+
const declaration = this.checker.getSymbolAtLocation(inner)?.valueDeclaration;
|
|
777
|
+
if (!declaration) return false;
|
|
778
|
+
for (let parent = declaration.parent; parent; parent = parent.parent) {
|
|
779
|
+
if (isOwner(ts, parent)) return false;
|
|
780
|
+
if (ts.isSourceFile(parent)) return true;
|
|
781
|
+
}
|
|
782
|
+
return false;
|
|
783
|
+
}
|
|
784
|
+
stateReference(node, symbol, state) {
|
|
785
|
+
const ts = this.ts;
|
|
786
|
+
const current = state.get(symbol);
|
|
787
|
+
if (!current) return;
|
|
788
|
+
if (!current.kinds.size) {
|
|
789
|
+
this.stateUse(this.carrier(node), this.typeKinds(this.checker.getTypeOfSymbol(symbol)) ?? NOTHING, false);
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
let wrapped = node;
|
|
793
|
+
while (ts.isParenthesizedExpression(wrapped.parent) || ts.isAsExpression(wrapped.parent) || ts.isNonNullExpression(wrapped.parent) || ts.isSatisfiesExpression(wrapped.parent) || ts.isTypeAssertionExpression(wrapped.parent)) wrapped = wrapped.parent;
|
|
794
|
+
const parent = wrapped.parent;
|
|
795
|
+
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;
|
|
796
|
+
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");
|
|
797
|
+
if (property === "value" || destructured) {
|
|
798
|
+
if (current.kinds.size === 1 && current.err) state.set(symbol, { kinds: NOTHING, err: false });
|
|
799
|
+
else if (this.typeKinds(this.checker.getTypeOfSymbol(symbol))?.size === 1) {
|
|
800
|
+
this.stateFound?.push({ origin: "unnarrowed-value", start: parent.getStart(this.sourceFile), subject: node.text, states: [...current.kinds].sort() });
|
|
801
|
+
state.set(symbol, { kinds: NOTHING, err: false });
|
|
802
|
+
}
|
|
803
|
+
return;
|
|
804
|
+
}
|
|
805
|
+
if (property !== void 0) return;
|
|
806
|
+
if (ts.isBinaryExpression(parent) && parent.left === node && isPlainOrLogicalAssignment(ts, parent.operatorToken.kind)) return;
|
|
807
|
+
if (this.stateUse(this.carrier(node), current.kinds, false)) state.set(symbol, { kinds: NOTHING, err: current.err });
|
|
808
|
+
}
|
|
705
809
|
// ---- expressions ----
|
|
706
810
|
/** Visits the expressions under `node` (not into nested functions) in source order, deciding the fate of each value that can fail. */
|
|
707
811
|
visit(node, state) {
|
|
@@ -736,7 +840,16 @@ var DropWalk = class {
|
|
|
736
840
|
/** A call's value: reported where it is dropped; followed when it is kept in a variable. */
|
|
737
841
|
temporary(node) {
|
|
738
842
|
if (!this.kindsOf(node) || this.derivedFromReceiver(node)) return;
|
|
843
|
+
if (this.stateFound && this.ts.isCallExpression(node)) {
|
|
844
|
+
const resolved = this.context.policy.calleeDeclaration(node);
|
|
845
|
+
const origin = resolved && this.context.policy.origins.originOf(resolved.declaration);
|
|
846
|
+
if (origin?.kind === "external" && origin.origin.startsWith("js:") && node.arguments.some((arg) => this.kindsOf(arg))) return;
|
|
847
|
+
}
|
|
739
848
|
const carrier = this.carrier(node);
|
|
849
|
+
if (this.stateFound) {
|
|
850
|
+
this.stateUse(carrier, this.kindsOf(node) ?? NOTHING);
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
740
853
|
const fate = this.fate(carrier);
|
|
741
854
|
if (fate !== "discarded" && fate !== "argument" && fate !== "stored") return;
|
|
742
855
|
if (fate !== "discarded" && this.returnedWithin(carrier)) return;
|
|
@@ -767,12 +880,17 @@ var DropWalk = class {
|
|
|
767
880
|
reference(node, state) {
|
|
768
881
|
const symbol = this.trackedSymbol(node, state);
|
|
769
882
|
if (!symbol) return;
|
|
883
|
+
if (this.stateFound) {
|
|
884
|
+
this.stateReference(node, symbol, state);
|
|
885
|
+
return;
|
|
886
|
+
}
|
|
770
887
|
const fate = this.fate(this.carrier(node));
|
|
771
888
|
if (fate === "track" || fate === "consume" || fate === "return") state.set(symbol, { kinds: NOTHING, err: state.get(symbol)?.err ?? false });
|
|
772
889
|
}
|
|
773
890
|
/** A function written inline as an argument: its failure is dropped when the callee takes no Result back. */
|
|
774
891
|
handedFunction(node) {
|
|
775
892
|
const ts = this.ts;
|
|
893
|
+
if (this.stateFound) return;
|
|
776
894
|
if (!ts.isArrowFunction(node) && !ts.isFunctionExpression(node)) return;
|
|
777
895
|
const call = node.parent;
|
|
778
896
|
if (!(ts.isCallExpression(call) || ts.isNewExpression(call)) || !argumentsOf(call).includes(node)) return;
|
|
@@ -836,7 +954,7 @@ var DropWalk = class {
|
|
|
836
954
|
if (!initializer || !ts.isIdentifier(declaration.name)) return;
|
|
837
955
|
const alias = this.checker.getSymbolAtLocation(declaration.name);
|
|
838
956
|
if (!alias) return;
|
|
839
|
-
if (ts.isPropertyAccessExpression(initializer) && initializer.name.text === "error") {
|
|
957
|
+
if (ts.isPropertyAccessExpression(initializer) && initializer.name.text === (this.stateFound ? "state" : "error")) {
|
|
840
958
|
const owner = this.trackedSymbol(initializer.expression, state);
|
|
841
959
|
if (owner) this.errorAliases.set(alias, owner);
|
|
842
960
|
}
|
|
@@ -865,6 +983,14 @@ var DropWalk = class {
|
|
|
865
983
|
kindCheck(node, state) {
|
|
866
984
|
const ts = this.ts;
|
|
867
985
|
const inner = unparen(ts, node);
|
|
986
|
+
if (this.stateFound) {
|
|
987
|
+
if (ts.isIdentifier(inner)) {
|
|
988
|
+
const alias = this.checker.getSymbolAtLocation(inner);
|
|
989
|
+
const owner = alias && this.errorAliases.get(alias);
|
|
990
|
+
return owner && state.has(owner) ? owner : null;
|
|
991
|
+
}
|
|
992
|
+
return ts.isPropertyAccessExpression(inner) && inner.name.text === "state" ? this.trackedSymbol(inner.expression, state) : null;
|
|
993
|
+
}
|
|
868
994
|
if (!ts.isPropertyAccessExpression(inner) || inner.name.text !== "kind") return null;
|
|
869
995
|
const error = unparen(ts, inner.expression);
|
|
870
996
|
if (ts.isIdentifier(error)) {
|
|
@@ -878,6 +1004,10 @@ var DropWalk = class {
|
|
|
878
1004
|
withOk(state, symbol, isOk) {
|
|
879
1005
|
const narrowed = new Map(state);
|
|
880
1006
|
const current = state.get(symbol);
|
|
1007
|
+
if (this.stateFound) {
|
|
1008
|
+
if (current && !isOk) narrowed.set(symbol, { kinds: NOTHING, err: false });
|
|
1009
|
+
return narrowed;
|
|
1010
|
+
}
|
|
881
1011
|
if (current) narrowed.set(symbol, isOk ? { kinds: NOTHING, err: false } : { kinds: current.kinds, err: true });
|
|
882
1012
|
return narrowed;
|
|
883
1013
|
}
|
|
@@ -888,7 +1018,7 @@ var DropWalk = class {
|
|
|
888
1018
|
if (current) {
|
|
889
1019
|
const rest = new Set(current.kinds);
|
|
890
1020
|
rest.delete(kind);
|
|
891
|
-
narrowed.set(symbol, isKind ? { kinds: NOTHING, err: true } : { kinds: rest, err: true });
|
|
1021
|
+
narrowed.set(symbol, isKind ? { kinds: NOTHING, err: true } : { kinds: rest, err: this.stateFound ? current.err || rest.size < current.kinds.size : true });
|
|
892
1022
|
}
|
|
893
1023
|
return narrowed;
|
|
894
1024
|
}
|
|
@@ -1010,6 +1140,10 @@ var DropWalk = class {
|
|
|
1010
1140
|
* (`if (!a.ok) return a;` also covers a `b` not checked yet: the first failure goes up).
|
|
1011
1141
|
*/
|
|
1012
1142
|
checkReturn(at, expression, state) {
|
|
1143
|
+
if (this.stateFound) {
|
|
1144
|
+
for (const [symbol, value] of state) if (value.kinds.size) this.reportVariable("return", at, symbol, value.kinds);
|
|
1145
|
+
return;
|
|
1146
|
+
}
|
|
1013
1147
|
const ts = this.ts;
|
|
1014
1148
|
const passes = expression !== void 0 && this.kindsOf(expression) !== null;
|
|
1015
1149
|
const referenced = /* @__PURE__ */ new Set();
|
|
@@ -1174,6 +1308,16 @@ function droppedResultsIn(sourceFile, context) {
|
|
|
1174
1308
|
ts.forEachChild(sourceFile, visit);
|
|
1175
1309
|
return found.sort((a, b) => a.start - b.start);
|
|
1176
1310
|
}
|
|
1311
|
+
function stateFindingsIn(sourceFile, context) {
|
|
1312
|
+
const found = [];
|
|
1313
|
+
new DropWalk(context, sourceFile, [], found).run();
|
|
1314
|
+
const visit = (node) => {
|
|
1315
|
+
if (isOwner(context.ts, node)) new DropWalk(context, node, [], found).run();
|
|
1316
|
+
context.ts.forEachChild(node, visit);
|
|
1317
|
+
};
|
|
1318
|
+
context.ts.forEachChild(sourceFile, visit);
|
|
1319
|
+
return found.sort((a, b) => a.start - b.start);
|
|
1320
|
+
}
|
|
1177
1321
|
|
|
1178
1322
|
// src/core/boundaries.ts
|
|
1179
1323
|
function boundaryContext(ts, checker, program, policy, config) {
|
|
@@ -1218,7 +1362,19 @@ function verdictOfType(context, type) {
|
|
|
1218
1362
|
if (awaited.flags & ts.TypeFlags.Any) return { kind: "unproven", type: "any" };
|
|
1219
1363
|
if (awaited.flags & ts.TypeFlags.Unknown) return { kind: "unproven", type: "unknown" };
|
|
1220
1364
|
const kinds = failureKinds(context.kinds, type);
|
|
1221
|
-
|
|
1365
|
+
if (kinds) return { kind: "result", kinds: [...kinds].sort() };
|
|
1366
|
+
if (isRuleEnabled(context.config, "trusted-state")) {
|
|
1367
|
+
const members2 = (value) => value.isUnion() ? value.types : [value];
|
|
1368
|
+
for (const member of members2(awaited)) {
|
|
1369
|
+
if (context.policy.resultVariant(member) !== null) return { kind: "result", kinds: [] };
|
|
1370
|
+
if (member.flags & (ts.TypeFlags.StringLike | ts.TypeFlags.Any)) continue;
|
|
1371
|
+
const element = checker.getIndexTypeOfType(member, ts.IndexKind.Number);
|
|
1372
|
+
if (element && members2(element).some((value) => context.policy.resultVariant(value) !== null)) {
|
|
1373
|
+
return { kind: "result", kinds: [] };
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
}
|
|
1377
|
+
return { kind: "plain" };
|
|
1222
1378
|
}
|
|
1223
1379
|
function verdictOfSignatures(context, signatures) {
|
|
1224
1380
|
for (const signature of signatures) {
|
|
@@ -1366,7 +1522,7 @@ function boundaryFindingsIn(sourceFile, context, strict) {
|
|
|
1366
1522
|
if (boundary.verdict.kind === "plain" || item.exclude.some((entry) => excludeMatches(entry, boundary))) continue;
|
|
1367
1523
|
const member = boundaryName(boundary);
|
|
1368
1524
|
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 } });
|
|
1525
|
+
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
1526
|
} else {
|
|
1371
1527
|
findings.push({ origin: "unproven-return", start: boundary.start, file: null, severity: strict ? "error" : "warning", data: { member, type: boundary.verdict.type, item: label } });
|
|
1372
1528
|
}
|
|
@@ -1667,6 +1823,29 @@ function acceptDirectiveFindings(ts, sourceFile, context) {
|
|
|
1667
1823
|
return found;
|
|
1668
1824
|
}
|
|
1669
1825
|
|
|
1826
|
+
// src/core/finally-returns.ts
|
|
1827
|
+
function isScopeBoundary(ts, node) {
|
|
1828
|
+
return isFunctionBoundary(ts, node) || ts.isClassLike(node) || ts.isClassStaticBlockDeclaration(node);
|
|
1829
|
+
}
|
|
1830
|
+
function finallyReturnsIn(ts, sourceFile) {
|
|
1831
|
+
const found = [];
|
|
1832
|
+
const visit = (node, inFinally) => {
|
|
1833
|
+
if (inFinally && ts.isReturnStatement(node)) {
|
|
1834
|
+
found.push({ start: node.getStart(sourceFile) });
|
|
1835
|
+
}
|
|
1836
|
+
if (ts.isTryStatement(node)) {
|
|
1837
|
+
visit(node.tryBlock, inFinally);
|
|
1838
|
+
if (node.catchClause) visit(node.catchClause, inFinally);
|
|
1839
|
+
if (node.finallyBlock) visit(node.finallyBlock, true);
|
|
1840
|
+
return;
|
|
1841
|
+
}
|
|
1842
|
+
const nextInFinally = isScopeBoundary(ts, node) ? false : inFinally;
|
|
1843
|
+
ts.forEachChild(node, (child) => visit(child, nextInFinally));
|
|
1844
|
+
};
|
|
1845
|
+
visit(sourceFile, false);
|
|
1846
|
+
return found;
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1670
1849
|
// src/core/redundant-guards.ts
|
|
1671
1850
|
var ORIGIN_LIMIT = 3;
|
|
1672
1851
|
function redundantGuardData(guard) {
|
|
@@ -1832,6 +2011,215 @@ function redundantGuardsIn(sourceFile, context) {
|
|
|
1832
2011
|
return found;
|
|
1833
2012
|
}
|
|
1834
2013
|
|
|
2014
|
+
// src/core/trusted-state.ts
|
|
2015
|
+
function trustedStateContext(ts, checker, policy) {
|
|
2016
|
+
return { ts, checker, policy };
|
|
2017
|
+
}
|
|
2018
|
+
function trustedStateMessage(finding) {
|
|
2019
|
+
return fillMessage(TRUSTED_STATE_MESSAGES[finding.origin], finding.data);
|
|
2020
|
+
}
|
|
2021
|
+
function oneType(type, ts) {
|
|
2022
|
+
if (!type.isUnion()) return true;
|
|
2023
|
+
const members2 = type.types;
|
|
2024
|
+
if (members2.some((m) => m.flags & (ts.TypeFlags.Null | ts.TypeFlags.Undefined))) return false;
|
|
2025
|
+
if (members2.every((m) => m.flags & ts.TypeFlags.BooleanLiteral)) return true;
|
|
2026
|
+
const enums = members2.map((m) => m.getSymbol()?.declarations?.find(ts.isEnumMember)?.parent);
|
|
2027
|
+
if (enums[0] && enums.every((e) => e === enums[0])) return true;
|
|
2028
|
+
if (members2.some((m) => m.flags & ts.TypeFlags.EnumLiteral)) return false;
|
|
2029
|
+
return members2.every((m) => m.isStringLiteral()) || members2.every((m) => m.isNumberLiteral());
|
|
2030
|
+
}
|
|
2031
|
+
function trustedStatesIn(sourceFile, context) {
|
|
2032
|
+
const { ts, checker, policy } = context;
|
|
2033
|
+
const findings = [];
|
|
2034
|
+
const covered = /* @__PURE__ */ new Set();
|
|
2035
|
+
const erasedArguments = /* @__PURE__ */ new Map();
|
|
2036
|
+
if (isTrustPackageFile(sourceFile.fileName)) return findings;
|
|
2037
|
+
const report = (node, origin, data = {}, warning = false) => {
|
|
2038
|
+
findings.push({ start: node.getStart(sourceFile), origin, data, ...warning ? { severity: "warning" } : {} });
|
|
2039
|
+
};
|
|
2040
|
+
const inspect = (type, node, extra = []) => {
|
|
2041
|
+
const members2 = [...new Set([type, ...extra].flatMap((t) => t.isUnion() ? t.types : [t]))];
|
|
2042
|
+
const oks = members2.filter((m) => policy.resultVariant(m) === "ok");
|
|
2043
|
+
const stateless = oks.filter((m) => policy.okStates(m) === null);
|
|
2044
|
+
const stated = oks.filter((m) => policy.okStates(m) !== null);
|
|
2045
|
+
if (stateless.length && stated.length) report(node, "mixed-success");
|
|
2046
|
+
if (!stated.length && (stateless.length > 1 || stateless.some((m) => {
|
|
2047
|
+
const value = m.getProperty("value");
|
|
2048
|
+
return value && !oneType(checker.getTypeOfSymbol(value), ts);
|
|
2049
|
+
}))) report(node, "stateless-union", { type: stateless.map((m) => {
|
|
2050
|
+
const value = m.getProperty("value");
|
|
2051
|
+
return checker.typeToString(checker.getTypeOfSymbol(value));
|
|
2052
|
+
}).join(" | ") });
|
|
2053
|
+
const states = /* @__PURE__ */ new Set();
|
|
2054
|
+
for (const member of stated) {
|
|
2055
|
+
const names = policy.okStates(member);
|
|
2056
|
+
if (names === void 0) report(node, "non-literal-state", { type: checker.typeToString(checker.getTypeOfSymbol(member.getProperty("state"))) });
|
|
2057
|
+
else if (names) {
|
|
2058
|
+
for (const name of names) states.add(name);
|
|
2059
|
+
const value = member.getProperty("value");
|
|
2060
|
+
const valueType = value && checker.getTypeOfSymbol(value);
|
|
2061
|
+
if (valueType && !oneType(valueType, ts)) for (const state of names) {
|
|
2062
|
+
report(node, "state-union", { state, type: checker.typeToString(valueType) });
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
return { stated: stated.length > 0, states };
|
|
2067
|
+
};
|
|
2068
|
+
const awaited = (type) => checker.getAwaitedType(type) ?? type;
|
|
2069
|
+
const stateInfo = (type) => {
|
|
2070
|
+
const members2 = type.isUnion() ? type.types : [type];
|
|
2071
|
+
const stated = members2.filter((m) => policy.resultVariant(m) === "ok" && policy.okStates(m) !== null);
|
|
2072
|
+
return { stated: stated.length > 0, states: [...new Set(stated.flatMap((m) => policy.okStates(m) ?? []))].sort() };
|
|
2073
|
+
};
|
|
2074
|
+
const subjectOf2 = (node) => {
|
|
2075
|
+
const text = node.getText(sourceFile).replace(/\s+/g, " ").trim();
|
|
2076
|
+
return text.length > 60 ? `${text.slice(0, 59)}\u2026` : text;
|
|
2077
|
+
};
|
|
2078
|
+
const formatStates = (states) => {
|
|
2079
|
+
const quoted = states.map((s) => `'${s}'`);
|
|
2080
|
+
return quoted.length < 2 ? quoted.join("") : `${quoted.slice(0, -1).join(", ")} or ${quoted[quoted.length - 1]}`;
|
|
2081
|
+
};
|
|
2082
|
+
const valueRead = (receiver, name, type = checker.getTypeAtLocation(receiver)) => {
|
|
2083
|
+
const access = name.parent;
|
|
2084
|
+
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;
|
|
2085
|
+
const info = stateInfo(type);
|
|
2086
|
+
if (info.states.length > 1) {
|
|
2087
|
+
covered.add(receiver.getStart(sourceFile));
|
|
2088
|
+
report(name, "unnarrowed-value", { subject: subjectOf2(receiver), states: formatStates(info.states) });
|
|
2089
|
+
}
|
|
2090
|
+
};
|
|
2091
|
+
const erasure = (expr, target) => {
|
|
2092
|
+
target = awaited(target);
|
|
2093
|
+
if (!policy.isResultType(target) || stateInfo(target).stated) return;
|
|
2094
|
+
const info = stateInfo(awaited(checker.getTypeAtLocation(expr)));
|
|
2095
|
+
if (info.stated) {
|
|
2096
|
+
report(expr, "erased-state", { subject: subjectOf2(expr), states: formatStates(info.states) });
|
|
2097
|
+
const argument = unparen(ts, expr);
|
|
2098
|
+
if (ts.isCallExpression(expr.parent) && expr.parent.arguments.includes(expr) && ts.isIdentifier(argument)) {
|
|
2099
|
+
const symbol = checker.getSymbolAtLocation(argument);
|
|
2100
|
+
const statement = expr.parent.parent;
|
|
2101
|
+
if (symbol && ts.isExpressionStatement(statement) && ts.isBlock(statement.parent)) {
|
|
2102
|
+
const entries = erasedArguments.get(symbol) ?? [];
|
|
2103
|
+
entries.push({ after: statement.end, exit: statement.parent.end - 1 });
|
|
2104
|
+
erasedArguments.set(symbol, entries);
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
};
|
|
2109
|
+
const visit = (node) => {
|
|
2110
|
+
if (ts.isPropertyAccessExpression(node) && node.name.text === "value") valueRead(node.expression, node.name);
|
|
2111
|
+
if (ts.isElementAccessExpression(node) && ts.isStringLiteral(node.argumentExpression) && node.argumentExpression.text === "value") valueRead(node.expression, node.argumentExpression);
|
|
2112
|
+
if ((ts.isVariableDeclaration(node) || ts.isParameter(node)) && ts.isObjectBindingPattern(node.name)) {
|
|
2113
|
+
const receiver = node.initializer ?? node.name;
|
|
2114
|
+
for (const element of node.name.elements) {
|
|
2115
|
+
const name = element.propertyName ?? element.name;
|
|
2116
|
+
if (!element.dotDotDotToken && (ts.isIdentifier(name) || ts.isStringLiteral(name)) && name.text === "value") valueRead(receiver, name);
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
if ((ts.isVariableDeclaration(node) || ts.isPropertyDeclaration(node)) && node.type && node.initializer) erasure(node.initializer, checker.getTypeFromTypeNode(node.type));
|
|
2120
|
+
if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken) erasure(node.right, checker.getTypeAtLocation(node.left));
|
|
2121
|
+
if (ts.isReturnStatement(node) && node.expression) {
|
|
2122
|
+
let parent = node.parent;
|
|
2123
|
+
while (parent && !ts.isFunctionLike(parent)) parent = parent.parent;
|
|
2124
|
+
if (parent && ts.isFunctionLike(parent) && parent.type) erasure(node.expression, checker.getTypeFromTypeNode(parent.type));
|
|
2125
|
+
}
|
|
2126
|
+
if (ts.isArrowFunction(node) && node.type && !ts.isBlock(node.body)) erasure(node.body, checker.getTypeFromTypeNode(node.type));
|
|
2127
|
+
if (ts.isCallExpression(node)) {
|
|
2128
|
+
const resolved = policy.calleeDeclaration(node);
|
|
2129
|
+
const file = resolved?.declaration.getSourceFile().fileName;
|
|
2130
|
+
const signature = checker.getResolvedSignature(node);
|
|
2131
|
+
if (file && !isTrustPackageFile(file) && policy.zones.zoneOf(file) === "checked" && signature) {
|
|
2132
|
+
node.arguments.forEach((arg, i) => {
|
|
2133
|
+
const parameter = signature.parameters[i] ?? signature.parameters[signature.parameters.length - 1];
|
|
2134
|
+
if (!parameter) return;
|
|
2135
|
+
let type = checker.getTypeOfSymbolAtLocation(parameter, node);
|
|
2136
|
+
const declaration = parameter.valueDeclaration;
|
|
2137
|
+
if (declaration && ts.isParameter(declaration) && declaration.dotDotDotToken) type = checker.getIndexTypeOfType(type, ts.IndexKind.Number) ?? type;
|
|
2138
|
+
erasure(arg, type);
|
|
2139
|
+
});
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
if (ts.isFunctionLike(node) && "body" in node && node.body) {
|
|
2143
|
+
const fn = node;
|
|
2144
|
+
const signature = checker.getSignatureFromDeclaration(fn);
|
|
2145
|
+
if (signature) {
|
|
2146
|
+
const returned = checker.getReturnTypeOfSignature(signature);
|
|
2147
|
+
const position = fn.type ?? fn.name ?? fn;
|
|
2148
|
+
const extra = [];
|
|
2149
|
+
const returns = (child) => {
|
|
2150
|
+
if (ts.isFunctionLike(child)) return;
|
|
2151
|
+
if (ts.isReturnStatement(child) && child.expression) collect(child.expression);
|
|
2152
|
+
ts.forEachChild(child, returns);
|
|
2153
|
+
};
|
|
2154
|
+
const collect = (expr) => {
|
|
2155
|
+
if (ts.isConditionalExpression(expr)) {
|
|
2156
|
+
collect(expr.whenTrue);
|
|
2157
|
+
collect(expr.whenFalse);
|
|
2158
|
+
} else {
|
|
2159
|
+
const t = checker.getTypeAtLocation(expr);
|
|
2160
|
+
extra.push(checker.getAwaitedType(t) ?? t);
|
|
2161
|
+
}
|
|
2162
|
+
};
|
|
2163
|
+
if (!fn.type) {
|
|
2164
|
+
if (ts.isBlock(fn.body)) returns(fn.body);
|
|
2165
|
+
else collect(fn.body);
|
|
2166
|
+
}
|
|
2167
|
+
const awaited2 = checker.getAwaitedType(returned) ?? returned;
|
|
2168
|
+
const signatureMembers = awaited2.isUnion() ? awaited2.types : [awaited2];
|
|
2169
|
+
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));
|
|
2170
|
+
const result = inspect(awaited2, position, lostState ? extra : []);
|
|
2171
|
+
let callsState = false;
|
|
2172
|
+
const calls = (child) => {
|
|
2173
|
+
if (ts.isFunctionLike(child)) return;
|
|
2174
|
+
if (ts.isCallExpression(child)) {
|
|
2175
|
+
const name = policy.trustFunctionName(child);
|
|
2176
|
+
if (name === "state") callsState = true;
|
|
2177
|
+
if (name === "ok" && result.stated) report(child, "mixed-success");
|
|
2178
|
+
}
|
|
2179
|
+
ts.forEachChild(child, calls);
|
|
2180
|
+
};
|
|
2181
|
+
calls(fn.body);
|
|
2182
|
+
if (result.states.size === 1 && callsState) report(position, "single-state", { state: [...result.states][0] }, true);
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
if ((ts.isVariableDeclaration(node) || ts.isParameter(node) || ts.isPropertyDeclaration(node) || ts.isPropertySignature(node)) && node.type) {
|
|
2186
|
+
inspect(checker.getTypeFromTypeNode(node.type), node.type);
|
|
2187
|
+
}
|
|
2188
|
+
ts.forEachChild(node, visit);
|
|
2189
|
+
};
|
|
2190
|
+
visit(sourceFile);
|
|
2191
|
+
for (const flow of stateFindingsIn(sourceFile, droppedResultContext(ts, checker, policy))) {
|
|
2192
|
+
if (covered.has(flow.start) && (flow.origin === "unchecked-state" || flow.origin === "unnarrowed-value")) continue;
|
|
2193
|
+
if (flow.origin === "unchecked-state" && erasedArguments.size) {
|
|
2194
|
+
let at = sourceFile;
|
|
2195
|
+
const locate = (node) => {
|
|
2196
|
+
if (node.getStart(sourceFile) <= flow.start && flow.start < node.end) {
|
|
2197
|
+
at = node;
|
|
2198
|
+
ts.forEachChild(node, locate);
|
|
2199
|
+
}
|
|
2200
|
+
};
|
|
2201
|
+
locate(sourceFile);
|
|
2202
|
+
const symbol = checker.getSymbolsInScope(at, ts.SymbolFlags.Variable).find((s) => s.name === flow.subject);
|
|
2203
|
+
if (symbol && erasedArguments.get(symbol)?.some(({ after, exit }) => {
|
|
2204
|
+
if (flow.start < after || flow.start > exit) return false;
|
|
2205
|
+
let reassigned = false;
|
|
2206
|
+
const writes = (node) => {
|
|
2207
|
+
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;
|
|
2208
|
+
ts.forEachChild(node, writes);
|
|
2209
|
+
};
|
|
2210
|
+
writes(sourceFile);
|
|
2211
|
+
return !reassigned;
|
|
2212
|
+
})) continue;
|
|
2213
|
+
}
|
|
2214
|
+
findings.push({ start: flow.start, origin: flow.origin, data: {
|
|
2215
|
+
subject: flow.subject,
|
|
2216
|
+
states: formatStates([...flow.states].sort()),
|
|
2217
|
+
callee: flow.callee ?? ""
|
|
2218
|
+
} });
|
|
2219
|
+
}
|
|
2220
|
+
return findings.sort((a, b) => a.start - b.start);
|
|
2221
|
+
}
|
|
2222
|
+
|
|
1835
2223
|
// src/core/type-text.ts
|
|
1836
2224
|
var OPEN = /* @__PURE__ */ new Set(["(", "[", "{", "<"]);
|
|
1837
2225
|
var CLOSE = { ")": "(", "]": "[", "}": "{", ">": "<" };
|
|
@@ -3225,6 +3613,12 @@ function checkSourceFileWithAccepted(options) {
|
|
|
3225
3613
|
findings.push(strict ? finding : { ...finding, severity: "warning" });
|
|
3226
3614
|
}
|
|
3227
3615
|
}
|
|
3616
|
+
if (isRuleEnabled(config, TRUSTED_STATE_RULE)) {
|
|
3617
|
+
for (const found of trustedStatesIn(sourceFile, trustedStateContext(ts, checker, policy))) {
|
|
3618
|
+
const finding = findingAt(found.start, TRUSTED_STATE_RULE, found.origin, trustedStateMessage(found));
|
|
3619
|
+
findings.push(found.severity ? { ...finding, severity: found.severity } : finding);
|
|
3620
|
+
}
|
|
3621
|
+
}
|
|
3228
3622
|
if (isRuleEnabled(config, DROPPED_RESULT_RULE)) {
|
|
3229
3623
|
for (const dropped of droppedResultsIn(sourceFile, droppedResultContext(ts, checker, policy))) {
|
|
3230
3624
|
reportAt(dropped.start, DROPPED_RESULT_RULE, dropped.origin, droppedResultMessage(dropped));
|
|
@@ -3235,6 +3629,11 @@ function checkSourceFileWithAccepted(options) {
|
|
|
3235
3629
|
reportAt(guard.start, REDUNDANT_GUARD_RULE, REDUNDANT_GUARD_ORIGIN, redundantGuardMessage(guard));
|
|
3236
3630
|
}
|
|
3237
3631
|
}
|
|
3632
|
+
if (isRuleEnabled(config, FINALLY_RETURN_RULE)) {
|
|
3633
|
+
for (const found of finallyReturnsIn(ts, sourceFile)) {
|
|
3634
|
+
reportAt(found.start, FINALLY_RETURN_RULE, FINALLY_RETURN_ORIGIN, FINALLY_RETURN_MESSAGES.return);
|
|
3635
|
+
}
|
|
3636
|
+
}
|
|
3238
3637
|
if (config.boundaries.length > 0) {
|
|
3239
3638
|
for (const found of boundaryFindingsIn(sourceFile, boundaryContext(ts, checker, options.program, policy, config), strict)) {
|
|
3240
3639
|
const finding = findingAt(found.start, BOUNDARY_RULE, found.origin, boundaryMessage(found));
|
|
@@ -3272,8 +3671,11 @@ export {
|
|
|
3272
3671
|
untrustedCallProblem,
|
|
3273
3672
|
acceptanceOf,
|
|
3274
3673
|
acceptDirectiveFindings,
|
|
3674
|
+
finallyReturnsIn,
|
|
3275
3675
|
redundantGuardData,
|
|
3276
3676
|
redundantGuardsIn,
|
|
3677
|
+
trustedStateContext,
|
|
3678
|
+
trustedStatesIn,
|
|
3277
3679
|
claimSpan,
|
|
3278
3680
|
typeClaimContext,
|
|
3279
3681
|
typeClaimMessage,
|
|
@@ -3295,4 +3697,4 @@ export {
|
|
|
3295
3697
|
checkSourceFile,
|
|
3296
3698
|
checkSourceFileWithAccepted
|
|
3297
3699
|
};
|
|
3298
|
-
//# sourceMappingURL=chunk-
|
|
3700
|
+
//# sourceMappingURL=chunk-35ETSKEC.js.map
|