@stateward/core 1.2.0 → 1.2.2
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/dist/backend/typescript6.d.ts +2 -0
- package/dist/backend/typescript6.js +2463 -89
- package/dist/cache.d.ts +17 -3
- package/dist/cache.js +74 -17
- package/dist/checker.d.ts +5 -0
- package/dist/checker.js +22 -2
- package/dist/config.js +51 -1
- package/dist/engine.d.ts +19 -1
- package/dist/engine.js +180 -25
- package/dist/fixes.js +5 -3
- package/dist/guarantees.d.ts +63 -11
- package/dist/guarantees.js +151 -21
- package/dist/index.d.ts +3 -1
- package/dist/index.js +4 -0
- package/dist/internal.d.ts +2 -1
- package/dist/internal.js +1 -0
- package/dist/ledger.d.ts +77 -0
- package/dist/ledger.js +423 -0
- package/dist/release.js +2 -0
- package/dist/safe-calls.d.ts +26 -0
- package/dist/safe-calls.js +345 -0
- package/dist/stated.d.ts +37 -0
- package/dist/stated.js +123 -0
- package/dist/testing-tsc.d.ts +16 -0
- package/dist/testing-tsc.js +86 -0
- package/dist/testing.d.ts +2 -1
- package/dist/testing.js +1 -1
- package/dist/timing.d.ts +7 -0
- package/dist/timing.js +6 -1
- package/dist/types.d.ts +395 -3
- package/package.json +1 -1
|
@@ -759,6 +759,20 @@ const PRIMITIVE_FLAGS = ts.TypeFlags.StringLike |
|
|
|
759
759
|
ts.TypeFlags.Null |
|
|
760
760
|
ts.TypeFlags.Undefined |
|
|
761
761
|
ts.TypeFlags.Void;
|
|
762
|
+
/** An instantiated generic type: `Array<T>`, `Map<K, V>`, a tuple. */
|
|
763
|
+
function isTypeReference(type) {
|
|
764
|
+
return ((type.flags & ts.TypeFlags.Object) !== 0 &&
|
|
765
|
+
"objectFlags" in type &&
|
|
766
|
+
typeof type.objectFlags === "number" &&
|
|
767
|
+
(type.objectFlags & ts.ObjectFlags.Reference) !== 0);
|
|
768
|
+
}
|
|
769
|
+
/** An object type written in place (`{ id: Id }`), not a class or an interface. */
|
|
770
|
+
function isAnonymousObject(type) {
|
|
771
|
+
return ((type.flags & ts.TypeFlags.Object) !== 0 &&
|
|
772
|
+
"objectFlags" in type &&
|
|
773
|
+
typeof type.objectFlags === "number" &&
|
|
774
|
+
(type.objectFlags & ts.ObjectFlags.Anonymous) !== 0);
|
|
775
|
+
}
|
|
762
776
|
function widened(checker, type) {
|
|
763
777
|
return type.isTypeParameter() ? checker.getBaseConstraintOfType(type) : type;
|
|
764
778
|
}
|
|
@@ -823,6 +837,128 @@ function declaresMarkerProperty(file, marker) {
|
|
|
823
837
|
visit(file);
|
|
824
838
|
return found;
|
|
825
839
|
}
|
|
840
|
+
/** Whether a property name is a marker's: `"__guarantee:<name>"`, or the 1.1.0 `__guarantee`. */
|
|
841
|
+
function isMarkerName(name) {
|
|
842
|
+
if (!(ts.isStringLiteral(name) || ts.isIdentifier(name)))
|
|
843
|
+
return false;
|
|
844
|
+
return name.text === GUARANTEE_PROPERTY || name.text.startsWith(GUARANTEE_PREFIX);
|
|
845
|
+
}
|
|
846
|
+
/** Whether `file` writes any marker property in a type or class (a quick text filter first). */
|
|
847
|
+
function declaresAnyMarker(file) {
|
|
848
|
+
if (!file.text.includes(GUARANTEE_PROPERTY))
|
|
849
|
+
return false;
|
|
850
|
+
let found = false;
|
|
851
|
+
const visit = (node) => {
|
|
852
|
+
if (found)
|
|
853
|
+
return;
|
|
854
|
+
if ((ts.isPropertySignature(node) || ts.isPropertyDeclaration(node)) &&
|
|
855
|
+
isMarkerName(node.name)) {
|
|
856
|
+
found = true;
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
ts.forEachChild(node, visit);
|
|
860
|
+
};
|
|
861
|
+
visit(file);
|
|
862
|
+
return found;
|
|
863
|
+
}
|
|
864
|
+
/** Whether a written type defines a marker itself (`T & { "__guarantee:owned"?: true }`). */
|
|
865
|
+
function definesMarker(type) {
|
|
866
|
+
let found = false;
|
|
867
|
+
const visit = (node) => {
|
|
868
|
+
if (found)
|
|
869
|
+
return;
|
|
870
|
+
if (ts.isPropertySignature(node) && isMarkerName(node.name)) {
|
|
871
|
+
found = true;
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
ts.forEachChild(node, visit);
|
|
875
|
+
};
|
|
876
|
+
visit(type);
|
|
877
|
+
return found;
|
|
878
|
+
}
|
|
879
|
+
/**
|
|
880
|
+
* The declarations of a file whose written type states a guarantee, in source order
|
|
881
|
+
* (`TypeQueries.statedDeclarationsIn`). A function's return type is not one: what it states
|
|
882
|
+
* (`Release<…>`) reaches the code through each call, which a rule reads there.
|
|
883
|
+
*/
|
|
884
|
+
function statedDeclarationsOf(checker, sourceFile) {
|
|
885
|
+
const found = [];
|
|
886
|
+
const guaranteesOfNode = (type) => definesMarker(type) ? [] : guaranteesOfType(checker, checker.getTypeFromTypeNode(type));
|
|
887
|
+
/** The first reference in the annotation that states a guarantee: where the marker is written. */
|
|
888
|
+
const markerReference = (type) => {
|
|
889
|
+
let found;
|
|
890
|
+
const visit = (node) => {
|
|
891
|
+
if (found !== undefined)
|
|
892
|
+
return;
|
|
893
|
+
if (ts.isTypeReferenceNode(node) &&
|
|
894
|
+
ts.isIdentifier(node.typeName) &&
|
|
895
|
+
guaranteesOfNode(node).length > 0) {
|
|
896
|
+
found = node;
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
ts.forEachChild(node, visit);
|
|
900
|
+
};
|
|
901
|
+
visit(type);
|
|
902
|
+
return found;
|
|
903
|
+
};
|
|
904
|
+
/**
|
|
905
|
+
* Whether a reference is to a type alias that states the guarantee itself (`type OrderStatus =
|
|
906
|
+
* OpenUnion<…>`): the alias is the declaration that states it, and each use of it is a use.
|
|
907
|
+
*/
|
|
908
|
+
const refersToStatedAlias = (reference) => {
|
|
909
|
+
const found = checker.getSymbolAtLocation(reference.typeName);
|
|
910
|
+
const symbol = found !== undefined && found.flags & ts.SymbolFlags.Alias
|
|
911
|
+
? checker.getAliasedSymbol(found)
|
|
912
|
+
: found;
|
|
913
|
+
return (symbol?.declarations?.some((declaration) => ts.isTypeAliasDeclaration(declaration) &&
|
|
914
|
+
declaration.typeParameters === undefined &&
|
|
915
|
+
guaranteesOfNode(declaration.type).length > 0) === true);
|
|
916
|
+
};
|
|
917
|
+
const add = (node, type, name) => {
|
|
918
|
+
// `o: typeof settings` names the type of a stated value; it states nothing of its own.
|
|
919
|
+
if (type === undefined || name === undefined || ts.isTypeQueryNode(type))
|
|
920
|
+
return;
|
|
921
|
+
const names = guaranteesOfNode(type);
|
|
922
|
+
if (names.length === 0)
|
|
923
|
+
return;
|
|
924
|
+
const reference = markerReference(type);
|
|
925
|
+
if (reference !== undefined && refersToStatedAlias(reference))
|
|
926
|
+
return;
|
|
927
|
+
found.push({
|
|
928
|
+
file: sourceFile.fileName,
|
|
929
|
+
start: node.getStart(sourceFile),
|
|
930
|
+
headEnd: type.end,
|
|
931
|
+
end: node.end,
|
|
932
|
+
name: name.getText(sourceFile),
|
|
933
|
+
marker: reference === undefined ? (names[0] ?? "") : reference.typeName.getText(sourceFile),
|
|
934
|
+
guarantees: names,
|
|
935
|
+
});
|
|
936
|
+
};
|
|
937
|
+
const hasBody = (node) => (ts.isFunctionDeclaration(node) ||
|
|
938
|
+
ts.isMethodDeclaration(node) ||
|
|
939
|
+
ts.isArrowFunction(node) ||
|
|
940
|
+
ts.isFunctionExpression(node) ||
|
|
941
|
+
ts.isConstructorDeclaration(node)) &&
|
|
942
|
+
node.body !== undefined;
|
|
943
|
+
const visit = (node) => {
|
|
944
|
+
if (ts.isVariableDeclaration(node)) {
|
|
945
|
+
add(node, node.type, ts.isIdentifier(node.name) ? node.name : undefined);
|
|
946
|
+
}
|
|
947
|
+
else if (ts.isPropertyDeclaration(node) || ts.isPropertySignature(node)) {
|
|
948
|
+
add(node, node.type, node.name);
|
|
949
|
+
}
|
|
950
|
+
else if (ts.isParameter(node)) {
|
|
951
|
+
if (ts.isIdentifier(node.name) && hasBody(node.parent))
|
|
952
|
+
add(node, node.type, node.name);
|
|
953
|
+
}
|
|
954
|
+
else if (ts.isTypeAliasDeclaration(node) && node.typeParameters === undefined) {
|
|
955
|
+
add(node, node.type, node.name);
|
|
956
|
+
}
|
|
957
|
+
ts.forEachChild(node, visit);
|
|
958
|
+
};
|
|
959
|
+
visit(sourceFile);
|
|
960
|
+
return found;
|
|
961
|
+
}
|
|
826
962
|
/** Adds the `as` names a file's import and export specifiers give: `onSnapshot` → `listen`. */
|
|
827
963
|
function addRenamed(file, renamed) {
|
|
828
964
|
const add = (specifier) => {
|
|
@@ -1397,6 +1533,37 @@ function isReadonlyChain(checker, node) {
|
|
|
1397
1533
|
}
|
|
1398
1534
|
return segments > 0;
|
|
1399
1535
|
}
|
|
1536
|
+
/** See `TypeQueries.propIsDeeplyReadonly`: no part of a value of this type can be changed. */
|
|
1537
|
+
function isDeeplyReadonly(checker, type, depth) {
|
|
1538
|
+
if (depth > 6)
|
|
1539
|
+
return false;
|
|
1540
|
+
const resolved = widened(checker, type);
|
|
1541
|
+
if (resolved === undefined)
|
|
1542
|
+
return false;
|
|
1543
|
+
if (resolved.flags & (PRIMITIVE_FLAGS | ts.TypeFlags.Never | ts.TypeFlags.ESSymbolLike)) {
|
|
1544
|
+
return true;
|
|
1545
|
+
}
|
|
1546
|
+
if (resolved.isUnion() || resolved.isIntersection()) {
|
|
1547
|
+
return resolved.types.every((part) => isDeeplyReadonly(checker, part, depth + 1));
|
|
1548
|
+
}
|
|
1549
|
+
if (!(resolved.flags & ts.TypeFlags.Object))
|
|
1550
|
+
return false;
|
|
1551
|
+
if (checker.getSignaturesOfType(resolved, ts.SignatureKind.Call).length > 0)
|
|
1552
|
+
return false;
|
|
1553
|
+
if (checker.getSignaturesOfType(resolved, ts.SignatureKind.Construct).length > 0)
|
|
1554
|
+
return false;
|
|
1555
|
+
const indexes = checker.getIndexInfosOfType(resolved);
|
|
1556
|
+
if (resolved.getSymbol()?.getName() === "ReadonlyArray") {
|
|
1557
|
+
return (indexes.length > 0 &&
|
|
1558
|
+
indexes.every((info) => info.isReadonly && isDeeplyReadonly(checker, info.type, depth + 1)));
|
|
1559
|
+
}
|
|
1560
|
+
return (indexes.every((info) => info.isReadonly && isDeeplyReadonly(checker, info.type, depth + 1)) &&
|
|
1561
|
+
checker
|
|
1562
|
+
.getPropertiesOfType(resolved)
|
|
1563
|
+
.every((property) => !(property.flags & ts.SymbolFlags.Method) &&
|
|
1564
|
+
isReadonlyProperty(resolved, property) &&
|
|
1565
|
+
isDeeplyReadonly(checker, checker.getTypeOfSymbol(property), depth + 1)));
|
|
1566
|
+
}
|
|
1400
1567
|
/** An object type, or a part of one, that a callee could write to or call a method of. */
|
|
1401
1568
|
function isWritableObject(checker, type) {
|
|
1402
1569
|
const resolved = widened(checker, type);
|
|
@@ -1499,104 +1666,2074 @@ export function idFileNames(root) {
|
|
|
1499
1666
|
return name;
|
|
1500
1667
|
};
|
|
1501
1668
|
}
|
|
1669
|
+
/** The type questions the flow scan asks in one file before it stops (`TypeQueries.typedFlowsIn`). */
|
|
1670
|
+
export const FLOW_BUDGET = 200_000;
|
|
1671
|
+
/** How deep a type is searched for a stated alias: `Promise<Order>` → `Order` → `id: OrderId`. */
|
|
1672
|
+
const FLOW_DEPTH = 3;
|
|
1673
|
+
/** The words of a reference's text: one line, at most this long. */
|
|
1674
|
+
const REFERENCE_TEXT_MAX = 60;
|
|
1675
|
+
/** A JSDoc tag as one line, without the end of the comment: `@throws {AErr}`. */
|
|
1676
|
+
function tagText(tag, sourceFile) {
|
|
1677
|
+
const [first = ""] = sourceFile.text.slice(tag.getStart(sourceFile), tag.end).split("\n");
|
|
1678
|
+
const text = first
|
|
1679
|
+
.replace(/\*\/.*$/, "")
|
|
1680
|
+
.replace(/\s+/g, " ")
|
|
1681
|
+
.trim();
|
|
1682
|
+
return text.length <= REFERENCE_TEXT_MAX ? text : `${text.slice(0, REFERENCE_TEXT_MAX - 1)}…`;
|
|
1683
|
+
}
|
|
1684
|
+
function shortText(node, sourceFile) {
|
|
1685
|
+
const text = node.getText(sourceFile).replace(/\s+/g, " ").trim();
|
|
1686
|
+
return text.length <= REFERENCE_TEXT_MAX ? text : `${text.slice(0, REFERENCE_TEXT_MAX - 1)}…`;
|
|
1687
|
+
}
|
|
1688
|
+
function isWrapperExpression(node) {
|
|
1689
|
+
return (ts.isParenthesizedExpression(node) ||
|
|
1690
|
+
ts.isAsExpression(node) ||
|
|
1691
|
+
ts.isSatisfiesExpression(node) ||
|
|
1692
|
+
ts.isNonNullExpression(node) ||
|
|
1693
|
+
ts.isTypeAssertionExpression(node));
|
|
1694
|
+
}
|
|
1695
|
+
function isAssignmentOperator(kind) {
|
|
1696
|
+
return kind >= ts.SyntaxKind.FirstAssignment && kind <= ts.SyntaxKind.LastAssignment;
|
|
1697
|
+
}
|
|
1698
|
+
/** Whether `node` sits in a type (`typeof x` in an annotation, `x` in `implements x`). */
|
|
1699
|
+
function inTypePosition(node) {
|
|
1700
|
+
for (let at = node; !ts.isSourceFile(at.parent); at = at.parent) {
|
|
1701
|
+
const parent = at.parent;
|
|
1702
|
+
if (ts.isExpressionWithTypeArguments(parent)) {
|
|
1703
|
+
const clause = parent.parent;
|
|
1704
|
+
return ts.isHeritageClause(clause) && clause.token === ts.SyntaxKind.ImplementsKeyword;
|
|
1705
|
+
}
|
|
1706
|
+
if (ts.isTypeNode(parent))
|
|
1707
|
+
return true;
|
|
1708
|
+
if (ts.isStatement(parent) || ts.isExpression(parent))
|
|
1709
|
+
return false;
|
|
1710
|
+
}
|
|
1711
|
+
return false;
|
|
1712
|
+
}
|
|
1502
1713
|
/**
|
|
1503
|
-
*
|
|
1504
|
-
*
|
|
1714
|
+
* The form of a reference at `node`, and the node whose text shows it. A reference that names a
|
|
1715
|
+
* member (`a.watch`) or an element key (`a["watch"]`) is the access around it.
|
|
1505
1716
|
*/
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1717
|
+
function referenceForm(node) {
|
|
1718
|
+
const parent = node.parent;
|
|
1719
|
+
if (inTypePosition(node))
|
|
1720
|
+
return { form: "type", shown: node };
|
|
1721
|
+
if (ts.isImportSpecifier(parent) ||
|
|
1722
|
+
ts.isImportClause(parent) ||
|
|
1723
|
+
ts.isNamespaceImport(parent) ||
|
|
1724
|
+
ts.isImportEqualsDeclaration(parent)) {
|
|
1725
|
+
return { form: "import", shown: parent };
|
|
1726
|
+
}
|
|
1727
|
+
if (ts.isExportSpecifier(parent))
|
|
1728
|
+
return { form: "export", shown: parent };
|
|
1729
|
+
if (ts.isJsxAttribute(parent) && parent.name === node) {
|
|
1730
|
+
return { form: "jsx-attribute", shown: parent };
|
|
1731
|
+
}
|
|
1732
|
+
if (ts.isBindingElement(parent))
|
|
1733
|
+
return { form: "alias", shown: parent };
|
|
1734
|
+
if ((ts.isPropertyAssignment(parent) || ts.isMethodDeclaration(parent)) && parent.name === node) {
|
|
1735
|
+
return { form: "write", shown: parent };
|
|
1736
|
+
}
|
|
1737
|
+
if (ts.isShorthandPropertyAssignment(parent) && parent.name === node) {
|
|
1738
|
+
return { form: "stored", shown: parent.parent };
|
|
1739
|
+
}
|
|
1740
|
+
let outer = node;
|
|
1741
|
+
if ((ts.isPropertyAccessExpression(parent) && parent.name === node) ||
|
|
1742
|
+
(ts.isElementAccessExpression(parent) && parent.argumentExpression === node)) {
|
|
1743
|
+
outer = parent;
|
|
1744
|
+
}
|
|
1745
|
+
while (isWrapperExpression(outer.parent))
|
|
1746
|
+
outer = outer.parent;
|
|
1747
|
+
const at = outer.parent;
|
|
1748
|
+
if (ts.isJsxOpeningElement(at) || ts.isJsxSelfClosingElement(at) || ts.isJsxClosingElement(at)) {
|
|
1749
|
+
return { form: "jsx-tag", shown: at };
|
|
1750
|
+
}
|
|
1751
|
+
if (ts.isExportAssignment(at))
|
|
1752
|
+
return { form: "export", shown: at };
|
|
1753
|
+
if (((ts.isCallExpression(at) || ts.isNewExpression(at) || ts.isDecorator(at)) &&
|
|
1754
|
+
at.expression === outer) ||
|
|
1755
|
+
(ts.isTaggedTemplateExpression(at) && at.tag === outer)) {
|
|
1756
|
+
return { form: "call", shown: at };
|
|
1757
|
+
}
|
|
1758
|
+
if ((ts.isCallExpression(at) || ts.isNewExpression(at)) && at.expression !== outer) {
|
|
1759
|
+
return { form: "argument", shown: at };
|
|
1760
|
+
}
|
|
1761
|
+
if (ts.isSpreadElement(at) && (ts.isCallExpression(at.parent) || ts.isNewExpression(at.parent))) {
|
|
1762
|
+
return { form: "argument", shown: at.parent };
|
|
1763
|
+
}
|
|
1764
|
+
if (ts.isJsxExpression(at) || ts.isJsxSpreadAttribute(at)) {
|
|
1765
|
+
return { form: "argument", shown: at.parent };
|
|
1766
|
+
}
|
|
1767
|
+
if ((ts.isBinaryExpression(at) &&
|
|
1768
|
+
isAssignmentOperator(at.operatorToken.kind) &&
|
|
1769
|
+
at.left === outer) ||
|
|
1770
|
+
((ts.isPrefixUnaryExpression(at) || ts.isPostfixUnaryExpression(at)) &&
|
|
1771
|
+
(at.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
1772
|
+
at.operator === ts.SyntaxKind.MinusMinusToken)) ||
|
|
1773
|
+
ts.isDeleteExpression(at) ||
|
|
1774
|
+
((ts.isForOfStatement(at) || ts.isForInStatement(at)) && at.initializer === outer)) {
|
|
1775
|
+
return { form: "write", shown: at };
|
|
1776
|
+
}
|
|
1777
|
+
if ((ts.isVariableDeclaration(at) ||
|
|
1778
|
+
ts.isPropertyDeclaration(at) ||
|
|
1779
|
+
ts.isParameter(at) ||
|
|
1780
|
+
ts.isBindingElement(at)) &&
|
|
1781
|
+
at.initializer === outer) {
|
|
1782
|
+
return { form: "alias", shown: at };
|
|
1783
|
+
}
|
|
1784
|
+
if (ts.isBinaryExpression(at) &&
|
|
1785
|
+
at.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
1786
|
+
at.right === outer) {
|
|
1787
|
+
return { form: "alias", shown: at };
|
|
1788
|
+
}
|
|
1789
|
+
if (ts.isArrayLiteralExpression(at) || ts.isSpreadAssignment(at)) {
|
|
1790
|
+
return { form: "stored", shown: ts.isSpreadAssignment(at) ? at.parent : at };
|
|
1791
|
+
}
|
|
1792
|
+
if (ts.isSpreadElement(at) && ts.isArrayLiteralExpression(at.parent)) {
|
|
1793
|
+
return { form: "stored", shown: at.parent };
|
|
1794
|
+
}
|
|
1795
|
+
if (ts.isPropertyAssignment(at) && at.initializer === outer) {
|
|
1796
|
+
return { form: "stored", shown: at.parent };
|
|
1797
|
+
}
|
|
1798
|
+
if (ts.isReturnStatement(at) ||
|
|
1799
|
+
ts.isYieldExpression(at) ||
|
|
1800
|
+
(ts.isArrowFunction(at) && at.body === outer)) {
|
|
1801
|
+
return { form: "returned", shown: at };
|
|
1802
|
+
}
|
|
1803
|
+
if (ts.isComputedPropertyName(at) ||
|
|
1804
|
+
(ts.isElementAccessExpression(at) && at.argumentExpression === outer)) {
|
|
1805
|
+
return { form: "computed", shown: at.parent };
|
|
1806
|
+
}
|
|
1807
|
+
if (ts.isConditionalExpression(at) && at.condition !== outer
|
|
1808
|
+
? true
|
|
1809
|
+
: (ts.isBinaryExpression(at) &&
|
|
1810
|
+
(at.operatorToken.kind === ts.SyntaxKind.QuestionQuestionToken ||
|
|
1811
|
+
at.operatorToken.kind === ts.SyntaxKind.BarBarToken ||
|
|
1812
|
+
at.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken ||
|
|
1813
|
+
at.operatorToken.kind === ts.SyntaxKind.CommaToken)) ||
|
|
1814
|
+
(ts.isForOfStatement(at) && at.expression === outer)) {
|
|
1815
|
+
return { form: "other", shown: at };
|
|
1816
|
+
}
|
|
1817
|
+
if (ts.isPropertyAccessExpression(at) ||
|
|
1818
|
+
ts.isElementAccessExpression(at) ||
|
|
1819
|
+
ts.isConditionalExpression(at) ||
|
|
1820
|
+
ts.isBinaryExpression(at) ||
|
|
1821
|
+
ts.isPrefixUnaryExpression(at) ||
|
|
1822
|
+
ts.isPostfixUnaryExpression(at) ||
|
|
1823
|
+
ts.isTypeOfExpression(at) ||
|
|
1824
|
+
ts.isVoidExpression(at) ||
|
|
1825
|
+
ts.isAwaitExpression(at) ||
|
|
1826
|
+
ts.isTemplateSpan(at) ||
|
|
1827
|
+
ts.isExpressionStatement(at) ||
|
|
1828
|
+
ts.isIfStatement(at) ||
|
|
1829
|
+
ts.isWhileStatement(at) ||
|
|
1830
|
+
ts.isDoStatement(at) ||
|
|
1831
|
+
ts.isSwitchStatement(at) ||
|
|
1832
|
+
ts.isCaseClause(at) ||
|
|
1833
|
+
ts.isForInStatement(at) ||
|
|
1834
|
+
ts.isForStatement(at)) {
|
|
1835
|
+
return { form: "read", shown: at };
|
|
1836
|
+
}
|
|
1837
|
+
return { form: "other", shown: at };
|
|
1838
|
+
}
|
|
1839
|
+
/**
|
|
1840
|
+
* The reference ledger's scan of one program: which declarations state a trait (`statedOf`, by
|
|
1841
|
+
* symbol, memoised), and every reference to them in a file (`referencesIn`).
|
|
1842
|
+
*/
|
|
1843
|
+
function createLedgerScan(program, typeChecker, isOwn, refOfSymbol) {
|
|
1844
|
+
const scans = new Map();
|
|
1845
|
+
const scanFor = (proves) => {
|
|
1846
|
+
const sorted = (list) => [...new Set(list ?? [])].sort();
|
|
1847
|
+
const traits = sorted(proves.traits);
|
|
1848
|
+
const returns = sorted(proves.returns);
|
|
1849
|
+
const made = sorted(proves.made);
|
|
1850
|
+
const holders = sorted(proves.holders);
|
|
1851
|
+
const produced = sorted(proves.flows?.produced);
|
|
1852
|
+
const decided = sorted(proves.flows?.decided);
|
|
1853
|
+
const escaped = sorted(proves.flows?.escaped);
|
|
1854
|
+
const tags = sorted(proves.tags);
|
|
1855
|
+
const key = JSON.stringify([traits, returns, made, holders, produced, decided, escaped, tags]);
|
|
1856
|
+
let scan = scans.get(key);
|
|
1857
|
+
if (scan === undefined) {
|
|
1858
|
+
scan = {
|
|
1859
|
+
traits: new Set(traits),
|
|
1860
|
+
returns: new Set(returns),
|
|
1861
|
+
made: new Set(made),
|
|
1862
|
+
holders: new Set(holders),
|
|
1863
|
+
produced: new Set(produced),
|
|
1864
|
+
decided: new Set(decided),
|
|
1865
|
+
escaped: new Set(escaped),
|
|
1866
|
+
tags: new Set(tags),
|
|
1867
|
+
tagged: new Map(),
|
|
1868
|
+
bySymbol: new Map(),
|
|
1869
|
+
names: undefined,
|
|
1870
|
+
targets: undefined,
|
|
1871
|
+
};
|
|
1872
|
+
scans.set(key, scan);
|
|
1873
|
+
}
|
|
1874
|
+
return scan;
|
|
1875
|
+
};
|
|
1876
|
+
/** The traits a type alias states for the flow scan: those of `produced` and `decided`. */
|
|
1877
|
+
const aliasTraits = (scan) => new Set([...scan.produced, ...scan.decided]);
|
|
1878
|
+
const writtenTraits = (type, wanted) => {
|
|
1879
|
+
if (definesMarker(type))
|
|
1880
|
+
return [];
|
|
1881
|
+
const checker = typeChecker();
|
|
1882
|
+
return guaranteesOfType(checker, checker.getTypeFromTypeNode(type)).filter((one) => wanted.has(one));
|
|
1883
|
+
};
|
|
1884
|
+
/** A rest parameter states a trait by its elements: `...stops: Release<…>[]`. */
|
|
1885
|
+
const restTraits = (type, wanted) => {
|
|
1886
|
+
const checker = typeChecker();
|
|
1887
|
+
const element = checker.getIndexTypeOfType(checker.getTypeFromTypeNode(type), ts.IndexKind.Number);
|
|
1888
|
+
return element === undefined
|
|
1889
|
+
? []
|
|
1890
|
+
: guaranteesOfType(checker, element).filter((one) => wanted.has(one));
|
|
1891
|
+
};
|
|
1892
|
+
/** What a function states by its written return type, or the promise it returns. */
|
|
1893
|
+
const returnTraits = (fn, wanted) => {
|
|
1894
|
+
if (fn.type === undefined || wanted.size === 0 || definesMarker(fn.type))
|
|
1895
|
+
return [];
|
|
1896
|
+
const checker = typeChecker();
|
|
1897
|
+
const type = checker.getTypeFromTypeNode(fn.type);
|
|
1898
|
+
const direct = guaranteesOfType(checker, type);
|
|
1899
|
+
const awaited = checker.getAwaitedType(type);
|
|
1900
|
+
const all = awaited === undefined || awaited === type
|
|
1901
|
+
? direct
|
|
1902
|
+
: [...direct, ...guaranteesOfType(checker, awaited)];
|
|
1903
|
+
return [...new Set(all)].filter((one) => wanted.has(one)).sort();
|
|
1904
|
+
};
|
|
1905
|
+
/** The marker as the annotation writes it: the first reference in it that states a trait. */
|
|
1906
|
+
const markerOf = (type, fallback) => {
|
|
1907
|
+
if (type === undefined)
|
|
1908
|
+
return fallback;
|
|
1909
|
+
const checker = typeChecker();
|
|
1910
|
+
let found;
|
|
1911
|
+
const visit = (node) => {
|
|
1912
|
+
if (found !== undefined)
|
|
1913
|
+
return;
|
|
1914
|
+
if (ts.isTypeReferenceNode(node) &&
|
|
1915
|
+
guaranteesOfType(checker, checker.getTypeFromTypeNode(node)).length > 0) {
|
|
1916
|
+
found = node.typeName.getText();
|
|
1917
|
+
return;
|
|
1918
|
+
}
|
|
1919
|
+
ts.forEachChild(node, visit);
|
|
1920
|
+
};
|
|
1921
|
+
visit(type);
|
|
1922
|
+
return found ?? fallback;
|
|
1923
|
+
};
|
|
1924
|
+
const heldFunction = (node) => {
|
|
1925
|
+
if (!(ts.isVariableDeclaration(node) || ts.isPropertyDeclaration(node)) ||
|
|
1926
|
+
node.initializer === undefined) {
|
|
1529
1927
|
return undefined;
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1928
|
+
}
|
|
1929
|
+
let value = node.initializer;
|
|
1930
|
+
while (ts.isParenthesizedExpression(value) ||
|
|
1931
|
+
ts.isAsExpression(value) ||
|
|
1932
|
+
ts.isSatisfiesExpression(value) ||
|
|
1933
|
+
ts.isNonNullExpression(value) ||
|
|
1934
|
+
ts.isTypeAssertionExpression(value)) {
|
|
1935
|
+
value = value.expression;
|
|
1936
|
+
}
|
|
1937
|
+
return ts.isArrowFunction(value) || ts.isFunctionExpression(value) ? value : undefined;
|
|
1533
1938
|
};
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
if (
|
|
1537
|
-
return
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
refsBySymbol.set(symbol, entry);
|
|
1939
|
+
/** Whether `file`'s text writes one of the tags `scan` lists. */
|
|
1940
|
+
const writesTags = (file, scan) => {
|
|
1941
|
+
if (scan.tags.size === 0)
|
|
1942
|
+
return false;
|
|
1943
|
+
let known = scan.tagged.get(file);
|
|
1944
|
+
if (known === undefined) {
|
|
1945
|
+
known = [...scan.tags].some((tag) => file.text.includes(`@${tag}`));
|
|
1946
|
+
scan.tagged.set(file, known);
|
|
1543
1947
|
}
|
|
1544
|
-
return
|
|
1948
|
+
return known;
|
|
1545
1949
|
};
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1950
|
+
/**
|
|
1951
|
+
* The JSDoc tags of `scan` written on a declaration a tag can state a trait of (a function, a
|
|
1952
|
+
* method, a variable, a property or method of a type, a type alias), with the tag nodes; a
|
|
1953
|
+
* variable's tags may sit on the function it holds too.
|
|
1954
|
+
*/
|
|
1955
|
+
const tagsOn = (node, scan) => {
|
|
1956
|
+
const found = new Map();
|
|
1957
|
+
if (!(ts.isFunctionDeclaration(node) ||
|
|
1958
|
+
ts.isMethodDeclaration(node) ||
|
|
1959
|
+
ts.isMethodSignature(node) ||
|
|
1960
|
+
ts.isVariableDeclaration(node) ||
|
|
1961
|
+
ts.isPropertyDeclaration(node) ||
|
|
1962
|
+
ts.isPropertySignature(node) ||
|
|
1963
|
+
ts.isTypeAliasDeclaration(node)) ||
|
|
1964
|
+
!writesTags(node.getSourceFile(), scan)) {
|
|
1965
|
+
return found;
|
|
1966
|
+
}
|
|
1967
|
+
const held = heldFunction(node);
|
|
1968
|
+
for (const at of held === undefined ? [node] : [node, held]) {
|
|
1969
|
+
for (const tag of ts.getJSDocTags(at)) {
|
|
1970
|
+
const name = tag.tagName.text;
|
|
1971
|
+
if (scan.tags.has(name) && !found.has(`@${name}`))
|
|
1972
|
+
found.set(`@${name}`, tag);
|
|
1552
1973
|
}
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1974
|
+
}
|
|
1975
|
+
return found;
|
|
1976
|
+
};
|
|
1977
|
+
/** The value a declaration is made by, through `(…)`, `as`, `satisfies` and `!`. */
|
|
1978
|
+
const madeBy = (node) => {
|
|
1979
|
+
let value = node.initializer;
|
|
1980
|
+
while (value !== undefined &&
|
|
1981
|
+
(ts.isParenthesizedExpression(value) ||
|
|
1982
|
+
ts.isAsExpression(value) ||
|
|
1983
|
+
ts.isSatisfiesExpression(value) ||
|
|
1984
|
+
ts.isNonNullExpression(value) ||
|
|
1985
|
+
ts.isTypeAssertionExpression(value))) {
|
|
1986
|
+
value = value.expression;
|
|
1987
|
+
}
|
|
1988
|
+
return value;
|
|
1989
|
+
};
|
|
1990
|
+
/**
|
|
1991
|
+
* What a module variable with no written type states by the call that makes it (`const
|
|
1992
|
+
* ThemeContext = requiredContext<Theme>()`), of the traits `wanted` lists.
|
|
1993
|
+
*/
|
|
1994
|
+
const madeTraits = (node, wanted) => {
|
|
1995
|
+
if (wanted.size === 0 || node.type !== undefined || !ts.isIdentifier(node.name))
|
|
1996
|
+
return [];
|
|
1997
|
+
// Only a module's own variables: a variable inside a function is asked no type question.
|
|
1998
|
+
const statement = node.parent.parent;
|
|
1999
|
+
if (!ts.isVariableStatement(statement) || !ts.isSourceFile(statement.parent))
|
|
2000
|
+
return [];
|
|
2001
|
+
const value = madeBy(node);
|
|
2002
|
+
if (value === undefined || !ts.isCallExpression(value))
|
|
2003
|
+
return [];
|
|
2004
|
+
const checker = typeChecker();
|
|
2005
|
+
return guaranteesOfType(checker, checker.getTypeAtLocation(node.name)).filter((one) => wanted.has(one));
|
|
2006
|
+
};
|
|
2007
|
+
/** The types written on a variable: its annotation, and the type arguments of the call that makes it. */
|
|
2008
|
+
const writtenOn = (node) => {
|
|
2009
|
+
if (node.type !== undefined)
|
|
2010
|
+
return [node.type];
|
|
2011
|
+
const value = madeBy(node);
|
|
2012
|
+
return value !== undefined && ts.isCallExpression(value)
|
|
2013
|
+
? [...(value.typeArguments ?? [])]
|
|
2014
|
+
: [];
|
|
2015
|
+
};
|
|
2016
|
+
/** The traits of `wanted` a type holds: itself, a union member, a parameter or a return, three levels down. */
|
|
2017
|
+
const heldTraits = (type, wanted, depth) => {
|
|
2018
|
+
const checker = typeChecker();
|
|
2019
|
+
const found = new Set(guaranteesOfType(checker, type).filter((one) => wanted.has(one)));
|
|
2020
|
+
if (depth < 3) {
|
|
2021
|
+
const parts = type.isUnion() ? type.types : [];
|
|
2022
|
+
for (const part of parts) {
|
|
2023
|
+
for (const one of heldTraits(part, wanted, depth + 1))
|
|
2024
|
+
found.add(one);
|
|
1572
2025
|
}
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
2026
|
+
for (const signature of type.getCallSignatures()) {
|
|
2027
|
+
const types = [
|
|
2028
|
+
...signature.parameters.map((parameter) => checker.getTypeOfSymbol(parameter)),
|
|
2029
|
+
signature.getReturnType(),
|
|
2030
|
+
];
|
|
2031
|
+
for (const one of types) {
|
|
2032
|
+
for (const trait of heldTraits(one, wanted, depth + 1))
|
|
2033
|
+
found.add(trait);
|
|
2034
|
+
}
|
|
1581
2035
|
}
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
2036
|
+
}
|
|
2037
|
+
return [...found].sort();
|
|
2038
|
+
};
|
|
2039
|
+
/**
|
|
2040
|
+
* An element of an array destructuring (`setSaving` of `const [saving, setSaving] =
|
|
2041
|
+
* useState<Pending<boolean>>(false)`) whose declaration writes a trait `scan.holders` lists, and
|
|
2042
|
+
* whose own type holds it: the variable declaration is the stated declaration, named by the
|
|
2043
|
+
* first element.
|
|
2044
|
+
*/
|
|
2045
|
+
const heldDeclaration = (node, scan) => {
|
|
2046
|
+
const pattern = node.parent;
|
|
2047
|
+
const holder = pattern.parent;
|
|
2048
|
+
if (scan.holders.size === 0 ||
|
|
2049
|
+
!ts.isArrayBindingPattern(pattern) ||
|
|
2050
|
+
!ts.isVariableDeclaration(holder) ||
|
|
2051
|
+
!ts.isIdentifier(node.name) ||
|
|
2052
|
+
node.dotDotDotToken !== undefined) {
|
|
2053
|
+
return undefined;
|
|
2054
|
+
}
|
|
2055
|
+
const written = writtenOn(holder).flatMap((type) => writtenTraits(type, scan.holders));
|
|
2056
|
+
if (written.length === 0)
|
|
2057
|
+
return undefined;
|
|
2058
|
+
const checker = typeChecker();
|
|
2059
|
+
const traits = heldTraits(checker.getTypeAtLocation(node.name), new Set(written), 0);
|
|
2060
|
+
const [first] = pattern.elements;
|
|
2061
|
+
if (traits.length === 0 || first === undefined)
|
|
2062
|
+
return undefined;
|
|
2063
|
+
const file = node.getSourceFile();
|
|
2064
|
+
return {
|
|
2065
|
+
nameNode: node.name,
|
|
2066
|
+
id: `${file.fileName}#${String(holder.getStart(file))}`,
|
|
2067
|
+
// `const [, setSaving] = …`: the setter alone names the state.
|
|
2068
|
+
name: ts.isBindingElement(first) ? first.name.getText(file) : node.name.text,
|
|
2069
|
+
word: node.name.text,
|
|
2070
|
+
marker: markerOf(writtenOn(holder)[0], traits[0] ?? ""),
|
|
2071
|
+
traits,
|
|
2072
|
+
};
|
|
2073
|
+
};
|
|
2074
|
+
/**
|
|
2075
|
+
* Whether a written type names, as its marker, another type alias that states a trait itself
|
|
2076
|
+
* (`type Id = OrderId`): that alias is the stated declaration, as core's scan reads it.
|
|
2077
|
+
*/
|
|
2078
|
+
const aliasOfStatedAlias = (type) => {
|
|
2079
|
+
const checker = typeChecker();
|
|
2080
|
+
let found;
|
|
2081
|
+
const visit = (node) => {
|
|
2082
|
+
if (found !== undefined)
|
|
2083
|
+
return;
|
|
2084
|
+
if (ts.isTypeReferenceNode(node) &&
|
|
2085
|
+
ts.isIdentifier(node.typeName) &&
|
|
2086
|
+
guaranteesOfType(checker, checker.getTypeFromTypeNode(node)).length > 0) {
|
|
2087
|
+
found = node;
|
|
2088
|
+
return;
|
|
1586
2089
|
}
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
2090
|
+
ts.forEachChild(node, visit);
|
|
2091
|
+
};
|
|
2092
|
+
visit(type);
|
|
2093
|
+
if (found === undefined)
|
|
2094
|
+
return false;
|
|
2095
|
+
const symbol = checker.getSymbolAtLocation(found.typeName);
|
|
2096
|
+
const target = symbol === undefined ? undefined : resolved(symbol);
|
|
2097
|
+
return (target?.declarations?.some((declaration) => ts.isTypeAliasDeclaration(declaration) &&
|
|
2098
|
+
declaration.typeParameters === undefined &&
|
|
2099
|
+
!definesMarker(declaration.type) &&
|
|
2100
|
+
guaranteesOfType(checker, checker.getTypeFromTypeNode(declaration.type)).length > 0) === true);
|
|
2101
|
+
};
|
|
2102
|
+
/** The traits a declaration states (of those `scan` lists), with its name and marker. */
|
|
2103
|
+
const statedDeclaration = (node, scan) => {
|
|
2104
|
+
const file = node.getSourceFile();
|
|
2105
|
+
if (!isOwn(file.fileName))
|
|
2106
|
+
return undefined;
|
|
2107
|
+
if (ts.isBindingElement(node))
|
|
2108
|
+
return heldDeclaration(node, scan);
|
|
2109
|
+
let traits = [];
|
|
2110
|
+
let type;
|
|
2111
|
+
let name;
|
|
2112
|
+
if (ts.isVariableDeclaration(node) ||
|
|
2113
|
+
ts.isPropertyDeclaration(node) ||
|
|
2114
|
+
ts.isPropertySignature(node)) {
|
|
2115
|
+
if (!(ts.isIdentifier(node.name) ||
|
|
2116
|
+
ts.isPrivateIdentifier(node.name) ||
|
|
2117
|
+
ts.isStringLiteral(node.name))) {
|
|
2118
|
+
return undefined;
|
|
1591
2119
|
}
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
}
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
2120
|
+
name = node.name;
|
|
2121
|
+
if (node.type !== undefined) {
|
|
2122
|
+
type = node.type;
|
|
2123
|
+
traits = writtenTraits(node.type, scan.traits);
|
|
2124
|
+
}
|
|
2125
|
+
const fn = heldFunction(node);
|
|
2126
|
+
if (fn !== undefined) {
|
|
2127
|
+
const returned = returnTraits(fn, scan.returns);
|
|
2128
|
+
if (returned.length > 0 && type === undefined)
|
|
2129
|
+
type = fn.type;
|
|
2130
|
+
traits = [...traits, ...returned];
|
|
2131
|
+
}
|
|
2132
|
+
if (ts.isVariableDeclaration(node))
|
|
2133
|
+
traits = [...traits, ...madeTraits(node, scan.made)];
|
|
2134
|
+
// `const st = useState<Pending<boolean>>(false)`: the pair kept whole holds the state; its
|
|
2135
|
+
// reads (`st[1](true)`) are references the proof reads or lists as not checked.
|
|
2136
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.type === undefined) {
|
|
2137
|
+
traits = [
|
|
2138
|
+
...traits,
|
|
2139
|
+
...writtenOn(node).flatMap((written) => writtenTraits(written, scan.holders)),
|
|
2140
|
+
];
|
|
2141
|
+
}
|
|
2142
|
+
traits = [...traits, ...tagsOn(node, scan).keys()];
|
|
2143
|
+
}
|
|
2144
|
+
else if (ts.isParameter(node)) {
|
|
2145
|
+
const fn = node.parent;
|
|
2146
|
+
if (!ts.isIdentifier(node.name) || node.type === undefined)
|
|
2147
|
+
return undefined;
|
|
2148
|
+
if (!("body" in fn) || fn.body === undefined)
|
|
2149
|
+
return undefined;
|
|
2150
|
+
name = node.name;
|
|
2151
|
+
type = node.type;
|
|
2152
|
+
// `o: typeof settings` names the type of a stated value; it states nothing of its own.
|
|
2153
|
+
traits = ts.isTypeQueryNode(node.type)
|
|
2154
|
+
? []
|
|
2155
|
+
: node.dotDotDotToken === undefined
|
|
2156
|
+
? writtenTraits(node.type, scan.traits)
|
|
2157
|
+
: restTraits(node.type, scan.traits);
|
|
2158
|
+
}
|
|
2159
|
+
else if ((ts.isFunctionDeclaration(node) || ts.isMethodDeclaration(node)) &&
|
|
2160
|
+
node.name !== undefined) {
|
|
2161
|
+
name = node.name;
|
|
2162
|
+
type = node.type;
|
|
2163
|
+
traits = [...returnTraits(node, scan.returns), ...tagsOn(node, scan).keys()];
|
|
2164
|
+
}
|
|
2165
|
+
else if (ts.isMethodSignature(node) && ts.isIdentifier(node.name)) {
|
|
2166
|
+
name = node.name;
|
|
2167
|
+
traits = [...tagsOn(node, scan).keys()];
|
|
2168
|
+
}
|
|
2169
|
+
else if (ts.isTypeAliasDeclaration(node)) {
|
|
2170
|
+
// `type OrderId = Opaque<string, "OrderId">`: a type a flow trait is stated on. An alias of
|
|
2171
|
+
// such an alias (`type Id = OrderId`) states nothing of its own. A tag may sit on any alias.
|
|
2172
|
+
const wanted = aliasTraits(scan);
|
|
2173
|
+
name = node.name;
|
|
2174
|
+
if (node.typeParameters === undefined && wanted.size > 0 && !aliasOfStatedAlias(node.type)) {
|
|
2175
|
+
type = node.type;
|
|
2176
|
+
traits = writtenTraits(node.type, wanted);
|
|
2177
|
+
}
|
|
2178
|
+
traits = [...traits, ...tagsOn(node, scan).keys()];
|
|
2179
|
+
}
|
|
2180
|
+
if (name === undefined || traits.length === 0)
|
|
2181
|
+
return undefined;
|
|
2182
|
+
// A property is listed at its name (after `static`, `readonly`), anything else where it starts.
|
|
2183
|
+
const start = ts.isPropertyDeclaration(node) || ts.isPropertySignature(node)
|
|
2184
|
+
? name.getStart(file)
|
|
2185
|
+
: node.getStart(file);
|
|
2186
|
+
const sorted = [...new Set(traits)].sort();
|
|
2187
|
+
return {
|
|
2188
|
+
nameNode: name,
|
|
2189
|
+
id: `${file.fileName}#${String(start)}`,
|
|
2190
|
+
name: name.getText(file),
|
|
2191
|
+
word: ts.isIdentifier(name) || ts.isPrivateIdentifier(name) || ts.isStringLiteral(name)
|
|
2192
|
+
? name.text
|
|
2193
|
+
: name.getText(file),
|
|
2194
|
+
marker: markerOf(type, sorted[0] ?? ""),
|
|
2195
|
+
traits: sorted,
|
|
2196
|
+
};
|
|
2197
|
+
};
|
|
2198
|
+
const statedOf = (symbol, scan) => {
|
|
2199
|
+
const known = scan.bySymbol.get(symbol);
|
|
2200
|
+
if (known !== undefined)
|
|
2201
|
+
return known ?? undefined;
|
|
2202
|
+
const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
|
|
2203
|
+
const found = declaration === undefined ? undefined : statedDeclaration(declaration, scan);
|
|
2204
|
+
scan.bySymbol.set(symbol, found ?? null);
|
|
2205
|
+
return found;
|
|
2206
|
+
};
|
|
2207
|
+
/** The names of every stated declaration of the project's files: the prefilter's words. */
|
|
2208
|
+
const namesOf = (scan) => {
|
|
2209
|
+
if (scan.names !== undefined)
|
|
2210
|
+
return scan.names;
|
|
2211
|
+
const names = new Set();
|
|
2212
|
+
const declared = (trait) => program
|
|
2213
|
+
.getSourceFiles()
|
|
2214
|
+
.some((file) => file.text.includes(`${GUARANTEE_PREFIX}${trait}`) &&
|
|
2215
|
+
declaresMarkerProperty(file, `${GUARANTEE_PREFIX}${trait}`));
|
|
2216
|
+
// Values are asked about only when a trait a value states is declared, type aliases only when
|
|
2217
|
+
// a flow trait is: a project that declares only `Opaque` asks nothing of its variables.
|
|
2218
|
+
const values = [...scan.traits, ...scan.returns, ...scan.made, ...scan.holders].some(declared);
|
|
2219
|
+
const aliases = [...aliasTraits(scan)].some(declared);
|
|
2220
|
+
const ownFiles = program.getSourceFiles().filter((file) => isOwn(file.fileName));
|
|
2221
|
+
const tags = ownFiles.some((file) => writesTags(file, scan));
|
|
2222
|
+
if (values || aliases || tags) {
|
|
2223
|
+
for (const file of ownFiles) {
|
|
2224
|
+
const tagged = writesTags(file, scan);
|
|
2225
|
+
if (!values && !aliases && !tagged)
|
|
2226
|
+
continue;
|
|
2227
|
+
const visit = (node) => {
|
|
2228
|
+
if ((tagged && tagsOn(node, scan).size > 0) ||
|
|
2229
|
+
(values &&
|
|
2230
|
+
(ts.isVariableDeclaration(node) ||
|
|
2231
|
+
ts.isPropertyDeclaration(node) ||
|
|
2232
|
+
ts.isPropertySignature(node) ||
|
|
2233
|
+
ts.isParameter(node) ||
|
|
2234
|
+
ts.isFunctionDeclaration(node) ||
|
|
2235
|
+
ts.isMethodDeclaration(node)) &&
|
|
2236
|
+
(node.type !== undefined ||
|
|
2237
|
+
heldFunction(node)?.type !== undefined ||
|
|
2238
|
+
(scan.made.size > 0 &&
|
|
2239
|
+
ts.isVariableDeclaration(node) &&
|
|
2240
|
+
node.initializer !== undefined))) ||
|
|
2241
|
+
(values &&
|
|
2242
|
+
scan.holders.size > 0 &&
|
|
2243
|
+
ts.isBindingElement(node) &&
|
|
2244
|
+
ts.isArrayBindingPattern(node.parent)) ||
|
|
2245
|
+
(aliases && ts.isTypeAliasDeclaration(node))) {
|
|
2246
|
+
const stated = statedDeclaration(node, scan);
|
|
2247
|
+
if (stated !== undefined)
|
|
2248
|
+
names.add(stated.word);
|
|
2249
|
+
}
|
|
2250
|
+
ts.forEachChild(node, visit);
|
|
2251
|
+
};
|
|
2252
|
+
visit(file);
|
|
2253
|
+
}
|
|
2254
|
+
}
|
|
2255
|
+
scan.names = names;
|
|
2256
|
+
return names;
|
|
2257
|
+
};
|
|
2258
|
+
const resolved = (symbol) => symbol.flags & ts.SymbolFlags.Alias ? typeChecker().getAliasedSymbol(symbol) : symbol;
|
|
2259
|
+
/** The symbol a name refers to, as find-references reads it. */
|
|
2260
|
+
const symbolOfReference = (node) => {
|
|
2261
|
+
const checker = typeChecker();
|
|
2262
|
+
const parent = node.parent;
|
|
2263
|
+
if (ts.isShorthandPropertyAssignment(parent) && parent.name === node) {
|
|
2264
|
+
return checker.getShorthandAssignmentValueSymbol(parent);
|
|
2265
|
+
}
|
|
2266
|
+
if (ts.isExportSpecifier(parent)) {
|
|
2267
|
+
return parent.parent.parent.moduleSpecifier === undefined
|
|
2268
|
+
? checker.getExportSpecifierLocalTargetSymbol(parent)
|
|
2269
|
+
: checker.getSymbolAtLocation(node);
|
|
2270
|
+
}
|
|
2271
|
+
if (ts.isBindingElement(parent) &&
|
|
2272
|
+
ts.isObjectBindingPattern(parent.parent) &&
|
|
2273
|
+
(parent.propertyName === node || (parent.propertyName === undefined && parent.name === node))) {
|
|
2274
|
+
const key = parent.propertyName ?? parent.name;
|
|
2275
|
+
if (!ts.isIdentifier(key) && !ts.isStringLiteral(key))
|
|
2276
|
+
return undefined;
|
|
2277
|
+
return checker.getTypeAtLocation(parent.parent).getProperty(key.text);
|
|
2278
|
+
}
|
|
2279
|
+
if (ts.isJsxAttribute(parent) && parent.name === node && ts.isIdentifier(node)) {
|
|
2280
|
+
// `<Search onSearch={post} />`: the prop of the component's props type, as find-references reads it.
|
|
2281
|
+
const contextual = checker.getContextualType(parent.parent);
|
|
2282
|
+
const whole = contextual?.getProperty(node.text);
|
|
2283
|
+
if (whole !== undefined || !contextual?.isUnion()) {
|
|
2284
|
+
return whole ?? checker.getSymbolAtLocation(node);
|
|
2285
|
+
}
|
|
2286
|
+
const found = contextual.types.flatMap((member) => member.getProperty(node.text) ?? []);
|
|
2287
|
+
return found.length === 1 ? found[0] : checker.getSymbolAtLocation(node);
|
|
2288
|
+
}
|
|
2289
|
+
if ((ts.isPropertyAssignment(parent) || ts.isMethodDeclaration(parent)) &&
|
|
2290
|
+
parent.name === node &&
|
|
2291
|
+
ts.isObjectLiteralExpression(parent.parent)) {
|
|
2292
|
+
const contextual = checker.getContextualType(parent.parent);
|
|
2293
|
+
if (contextual === undefined || !ts.isIdentifier(node))
|
|
2294
|
+
return undefined;
|
|
2295
|
+
// `setForm({ ...form, saving: true })`: the setter takes `Form | ((prev: Form) => Form)`, and
|
|
2296
|
+
// the property is `Form`'s, as find-references reads it.
|
|
2297
|
+
const whole = contextual.getProperty(node.text);
|
|
2298
|
+
if (whole !== undefined || !contextual.isUnion())
|
|
2299
|
+
return whole;
|
|
2300
|
+
const found = contextual.types.flatMap((member) => member.getProperty(node.text) ?? []);
|
|
2301
|
+
return found.length === 1 ? found[0] : undefined;
|
|
2302
|
+
}
|
|
2303
|
+
return checker.getSymbolAtLocation(node);
|
|
2304
|
+
};
|
|
2305
|
+
/**
|
|
2306
|
+
* The local names `file` gives stated declarations by an import (`import { a as b }`, a default
|
|
2307
|
+
* import, `import x = …`) or a re-export (`export { default as b } from …`): a reference by such
|
|
2308
|
+
* a name does not read the declaration's name.
|
|
2309
|
+
*/
|
|
2310
|
+
const importedNames = (file, scan) => {
|
|
2311
|
+
const found = new Set();
|
|
2312
|
+
const consider = (name) => {
|
|
2313
|
+
const symbol = typeChecker().getSymbolAtLocation(name);
|
|
2314
|
+
if (symbol === undefined)
|
|
2315
|
+
return;
|
|
2316
|
+
const target = resolved(symbol);
|
|
2317
|
+
for (const root of typeChecker().getRootSymbols(target)) {
|
|
2318
|
+
if (statedOf(root, scan) !== undefined)
|
|
2319
|
+
found.add(name.text);
|
|
2320
|
+
}
|
|
2321
|
+
};
|
|
2322
|
+
for (const statement of file.statements) {
|
|
2323
|
+
if (ts.isImportDeclaration(statement)) {
|
|
2324
|
+
const clause = statement.importClause;
|
|
2325
|
+
if (clause?.name !== undefined)
|
|
2326
|
+
consider(clause.name);
|
|
2327
|
+
const bindings = clause?.namedBindings;
|
|
2328
|
+
if (bindings !== undefined && ts.isNamedImports(bindings)) {
|
|
2329
|
+
for (const element of bindings.elements)
|
|
2330
|
+
consider(element.name);
|
|
2331
|
+
}
|
|
2332
|
+
}
|
|
2333
|
+
else if (ts.isImportEqualsDeclaration(statement)) {
|
|
2334
|
+
consider(statement.name);
|
|
2335
|
+
}
|
|
2336
|
+
else if (ts.isExportDeclaration(statement) &&
|
|
2337
|
+
statement.moduleSpecifier !== undefined &&
|
|
2338
|
+
statement.exportClause !== undefined &&
|
|
2339
|
+
ts.isNamedExports(statement.exportClause)) {
|
|
2340
|
+
// `export { default as dflt } from "./owner.js"`: both names are references.
|
|
2341
|
+
for (const element of statement.exportClause.elements) {
|
|
2342
|
+
if (ts.isIdentifier(element.name))
|
|
2343
|
+
consider(element.name);
|
|
2344
|
+
if (element.propertyName !== undefined && ts.isIdentifier(element.propertyName)) {
|
|
2345
|
+
consider(element.propertyName);
|
|
2346
|
+
}
|
|
2347
|
+
}
|
|
2348
|
+
}
|
|
2349
|
+
}
|
|
2350
|
+
return found;
|
|
2351
|
+
};
|
|
2352
|
+
/** Whether `node` is the name of the declaration it resolves to (not a reference to it). */
|
|
2353
|
+
const isOwnName = (node, stated) => stated.nameNode === node;
|
|
2354
|
+
const referencesIn = (sourceFile, proves) => {
|
|
2355
|
+
const scan = scanFor(proves);
|
|
2356
|
+
if (scan.traits.size === 0 &&
|
|
2357
|
+
scan.returns.size === 0 &&
|
|
2358
|
+
scan.made.size === 0 &&
|
|
2359
|
+
scan.holders.size === 0 &&
|
|
2360
|
+
scan.tags.size === 0 &&
|
|
2361
|
+
aliasTraits(scan).size === 0) {
|
|
2362
|
+
return [];
|
|
2363
|
+
}
|
|
2364
|
+
const names = namesOf(scan);
|
|
2365
|
+
if (names.size === 0)
|
|
2366
|
+
return [];
|
|
2367
|
+
const words = new Set(names);
|
|
2368
|
+
const { text } = sourceFile;
|
|
2369
|
+
const mentions = [...names].some((name) => text.includes(name.replace(/^#/, "")));
|
|
2370
|
+
for (const name of importedNames(sourceFile, scan))
|
|
2371
|
+
words.add(name);
|
|
2372
|
+
if (!mentions && words.size === names.size)
|
|
2373
|
+
return [];
|
|
2374
|
+
const found = [];
|
|
2375
|
+
const add = (node) => {
|
|
2376
|
+
const symbol = symbolOfReference(node);
|
|
2377
|
+
if (symbol === undefined)
|
|
2378
|
+
return;
|
|
2379
|
+
const seen = new Set();
|
|
2380
|
+
for (const root of typeChecker().getRootSymbols(resolved(symbol))) {
|
|
2381
|
+
const stated = statedOf(root, scan);
|
|
2382
|
+
if (stated === undefined || seen.has(stated.id) || isOwnName(node, stated))
|
|
2383
|
+
continue;
|
|
2384
|
+
seen.add(stated.id);
|
|
2385
|
+
const ref = refOfSymbol(root);
|
|
2386
|
+
if (ref === undefined)
|
|
2387
|
+
continue;
|
|
2388
|
+
const { form, shown } = referenceForm(node);
|
|
2389
|
+
for (const trait of stated.traits) {
|
|
2390
|
+
found.push({
|
|
2391
|
+
declaration: stated.id,
|
|
2392
|
+
symbol: ref,
|
|
2393
|
+
name: stated.name,
|
|
2394
|
+
marker: stated.marker,
|
|
2395
|
+
trait,
|
|
2396
|
+
node,
|
|
2397
|
+
form,
|
|
2398
|
+
text: shortText(shown, sourceFile),
|
|
2399
|
+
});
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
};
|
|
2403
|
+
const tagged = writesTags(sourceFile, scan);
|
|
2404
|
+
/** The tags on a declaration of this file: each is a reference that stands for its body. */
|
|
2405
|
+
const addTags = (node) => {
|
|
2406
|
+
const tags = tagsOn(node, scan);
|
|
2407
|
+
if (tags.size === 0)
|
|
2408
|
+
return;
|
|
2409
|
+
const stated = statedDeclaration(node, scan);
|
|
2410
|
+
const symbol = stated === undefined ? undefined : symbolOfReference(stated.nameNode);
|
|
2411
|
+
const ref = symbol === undefined ? undefined : refOfSymbol(symbol);
|
|
2412
|
+
if (stated === undefined || ref === undefined)
|
|
2413
|
+
return;
|
|
2414
|
+
for (const [trait, tag] of tags) {
|
|
2415
|
+
found.push({
|
|
2416
|
+
declaration: stated.id,
|
|
2417
|
+
symbol: ref,
|
|
2418
|
+
name: stated.name,
|
|
2419
|
+
marker: stated.marker,
|
|
2420
|
+
trait,
|
|
2421
|
+
node: tag,
|
|
2422
|
+
form: "tag",
|
|
2423
|
+
text: tagText(tag, sourceFile),
|
|
2424
|
+
});
|
|
2425
|
+
}
|
|
2426
|
+
};
|
|
2427
|
+
const visit = (node) => {
|
|
2428
|
+
if (tagged)
|
|
2429
|
+
addTags(node);
|
|
2430
|
+
if ((ts.isIdentifier(node) || ts.isPrivateIdentifier(node)) && words.has(node.text)) {
|
|
2431
|
+
add(node);
|
|
2432
|
+
}
|
|
2433
|
+
else if (ts.isStringLiteral(node) &&
|
|
2434
|
+
ts.isElementAccessExpression(node.parent) &&
|
|
2435
|
+
node.parent.argumentExpression === node &&
|
|
2436
|
+
names.has(node.text)) {
|
|
2437
|
+
add(node);
|
|
2438
|
+
}
|
|
2439
|
+
ts.forEachChild(node, visit);
|
|
2440
|
+
};
|
|
2441
|
+
visit(sourceFile);
|
|
2442
|
+
return found.sort((a, b) => a.node.getStart(sourceFile) - b.node.getStart(sourceFile) ||
|
|
2443
|
+
(a.declaration < b.declaration ? -1 : a.declaration > b.declaration ? 1 : 0) ||
|
|
2444
|
+
(a.trait < b.trait ? -1 : a.trait > b.trait ? 1 : 0));
|
|
2445
|
+
};
|
|
2446
|
+
/** The own files each own file imports (and re-exports from, and `import()`s). */
|
|
2447
|
+
const importsOf = (file) => {
|
|
2448
|
+
const checker = typeChecker();
|
|
2449
|
+
const found = [];
|
|
2450
|
+
const add = (specifier) => {
|
|
2451
|
+
if (specifier === undefined || !ts.isStringLiteralLike(specifier))
|
|
2452
|
+
return;
|
|
2453
|
+
const symbol = checker.getSymbolAtLocation(specifier);
|
|
2454
|
+
const declaration = symbol?.valueDeclaration ?? symbol?.declarations?.[0];
|
|
2455
|
+
if (declaration !== undefined && ts.isSourceFile(declaration)) {
|
|
2456
|
+
found.push(declaration.fileName);
|
|
2457
|
+
}
|
|
2458
|
+
};
|
|
2459
|
+
const dynamic = file.text.includes("import(") || file.text.includes("require(");
|
|
2460
|
+
const visit = (node) => {
|
|
2461
|
+
if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node))
|
|
2462
|
+
add(node.moduleSpecifier);
|
|
2463
|
+
else if (ts.isImportEqualsDeclaration(node) &&
|
|
2464
|
+
ts.isExternalModuleReference(node.moduleReference)) {
|
|
2465
|
+
add(node.moduleReference.expression);
|
|
2466
|
+
}
|
|
2467
|
+
else if (ts.isImportTypeNode(node) && ts.isLiteralTypeNode(node.argument)) {
|
|
2468
|
+
add(node.argument.literal);
|
|
2469
|
+
}
|
|
2470
|
+
else if (ts.isCallExpression(node) &&
|
|
2471
|
+
(node.expression.kind === ts.SyntaxKind.ImportKeyword ||
|
|
2472
|
+
(ts.isIdentifier(node.expression) && node.expression.text === "require"))) {
|
|
2473
|
+
add(node.arguments[0]);
|
|
2474
|
+
}
|
|
2475
|
+
if (dynamic ||
|
|
2476
|
+
ts.isSourceFile(node) ||
|
|
2477
|
+
ts.isModuleDeclaration(node) ||
|
|
2478
|
+
ts.isModuleBlock(node)) {
|
|
2479
|
+
ts.forEachChild(node, visit);
|
|
2480
|
+
}
|
|
2481
|
+
};
|
|
2482
|
+
visit(file);
|
|
2483
|
+
return found;
|
|
2484
|
+
};
|
|
2485
|
+
/**
|
|
2486
|
+
* The own files whose imports reach one of `declaring`: the declaring files, and every file that
|
|
2487
|
+
* imports one of the set, until none is added. A declaring file that is not a module (its
|
|
2488
|
+
* types are global) is reached by every file.
|
|
2489
|
+
*/
|
|
2490
|
+
const reachingFiles = (declaring) => {
|
|
2491
|
+
const files = program.getSourceFiles().filter((file) => isOwn(file.fileName));
|
|
2492
|
+
if (files.some((file) => declaring.has(file.fileName) && !ts.isExternalModule(file))) {
|
|
2493
|
+
return "all";
|
|
2494
|
+
}
|
|
2495
|
+
const importers = new Map();
|
|
2496
|
+
for (const file of files) {
|
|
2497
|
+
for (const imported of importsOf(file)) {
|
|
2498
|
+
const list = importers.get(imported) ?? [];
|
|
2499
|
+
list.push(file.fileName);
|
|
2500
|
+
importers.set(imported, list);
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
const reached = new Set(declaring);
|
|
2504
|
+
const queue = [...declaring];
|
|
2505
|
+
for (let next = queue.pop(); next !== undefined; next = queue.pop()) {
|
|
2506
|
+
for (const importer of importers.get(next) ?? []) {
|
|
2507
|
+
if (reached.has(importer))
|
|
2508
|
+
continue;
|
|
2509
|
+
reached.add(importer);
|
|
2510
|
+
queue.push(importer);
|
|
2511
|
+
}
|
|
2512
|
+
}
|
|
2513
|
+
return reached;
|
|
2514
|
+
};
|
|
2515
|
+
/** The stated type aliases and members of `scan`'s flow traits, found once per scan. */
|
|
2516
|
+
const flowTargets = (scan) => {
|
|
2517
|
+
if (scan.targets !== undefined)
|
|
2518
|
+
return scan.targets;
|
|
2519
|
+
const checker = typeChecker();
|
|
2520
|
+
const aliases = [];
|
|
2521
|
+
const bySymbol = new Map();
|
|
2522
|
+
const byMember = new Map();
|
|
2523
|
+
const members = new Map();
|
|
2524
|
+
const alias = aliasTraits(scan);
|
|
2525
|
+
const wanted = [...alias, ...scan.escaped];
|
|
2526
|
+
const declared = wanted.filter((trait) => program
|
|
2527
|
+
.getSourceFiles()
|
|
2528
|
+
.some((file) => file.text.includes(`${GUARANTEE_PREFIX}${trait}`) &&
|
|
2529
|
+
declaresMarkerProperty(file, `${GUARANTEE_PREFIX}${trait}`)));
|
|
2530
|
+
const target = (stated, symbol, file) => {
|
|
2531
|
+
const ref = refOfSymbol(symbol);
|
|
2532
|
+
if (ref === undefined)
|
|
2533
|
+
return undefined;
|
|
2534
|
+
return {
|
|
2535
|
+
stated,
|
|
2536
|
+
symbol,
|
|
2537
|
+
ref,
|
|
2538
|
+
file,
|
|
2539
|
+
produced: stated.traits.filter((trait) => scan.produced.has(trait)),
|
|
2540
|
+
decided: stated.traits.filter((trait) => scan.decided.has(trait)),
|
|
2541
|
+
escaped: stated.traits.filter((trait) => scan.escaped.has(trait)),
|
|
2542
|
+
};
|
|
2543
|
+
};
|
|
2544
|
+
if (declared.length > 0) {
|
|
2545
|
+
for (const file of program.getSourceFiles()) {
|
|
2546
|
+
if (!isOwn(file.fileName))
|
|
2547
|
+
continue;
|
|
2548
|
+
const visit = (node) => {
|
|
2549
|
+
if (ts.isTypeAliasDeclaration(node) && alias.size > 0) {
|
|
2550
|
+
const stated = statedDeclaration(node, scan);
|
|
2551
|
+
const symbol = checker.getSymbolAtLocation(node.name);
|
|
2552
|
+
const found = stated === undefined || symbol === undefined
|
|
2553
|
+
? undefined
|
|
2554
|
+
: target(stated, symbol, file.fileName);
|
|
2555
|
+
if (found !== undefined && (found.produced.length > 0 || found.decided.length > 0)) {
|
|
2556
|
+
aliases.push(found);
|
|
2557
|
+
bySymbol.set(found.symbol, found);
|
|
2558
|
+
const type = checker.getDeclaredTypeOfSymbol(found.symbol);
|
|
2559
|
+
for (const member of type.isUnion() ? type.types : [type]) {
|
|
2560
|
+
if (member.flags & NULLISH)
|
|
2561
|
+
continue;
|
|
2562
|
+
byMember.set(member, [...(byMember.get(member) ?? []), found]);
|
|
2563
|
+
}
|
|
2564
|
+
}
|
|
2565
|
+
}
|
|
2566
|
+
else if (scan.escaped.size > 0 &&
|
|
2567
|
+
(ts.isPropertyDeclaration(node) || ts.isPropertySignature(node)) &&
|
|
2568
|
+
node.type !== undefined) {
|
|
2569
|
+
const stated = statedDeclaration(node, scan);
|
|
2570
|
+
const symbol = checker.getSymbolAtLocation(node.name);
|
|
2571
|
+
const found = stated === undefined || symbol === undefined
|
|
2572
|
+
? undefined
|
|
2573
|
+
: target(stated, symbol, file.fileName);
|
|
2574
|
+
if (found !== undefined && found.escaped.length > 0) {
|
|
2575
|
+
members.set(stated?.word ?? "", [...(members.get(stated?.word ?? "") ?? []), found]);
|
|
2576
|
+
}
|
|
2577
|
+
}
|
|
2578
|
+
ts.forEachChild(node, visit);
|
|
2579
|
+
};
|
|
2580
|
+
visit(file);
|
|
2581
|
+
}
|
|
2582
|
+
}
|
|
2583
|
+
const declaring = new Set([
|
|
2584
|
+
...aliases.map((one) => one.file),
|
|
2585
|
+
...[...members.values()].flat().map((one) => one.file),
|
|
2586
|
+
]);
|
|
2587
|
+
scan.targets = {
|
|
2588
|
+
aliases,
|
|
2589
|
+
bySymbol,
|
|
2590
|
+
byMember,
|
|
2591
|
+
members,
|
|
2592
|
+
reaching: declaring.size === 0 ? new Set() : reachingFiles(declaring),
|
|
2593
|
+
};
|
|
2594
|
+
return scan.targets;
|
|
2595
|
+
};
|
|
2596
|
+
/** Through `(…)`, `!` and a branch of `?:`, `??`, `||`, `&&` or `,`: the values an expression gives. */
|
|
2597
|
+
const leavesOf = (expression) => {
|
|
2598
|
+
if (ts.isParenthesizedExpression(expression) || ts.isNonNullExpression(expression)) {
|
|
2599
|
+
return leavesOf(expression.expression);
|
|
2600
|
+
}
|
|
2601
|
+
if (ts.isConditionalExpression(expression)) {
|
|
2602
|
+
return [...leavesOf(expression.whenTrue), ...leavesOf(expression.whenFalse)];
|
|
2603
|
+
}
|
|
2604
|
+
if (ts.isBinaryExpression(expression)) {
|
|
2605
|
+
const kind = expression.operatorToken.kind;
|
|
2606
|
+
if (kind === ts.SyntaxKind.QuestionQuestionToken ||
|
|
2607
|
+
kind === ts.SyntaxKind.BarBarToken ||
|
|
2608
|
+
kind === ts.SyntaxKind.AmpersandAmpersandToken) {
|
|
2609
|
+
return [...leavesOf(expression.left), ...leavesOf(expression.right)];
|
|
2610
|
+
}
|
|
2611
|
+
if (kind === ts.SyntaxKind.CommaToken)
|
|
2612
|
+
return leavesOf(expression.right);
|
|
2613
|
+
}
|
|
2614
|
+
return [expression];
|
|
2615
|
+
};
|
|
2616
|
+
/** Whether every value of `type` is `undefined`, `null`, `void` or `never`: it builds nothing. */
|
|
2617
|
+
const holdsNothing = (type) => (type.isUnion() ? type.types : [type]).every((member) => (member.flags &
|
|
2618
|
+
(ts.TypeFlags.Undefined | ts.TypeFlags.Null | ts.TypeFlags.Void | ts.TypeFlags.Never)) !==
|
|
2619
|
+
0);
|
|
2620
|
+
/** `Object.assign`, `Reflect.set`: the callee's two words, or undefined. */
|
|
2621
|
+
const calleeWords = (call) => {
|
|
2622
|
+
const callee = call.expression;
|
|
2623
|
+
if (ts.isIdentifier(callee))
|
|
2624
|
+
return callee.text;
|
|
2625
|
+
if (ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.expression)) {
|
|
2626
|
+
return `${callee.expression.text}.${callee.name.text}`;
|
|
2627
|
+
}
|
|
2628
|
+
return undefined;
|
|
2629
|
+
};
|
|
2630
|
+
/** Methods that decide by the value they are given: `list.includes(s)`, `map.get(s)`. */
|
|
2631
|
+
const DECIDING_METHODS = new Set([
|
|
2632
|
+
"includes",
|
|
2633
|
+
"indexOf",
|
|
2634
|
+
"lastIndexOf",
|
|
2635
|
+
"has",
|
|
2636
|
+
"get",
|
|
2637
|
+
]);
|
|
2638
|
+
/** Calls that read a value as a key of an object: a lookup by it. */
|
|
2639
|
+
const KEYED_CALLS = new Set([
|
|
2640
|
+
"Reflect.get",
|
|
2641
|
+
"Reflect.has",
|
|
2642
|
+
"Reflect.getOwnPropertyDescriptor",
|
|
2643
|
+
"Object.hasOwn",
|
|
2644
|
+
]);
|
|
2645
|
+
/** Calls that write a property of their first argument whatever its type says. */
|
|
2646
|
+
const REFLECT_WRITES = new Set([
|
|
2647
|
+
"Object.assign",
|
|
2648
|
+
"Object.defineProperty",
|
|
2649
|
+
"Object.defineProperties",
|
|
2650
|
+
"Object.setPrototypeOf",
|
|
2651
|
+
"Reflect.set",
|
|
2652
|
+
"Reflect.defineProperty",
|
|
2653
|
+
"Reflect.setPrototypeOf",
|
|
2654
|
+
]);
|
|
2655
|
+
const flowsIn = (sourceFile, proves, budget) => {
|
|
2656
|
+
const scan = scanFor(proves);
|
|
2657
|
+
if (aliasTraits(scan).size === 0 && scan.escaped.size === 0)
|
|
2658
|
+
return [];
|
|
2659
|
+
const targets = flowTargets(scan);
|
|
2660
|
+
if (targets.aliases.length === 0 && targets.members.size === 0)
|
|
2661
|
+
return [];
|
|
2662
|
+
if (targets.reaching !== "all" && !targets.reaching.has(sourceFile.fileName))
|
|
2663
|
+
return [];
|
|
2664
|
+
const checker = typeChecker();
|
|
2665
|
+
const found = [];
|
|
2666
|
+
const seen = new Set();
|
|
2667
|
+
let questions = 0;
|
|
2668
|
+
let stopped;
|
|
2669
|
+
const add = (node, shown, flow, kind, via) => {
|
|
2670
|
+
const traits = flow[kind];
|
|
2671
|
+
for (const trait of traits) {
|
|
2672
|
+
const key = `${String(node.getStart(sourceFile))}-${String(node.end)}-${flow.stated.id}-${trait}-${kind}`;
|
|
2673
|
+
if (seen.has(key))
|
|
2674
|
+
continue;
|
|
2675
|
+
seen.add(key);
|
|
2676
|
+
found.push({
|
|
2677
|
+
declaration: flow.stated.id,
|
|
2678
|
+
symbol: flow.ref,
|
|
2679
|
+
name: flow.stated.name,
|
|
2680
|
+
marker: flow.stated.marker,
|
|
2681
|
+
trait,
|
|
2682
|
+
node,
|
|
2683
|
+
form: kind,
|
|
2684
|
+
via,
|
|
2685
|
+
text: shortText(shown, sourceFile),
|
|
2686
|
+
});
|
|
2687
|
+
}
|
|
2688
|
+
};
|
|
2689
|
+
const typeAt = (node) => {
|
|
2690
|
+
questions += 1;
|
|
2691
|
+
return checker.getTypeAtLocation(node);
|
|
2692
|
+
};
|
|
2693
|
+
const contextual = (node) => {
|
|
2694
|
+
questions += 1;
|
|
2695
|
+
return checker.getContextualType(node);
|
|
2696
|
+
};
|
|
2697
|
+
// ---- what a type holds -------------------------------------------------------------------
|
|
2698
|
+
const heldMemo = new Map();
|
|
2699
|
+
/** The stated aliases a type holds: itself, a member, a constraint, three levels in. */
|
|
2700
|
+
const aliasesIn = (type) => {
|
|
2701
|
+
const known = heldMemo.get(type);
|
|
2702
|
+
if (known !== undefined)
|
|
2703
|
+
return known;
|
|
2704
|
+
const into = new Set();
|
|
2705
|
+
const visiting = new Set();
|
|
2706
|
+
const enter = (given, depth) => {
|
|
2707
|
+
if (depth > FLOW_DEPTH || visiting.has(given))
|
|
2708
|
+
return;
|
|
2709
|
+
questions += 1;
|
|
2710
|
+
const named = given.aliasSymbol === undefined ? undefined : targets.bySymbol.get(given.aliasSymbol);
|
|
2711
|
+
if (named !== undefined)
|
|
2712
|
+
into.add(named);
|
|
2713
|
+
for (const one of targets.byMember.get(given) ?? [])
|
|
2714
|
+
into.add(one);
|
|
2715
|
+
if (given.flags & PRIMITIVE_FLAGS)
|
|
2716
|
+
return;
|
|
2717
|
+
visiting.add(given);
|
|
2718
|
+
if (given.flags & ts.TypeFlags.TypeParameter) {
|
|
2719
|
+
const constraint = checker.getBaseConstraintOfType(given);
|
|
2720
|
+
if (constraint !== undefined && constraint !== given)
|
|
2721
|
+
enter(constraint, depth);
|
|
2722
|
+
}
|
|
2723
|
+
else if (given.isUnionOrIntersection()) {
|
|
2724
|
+
for (const member of given.types)
|
|
2725
|
+
enter(member, depth);
|
|
2726
|
+
}
|
|
2727
|
+
else if (depth < FLOW_DEPTH && given.flags & ts.TypeFlags.Object) {
|
|
2728
|
+
if (isTypeReference(given)) {
|
|
2729
|
+
for (const argument of checker.getTypeArguments(given))
|
|
2730
|
+
enter(argument, depth + 1);
|
|
2731
|
+
}
|
|
2732
|
+
for (const signature of given.getCallSignatures()) {
|
|
2733
|
+
enter(signature.getReturnType(), depth + 1);
|
|
2734
|
+
}
|
|
2735
|
+
// `Record<string, OrderId>`: an alias's type arguments, and any index signature.
|
|
2736
|
+
for (const argument of given.aliasTypeArguments ?? [])
|
|
2737
|
+
enter(argument, depth + 1);
|
|
2738
|
+
for (const info of checker.getIndexInfosOfType(given))
|
|
2739
|
+
enter(info.type, depth + 1);
|
|
2740
|
+
if (isOwnObject(given)) {
|
|
2741
|
+
for (const property of checker.getPropertiesOfType(given)) {
|
|
2742
|
+
enter(checker.getTypeOfSymbol(property), depth + 1);
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
}
|
|
2746
|
+
visiting.delete(given);
|
|
2747
|
+
};
|
|
2748
|
+
enter(type, 0);
|
|
2749
|
+
const list = [...into].sort((a, b) => compareIds(a.stated.id, b.stated.id));
|
|
2750
|
+
heldMemo.set(type, list);
|
|
2751
|
+
return list;
|
|
2752
|
+
};
|
|
2753
|
+
/** An object type the scan looks into: written in place, or declared in the project. */
|
|
2754
|
+
const isOwnObject = (type) => {
|
|
2755
|
+
if (isAnonymousObject(type))
|
|
2756
|
+
return true;
|
|
2757
|
+
const declarations = type.getSymbol()?.declarations ?? [];
|
|
2758
|
+
return declarations.some((one) => isOwn(one.getSourceFile().fileName));
|
|
2759
|
+
};
|
|
2760
|
+
/** The stated aliases `type` is itself (a member of): not what it holds inside. */
|
|
2761
|
+
const aliasesOf = (type) => {
|
|
2762
|
+
const named = type.aliasSymbol === undefined ? undefined : targets.bySymbol.get(type.aliasSymbol);
|
|
2763
|
+
if (named !== undefined)
|
|
2764
|
+
return [named];
|
|
2765
|
+
const into = new Set();
|
|
2766
|
+
for (const member of type.isUnion() ? type.types : [type]) {
|
|
2767
|
+
if (member.flags & NULLISH)
|
|
2768
|
+
continue;
|
|
2769
|
+
const own = member.aliasSymbol === undefined ? undefined : targets.bySymbol.get(member.aliasSymbol);
|
|
2770
|
+
if (own !== undefined)
|
|
2771
|
+
into.add(own);
|
|
2772
|
+
for (const one of targets.byMember.get(member) ?? [])
|
|
2773
|
+
into.add(one);
|
|
2774
|
+
}
|
|
2775
|
+
return [...into];
|
|
2776
|
+
};
|
|
2777
|
+
/**
|
|
2778
|
+
* Whether every member of `source` fits a member of `target` that holds no stated alias: a
|
|
2779
|
+
* `number` given as `OrderId | number` builds no `OrderId`.
|
|
2780
|
+
*/
|
|
2781
|
+
const fitsOther = (source, target) => {
|
|
2782
|
+
const others = (target.isUnion() ? target.types : [target]).filter((member) => aliasesIn(member).length === 0);
|
|
2783
|
+
if (others.length === 0)
|
|
2784
|
+
return false;
|
|
2785
|
+
return (source.isUnion() ? source.types : [source]).every((member) => others.some((other) => checker.isTypeAssignableTo(member, other)));
|
|
2786
|
+
};
|
|
2787
|
+
// ---- produced ----------------------------------------------------------------------------
|
|
2788
|
+
/** A value given to a place of type `target`: each of its values whose own type lacks an alias it holds. */
|
|
2789
|
+
const given = (expression, target, via) => {
|
|
2790
|
+
if (target === undefined)
|
|
2791
|
+
return;
|
|
2792
|
+
const wanted = aliasesIn(target).filter((one) => one.produced.length > 0);
|
|
2793
|
+
if (wanted.length === 0)
|
|
2794
|
+
return;
|
|
2795
|
+
for (const leaf of leavesOf(expression)) {
|
|
2796
|
+
// A literal's members are places of their own; a cast is read as one.
|
|
2797
|
+
if (ts.isObjectLiteralExpression(leaf) || ts.isArrayLiteralExpression(leaf))
|
|
2798
|
+
continue;
|
|
2799
|
+
const source = typeAt(leaf);
|
|
2800
|
+
if (holdsNothing(source))
|
|
2801
|
+
continue;
|
|
2802
|
+
const any = (source.flags & ts.TypeFlags.Any) !== 0;
|
|
2803
|
+
const own = any ? [] : aliasesIn(source);
|
|
2804
|
+
if (!any && fitsOther(source, target))
|
|
2805
|
+
continue;
|
|
2806
|
+
const kind = via ?? (any ? "any" : source.getCallSignatures().length > 0 ? "function-type" : "value");
|
|
2807
|
+
for (const one of wanted) {
|
|
2808
|
+
if (!own.includes(one))
|
|
2809
|
+
add(leaf, leaf, one, "produced", kind);
|
|
2810
|
+
}
|
|
2811
|
+
}
|
|
2812
|
+
};
|
|
2813
|
+
/** The return types of the other declarations of an overloaded function, for its implementation's returns. */
|
|
2814
|
+
const overloadTarget = (node) => {
|
|
2815
|
+
let fn = node.parent;
|
|
2816
|
+
while (!ts.isFunctionLike(fn) && !ts.isSourceFile(fn))
|
|
2817
|
+
fn = fn.parent;
|
|
2818
|
+
if (!(ts.isFunctionDeclaration(fn) || ts.isMethodDeclaration(fn)) ||
|
|
2819
|
+
fn.body === undefined ||
|
|
2820
|
+
fn.name === undefined) {
|
|
2821
|
+
return undefined;
|
|
2822
|
+
}
|
|
2823
|
+
const symbol = checker.getSymbolAtLocation(fn.name);
|
|
2824
|
+
const others = (symbol?.declarations ?? []).filter((one) => one !== fn && ts.isFunctionLike(one) && !("body" in one && one.body !== undefined));
|
|
2825
|
+
if (others.length === 0)
|
|
2826
|
+
return undefined;
|
|
2827
|
+
return others.flatMap((one) => {
|
|
2828
|
+
const signature = checker.getSignatureFromDeclaration(one);
|
|
2829
|
+
return signature === undefined ? [] : [checker.getReturnTypeOfSignature(signature)];
|
|
2830
|
+
});
|
|
2831
|
+
};
|
|
2832
|
+
/**
|
|
2833
|
+
* The types a class member takes from the classes and interfaces its class extends or
|
|
2834
|
+
* implements (`override id()` over `id(): OrderId`): a value it gives is given to them too.
|
|
2835
|
+
*/
|
|
2836
|
+
const inheritedTypes = (member) => {
|
|
2837
|
+
const owner = member.parent;
|
|
2838
|
+
if (!ts.isClassLike(owner) || member.name === undefined)
|
|
2839
|
+
return [];
|
|
2840
|
+
const name = ts.isIdentifier(member.name) || ts.isPrivateIdentifier(member.name)
|
|
2841
|
+
? member.name.text
|
|
2842
|
+
: ts.isStringLiteral(member.name)
|
|
2843
|
+
? member.name.text
|
|
2844
|
+
: undefined;
|
|
2845
|
+
if (name === undefined)
|
|
2846
|
+
return [];
|
|
2847
|
+
return (owner.heritageClauses ?? []).flatMap((clause) => clause.types.flatMap((base) => {
|
|
2848
|
+
questions += 1;
|
|
2849
|
+
const property = checker.getPropertyOfType(checker.getTypeAtLocation(base), name);
|
|
2850
|
+
return property === undefined ? [] : [checker.getTypeOfSymbol(property)];
|
|
2851
|
+
}));
|
|
2852
|
+
};
|
|
2853
|
+
/** The return types a method or getter takes from the members it overrides. */
|
|
2854
|
+
const inheritedReturns = (node) => {
|
|
2855
|
+
let fn = node.parent;
|
|
2856
|
+
while (!ts.isFunctionLike(fn) && !ts.isSourceFile(fn))
|
|
2857
|
+
fn = fn.parent;
|
|
2858
|
+
if (!(ts.isMethodDeclaration(fn) || ts.isGetAccessorDeclaration(fn)))
|
|
2859
|
+
return [];
|
|
2860
|
+
const types = inheritedTypes(fn);
|
|
2861
|
+
if (ts.isGetAccessorDeclaration(fn))
|
|
2862
|
+
return types;
|
|
2863
|
+
return types.flatMap((type) => type.getCallSignatures().map((signature) => signature.getReturnType()));
|
|
2864
|
+
};
|
|
2865
|
+
const returned = (expression) => {
|
|
2866
|
+
const overloads = overloadTarget(expression);
|
|
2867
|
+
if (overloads !== undefined) {
|
|
2868
|
+
for (const type of overloads)
|
|
2869
|
+
given(expression, type, "overload");
|
|
2870
|
+
return;
|
|
2871
|
+
}
|
|
2872
|
+
given(expression, contextual(expression));
|
|
2873
|
+
for (const type of inheritedReturns(expression))
|
|
2874
|
+
given(expression, type, "override");
|
|
2875
|
+
};
|
|
2876
|
+
/**
|
|
2877
|
+
* Each property of `source` that the target type (`target`) holds a stated alias in while the
|
|
2878
|
+
* source's own property does not: a spread attribute or property.
|
|
2879
|
+
*/
|
|
2880
|
+
const spreadInto = (node, target, shown) => {
|
|
2881
|
+
if (target === undefined)
|
|
2882
|
+
return;
|
|
2883
|
+
const source = typeAt(node);
|
|
2884
|
+
if (holdsNothing(source))
|
|
2885
|
+
return;
|
|
2886
|
+
const any = (source.flags & ts.TypeFlags.Any) !== 0;
|
|
2887
|
+
for (const property of checker.getPropertiesOfType(source)) {
|
|
2888
|
+
const into = target.isUnion()
|
|
2889
|
+
? target.types.flatMap((member) => checker.getPropertyOfType(member, property.getName()) ?? [])
|
|
2890
|
+
: [checker.getPropertyOfType(target, property.getName())].flatMap((one) => one ?? []);
|
|
2891
|
+
for (const wanted of into) {
|
|
2892
|
+
const held = aliasesIn(checker.getTypeOfSymbol(wanted)).filter((one) => one.produced.length > 0);
|
|
2893
|
+
if (held.length === 0)
|
|
2894
|
+
continue;
|
|
2895
|
+
const own = any ? [] : aliasesIn(checker.getTypeOfSymbol(property));
|
|
2896
|
+
for (const one of held) {
|
|
2897
|
+
if (!own.includes(one))
|
|
2898
|
+
add(node, shown, one, "produced", any ? "any" : "spread");
|
|
2899
|
+
}
|
|
2900
|
+
}
|
|
2901
|
+
}
|
|
2902
|
+
if (any) {
|
|
2903
|
+
for (const one of aliasesIn(target).filter((alias) => alias.produced.length > 0)) {
|
|
2904
|
+
add(node, shown, one, "produced", "any");
|
|
2905
|
+
}
|
|
2906
|
+
}
|
|
2907
|
+
};
|
|
2908
|
+
/** The element type an iterable or array gives, when the checker can say. */
|
|
2909
|
+
const elementOf = (type) => checker.getIndexTypeOfType(type, ts.IndexKind.Number);
|
|
2910
|
+
/** A spread argument: its elements given to the parameters from its index on. */
|
|
2911
|
+
const spreadArgument = (call, spread) => {
|
|
2912
|
+
questions += 1;
|
|
2913
|
+
const signature = checker.getResolvedSignature(call);
|
|
2914
|
+
const index = call.arguments?.indexOf(spread) ?? -1;
|
|
2915
|
+
if (signature === undefined || index < 0)
|
|
2916
|
+
return;
|
|
2917
|
+
const wanted = signature.parameters
|
|
2918
|
+
.slice(index)
|
|
2919
|
+
.flatMap((parameter) => aliasesIn(checker.getTypeOfSymbol(parameter)))
|
|
2920
|
+
.filter((one) => one.produced.length > 0);
|
|
2921
|
+
if (wanted.length === 0)
|
|
2922
|
+
return;
|
|
2923
|
+
const source = typeAt(spread.expression);
|
|
2924
|
+
const any = (source.flags & ts.TypeFlags.Any) !== 0;
|
|
2925
|
+
const own = any ? [] : aliasesIn(source);
|
|
2926
|
+
for (const one of wanted) {
|
|
2927
|
+
if (!own.includes(one))
|
|
2928
|
+
add(spread, call, one, "produced", any ? "any" : "spread");
|
|
2929
|
+
}
|
|
2930
|
+
};
|
|
2931
|
+
/** A call whose type arguments give it a stated alias its arguments do not hold: `getJson<Order>(url)`. */
|
|
2932
|
+
const genericCall = (call) => {
|
|
2933
|
+
questions += 1;
|
|
2934
|
+
const signature = checker.getResolvedSignature(call);
|
|
2935
|
+
const declaration = signature?.getDeclaration();
|
|
2936
|
+
if (signature === undefined || declaration === undefined)
|
|
2937
|
+
return;
|
|
2938
|
+
const generic = checker.getSignatureFromDeclaration(declaration);
|
|
2939
|
+
if (generic?.typeParameters === undefined || generic.typeParameters.length === 0)
|
|
2940
|
+
return;
|
|
2941
|
+
const held = aliasesIn(checker.getReturnTypeOfSignature(signature)).filter((one) => one.produced.length > 0);
|
|
2942
|
+
if (held.length === 0)
|
|
2943
|
+
return;
|
|
2944
|
+
const declared = new Set(aliasesIn(checker.getReturnTypeOfSignature(generic)));
|
|
2945
|
+
// A type argument a parameter takes too (`useState<OrderId | null>(null)`, `new
|
|
2946
|
+
// Map<OrderId, number>()`): its values come in through the arguments, each read where it
|
|
2947
|
+
// is given, so the call makes none of its own. Only for a function of a package or the
|
|
2948
|
+
// standard library: a function of the project could still cast inside its body.
|
|
2949
|
+
if (!isOwn(declaration.getSourceFile().fileName)) {
|
|
2950
|
+
for (const parameter of signature.parameters) {
|
|
2951
|
+
for (const one of aliasesIn(checker.getTypeOfSymbol(parameter)))
|
|
2952
|
+
declared.add(one);
|
|
2953
|
+
}
|
|
2954
|
+
}
|
|
2955
|
+
const fromArguments = new Set();
|
|
2956
|
+
for (const argument of call.arguments ?? []) {
|
|
2957
|
+
const type = typeAt(ts.isSpreadElement(argument) ? argument.expression : argument);
|
|
2958
|
+
for (const one of aliasesIn(type))
|
|
2959
|
+
fromArguments.add(one);
|
|
2960
|
+
}
|
|
2961
|
+
if (ts.isPropertyAccessExpression(call.expression) ||
|
|
2962
|
+
ts.isElementAccessExpression(call.expression)) {
|
|
2963
|
+
for (const one of aliasesIn(typeAt(call.expression.expression)))
|
|
2964
|
+
fromArguments.add(one);
|
|
2965
|
+
}
|
|
2966
|
+
for (const one of held) {
|
|
2967
|
+
if (!declared.has(one) && !fromArguments.has(one)) {
|
|
2968
|
+
add(call, call, one, "produced", "generic-call");
|
|
2969
|
+
}
|
|
2970
|
+
}
|
|
2971
|
+
};
|
|
2972
|
+
/** `.call`, `.apply`, `.bind`, `Reflect.apply`, `Reflect.construct`: arguments no parameter types. */
|
|
2973
|
+
const indirectCall = (call) => {
|
|
2974
|
+
const callee = call.expression;
|
|
2975
|
+
const words = calleeWords(call);
|
|
2976
|
+
let fn;
|
|
2977
|
+
let passed = [];
|
|
2978
|
+
if (words === "Reflect.apply" || words === "Reflect.construct") {
|
|
2979
|
+
fn = call.arguments[0];
|
|
2980
|
+
const list = call.arguments[words === "Reflect.apply" ? 2 : 1];
|
|
2981
|
+
passed = list === undefined ? [] : [list];
|
|
2982
|
+
}
|
|
2983
|
+
else if (ts.isPropertyAccessExpression(callee) &&
|
|
2984
|
+
(callee.name.text === "call" || callee.name.text === "apply" || callee.name.text === "bind")) {
|
|
2985
|
+
fn = callee.expression;
|
|
2986
|
+
passed = call.arguments.slice(1);
|
|
2987
|
+
}
|
|
2988
|
+
if (fn === undefined)
|
|
2989
|
+
return;
|
|
2990
|
+
const type = typeAt(fn);
|
|
2991
|
+
const signatures = [...type.getCallSignatures(), ...type.getConstructSignatures()];
|
|
2992
|
+
if (signatures.length === 0)
|
|
2993
|
+
return;
|
|
2994
|
+
const wanted = signatures
|
|
2995
|
+
.flatMap((signature) => signature.parameters)
|
|
2996
|
+
.flatMap((parameter) => aliasesIn(checker.getTypeOfSymbol(parameter)))
|
|
2997
|
+
.filter((one) => one.produced.length > 0);
|
|
2998
|
+
if (wanted.length === 0)
|
|
2999
|
+
return;
|
|
3000
|
+
const own = new Set();
|
|
3001
|
+
let any = false;
|
|
3002
|
+
for (const argument of passed) {
|
|
3003
|
+
const argumentType = typeAt(argument);
|
|
3004
|
+
if (argumentType.flags & ts.TypeFlags.Any)
|
|
3005
|
+
any = true;
|
|
3006
|
+
for (const one of aliasesIn(argumentType))
|
|
3007
|
+
own.add(one);
|
|
3008
|
+
}
|
|
3009
|
+
for (const one of wanted) {
|
|
3010
|
+
if (any || !own.has(one))
|
|
3011
|
+
add(call, call, one, "produced", "indirect-call");
|
|
3012
|
+
}
|
|
3013
|
+
};
|
|
3014
|
+
/** `Object.assign(o, …)`, `Reflect.set(o, "id", …)`, `new Proxy(o, …)`: a holder written past its type. */
|
|
3015
|
+
const reflectWrite = (call, words) => {
|
|
3016
|
+
const first = call.arguments?.[0];
|
|
3017
|
+
if (first === undefined)
|
|
3018
|
+
return;
|
|
3019
|
+
const holder = typeAt(first);
|
|
3020
|
+
const produced = aliasesIn(holder).filter((one) => one.produced.length > 0);
|
|
3021
|
+
if (produced.length > 0) {
|
|
3022
|
+
let wanted = produced;
|
|
3023
|
+
const key = call.arguments?.[1];
|
|
3024
|
+
if ((words === "Reflect.set" ||
|
|
3025
|
+
words === "Object.defineProperty" ||
|
|
3026
|
+
words === "Reflect.defineProperty") &&
|
|
3027
|
+
key !== undefined &&
|
|
3028
|
+
ts.isStringLiteralLike(key)) {
|
|
3029
|
+
const property = checker.getPropertyOfType(holder, key.text);
|
|
3030
|
+
wanted = property === undefined ? [] : aliasesIn(checker.getTypeOfSymbol(property));
|
|
3031
|
+
}
|
|
3032
|
+
else if (words === "Object.assign") {
|
|
3033
|
+
const fromSources = new Set();
|
|
3034
|
+
for (const source of (call.arguments ?? []).slice(1)) {
|
|
3035
|
+
for (const property of checker.getPropertiesOfType(typeAt(source))) {
|
|
3036
|
+
const into = checker.getPropertyOfType(holder, property.getName());
|
|
3037
|
+
if (into === undefined)
|
|
3038
|
+
continue;
|
|
3039
|
+
const held = aliasesIn(checker.getTypeOfSymbol(into));
|
|
3040
|
+
const own = aliasesIn(checker.getTypeOfSymbol(property));
|
|
3041
|
+
for (const one of held)
|
|
3042
|
+
if (!own.includes(one))
|
|
3043
|
+
fromSources.add(one);
|
|
3044
|
+
}
|
|
3045
|
+
}
|
|
3046
|
+
wanted = produced.filter((one) => fromSources.has(one));
|
|
3047
|
+
}
|
|
3048
|
+
for (const one of wanted) {
|
|
3049
|
+
if (one.produced.length > 0)
|
|
3050
|
+
add(call, call, one, "produced", "reflect");
|
|
3051
|
+
}
|
|
3052
|
+
}
|
|
3053
|
+
if (words !== "Proxy") {
|
|
3054
|
+
for (const one of heldMembers(holder))
|
|
3055
|
+
add(call, call, one, "escaped", "reflect");
|
|
3056
|
+
}
|
|
3057
|
+
};
|
|
3058
|
+
// ---- escaped -----------------------------------------------------------------------------
|
|
3059
|
+
/** The stated members `type` holds itself (or a member of its union): `this` of a class with `saving`. */
|
|
3060
|
+
const heldMembers = (type) => {
|
|
3061
|
+
if (targets.members.size === 0)
|
|
3062
|
+
return [];
|
|
3063
|
+
const into = new Set();
|
|
3064
|
+
const base = widened(checker, type) ?? type;
|
|
3065
|
+
for (const member of base.isUnionOrIntersection() ? base.types : [base]) {
|
|
3066
|
+
if (!(member.flags & ts.TypeFlags.Object))
|
|
3067
|
+
continue;
|
|
3068
|
+
for (const [name, list] of targets.members) {
|
|
3069
|
+
questions += 1;
|
|
3070
|
+
const property = checker.getPropertyOfType(member, name);
|
|
3071
|
+
if (property === undefined)
|
|
3072
|
+
continue;
|
|
3073
|
+
const roots = checker.getRootSymbols(property);
|
|
3074
|
+
for (const one of list)
|
|
3075
|
+
if (roots.includes(one.symbol))
|
|
3076
|
+
into.add(one);
|
|
3077
|
+
}
|
|
3078
|
+
}
|
|
3079
|
+
return [...into];
|
|
3080
|
+
};
|
|
3081
|
+
/**
|
|
3082
|
+
* An object literal for a holder whose stated member it writes where no reference names it
|
|
3083
|
+
* (the literal is typed by a union with a function, as a state setter takes), or into which it
|
|
3084
|
+
* spreads a holder.
|
|
3085
|
+
*/
|
|
3086
|
+
const literalHolder = (literal) => {
|
|
3087
|
+
const type = contextual(literal);
|
|
3088
|
+
if (type === undefined)
|
|
3089
|
+
return;
|
|
3090
|
+
const held = heldMembers(type);
|
|
3091
|
+
for (const one of held) {
|
|
3092
|
+
const written = literal.properties.some((property) => {
|
|
3093
|
+
if (ts.isSpreadAssignment(property))
|
|
3094
|
+
return heldMembers(typeAt(property.expression)).includes(one);
|
|
3095
|
+
const name = property.name;
|
|
3096
|
+
if (!(ts.isIdentifier(name) || ts.isStringLiteral(name)))
|
|
3097
|
+
return false;
|
|
3098
|
+
if (name.text !== one.stated.word)
|
|
3099
|
+
return false;
|
|
3100
|
+
const symbol = symbolOfReference(name);
|
|
3101
|
+
return (symbol === undefined || !checker.getRootSymbols(resolved(symbol)).includes(one.symbol));
|
|
3102
|
+
});
|
|
3103
|
+
if (written)
|
|
3104
|
+
add(literal, literal, one, "escaped", "literal");
|
|
3105
|
+
}
|
|
3106
|
+
};
|
|
3107
|
+
// ---- decided -----------------------------------------------------------------------------
|
|
3108
|
+
/** A value of a stated alias the code decides by: its declared type, else the object whose tag it is. */
|
|
3109
|
+
const decidedBy = (expression, shown) => {
|
|
3110
|
+
let inner = expression;
|
|
3111
|
+
while (ts.isParenthesizedExpression(inner) || ts.isNonNullExpression(inner))
|
|
3112
|
+
inner = inner.expression;
|
|
3113
|
+
if (ts.isLiteralExpression(inner) ||
|
|
3114
|
+
inner.kind === ts.SyntaxKind.TrueKeyword ||
|
|
3115
|
+
inner.kind === ts.SyntaxKind.FalseKeyword) {
|
|
3116
|
+
return;
|
|
3117
|
+
}
|
|
3118
|
+
const name = ts.isPropertyAccessExpression(inner) ? inner.name : inner;
|
|
3119
|
+
questions += 1;
|
|
3120
|
+
const symbol = ts.isIdentifier(name) ? checker.getSymbolAtLocation(name) : undefined;
|
|
3121
|
+
const declared = symbol !== undefined && symbol.flags & (ts.SymbolFlags.Variable | ts.SymbolFlags.Property)
|
|
3122
|
+
? checker.getTypeOfSymbol(symbol)
|
|
3123
|
+
: undefined;
|
|
3124
|
+
let found = [
|
|
3125
|
+
...(declared === undefined ? [] : aliasesOf(declared)),
|
|
3126
|
+
...aliasesOf(typeAt(inner)),
|
|
3127
|
+
];
|
|
3128
|
+
if (found.length === 0 &&
|
|
3129
|
+
(ts.isPropertyAccessExpression(inner) || ts.isElementAccessExpression(inner))) {
|
|
3130
|
+
found = aliasesOf(typeAt(inner.expression));
|
|
3131
|
+
}
|
|
3132
|
+
for (const one of new Set(found)) {
|
|
3133
|
+
if (one.decided.length > 0)
|
|
3134
|
+
add(inner, shown, one, "decided", "value");
|
|
3135
|
+
}
|
|
3136
|
+
// A value made from one (`s.toLowerCase()`, `s as string`, `String(s)`, `` `${s}` ``): the
|
|
3137
|
+
// code decides by what is made, not by the value itself.
|
|
3138
|
+
if (found.length === 0) {
|
|
3139
|
+
for (const source of derivedSources(inner)) {
|
|
3140
|
+
for (const one of new Set(aliasesOf(typeAt(source)))) {
|
|
3141
|
+
if (one.decided.length > 0)
|
|
3142
|
+
add(inner, shown, one, "decided", "derived");
|
|
3143
|
+
}
|
|
3144
|
+
}
|
|
3145
|
+
}
|
|
3146
|
+
};
|
|
3147
|
+
/**
|
|
3148
|
+
* The expressions a value is made from, when it is made from another value: the operand of a
|
|
3149
|
+
* cast, the receiver of a method call, the arguments of a call, the parts of a template.
|
|
3150
|
+
*/
|
|
3151
|
+
const derivedSources = (expression) => {
|
|
3152
|
+
let inner = expression;
|
|
3153
|
+
while (ts.isParenthesizedExpression(inner) || ts.isNonNullExpression(inner)) {
|
|
3154
|
+
inner = inner.expression;
|
|
3155
|
+
}
|
|
3156
|
+
if (ts.isAsExpression(inner) ||
|
|
3157
|
+
ts.isSatisfiesExpression(inner) ||
|
|
3158
|
+
ts.isTypeAssertionExpression(inner)) {
|
|
3159
|
+
return [inner.expression, ...derivedSources(inner.expression)];
|
|
3160
|
+
}
|
|
3161
|
+
if (ts.isTemplateExpression(inner)) {
|
|
3162
|
+
return inner.templateSpans.flatMap((span) => [
|
|
3163
|
+
span.expression,
|
|
3164
|
+
...derivedSources(span.expression),
|
|
3165
|
+
]);
|
|
3166
|
+
}
|
|
3167
|
+
if (ts.isCallExpression(inner)) {
|
|
3168
|
+
const receiver = ts.isPropertyAccessExpression(inner.expression)
|
|
3169
|
+
? [inner.expression.expression]
|
|
3170
|
+
: [];
|
|
3171
|
+
return [...receiver, ...inner.arguments.filter((one) => !ts.isSpreadElement(one))];
|
|
3172
|
+
}
|
|
3173
|
+
return [];
|
|
3174
|
+
};
|
|
3175
|
+
/**
|
|
3176
|
+
* A method called on a value of an alias the code decides by (`s.startsWith("p")`): what it
|
|
3177
|
+
* gives is made from the value.
|
|
3178
|
+
*/
|
|
3179
|
+
const derivedCall = (call) => {
|
|
3180
|
+
if (!ts.isPropertyAccessExpression(call.expression))
|
|
3181
|
+
return;
|
|
3182
|
+
for (const one of new Set(aliasesOf(typeAt(call.expression.expression)))) {
|
|
3183
|
+
if (one.decided.length > 0)
|
|
3184
|
+
add(call, call, one, "decided", "derived");
|
|
3185
|
+
}
|
|
3186
|
+
};
|
|
3187
|
+
/**
|
|
3188
|
+
* A value of a stated alias given to a call that decides by it, to a parameter whose type does
|
|
3189
|
+
* not hold the alias: `list.includes(s)`, `map.get(s)`, `Object.is(s, …)`.
|
|
3190
|
+
*/
|
|
3191
|
+
const decidingCall = (call) => {
|
|
3192
|
+
const callee = call.expression;
|
|
3193
|
+
const words = calleeWords(call);
|
|
3194
|
+
const method = ts.isPropertyAccessExpression(callee) ? callee.name.text : undefined;
|
|
3195
|
+
// Besides the methods known to decide, a call that answers yes or no (`pattern.test(s)`,
|
|
3196
|
+
// `Object.hasOwn(labels, s)`) or takes the value as a key (`Reflect.get(labels, s)`).
|
|
3197
|
+
// A generic key (`Reflect.get<T, P extends PropertyKey>`) is inferred as the value's own
|
|
3198
|
+
// type, so these read the value as a key whatever the parameter is inferred as.
|
|
3199
|
+
const keyed = words !== undefined && KEYED_CALLS.has(words);
|
|
3200
|
+
const known = keyed || words === "Object.is" || (method !== undefined && DECIDING_METHODS.has(method));
|
|
3201
|
+
const answers = known || (typeAt(call).flags & ts.TypeFlags.BooleanLike) !== 0;
|
|
3202
|
+
for (const argument of call.arguments) {
|
|
3203
|
+
if (ts.isSpreadElement(argument))
|
|
3204
|
+
continue;
|
|
3205
|
+
const target = contextual(argument);
|
|
3206
|
+
if (!answers && (target === undefined || !isPropertyKey(target)))
|
|
3207
|
+
continue;
|
|
3208
|
+
const wanted = target === undefined ? [] : aliasesIn(target);
|
|
3209
|
+
for (const one of new Set(aliasesOf(typeAt(argument)))) {
|
|
3210
|
+
if (one.decided.length > 0 && (keyed || !wanted.includes(one))) {
|
|
3211
|
+
add(argument, call, one, "decided", "call");
|
|
3212
|
+
}
|
|
3213
|
+
}
|
|
3214
|
+
}
|
|
3215
|
+
};
|
|
3216
|
+
/** `PropertyKey`: `string | number | symbol`, the type of a key (`Reflect.get`'s second parameter). */
|
|
3217
|
+
const isPropertyKey = (given) => {
|
|
3218
|
+
// `Reflect.get<T, P extends PropertyKey>(…)`: the parameter's constraint says it.
|
|
3219
|
+
const type = given.flags & ts.TypeFlags.TypeParameter
|
|
3220
|
+
? (checker.getBaseConstraintOfType(given) ?? given)
|
|
3221
|
+
: given;
|
|
3222
|
+
return (type.isUnion() &&
|
|
3223
|
+
type.types.some((one) => (one.flags & ts.TypeFlags.ESSymbolLike) !== 0) &&
|
|
3224
|
+
type.types.some((one) => (one.flags & ts.TypeFlags.StringLike) !== 0));
|
|
3225
|
+
};
|
|
3226
|
+
/** A union of literals only (`"pending" | "paid"`): a closed set of values. */
|
|
3227
|
+
const isClosedLiterals = (type) => type.isUnion() &&
|
|
3228
|
+
type.types.every((member) => (member.flags &
|
|
3229
|
+
(ts.TypeFlags.Literal |
|
|
3230
|
+
ts.TypeFlags.EnumLiteral |
|
|
3231
|
+
ts.TypeFlags.Undefined |
|
|
3232
|
+
ts.TypeFlags.Null)) !==
|
|
3233
|
+
0);
|
|
3234
|
+
/**
|
|
3235
|
+
* A value of a stated alias handed to a closed union that does not hold the alias (a parameter
|
|
3236
|
+
* or variable typed `"pending" | "paid"`): the code there decides by the value as if it could
|
|
3237
|
+
* hold nothing else.
|
|
3238
|
+
*/
|
|
3239
|
+
const handedToClosed = (value, target, shown, via) => {
|
|
3240
|
+
if (target === undefined || !isClosedLiterals(target))
|
|
3241
|
+
return;
|
|
3242
|
+
const wanted = aliasesIn(target);
|
|
3243
|
+
for (const one of new Set(aliasesOf(typeAt(value)))) {
|
|
3244
|
+
if (one.decided.length > 0 && !wanted.includes(one))
|
|
3245
|
+
add(value, shown, one, "decided", via);
|
|
3246
|
+
}
|
|
3247
|
+
};
|
|
3248
|
+
const isRelation = (kind) => kind === ts.SyntaxKind.LessThanToken ||
|
|
3249
|
+
kind === ts.SyntaxKind.LessThanEqualsToken ||
|
|
3250
|
+
kind === ts.SyntaxKind.GreaterThanToken ||
|
|
3251
|
+
kind === ts.SyntaxKind.GreaterThanEqualsToken;
|
|
3252
|
+
const isEquality = (kind) => kind === ts.SyntaxKind.EqualsEqualsEqualsToken ||
|
|
3253
|
+
kind === ts.SyntaxKind.EqualsEqualsToken ||
|
|
3254
|
+
kind === ts.SyntaxKind.ExclamationEqualsEqualsToken ||
|
|
3255
|
+
kind === ts.SyntaxKind.ExclamationEqualsToken;
|
|
3256
|
+
const producing = targets.aliases.some((one) => one.produced.length > 0);
|
|
3257
|
+
const deciding = targets.aliases.some((one) => one.decided.length > 0);
|
|
3258
|
+
const escaping = targets.members.size > 0;
|
|
3259
|
+
const visit = (node) => {
|
|
3260
|
+
if (stopped !== undefined)
|
|
3261
|
+
return;
|
|
3262
|
+
if (questions >= budget) {
|
|
3263
|
+
stopped = node;
|
|
3264
|
+
return;
|
|
3265
|
+
}
|
|
3266
|
+
if (producing) {
|
|
3267
|
+
if (ts.isReturnStatement(node) && node.expression !== undefined)
|
|
3268
|
+
returned(node.expression);
|
|
3269
|
+
else if (ts.isArrowFunction(node) && !ts.isBlock(node.body))
|
|
3270
|
+
given(node.body, contextual(node.body));
|
|
3271
|
+
else if (ts.isYieldExpression(node) && node.expression !== undefined) {
|
|
3272
|
+
given(node.expression, contextual(node.expression));
|
|
3273
|
+
}
|
|
3274
|
+
else if ((ts.isVariableDeclaration(node) ||
|
|
3275
|
+
ts.isPropertyDeclaration(node) ||
|
|
3276
|
+
ts.isParameter(node) ||
|
|
3277
|
+
ts.isBindingElement(node)) &&
|
|
3278
|
+
node.initializer !== undefined) {
|
|
3279
|
+
given(node.initializer, contextual(node.initializer));
|
|
3280
|
+
if (ts.isPropertyDeclaration(node)) {
|
|
3281
|
+
for (const type of inheritedTypes(node))
|
|
3282
|
+
given(node.initializer, type, "override");
|
|
3283
|
+
}
|
|
3284
|
+
}
|
|
3285
|
+
else if (ts.isBinaryExpression(node) &&
|
|
3286
|
+
isAssignmentOperator(node.operatorToken.kind) &&
|
|
3287
|
+
!ts.isObjectLiteralExpression(node.left) &&
|
|
3288
|
+
!ts.isArrayLiteralExpression(node.left)) {
|
|
3289
|
+
given(node.right, contextual(node.right) ?? typeAt(node.left));
|
|
3290
|
+
}
|
|
3291
|
+
else if (ts.isPropertyAssignment(node))
|
|
3292
|
+
given(node.initializer, contextual(node.initializer));
|
|
3293
|
+
else if (ts.isShorthandPropertyAssignment(node))
|
|
3294
|
+
given(node.name, contextual(node.name));
|
|
3295
|
+
else if (ts.isSpreadAssignment(node) && ts.isObjectLiteralExpression(node.parent)) {
|
|
3296
|
+
spreadInto(node.expression, contextual(node.parent), node.parent);
|
|
3297
|
+
}
|
|
3298
|
+
else if (ts.isArrayLiteralExpression(node)) {
|
|
3299
|
+
for (const element of node.elements) {
|
|
3300
|
+
if (ts.isSpreadElement(element)) {
|
|
3301
|
+
const target = contextual(node);
|
|
3302
|
+
const item = target === undefined ? undefined : elementOf(target);
|
|
3303
|
+
const wanted = item === undefined ? [] : aliasesIn(item);
|
|
3304
|
+
const source = typeAt(element.expression);
|
|
3305
|
+
const any = (source.flags & ts.TypeFlags.Any) !== 0;
|
|
3306
|
+
const own = any ? [] : aliasesIn(elementOf(source) ?? source);
|
|
3307
|
+
for (const one of wanted) {
|
|
3308
|
+
if (one.produced.length > 0 && !own.includes(one)) {
|
|
3309
|
+
add(element, node, one, "produced", any ? "any" : "spread");
|
|
3310
|
+
}
|
|
3311
|
+
}
|
|
3312
|
+
}
|
|
3313
|
+
else if (!ts.isOmittedExpression(element)) {
|
|
3314
|
+
given(element, contextual(element));
|
|
3315
|
+
}
|
|
3316
|
+
}
|
|
3317
|
+
}
|
|
3318
|
+
else if (ts.isJsxAttribute(node) && node.initializer !== undefined) {
|
|
3319
|
+
const value = ts.isJsxExpression(node.initializer)
|
|
3320
|
+
? node.initializer.expression
|
|
3321
|
+
: node.initializer;
|
|
3322
|
+
if (value !== undefined && ts.isExpression(value))
|
|
3323
|
+
given(value, contextual(value));
|
|
3324
|
+
}
|
|
3325
|
+
else if (ts.isJsxSpreadAttribute(node)) {
|
|
3326
|
+
// The element shows what the spread is given to: `<Show {...props} />`.
|
|
3327
|
+
spreadInto(node.expression, contextual(node.parent), node.parent.parent);
|
|
3328
|
+
}
|
|
3329
|
+
else if (ts.isAsExpression(node) ||
|
|
3330
|
+
ts.isTypeAssertionExpression(node) ||
|
|
3331
|
+
ts.isSatisfiesExpression(node)) {
|
|
3332
|
+
const constant = ts.isTypeReferenceNode(node.type) &&
|
|
3333
|
+
ts.isIdentifier(node.type.typeName) &&
|
|
3334
|
+
node.type.typeName.text === "const";
|
|
3335
|
+
if (!constant) {
|
|
3336
|
+
questions += 1;
|
|
3337
|
+
given(node.expression, checker.getTypeFromTypeNode(node.type), "cast");
|
|
3338
|
+
}
|
|
3339
|
+
}
|
|
3340
|
+
else if (ts.isForOfStatement(node) && !ts.isVariableDeclarationList(node.initializer)) {
|
|
3341
|
+
const target = typeAt(node.initializer);
|
|
3342
|
+
const item = elementOf(typeAt(node.expression));
|
|
3343
|
+
const wanted = aliasesIn(target).filter((one) => one.produced.length > 0);
|
|
3344
|
+
const own = item === undefined ? [] : aliasesIn(item);
|
|
3345
|
+
for (const one of wanted) {
|
|
3346
|
+
if (!own.includes(one))
|
|
3347
|
+
add(node.initializer, node, one, "produced", "for-of");
|
|
3348
|
+
}
|
|
3349
|
+
}
|
|
3350
|
+
// `p--`, `p += 1`, `id += s`: the place typed by the brand gets a value computed from it,
|
|
3351
|
+
// which no builder made.
|
|
3352
|
+
const computed = ((ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) &&
|
|
3353
|
+
(node.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
3354
|
+
node.operator === ts.SyntaxKind.MinusMinusToken)) ||
|
|
3355
|
+
(ts.isBinaryExpression(node) &&
|
|
3356
|
+
node.operatorToken.kind >= ts.SyntaxKind.FirstCompoundAssignment &&
|
|
3357
|
+
node.operatorToken.kind <= ts.SyntaxKind.LastCompoundAssignment &&
|
|
3358
|
+
// `??=`, `||=`, `&&=` give the right side as it is, read as an assignment.
|
|
3359
|
+
node.operatorToken.kind !== ts.SyntaxKind.QuestionQuestionEqualsToken &&
|
|
3360
|
+
node.operatorToken.kind !== ts.SyntaxKind.BarBarEqualsToken &&
|
|
3361
|
+
node.operatorToken.kind !== ts.SyntaxKind.AmpersandAmpersandEqualsToken);
|
|
3362
|
+
if (computed) {
|
|
3363
|
+
const target = ts.isBinaryExpression(node) ? node.left : node.operand;
|
|
3364
|
+
for (const one of aliasesIn(typeAt(target))) {
|
|
3365
|
+
if (one.produced.length > 0)
|
|
3366
|
+
add(node, node, one, "produced", "derived");
|
|
3367
|
+
}
|
|
3368
|
+
}
|
|
3369
|
+
if (ts.isCallExpression(node) || ts.isNewExpression(node)) {
|
|
3370
|
+
for (const argument of node.arguments ?? []) {
|
|
3371
|
+
if (ts.isSpreadElement(argument))
|
|
3372
|
+
spreadArgument(node, argument);
|
|
3373
|
+
else
|
|
3374
|
+
given(argument, contextual(argument));
|
|
3375
|
+
}
|
|
3376
|
+
genericCall(node);
|
|
3377
|
+
if (ts.isCallExpression(node))
|
|
3378
|
+
indirectCall(node);
|
|
3379
|
+
}
|
|
3380
|
+
}
|
|
3381
|
+
if ((producing || escaping) && (ts.isCallExpression(node) || ts.isNewExpression(node))) {
|
|
3382
|
+
const words = calleeWords(node);
|
|
3383
|
+
if (words !== undefined &&
|
|
3384
|
+
(REFLECT_WRITES.has(words) || (words === "Proxy" && ts.isNewExpression(node)))) {
|
|
3385
|
+
reflectWrite(node, words);
|
|
3386
|
+
}
|
|
3387
|
+
}
|
|
3388
|
+
if (escaping) {
|
|
3389
|
+
if (ts.isObjectLiteralExpression(node))
|
|
3390
|
+
literalHolder(node);
|
|
3391
|
+
else if (ts.isBinaryExpression(node) &&
|
|
3392
|
+
isAssignmentOperator(node.operatorToken.kind) &&
|
|
3393
|
+
ts.isElementAccessExpression(node.left) &&
|
|
3394
|
+
!ts.isStringLiteralLike(node.left.argumentExpression)) {
|
|
3395
|
+
for (const one of heldMembers(typeAt(node.left.expression))) {
|
|
3396
|
+
add(node.left, node, one, "escaped", "reflect");
|
|
3397
|
+
}
|
|
3398
|
+
}
|
|
3399
|
+
}
|
|
3400
|
+
if (deciding) {
|
|
3401
|
+
if (ts.isSwitchStatement(node))
|
|
3402
|
+
decidedBy(node.expression, node.expression);
|
|
3403
|
+
else if (ts.isBinaryExpression(node) &&
|
|
3404
|
+
(isEquality(node.operatorToken.kind) || isRelation(node.operatorToken.kind))) {
|
|
3405
|
+
decidedBy(node.left, node);
|
|
3406
|
+
decidedBy(node.right, node);
|
|
3407
|
+
}
|
|
3408
|
+
else if (ts.isBinaryExpression(node) &&
|
|
3409
|
+
node.operatorToken.kind === ts.SyntaxKind.InKeyword) {
|
|
3410
|
+
decidedBy(node.left, node);
|
|
3411
|
+
}
|
|
3412
|
+
else if (ts.isElementAccessExpression(node) &&
|
|
3413
|
+
!ts.isStringLiteralLike(node.argumentExpression) &&
|
|
3414
|
+
!ts.isNumericLiteral(node.argumentExpression)) {
|
|
3415
|
+
decidedBy(node.argumentExpression, node);
|
|
3416
|
+
}
|
|
3417
|
+
else if (ts.isCallExpression(node)) {
|
|
3418
|
+
decidingCall(node);
|
|
3419
|
+
derivedCall(node);
|
|
3420
|
+
for (const argument of node.arguments) {
|
|
3421
|
+
if (!ts.isSpreadElement(argument)) {
|
|
3422
|
+
handedToClosed(argument, contextual(argument), node, "call");
|
|
3423
|
+
}
|
|
3424
|
+
}
|
|
3425
|
+
}
|
|
3426
|
+
else if (ts.isVariableDeclaration(node) &&
|
|
3427
|
+
node.type !== undefined &&
|
|
3428
|
+
node.initializer !== undefined) {
|
|
3429
|
+
handedToClosed(node.initializer, typeAt(node.name), node, "value");
|
|
3430
|
+
}
|
|
3431
|
+
}
|
|
3432
|
+
ts.forEachChild(node, visit);
|
|
3433
|
+
};
|
|
3434
|
+
visit(sourceFile);
|
|
3435
|
+
if (stopped !== undefined) {
|
|
3436
|
+
const at = stopped;
|
|
3437
|
+
for (const one of targets.aliases) {
|
|
3438
|
+
add(at, at, one, "produced", "stopped");
|
|
3439
|
+
add(at, at, one, "decided", "stopped");
|
|
3440
|
+
}
|
|
3441
|
+
for (const one of [...targets.members.values()].flat())
|
|
3442
|
+
add(at, at, one, "escaped", "stopped");
|
|
3443
|
+
}
|
|
3444
|
+
return found.sort((a, b) => a.node.getStart(sourceFile) - b.node.getStart(sourceFile) ||
|
|
3445
|
+
a.node.end - b.node.end ||
|
|
3446
|
+
compareIds(a.declaration, b.declaration) ||
|
|
3447
|
+
compareIds(a.trait, b.trait) ||
|
|
3448
|
+
compareIds(a.form, b.form));
|
|
3449
|
+
};
|
|
3450
|
+
return { referencesIn, flowsIn };
|
|
3451
|
+
}
|
|
3452
|
+
function compareIds(a, b) {
|
|
3453
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
3454
|
+
}
|
|
3455
|
+
/** Whether every value of `type` is a primitive, or an array or tuple of them, `depth` levels down. */
|
|
3456
|
+
function onlyPrimitives(checker, found, depth) {
|
|
3457
|
+
const type = widened(checker, found);
|
|
3458
|
+
if (type === undefined || depth > 3)
|
|
3459
|
+
return false;
|
|
3460
|
+
const members = type.isUnion() ? type.types : [type];
|
|
3461
|
+
return members.every((member) => {
|
|
3462
|
+
if (member.flags & PRIMITIVE_FLAGS)
|
|
3463
|
+
return true;
|
|
3464
|
+
if (!isTypeReference(member) || !checker.isArrayLikeType(member))
|
|
3465
|
+
return false;
|
|
3466
|
+
return checker
|
|
3467
|
+
.getTypeArguments(member)
|
|
3468
|
+
.every((argument) => onlyPrimitives(checker, argument, depth + 1));
|
|
3469
|
+
});
|
|
3470
|
+
}
|
|
3471
|
+
/**
|
|
3472
|
+
* TypeScript's errors for a function whose end can be reached although it must return a value:
|
|
3473
|
+
* TS2366, TS2355, TS2534 and TS7030 (`TypeQueries.reportsReachableEnd`).
|
|
3474
|
+
*/
|
|
3475
|
+
const REACHABLE_END_CODES = new Set([2366, 2355, 2534, 7030]);
|
|
3476
|
+
/** A file of the platform: TypeScript's own lib, or Node's types. */
|
|
3477
|
+
function isPlatformSourceFile(program, file) {
|
|
3478
|
+
return (program.isSourceFileDefaultLibrary(file) ||
|
|
3479
|
+
/[\\/]node_modules[\\/]@types[\\/]node[\\/]/.test(file.fileName));
|
|
3480
|
+
}
|
|
3481
|
+
/** Properties of the platform's types that are plain data on the closed list, as `<type>.<name>`. */
|
|
3482
|
+
const PLATFORM_PROPERTIES = new Set([
|
|
3483
|
+
"Array.length",
|
|
3484
|
+
"ReadonlyArray.length",
|
|
3485
|
+
"String.length",
|
|
3486
|
+
"Map.size",
|
|
3487
|
+
"ReadonlyMap.size",
|
|
3488
|
+
"Set.size",
|
|
3489
|
+
"ReadonlySet.size",
|
|
3490
|
+
"AbortController.signal",
|
|
3491
|
+
"AbortSignal.aborted",
|
|
3492
|
+
"Error.message",
|
|
3493
|
+
"Error.name",
|
|
3494
|
+
]);
|
|
3495
|
+
/** Platform values whose every property is a constant (`Math.PI`, `Number.MAX_SAFE_INTEGER`). */
|
|
3496
|
+
const PLATFORM_CONSTANTS = new Set(["Math", "NumberConstructor"]);
|
|
3497
|
+
/** The members of a type, a type parameter widened to its constraint, without `null` and `undefined`. */
|
|
3498
|
+
function definedMembers(checker, type) {
|
|
3499
|
+
const wide = widened(checker, type);
|
|
3500
|
+
if (wide === undefined)
|
|
3501
|
+
return undefined;
|
|
3502
|
+
const defined = checker.getNonNullableType(wide);
|
|
3503
|
+
if (defined.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.Never)) {
|
|
3504
|
+
return undefined;
|
|
3505
|
+
}
|
|
3506
|
+
return defined.isUnion() ? defined.types : [defined];
|
|
3507
|
+
}
|
|
3508
|
+
/** Whether `type` is a number type: `number`, a literal, a numeric enum, or an intersection with one. */
|
|
3509
|
+
function isNumberType(type) {
|
|
3510
|
+
if (type.isIntersection())
|
|
3511
|
+
return type.types.some(isNumberType);
|
|
3512
|
+
return (type.flags & ts.TypeFlags.NumberLike) !== 0;
|
|
3513
|
+
}
|
|
3514
|
+
export function createTypeQueries(program, own,
|
|
3515
|
+
/** The project folder qualified ids are relative to. */
|
|
3516
|
+
root, packageOf, isEntry, dependenciesOf) {
|
|
3517
|
+
// The only place in the codebase that reaches the type checker. Created on the first question:
|
|
3518
|
+
// a run whose results all come from the cache never builds one.
|
|
3519
|
+
let created;
|
|
3520
|
+
const typeChecker = () => (created ??= program.getTypeChecker());
|
|
3521
|
+
const declarations = new Map();
|
|
3522
|
+
const declaredTraits = new Map();
|
|
3523
|
+
let anyMarker;
|
|
3524
|
+
let ledgerScan;
|
|
3525
|
+
// The starts of TypeScript's "end of the function can be reached" errors, per file asked about.
|
|
3526
|
+
const reachableEndErrors = new WeakMap();
|
|
3527
|
+
let declaredDispose;
|
|
3528
|
+
let renamedNames;
|
|
3529
|
+
const idFileName = idFileNames(root);
|
|
3530
|
+
// symbolAt is asked about the same node by several rules (on excalidraw 570,000 questions about
|
|
3531
|
+
// 170,000 nodes, 52,000 symbols): the program does not change under these queries, so each
|
|
3532
|
+
// node's answer and each symbol's ref are computed once.
|
|
3533
|
+
const refsByNode = new WeakMap();
|
|
3534
|
+
const refsBySymbol = new WeakMap();
|
|
3535
|
+
const refOfSymbol = (symbol) => {
|
|
3536
|
+
const declaration = symbol.valueDeclaration ?? symbol.declarations?.[0];
|
|
3537
|
+
if (declaration === undefined)
|
|
3538
|
+
return undefined;
|
|
3539
|
+
const id = `${idFileName(declaration.getSourceFile().fileName)}#${declarationPath(declaration)}`;
|
|
3540
|
+
declarations.set(id, declaration);
|
|
3541
|
+
return { id, name: symbol.getName() };
|
|
3542
|
+
};
|
|
3543
|
+
const refAnswer = (node) => {
|
|
3544
|
+
const found = typeChecker().getSymbolAtLocation(node);
|
|
3545
|
+
if (found === undefined)
|
|
3546
|
+
return NO_REF;
|
|
3547
|
+
const symbol = found.flags & ts.SymbolFlags.Alias ? typeChecker().getAliasedSymbol(found) : found;
|
|
3548
|
+
let entry = refsBySymbol.get(symbol);
|
|
3549
|
+
if (entry === undefined) {
|
|
3550
|
+
entry = { ref: refOfSymbol(symbol) };
|
|
3551
|
+
refsBySymbol.set(symbol, entry);
|
|
3552
|
+
}
|
|
3553
|
+
return entry;
|
|
3554
|
+
};
|
|
3555
|
+
// The platform's types that a class or interface of the project extends or implements: a value
|
|
3556
|
+
// typed by one of them may be the project's own code.
|
|
3557
|
+
let extendedPlatform;
|
|
3558
|
+
const platformExtended = () => {
|
|
3559
|
+
if (extendedPlatform !== undefined)
|
|
3560
|
+
return extendedPlatform;
|
|
3561
|
+
const checker = typeChecker();
|
|
3562
|
+
const found = new Set();
|
|
3563
|
+
const visit = (node) => {
|
|
3564
|
+
if (ts.isHeritageClause(node)) {
|
|
3565
|
+
for (const written of node.types) {
|
|
3566
|
+
const symbol = checker.getTypeAtLocation(written).getSymbol();
|
|
3567
|
+
const declarations = symbol?.declarations ?? [];
|
|
3568
|
+
if (symbol !== undefined &&
|
|
3569
|
+
declarations.length > 0 &&
|
|
3570
|
+
declarations.every((one) => isPlatformSourceFile(program, one.getSourceFile()))) {
|
|
3571
|
+
found.add(symbol);
|
|
3572
|
+
}
|
|
3573
|
+
}
|
|
3574
|
+
return;
|
|
3575
|
+
}
|
|
3576
|
+
ts.forEachChild(node, visit);
|
|
3577
|
+
};
|
|
3578
|
+
for (const file of program.getSourceFiles()) {
|
|
3579
|
+
if (!own.has(toTypeScriptPath(file.fileName)))
|
|
3580
|
+
continue;
|
|
3581
|
+
if (!file.text.includes("extends") && !file.text.includes("implements"))
|
|
3582
|
+
continue;
|
|
3583
|
+
visit(file);
|
|
3584
|
+
}
|
|
3585
|
+
extendedPlatform = found;
|
|
3586
|
+
return found;
|
|
3587
|
+
};
|
|
3588
|
+
/** The platform's name of one type, as `platformTypeOf` says it. */
|
|
3589
|
+
const platformNameOfType = (type) => {
|
|
3590
|
+
if (type.flags & ts.TypeFlags.StringLike)
|
|
3591
|
+
return "String";
|
|
3592
|
+
// A branded string (`string & { … }`): its string part.
|
|
3593
|
+
if (type.isIntersection() && type.types.some((part) => part.flags & ts.TypeFlags.StringLike)) {
|
|
3594
|
+
return "String";
|
|
3595
|
+
}
|
|
3596
|
+
if (type.flags & ts.TypeFlags.NumberLike)
|
|
3597
|
+
return "Number";
|
|
3598
|
+
if (type.flags & ts.TypeFlags.BigIntLike)
|
|
3599
|
+
return "BigInt";
|
|
3600
|
+
if (type.flags & ts.TypeFlags.BooleanLike)
|
|
3601
|
+
return "Boolean";
|
|
3602
|
+
if ((type.flags & ts.TypeFlags.Object) === 0)
|
|
3603
|
+
return undefined;
|
|
3604
|
+
if (typeChecker().isTupleType(type))
|
|
3605
|
+
return "Array";
|
|
3606
|
+
const symbol = type.getSymbol();
|
|
3607
|
+
const declarations = symbol?.declarations ?? [];
|
|
3608
|
+
if (symbol === undefined ||
|
|
3609
|
+
declarations.length === 0 ||
|
|
3610
|
+
!declarations.every((one) => isPlatformSourceFile(program, one.getSourceFile())) ||
|
|
3611
|
+
platformExtended().has(symbol)) {
|
|
3612
|
+
return undefined;
|
|
3613
|
+
}
|
|
3614
|
+
return symbol.getName();
|
|
3615
|
+
};
|
|
3616
|
+
const platformNameOf = (type) => {
|
|
3617
|
+
const members = definedMembers(typeChecker(), type);
|
|
3618
|
+
if (members === undefined)
|
|
3619
|
+
return undefined;
|
|
3620
|
+
const names = new Set(members.map(platformNameOfType));
|
|
3621
|
+
const [only] = names;
|
|
3622
|
+
return names.size === 1 ? only : undefined;
|
|
3623
|
+
};
|
|
3624
|
+
/** Whether reading `property` from a value of type `receiver` runs no code and cannot throw. */
|
|
3625
|
+
const plainProperty = (property, receiver) => {
|
|
3626
|
+
const declarations = property.declarations ?? [];
|
|
3627
|
+
if (declarations.length === 0) {
|
|
3628
|
+
// A key of a mapped type with no declaration of its own (`Record<"a" | "b", number>`).
|
|
3629
|
+
const members = definedMembers(typeChecker(), receiver);
|
|
3630
|
+
return (members?.every((member) => "objectFlags" in member &&
|
|
3631
|
+
typeof member.objectFlags === "number" &&
|
|
3632
|
+
(member.objectFlags & ts.ObjectFlags.Mapped) !== 0) === true);
|
|
3633
|
+
}
|
|
3634
|
+
if (declarations.some((one) => ts.isGetAccessor(one) || ts.isSetAccessor(one)))
|
|
3635
|
+
return false;
|
|
3636
|
+
if (declarations.every((one) => isPlatformSourceFile(program, one.getSourceFile()))) {
|
|
3637
|
+
const owner = platformNameOf(receiver);
|
|
3638
|
+
return (owner !== undefined &&
|
|
3639
|
+
(PLATFORM_CONSTANTS.has(owner) || PLATFORM_PROPERTIES.has(`${owner}.${property.getName()}`)));
|
|
3640
|
+
}
|
|
3641
|
+
const members = definedMembers(typeChecker(), receiver);
|
|
3642
|
+
if (members === undefined)
|
|
3643
|
+
return false;
|
|
3644
|
+
const inModule = members.every((member) => ((member.getSymbol()?.flags ?? 0) & (ts.SymbolFlags.ValueModule | ts.SymbolFlags.Enum)) !==
|
|
3645
|
+
0);
|
|
3646
|
+
if (inModule) {
|
|
3647
|
+
return declarations.every((one) => ts.isEnumMember(one) ||
|
|
3648
|
+
ts.isVariableDeclaration(one) ||
|
|
3649
|
+
ts.isFunctionDeclaration(one) ||
|
|
3650
|
+
ts.isClassDeclaration(one));
|
|
3651
|
+
}
|
|
3652
|
+
// A member declared as a property is read as one, as far as the declared types say: a class
|
|
3653
|
+
// that implements an interface may give it as a getter, which this does not see.
|
|
3654
|
+
return declarations.every((one) => ts.isPropertyDeclaration(one) ||
|
|
3655
|
+
(ts.isParameter(one) && ts.isConstructorDeclaration(one.parent)) ||
|
|
3656
|
+
ts.isPropertyAssignment(one) ||
|
|
3657
|
+
ts.isShorthandPropertyAssignment(one) ||
|
|
3658
|
+
ts.isPropertySignature(one));
|
|
3659
|
+
};
|
|
3660
|
+
const arrayElements = (type) => {
|
|
3661
|
+
const checker = typeChecker();
|
|
3662
|
+
const members = definedMembers(checker, type);
|
|
3663
|
+
if (members === undefined)
|
|
3664
|
+
return undefined;
|
|
3665
|
+
let primitives = true;
|
|
3666
|
+
for (const member of members) {
|
|
3667
|
+
const tuple = checker.isTupleType(member);
|
|
3668
|
+
const name = tuple ? "Array" : platformNameOfType(member);
|
|
3669
|
+
if (name !== "Array" && name !== "ReadonlyArray")
|
|
3670
|
+
return undefined;
|
|
3671
|
+
if (!isTypeReference(member))
|
|
3672
|
+
return undefined;
|
|
3673
|
+
for (const element of checker.getTypeArguments(member)) {
|
|
3674
|
+
const wide = widened(checker, element);
|
|
3675
|
+
const parts = wide === undefined ? [] : wide.isUnion() ? wide.types : [wide];
|
|
3676
|
+
if (wide === undefined || !parts.every((part) => (part.flags & PRIMITIVE_FLAGS) !== 0)) {
|
|
3677
|
+
primitives = false;
|
|
3678
|
+
}
|
|
3679
|
+
}
|
|
3680
|
+
}
|
|
3681
|
+
return primitives ? "primitives" : "values";
|
|
3682
|
+
};
|
|
3683
|
+
return {
|
|
3684
|
+
symbolAt(node) {
|
|
3685
|
+
let answer = refsByNode.get(node);
|
|
3686
|
+
if (answer === undefined) {
|
|
3687
|
+
answer = refAnswer(node);
|
|
3688
|
+
refsByNode.set(node, answer);
|
|
3689
|
+
}
|
|
3690
|
+
return answer.ref;
|
|
3691
|
+
},
|
|
3692
|
+
declarationOf(symbol) {
|
|
3693
|
+
const node = declarations.get(symbol.id);
|
|
3694
|
+
return node === undefined ? undefined : { file: node.getSourceFile().fileName, node };
|
|
3695
|
+
},
|
|
3696
|
+
isOwnFile: (file) => own.has(toTypeScriptPath(file)),
|
|
3697
|
+
packageOf: (file) => packageOf(toTypeScriptPath(file)),
|
|
3698
|
+
literalsOf(node) {
|
|
3699
|
+
const found = typeChecker().getTypeAtLocation(node);
|
|
3700
|
+
const type = found.isTypeParameter() ? typeChecker().getBaseConstraintOfType(found) : found;
|
|
3701
|
+
if (type === undefined)
|
|
3702
|
+
return "not-finite";
|
|
3703
|
+
const literals = [];
|
|
3704
|
+
for (const member of type.isUnion() ? type.types : [type]) {
|
|
3705
|
+
const literal = literalOf(typeChecker(), member);
|
|
3706
|
+
if (literal === undefined)
|
|
3707
|
+
return "not-finite";
|
|
3708
|
+
literals.push(literal);
|
|
3709
|
+
}
|
|
3710
|
+
// The order TypeScript keeps a union in depends on what it checked first.
|
|
3711
|
+
return literals.sort(compareLabels);
|
|
3712
|
+
},
|
|
3713
|
+
propertyKeysOf(node) {
|
|
3714
|
+
const found = typeChecker().getTypeAtLocation(node);
|
|
3715
|
+
const type = widened(typeChecker(), found);
|
|
3716
|
+
if (type === undefined || !(type.flags & (ts.TypeFlags.Object | ts.TypeFlags.Intersection))) {
|
|
3717
|
+
return "not-object";
|
|
3718
|
+
}
|
|
3719
|
+
if (isArrayLike(typeChecker(), type))
|
|
3720
|
+
return "not-object";
|
|
3721
|
+
if (hasGenericKeys(typeChecker(), type) || hasUnresolvedKeys(typeChecker(), type)) {
|
|
3722
|
+
return "generic";
|
|
3723
|
+
}
|
|
3724
|
+
const required = [];
|
|
3725
|
+
const optional = [];
|
|
3726
|
+
for (const property of typeChecker().getPropertiesOfType(type)) {
|
|
3727
|
+
(property.flags & ts.SymbolFlags.Optional ? optional : required).push(property.getName());
|
|
3728
|
+
}
|
|
3729
|
+
return {
|
|
3730
|
+
required: required.sort(),
|
|
3731
|
+
optional: optional.sort(),
|
|
3732
|
+
indexSignature: typeChecker().getIndexInfosOfType(type).length > 0,
|
|
3733
|
+
};
|
|
3734
|
+
},
|
|
3735
|
+
isDiscriminant(object, propertyName) {
|
|
3736
|
+
const type = widened(typeChecker(), typeChecker().getTypeAtLocation(object));
|
|
1600
3737
|
if (!type?.isUnion())
|
|
1601
3738
|
return false;
|
|
1602
3739
|
// `s?.kind` narrows `s` when the rest is a discriminated union, so `undefined` and `null`
|
|
@@ -1622,6 +3759,38 @@ root, packageOf, isEntry, dependenciesOf) {
|
|
|
1622
3759
|
// When every member has the same tag type, narrowing on it removes no member.
|
|
1623
3760
|
return rest.some((tag) => tag !== first);
|
|
1624
3761
|
},
|
|
3762
|
+
declaredEntryCanBeMissing(access) {
|
|
3763
|
+
const checker = typeChecker();
|
|
3764
|
+
const object = widened(checker, checker.getTypeAtLocation(access.expression));
|
|
3765
|
+
const key = widened(checker, checker.getTypeAtLocation(access.argumentExpression));
|
|
3766
|
+
if (object === undefined || key === undefined)
|
|
3767
|
+
return false;
|
|
3768
|
+
const keys = key.isUnion() ? key.types : [key];
|
|
3769
|
+
return keys.some((one) => {
|
|
3770
|
+
const number = (one.flags & ts.TypeFlags.NumberLike) !== 0;
|
|
3771
|
+
if (!number && (one.flags & ts.TypeFlags.StringLike) === 0)
|
|
3772
|
+
return false;
|
|
3773
|
+
let declared;
|
|
3774
|
+
if (one.isStringLiteral() || one.isNumberLiteral()) {
|
|
3775
|
+
const property = checker.getPropertyOfType(object, String(one.value));
|
|
3776
|
+
if (property !== undefined)
|
|
3777
|
+
declared = checker.getTypeOfSymbol(property);
|
|
3778
|
+
}
|
|
3779
|
+
declared ??= ((number ? checker.getIndexInfoOfType(object, ts.IndexKind.Number) : undefined) ??
|
|
3780
|
+
checker.getIndexInfoOfType(object, ts.IndexKind.String))?.type;
|
|
3781
|
+
return declared !== undefined && includesUndefined(declared);
|
|
3782
|
+
});
|
|
3783
|
+
},
|
|
3784
|
+
sameType(a, b) {
|
|
3785
|
+
const checker = typeChecker();
|
|
3786
|
+
const left = checker.getTypeAtLocation(a);
|
|
3787
|
+
const right = checker.getTypeAtLocation(b);
|
|
3788
|
+
// `any` (written, or a type that does not resolve) is assignable both ways: it says nothing.
|
|
3789
|
+
if ((left.flags | right.flags) & ts.TypeFlags.Any)
|
|
3790
|
+
return false;
|
|
3791
|
+
return (left === right ||
|
|
3792
|
+
(checker.isTypeAssignableTo(left, right) && checker.isTypeAssignableTo(right, left)));
|
|
3793
|
+
},
|
|
1625
3794
|
typeTextOf(node) {
|
|
1626
3795
|
return typeChecker()
|
|
1627
3796
|
.typeToString(typeChecker().getTypeAtLocation(node))
|
|
@@ -1726,6 +3895,53 @@ root, packageOf, isEntry, dependenciesOf) {
|
|
|
1726
3895
|
const type = declaration === undefined ? undefined : declaredTypeOf(checker, declaration);
|
|
1727
3896
|
return type === undefined ? undefined : guaranteeValuesOfType(checker, type, name);
|
|
1728
3897
|
},
|
|
3898
|
+
heldAliasesOf(node, contextual = false) {
|
|
3899
|
+
const checker = typeChecker();
|
|
3900
|
+
const type = contextual ? checker.getContextualType(node) : checker.getTypeAtLocation(node);
|
|
3901
|
+
if (type === undefined)
|
|
3902
|
+
return [];
|
|
3903
|
+
const found = new Map();
|
|
3904
|
+
const seen = new Set();
|
|
3905
|
+
const enter = (given, depth, top) => {
|
|
3906
|
+
const base = widened(checker, given);
|
|
3907
|
+
if (base === undefined || depth > 3 || seen.has(base))
|
|
3908
|
+
return;
|
|
3909
|
+
seen.add(base);
|
|
3910
|
+
const alias = base.aliasSymbol;
|
|
3911
|
+
if (!top &&
|
|
3912
|
+
alias?.declarations?.some(ts.isTypeAliasDeclaration) === true &&
|
|
3913
|
+
(base.aliasTypeArguments?.length ?? 0) === 0) {
|
|
3914
|
+
found.set(`${alias.getName()}#${String(alias.declarations[0]?.pos ?? 0)}`, alias);
|
|
3915
|
+
return;
|
|
3916
|
+
}
|
|
3917
|
+
if (base.isUnion()) {
|
|
3918
|
+
for (const member of base.types)
|
|
3919
|
+
enter(member, depth, top);
|
|
3920
|
+
}
|
|
3921
|
+
else if (base.flags & ts.TypeFlags.Object) {
|
|
3922
|
+
if (isTypeReference(base)) {
|
|
3923
|
+
for (const argument of checker.getTypeArguments(base)) {
|
|
3924
|
+
enter(argument, depth + 1, false);
|
|
3925
|
+
}
|
|
3926
|
+
}
|
|
3927
|
+
else if (isAnonymousObject(base)) {
|
|
3928
|
+
for (const property of checker.getPropertiesOfType(base)) {
|
|
3929
|
+
const declaration = property.valueDeclaration ?? property.declarations?.[0];
|
|
3930
|
+
if (declaration !== undefined) {
|
|
3931
|
+
enter(checker.getTypeOfSymbolAtLocation(property, declaration), depth + 1, false);
|
|
3932
|
+
}
|
|
3933
|
+
}
|
|
3934
|
+
}
|
|
3935
|
+
}
|
|
3936
|
+
};
|
|
3937
|
+
enter(type, 0, true);
|
|
3938
|
+
return [...found.values()]
|
|
3939
|
+
.sort((a, b) => (a.getName() < b.getName() ? -1 : a.getName() > b.getName() ? 1 : 0))
|
|
3940
|
+
.flatMap((symbol) => {
|
|
3941
|
+
const ref = refOfSymbol(symbol);
|
|
3942
|
+
return ref === undefined ? [] : [ref];
|
|
3943
|
+
});
|
|
3944
|
+
},
|
|
1729
3945
|
contextualAliasOf(node) {
|
|
1730
3946
|
const checker = typeChecker();
|
|
1731
3947
|
const found = checker.getContextualType(node);
|
|
@@ -1739,8 +3955,16 @@ root, packageOf, isEntry, dependenciesOf) {
|
|
|
1739
3955
|
const whole = aliasOf(type);
|
|
1740
3956
|
if (whole !== undefined)
|
|
1741
3957
|
return refOfSymbol(whole);
|
|
1742
|
-
|
|
1743
|
-
const
|
|
3958
|
+
// `return "x"` in an async function expects `OrderId | PromiseLike<OrderId>`.
|
|
3959
|
+
const members = (type.isUnion() ? type.types : [type]).filter((member) => (member.flags & NULLISH) === 0 &&
|
|
3960
|
+
!(member.getSymbol()?.getName() === "PromiseLike" &&
|
|
3961
|
+
member.symbol.declarations?.some((declaration) => program.isSourceFileDefaultLibrary(declaration.getSourceFile())) === true));
|
|
3962
|
+
// `show("bad")` with a parameter `OrderId | number`: a member without an alias that the value
|
|
3963
|
+
// does not fit (`number`) is not what it becomes.
|
|
3964
|
+
const own = checker.getTypeAtLocation(node);
|
|
3965
|
+
const aliases = new Set(members
|
|
3966
|
+
.filter((member) => aliasOf(member) !== undefined || checker.isTypeAssignableTo(own, member))
|
|
3967
|
+
.map(aliasOf));
|
|
1744
3968
|
const [only] = aliases;
|
|
1745
3969
|
return aliases.size === 1 && only !== undefined ? refOfSymbol(only) : undefined;
|
|
1746
3970
|
},
|
|
@@ -1776,6 +4000,32 @@ root, packageOf, isEntry, dependenciesOf) {
|
|
|
1776
4000
|
}
|
|
1777
4001
|
return renamedNames;
|
|
1778
4002
|
},
|
|
4003
|
+
statedDeclarationsIn(sourceFile) {
|
|
4004
|
+
anyMarker ??= program.getSourceFiles().some(declaresAnyMarker);
|
|
4005
|
+
return anyMarker ? statedDeclarationsOf(typeChecker(), sourceFile) : [];
|
|
4006
|
+
},
|
|
4007
|
+
statedReferencesIn(sourceFile, proves) {
|
|
4008
|
+
anyMarker ??= program.getSourceFiles().some(declaresAnyMarker);
|
|
4009
|
+
// A tag needs no marker: its scan reads only the files whose text writes it.
|
|
4010
|
+
if (!anyMarker && (proves.tags ?? []).length === 0)
|
|
4011
|
+
return [];
|
|
4012
|
+
ledgerScan ??= createLedgerScan(program, typeChecker, (file) => own.has(toTypeScriptPath(file)), refOfSymbol);
|
|
4013
|
+
return ledgerScan.referencesIn(sourceFile, proves);
|
|
4014
|
+
},
|
|
4015
|
+
typedFlowsIn(sourceFile, proves, budget = FLOW_BUDGET) {
|
|
4016
|
+
anyMarker ??= program.getSourceFiles().some(declaresAnyMarker);
|
|
4017
|
+
if (!anyMarker)
|
|
4018
|
+
return [];
|
|
4019
|
+
ledgerScan ??= createLedgerScan(program, typeChecker, (file) => own.has(toTypeScriptPath(file)), refOfSymbol);
|
|
4020
|
+
return ledgerScan.flowsIn(sourceFile, proves, budget);
|
|
4021
|
+
},
|
|
4022
|
+
holdsOnlyPrimitives(node) {
|
|
4023
|
+
return onlyPrimitives(typeChecker(), typeChecker().getTypeAtLocation(node), 0);
|
|
4024
|
+
},
|
|
4025
|
+
valueOriginOf(node) {
|
|
4026
|
+
const symbol = typeChecker().getTypeAtLocation(node).getSymbol();
|
|
4027
|
+
return symbol === undefined ? undefined : refOfSymbol(symbol);
|
|
4028
|
+
},
|
|
1779
4029
|
declaresTrait(name) {
|
|
1780
4030
|
const known = declaredTraits.get(name);
|
|
1781
4031
|
if (known !== undefined)
|
|
@@ -1876,6 +4126,21 @@ root, packageOf, isEntry, dependenciesOf) {
|
|
|
1876
4126
|
awaitedTagsOf(node, property) {
|
|
1877
4127
|
return awaitedTags(typeChecker(), typeChecker().getTypeAtLocation(node), property);
|
|
1878
4128
|
},
|
|
4129
|
+
propIsDeeplyReadonly(attribute) {
|
|
4130
|
+
const checker = typeChecker();
|
|
4131
|
+
// The component's props type, as the attributes are checked against it.
|
|
4132
|
+
const props = checker.getContextualType(attribute.parent);
|
|
4133
|
+
const symbol = props === undefined
|
|
4134
|
+
? undefined
|
|
4135
|
+
: checker.getPropertyOfType(props, attribute.name.getText());
|
|
4136
|
+
const declarations = symbol?.declarations ?? [];
|
|
4137
|
+
if (symbol === undefined ||
|
|
4138
|
+
declarations.length === 0 ||
|
|
4139
|
+
!declarations.every((one) => own.has(one.getSourceFile().fileName))) {
|
|
4140
|
+
return false;
|
|
4141
|
+
}
|
|
4142
|
+
return isDeeplyReadonly(checker, checker.getTypeOfSymbol(symbol), 0);
|
|
4143
|
+
},
|
|
1879
4144
|
isReadonlyPath(node) {
|
|
1880
4145
|
return isReadonlyChain(typeChecker(), node);
|
|
1881
4146
|
},
|
|
@@ -1908,6 +4173,115 @@ root, packageOf, isEntry, dependenciesOf) {
|
|
|
1908
4173
|
const members = type.isUnion() ? type.types : [type];
|
|
1909
4174
|
return members.every((member) => (member.flags & PRIMITIVE_FLAGS) !== 0);
|
|
1910
4175
|
},
|
|
4176
|
+
isNumber(node) {
|
|
4177
|
+
const type = widened(typeChecker(), typeChecker().getTypeAtLocation(node));
|
|
4178
|
+
if (type === undefined)
|
|
4179
|
+
return false;
|
|
4180
|
+
return (type.isUnion() ? type.types : [type]).every(isNumberType);
|
|
4181
|
+
},
|
|
4182
|
+
platformTypeOf(node) {
|
|
4183
|
+
return platformNameOf(typeChecker().getTypeAtLocation(node));
|
|
4184
|
+
},
|
|
4185
|
+
arrayElementsOf(node) {
|
|
4186
|
+
return arrayElements(typeChecker().getTypeAtLocation(node));
|
|
4187
|
+
},
|
|
4188
|
+
readIsPlain(node) {
|
|
4189
|
+
const checker = typeChecker();
|
|
4190
|
+
if (ts.isBindingElement(node)) {
|
|
4191
|
+
const pattern = node.parent;
|
|
4192
|
+
const source = checker.getTypeAtLocation(pattern);
|
|
4193
|
+
if (ts.isArrayBindingPattern(pattern))
|
|
4194
|
+
return arrayElements(source) !== undefined;
|
|
4195
|
+
if (node.dotDotDotToken !== undefined)
|
|
4196
|
+
return false;
|
|
4197
|
+
const key = node.propertyName ?? node.name;
|
|
4198
|
+
if (!ts.isIdentifier(key) && !ts.isStringLiteral(key))
|
|
4199
|
+
return false;
|
|
4200
|
+
const members = definedMembers(checker, source);
|
|
4201
|
+
const [only] = members ?? [];
|
|
4202
|
+
const property = members?.length === 1 && only !== undefined
|
|
4203
|
+
? checker.getPropertyOfType(only, key.text)
|
|
4204
|
+
: undefined;
|
|
4205
|
+
return property !== undefined && plainProperty(property, source);
|
|
4206
|
+
}
|
|
4207
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
4208
|
+
const property = checker.getSymbolAtLocation(node.name);
|
|
4209
|
+
return (property !== undefined &&
|
|
4210
|
+
plainProperty(property, checker.getTypeAtLocation(node.expression)));
|
|
4211
|
+
}
|
|
4212
|
+
const receiver = checker.getTypeAtLocation(node.expression);
|
|
4213
|
+
const members = definedMembers(checker, receiver);
|
|
4214
|
+
const [only] = members ?? [];
|
|
4215
|
+
if (members?.length !== 1 || only === undefined)
|
|
4216
|
+
return false;
|
|
4217
|
+
const argument = node.argumentExpression;
|
|
4218
|
+
if (ts.isNumericLiteral(argument) && checker.isTupleType(only) && isTypeReference(only)) {
|
|
4219
|
+
const target = only.target;
|
|
4220
|
+
const fixed = "minLength" in target && typeof target.minLength === "number" ? target.minLength : 0;
|
|
4221
|
+
return Number(argument.text) < fixed;
|
|
4222
|
+
}
|
|
4223
|
+
if (ts.isStringLiteralLike(argument)) {
|
|
4224
|
+
const property = checker.getPropertyOfType(only, argument.text);
|
|
4225
|
+
if (property !== undefined)
|
|
4226
|
+
return plainProperty(property, receiver);
|
|
4227
|
+
}
|
|
4228
|
+
// A key of an index signature: the read itself runs nothing, but it may find nothing, so the
|
|
4229
|
+
// value must be a primitive that is not read further.
|
|
4230
|
+
if (checker.getIndexInfosOfType(only).length === 0)
|
|
4231
|
+
return false;
|
|
4232
|
+
const value = checker.getTypeAtLocation(node);
|
|
4233
|
+
const parts = value.isUnion() ? value.types : [value];
|
|
4234
|
+
if (!parts.every((part) => (part.flags & PRIMITIVE_FLAGS) !== 0))
|
|
4235
|
+
return false;
|
|
4236
|
+
let outer = node;
|
|
4237
|
+
while (ts.isParenthesizedExpression(outer.parent) ||
|
|
4238
|
+
ts.isNonNullExpression(outer.parent) ||
|
|
4239
|
+
ts.isAsExpression(outer.parent) ||
|
|
4240
|
+
ts.isSatisfiesExpression(outer.parent)) {
|
|
4241
|
+
outer = outer.parent;
|
|
4242
|
+
}
|
|
4243
|
+
const parent = outer.parent;
|
|
4244
|
+
return !((ts.isPropertyAccessExpression(parent) ||
|
|
4245
|
+
ts.isElementAccessExpression(parent) ||
|
|
4246
|
+
ts.isCallExpression(parent) ||
|
|
4247
|
+
ts.isTaggedTemplateExpression(parent)) &&
|
|
4248
|
+
(ts.isTaggedTemplateExpression(parent) ? parent.tag : parent.expression) === outer);
|
|
4249
|
+
},
|
|
4250
|
+
readMayRunAccessor(node) {
|
|
4251
|
+
const property = typeChecker().getSymbolAtLocation(node.name);
|
|
4252
|
+
return (property?.declarations ?? []).some((one) => own.has(toTypeScriptPath(one.getSourceFile().fileName)) &&
|
|
4253
|
+
(ts.isGetAccessorDeclaration(one) || ts.isSetAccessorDeclaration(one)) &&
|
|
4254
|
+
one.body !== undefined);
|
|
4255
|
+
},
|
|
4256
|
+
isPlatformName(node) {
|
|
4257
|
+
const found = typeChecker().getSymbolAtLocation(node);
|
|
4258
|
+
if (found === undefined)
|
|
4259
|
+
return false;
|
|
4260
|
+
const symbol = found.flags & ts.SymbolFlags.Alias ? typeChecker().getAliasedSymbol(found) : found;
|
|
4261
|
+
const declarations = symbol.declarations ?? [];
|
|
4262
|
+
return (declarations.length > 0 &&
|
|
4263
|
+
declarations.every((one) => isPlatformSourceFile(program, one.getSourceFile())));
|
|
4264
|
+
},
|
|
4265
|
+
isPlainClass(node) {
|
|
4266
|
+
const checker = typeChecker();
|
|
4267
|
+
const type = checker.getTypeAtLocation(node);
|
|
4268
|
+
if (platformNameOf(type)?.endsWith("Constructor") === true)
|
|
4269
|
+
return true;
|
|
4270
|
+
const found = checker.getSymbolAtLocation(node);
|
|
4271
|
+
if (found === undefined)
|
|
4272
|
+
return false;
|
|
4273
|
+
const symbol = found.flags & ts.SymbolFlags.Alias ? checker.getAliasedSymbol(found) : found;
|
|
4274
|
+
const declarations = symbol.declarations ?? [];
|
|
4275
|
+
const ownFile = (one) => own.has(toTypeScriptPath(one.getSourceFile().fileName));
|
|
4276
|
+
// A static `Symbol.hasInstance` of the class or a class it extends runs code.
|
|
4277
|
+
const runsCode = checker
|
|
4278
|
+
.getPropertiesOfType(type)
|
|
4279
|
+
.some((property) => property.getName().includes("hasInstance") &&
|
|
4280
|
+
(property.declarations ?? []).some(ownFile));
|
|
4281
|
+
return (!runsCode &&
|
|
4282
|
+
declarations.length > 0 &&
|
|
4283
|
+
declarations.every((one) => ts.isClassDeclaration(one) && ownFile(one)));
|
|
4284
|
+
},
|
|
1911
4285
|
isLibraryType(node) {
|
|
1912
4286
|
const inLibrary = (declaration) => {
|
|
1913
4287
|
const file = declaration.getSourceFile();
|