@openrewrite/rewrite 8.116.1 → 8.118.0
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/java/type.d.ts.map +1 -1
- package/dist/java/type.js +12 -3
- package/dist/java/type.js.map +1 -1
- package/dist/javascript/parser.d.ts.map +1 -1
- package/dist/javascript/parser.js +5 -2
- package/dist/javascript/parser.js.map +1 -1
- package/dist/javascript/rpc.js +6 -2
- package/dist/javascript/rpc.js.map +1 -1
- package/dist/javascript/tree.d.ts +2 -0
- package/dist/javascript/tree.d.ts.map +1 -1
- package/dist/javascript/tree.js.map +1 -1
- package/dist/javascript/type-mapping.d.ts +98 -4
- package/dist/javascript/type-mapping.d.ts.map +1 -1
- package/dist/javascript/type-mapping.js +562 -67
- package/dist/javascript/type-mapping.js.map +1 -1
- package/dist/javascript/visitor.d.ts.map +1 -1
- package/dist/javascript/visitor.js +4 -2
- package/dist/javascript/visitor.js.map +1 -1
- package/dist/rewrite-javascript-version.txt +1 -1
- package/dist/yaml/parser.js +4 -2
- package/dist/yaml/parser.js.map +1 -1
- package/package.json +1 -1
- package/src/java/type.ts +12 -2
- package/src/javascript/parser.ts +5 -2
- package/src/javascript/rpc.ts +6 -2
- package/src/javascript/tree.ts +2 -0
- package/src/javascript/type-mapping.ts +590 -63
- package/src/javascript/visitor.ts +4 -2
- package/src/yaml/parser.ts +4 -2
|
@@ -48,6 +48,17 @@ interface ModuleBinding {
|
|
|
48
48
|
exportName?: string;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
/** What a heritage clause gives a class. */
|
|
52
|
+
interface Heritage {
|
|
53
|
+
supertype?: Type.Class;
|
|
54
|
+
interfaces: Type.Class[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function asClass(type: Type): Type.Class | undefined {
|
|
58
|
+
const unwrapped = Type.isParameterized(type) ? type.type : type;
|
|
59
|
+
return Type.isClass(unwrapped) ? unwrapped : undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
51
62
|
function isGlobalAugmentation(declaration: ts.Declaration): boolean {
|
|
52
63
|
return ts.isModuleDeclaration(declaration) && (declaration.flags & ts.NodeFlags.GlobalAugmentation) !== 0;
|
|
53
64
|
}
|
|
@@ -70,6 +81,11 @@ export function moduleClass(fullyQualifiedName: string): Type.Class {
|
|
|
70
81
|
} as Type.Class;
|
|
71
82
|
}
|
|
72
83
|
|
|
84
|
+
interface ImplementedMember {
|
|
85
|
+
owner: Type.FullyQualified;
|
|
86
|
+
name: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
73
89
|
export class JavaScriptTypeMapping {
|
|
74
90
|
// Primary cache: Use type signatures (preferring type.id) as cache keys
|
|
75
91
|
// TypeScript assigns stable IDs to all types, so we don't need secondary caches
|
|
@@ -87,6 +103,11 @@ export class JavaScriptTypeMapping {
|
|
|
87
103
|
// instantiation has a type id of its own that the type cache cannot match.
|
|
88
104
|
private readonly functionTypesInProgress = new Set<ts.Symbol>();
|
|
89
105
|
|
|
106
|
+
// Classes whose bases are being mapped, each with what waits to read them. A namesake base
|
|
107
|
+
// gives its own bases in its place, and has none to give until it is done.
|
|
108
|
+
private readonly heritageInProgress = new Map<Type.Class, (() => void)[]>();
|
|
109
|
+
|
|
110
|
+
private readonly referencedImplementations = new WeakMap<ts.SourceFile, Map<ts.VariableDeclaration, ImplementedMember | null>>();
|
|
90
111
|
|
|
91
112
|
/**
|
|
92
113
|
* @param sourceRoot What a parsed file's module is named relative to.
|
|
@@ -656,7 +677,10 @@ export class JavaScriptTypeMapping {
|
|
|
656
677
|
} else {
|
|
657
678
|
// For non-imported variables, check if they belong to a class/interface/namespace
|
|
658
679
|
const parentSymbol = (actualSymbol as any).parent as ts.Symbol | undefined;
|
|
659
|
-
|
|
680
|
+
const typeLiteral = this.aliasedTypeLiteral(actualSymbol.valueDeclaration?.parent);
|
|
681
|
+
if (typeLiteral) {
|
|
682
|
+
ownerType = this.getType(typeLiteral);
|
|
683
|
+
} else if (parentSymbol) {
|
|
660
684
|
const parentType = this.checker.getDeclaredTypeOfSymbol(parentSymbol);
|
|
661
685
|
if (parentType) {
|
|
662
686
|
ownerType = this.getType(parentType);
|
|
@@ -739,6 +763,22 @@ export class JavaScriptTypeMapping {
|
|
|
739
763
|
this.moduleSpecifierArgument(node) : undefined;
|
|
740
764
|
}
|
|
741
765
|
|
|
766
|
+
/**
|
|
767
|
+
* TypeScript tells an intrinsic element (`<div>`, `<my-el>`, `<svg:rect>`) from a component by the tag's spelling,
|
|
768
|
+
* and a tag holding a string, as `const Tag = 'h1'; <Tag/>`, renders an intrinsic element too.
|
|
769
|
+
*/
|
|
770
|
+
private isJsxComponent(node: ts.Node): node is ts.JsxOpeningLikeElement {
|
|
771
|
+
if (!ts.isJsxOpeningLikeElement(node) || ts.isJsxNamespacedName(node.tagName)) {
|
|
772
|
+
return false;
|
|
773
|
+
}
|
|
774
|
+
const tagName = node.tagName;
|
|
775
|
+
if (ts.isIdentifier(tagName) && (/^[a-z]/.test(tagName.text) || tagName.text.includes('-'))) {
|
|
776
|
+
return false;
|
|
777
|
+
}
|
|
778
|
+
const tagType = this.checker.getTypeAtLocation(tagName);
|
|
779
|
+
return !(tagType.isUnion() ? tagType.types : [tagType]).some(t => t.flags & ts.TypeFlags.StringLike);
|
|
780
|
+
}
|
|
781
|
+
|
|
742
782
|
private isImportDefer(node: ts.Node): boolean {
|
|
743
783
|
return ts.isMetaProperty(node) && node.keywordToken === ts.SyntaxKind.ImportKeyword && node.name.text === 'defer';
|
|
744
784
|
}
|
|
@@ -881,14 +921,349 @@ export class JavaScriptTypeMapping {
|
|
|
881
921
|
return moduleOfFile(this.sourceRoot && path.isAbsolute(fileName) ? path.relative(this.sourceRoot, fileName) : fileName);
|
|
882
922
|
}
|
|
883
923
|
|
|
924
|
+
/** Whether a declaration is in a parsed source file, which a `.d.ts` file or an installed package is not. */
|
|
925
|
+
private isParsedSource(declaration: ts.Node): boolean {
|
|
926
|
+
const sourceFile = declaration.getSourceFile() as ts.SourceFile | undefined;
|
|
927
|
+
return !!sourceFile && !sourceFile.isDeclarationFile && !this.isInstalled(sourceFile.fileName);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
/**
|
|
931
|
+
* What a call into a parsed source file calls, as that file's declaration names it: the function
|
|
932
|
+
* the callee is declared as, or the top-level variable holding it, as `const f = () => …` or
|
|
933
|
+
* `const ky = createInstance()` does, where the signature it calls is declared in parsed source too.
|
|
934
|
+
* A local variable holding a function names it on its file, as a nested function declaration is.
|
|
935
|
+
* One destructured from an object, as in `const {setState} = createStore()`, calls the member it is
|
|
936
|
+
* destructured from, as `store.setState()` does, where a named type in parsed source declares it.
|
|
937
|
+
* A callee bound to a module, by an import or a `require`, calls the declaration its signature
|
|
938
|
+
* resolves to, which may be one a re-export such as `module.exports = require('./lib/express')` leads to.
|
|
939
|
+
*/
|
|
940
|
+
private parsedCallee(symbol: ts.Symbol | undefined, signature: ts.Signature, boundToModule: boolean):
|
|
941
|
+
{declaringType: Type.FullyQualified, name: string, isStatic?: boolean} | undefined {
|
|
942
|
+
const target = signature.getDeclaration();
|
|
943
|
+
if (!target || !this.isParsedSource(target)) {
|
|
944
|
+
return undefined;
|
|
945
|
+
}
|
|
946
|
+
const declaration = symbol?.valueDeclaration;
|
|
947
|
+
if (declaration && this.isParsedSource(declaration)) {
|
|
948
|
+
if (ts.isFunctionDeclaration(declaration)) {
|
|
949
|
+
return {declaringType: this.functionOwner(declaration), name: this.functionDeclarationName(declaration)};
|
|
950
|
+
}
|
|
951
|
+
if (this.isTopLevelVariable(declaration) && !this.moduleBinding(declaration, declaration)) {
|
|
952
|
+
return {declaringType: this.sourceModuleType(declaration.getSourceFile()), name: declaration.name.text, isStatic: true};
|
|
953
|
+
}
|
|
954
|
+
const destructured = ts.isBindingElement(declaration) ? this.destructuredMember(declaration) : undefined;
|
|
955
|
+
if (destructured) {
|
|
956
|
+
return {declaringType: destructured.owner, name: destructured.name};
|
|
957
|
+
}
|
|
958
|
+
const fn = ts.isVariableDeclaration(declaration) && declaration.initializer ?
|
|
959
|
+
this.heldFunction(declaration.initializer) : undefined;
|
|
960
|
+
if (fn && ts.isVariableDeclaration(declaration) && ts.isIdentifier(declaration.name)) {
|
|
961
|
+
const local = this.implementedMember(fn);
|
|
962
|
+
return local ?
|
|
963
|
+
{declaringType: local.owner, name: local.name} :
|
|
964
|
+
{declaringType: this.sourceModuleType(declaration.getSourceFile()), name: declaration.name.text};
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
if (!boundToModule) {
|
|
968
|
+
return undefined;
|
|
969
|
+
}
|
|
970
|
+
if (ts.isFunctionDeclaration(target)) {
|
|
971
|
+
return {declaringType: this.functionOwner(target), name: this.functionDeclarationName(target)};
|
|
972
|
+
}
|
|
973
|
+
const identity = (ts.isArrowFunction(target) || ts.isFunctionExpression(target)) ? this.functionIdentity(target) : undefined;
|
|
974
|
+
return identity && {declaringType: identity.owner, name: identity.name};
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
private castTarget(fn: ts.ArrowFunction | ts.FunctionExpression): ts.Node {
|
|
978
|
+
let node: ts.Node = fn;
|
|
979
|
+
while (this.isCast(node.parent)) {
|
|
980
|
+
node = node.parent;
|
|
981
|
+
}
|
|
982
|
+
return node;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
private isCast(node: ts.Node): node is ts.ParenthesizedExpression | ts.AsExpression | ts.SatisfiesExpression | ts.TypeAssertion {
|
|
986
|
+
return ts.isParenthesizedExpression(node) || ts.isAsExpression(node) ||
|
|
987
|
+
ts.isSatisfiesExpression(node) || ts.isTypeAssertionExpression(node);
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
private isTopLevelVariable(declaration: ts.Node): declaration is ts.VariableDeclaration & {name: ts.Identifier} {
|
|
991
|
+
return ts.isVariableDeclaration(declaration) && ts.isIdentifier(declaration.name) &&
|
|
992
|
+
ts.isVariableStatement(declaration.parent.parent) && ts.isSourceFile(declaration.parent.parent.parent);
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
/**
|
|
996
|
+
* What declares a function expression or arrow function: the member of a named type it implements,
|
|
997
|
+
* where no module binding names it, or else its file, under its own name, or else the one a top-level
|
|
998
|
+
* `const f = …`, an `exports.f = …` assignment, the property it is assigned to or the local
|
|
999
|
+
* variable it initializes gives it.
|
|
1000
|
+
*/
|
|
1001
|
+
private functionIdentity(fn: ts.ArrowFunction | ts.FunctionExpression): ImplementedMember | undefined {
|
|
1002
|
+
const bound = this.moduleBindingName(fn);
|
|
1003
|
+
const implemented = bound === undefined ? this.implementedMember(fn) : undefined;
|
|
1004
|
+
if (implemented) {
|
|
1005
|
+
return implemented;
|
|
1006
|
+
}
|
|
1007
|
+
const name = fn.name?.text ?? bound ?? this.memberFunctionName(fn) ?? this.localBindingName(fn);
|
|
1008
|
+
return name === undefined ? undefined : {owner: this.sourceModuleType(fn.getSourceFile()), name};
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
private localBindingName(fn: ts.ArrowFunction | ts.FunctionExpression): string | undefined {
|
|
1012
|
+
const node = this.castTarget(fn);
|
|
1013
|
+
const parent = node.parent;
|
|
1014
|
+
return ts.isVariableDeclaration(parent) && parent.initializer === node && ts.isIdentifier(parent.name) ?
|
|
1015
|
+
parent.name.text : undefined;
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
/**
|
|
1019
|
+
* The member of a named type declared in parsed source that a function implements, which is
|
|
1020
|
+
* where a call through that type is declared, as in Java a call is declared on the interface it is
|
|
1021
|
+
* made through. That is the member of an object literal's contextual type, as in
|
|
1022
|
+
* `const api: Store = {setState() {}}`, or of the receiver a function is assigned to a property of,
|
|
1023
|
+
* as in `inst.create = () => …`. A local variable holding a function implements the member its
|
|
1024
|
+
* annotation indexes, as in `const setState: Store['setState'] = …`, or the one an object literal
|
|
1025
|
+
* holding it as a property implements, as `{setState}` does.
|
|
1026
|
+
*/
|
|
1027
|
+
private implementedMember(fn: ts.ArrowFunction | ts.FunctionExpression | ts.MethodDeclaration): ImplementedMember | undefined {
|
|
1028
|
+
if (!this.isParsedSource(fn)) {
|
|
1029
|
+
return undefined;
|
|
1030
|
+
}
|
|
1031
|
+
if (ts.isMethodDeclaration(fn)) {
|
|
1032
|
+
return ts.isObjectLiteralExpression(fn.parent) ? this.memberOfContextualType(fn.parent, fn.name) : undefined;
|
|
1033
|
+
}
|
|
1034
|
+
const node = this.castTarget(fn);
|
|
1035
|
+
const parent = node.parent;
|
|
1036
|
+
if (ts.isPropertyAssignment(parent) && parent.initializer === node) {
|
|
1037
|
+
return this.memberOfContextualType(parent.parent, parent.name);
|
|
1038
|
+
}
|
|
1039
|
+
if (ts.isBinaryExpression(parent) && parent.right === node) {
|
|
1040
|
+
return this.assignedMember(parent);
|
|
1041
|
+
}
|
|
1042
|
+
if (ts.isVariableDeclaration(parent) && parent.initializer === node && ts.isIdentifier(parent.name) &&
|
|
1043
|
+
!this.isTopLevelVariable(parent)) {
|
|
1044
|
+
return this.indexedMember(parent.type) ?? this.referencedImplementation(parent);
|
|
1045
|
+
}
|
|
1046
|
+
return undefined;
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
private assignedMember(assignment: ts.BinaryExpression): ImplementedMember | undefined {
|
|
1050
|
+
const target = assignment.left;
|
|
1051
|
+
if (assignment.operatorToken.kind !== ts.SyntaxKind.EqualsToken ||
|
|
1052
|
+
!(ts.isPropertyAccessExpression(target) || ts.isElementAccessExpression(target)) ||
|
|
1053
|
+
this.isPrototype(target.expression)) {
|
|
1054
|
+
return undefined;
|
|
1055
|
+
}
|
|
1056
|
+
const name = ts.isPropertyAccessExpression(target) ? target.name :
|
|
1057
|
+
ts.isStringLiteralLike(target.argumentExpression) ? target.argumentExpression : undefined;
|
|
1058
|
+
return name && this.receiverTypes(target.expression)
|
|
1059
|
+
.map(type => this.memberOf(type, name.text))
|
|
1060
|
+
.find(member => member !== undefined);
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
private destructuredMember(element: ts.BindingElement): ImplementedMember | undefined {
|
|
1064
|
+
const name = element.propertyName ?? element.name;
|
|
1065
|
+
if (!ts.isObjectBindingPattern(element.parent) || element.dotDotDotToken ||
|
|
1066
|
+
!(ts.isIdentifier(name) || ts.isStringLiteralLike(name))) {
|
|
1067
|
+
return undefined;
|
|
1068
|
+
}
|
|
1069
|
+
return this.memberOf(this.checker.getTypeAtLocation(element.parent), name.text);
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
private memberOfContextualType(literal: ts.ObjectLiteralExpression, name: ts.PropertyName): ImplementedMember | undefined {
|
|
1073
|
+
if (!ts.isIdentifier(name) && !ts.isStringLiteralLike(name) && !ts.isNumericLiteral(name)) {
|
|
1074
|
+
return undefined;
|
|
1075
|
+
}
|
|
1076
|
+
const type = this.checker.getContextualType(literal);
|
|
1077
|
+
return type && this.memberOf(type, name.text);
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
/**
|
|
1081
|
+
* The types a receiver may be written through: its own, or where that is `any`, as with
|
|
1082
|
+
* `const ky: Partial<KyInstance> & any`, each part of the intersection its declaration is annotated with.
|
|
1083
|
+
*/
|
|
1084
|
+
private receiverTypes(receiver: ts.Expression): ts.Type[] {
|
|
1085
|
+
const type = this.checker.getTypeAtLocation(receiver);
|
|
1086
|
+
if (!(type.flags & ts.TypeFlags.Any)) {
|
|
1087
|
+
return [type];
|
|
1088
|
+
}
|
|
1089
|
+
const declaration = ts.isIdentifier(receiver) ? this.checker.getSymbolAtLocation(receiver)?.valueDeclaration : undefined;
|
|
1090
|
+
const annotation = declaration && (ts.isVariableDeclaration(declaration) || ts.isParameter(declaration)) ? declaration.type : undefined;
|
|
1091
|
+
const parts = annotation && ts.isIntersectionTypeNode(annotation) ? annotation.types : [];
|
|
1092
|
+
return parts.map(part => this.checker.getTypeFromTypeNode(part));
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
/** The member an indexed access type with a literal index names, as `Store['setState']` names `setState` of `Store`. */
|
|
1096
|
+
private indexedMember(annotation: ts.TypeNode | undefined): ImplementedMember | undefined {
|
|
1097
|
+
if (!annotation || !ts.isIndexedAccessTypeNode(annotation) || !ts.isLiteralTypeNode(annotation.indexType) ||
|
|
1098
|
+
!ts.isStringLiteral(annotation.indexType.literal)) {
|
|
1099
|
+
return undefined;
|
|
1100
|
+
}
|
|
1101
|
+
return this.memberOf(this.checker.getTypeFromTypeNode(annotation.objectType), annotation.indexType.literal.text);
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
private memberOf(type: ts.Type, name: string): ImplementedMember | undefined {
|
|
1105
|
+
if (type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
|
|
1106
|
+
return undefined;
|
|
1107
|
+
}
|
|
1108
|
+
const member = this.checker.getPropertyOfType(this.checker.getApparentType(this.checker.getNonNullableType(type)), name);
|
|
1109
|
+
if (!member?.declarations?.length || !member.declarations.every(declaration => this.isParsedSource(declaration))) {
|
|
1110
|
+
return undefined;
|
|
1111
|
+
}
|
|
1112
|
+
const owner = this.declaringClassOf(member);
|
|
1113
|
+
return owner && {owner, name: escapeLoneSurrogates(name)};
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
/**
|
|
1117
|
+
* The member a local variable holding a function implements, where an object literal in its file
|
|
1118
|
+
* holds it as a property, as `{setState}` or `{setState: set}` does, and that literal's contextual
|
|
1119
|
+
* type has that member, or where it is assigned to such a member, as in `inst.setState = set`.
|
|
1120
|
+
* A variable held as members of different types implements none.
|
|
1121
|
+
*/
|
|
1122
|
+
private referencedImplementation(variable: ts.VariableDeclaration): ImplementedMember | undefined {
|
|
1123
|
+
const sourceFile = variable.getSourceFile();
|
|
1124
|
+
let implementations = this.referencedImplementations.get(sourceFile);
|
|
1125
|
+
if (!implementations) {
|
|
1126
|
+
implementations = new Map();
|
|
1127
|
+
this.referencedImplementations.set(sourceFile, implementations);
|
|
1128
|
+
const found = implementations;
|
|
1129
|
+
const hold = (symbol: ts.Symbol | undefined, member: () => ImplementedMember | undefined): void => {
|
|
1130
|
+
const declaration = symbol?.valueDeclaration;
|
|
1131
|
+
if (!declaration || !ts.isVariableDeclaration(declaration) || declaration.getSourceFile() !== sourceFile ||
|
|
1132
|
+
!declaration.initializer || !this.heldFunction(declaration.initializer) || found.get(declaration) === null) {
|
|
1133
|
+
return;
|
|
1134
|
+
}
|
|
1135
|
+
const implemented = member();
|
|
1136
|
+
if (implemented) {
|
|
1137
|
+
const previous = found.get(declaration);
|
|
1138
|
+
found.set(declaration, !previous || this.sameMember(previous, implemented) ? implemented : null);
|
|
1139
|
+
}
|
|
1140
|
+
};
|
|
1141
|
+
const visit = (node: ts.Node): void => {
|
|
1142
|
+
if (ts.isShorthandPropertyAssignment(node)) {
|
|
1143
|
+
hold(this.checker.getShorthandAssignmentValueSymbol(node), () => this.memberOfContextualType(node.parent, node.name));
|
|
1144
|
+
} else if (ts.isPropertyAssignment(node) && ts.isIdentifier(node.initializer)) {
|
|
1145
|
+
hold(this.checker.getSymbolAtLocation(node.initializer), () => this.memberOfContextualType(node.parent, node.name));
|
|
1146
|
+
} else if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
1147
|
+
ts.isIdentifier(node.right) && !ts.isIdentifier(node.left)) {
|
|
1148
|
+
hold(this.checker.getSymbolAtLocation(node.right), () => this.assignedMember(node));
|
|
1149
|
+
}
|
|
1150
|
+
ts.forEachChild(node, visit);
|
|
1151
|
+
};
|
|
1152
|
+
visit(sourceFile);
|
|
1153
|
+
}
|
|
1154
|
+
return implementations.get(variable) ?? undefined;
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/** The function an initializer is, also through a cast, as in `(() => …) as F`. */
|
|
1158
|
+
private heldFunction(initializer: ts.Expression): ts.ArrowFunction | ts.FunctionExpression | undefined {
|
|
1159
|
+
let node = initializer;
|
|
1160
|
+
while (this.isCast(node)) {
|
|
1161
|
+
node = node.expression;
|
|
1162
|
+
}
|
|
1163
|
+
return ts.isArrowFunction(node) || ts.isFunctionExpression(node) ? node : undefined;
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
private sameMember(a: ImplementedMember, b: ImplementedMember): boolean {
|
|
1167
|
+
return a.name === b.name && this.sameType(a.owner, b.owner);
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
/** Only a default export, as in `export default function () {}`, declares a function without a name. */
|
|
1171
|
+
private functionDeclarationName(declaration: ts.FunctionDeclaration): string {
|
|
1172
|
+
return declaration.name?.text ?? '<default>';
|
|
1173
|
+
}
|
|
1174
|
+
|
|
884
1175
|
/**
|
|
885
|
-
* The
|
|
886
|
-
*
|
|
1176
|
+
* The name a top-level `const f = …` or an `exports.f = …` assignment gives the function it holds,
|
|
1177
|
+
* also through a cast, as in `const f = (() => …) as F`. One a module exports as its default, by
|
|
1178
|
+
* `export default`, `export =` or `module.exports =`, is named `<default>`, as a call of the module is.
|
|
887
1179
|
*/
|
|
888
|
-
private
|
|
889
|
-
const
|
|
890
|
-
|
|
891
|
-
|
|
1180
|
+
private moduleBindingName(fn: ts.ArrowFunction | ts.FunctionExpression): string | undefined {
|
|
1181
|
+
const node = this.castTarget(fn);
|
|
1182
|
+
const parent = node.parent;
|
|
1183
|
+
if (this.isTopLevelVariable(parent) && parent.initializer === node) {
|
|
1184
|
+
return parent.name.text;
|
|
1185
|
+
}
|
|
1186
|
+
if (ts.isExportAssignment(parent)) {
|
|
1187
|
+
return '<default>';
|
|
1188
|
+
}
|
|
1189
|
+
if (!ts.isBinaryExpression(parent) || parent.right !== node ||
|
|
1190
|
+
parent.operatorToken.kind !== ts.SyntaxKind.EqualsToken || !ts.isPropertyAccessExpression(parent.left)) {
|
|
1191
|
+
return undefined;
|
|
1192
|
+
}
|
|
1193
|
+
if (this.isModuleExports(parent.left)) {
|
|
1194
|
+
return '<default>';
|
|
1195
|
+
}
|
|
1196
|
+
return this.isExportsObject(parent.left.expression) ? parent.left.name.text : undefined;
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
private isExportsObject(node: ts.Expression): boolean {
|
|
1200
|
+
return ts.isIdentifier(node) ? node.text === 'exports' : this.isModuleExports(node);
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
private isModuleExports(node: ts.Expression): boolean {
|
|
1204
|
+
return ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.expression) &&
|
|
1205
|
+
node.expression.text === 'module' && node.name.text === 'exports';
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/**
|
|
1209
|
+
* A function that an object of no class holds as a member, in a parsed source file: a function
|
|
1210
|
+
* expression or arrow assigned to a property, as in `app.use = function () {}` or
|
|
1211
|
+
* `{use: function () {}}`, or a method of an object literal. It is declared on its file, as a
|
|
1212
|
+
* function declaration is, under the member's name, unless it implements a member of a named type.
|
|
1213
|
+
*/
|
|
1214
|
+
private parsedMemberFunction(declaration: ts.Declaration | undefined): ImplementedMember | undefined {
|
|
1215
|
+
if (!declaration || !this.isParsedSource(declaration)) {
|
|
1216
|
+
return undefined;
|
|
1217
|
+
}
|
|
1218
|
+
if (ts.isFunctionDeclaration(declaration)) {
|
|
1219
|
+
return {owner: this.functionOwner(declaration), name: escapeLoneSurrogates(this.functionDeclarationName(declaration))};
|
|
1220
|
+
}
|
|
1221
|
+
if (ts.isFunctionExpression(declaration) || ts.isArrowFunction(declaration)) {
|
|
1222
|
+
const identity = this.functionIdentity(declaration);
|
|
1223
|
+
return identity && {owner: identity.owner, name: escapeLoneSurrogates(identity.name)};
|
|
1224
|
+
}
|
|
1225
|
+
if (!ts.isMethodDeclaration(declaration) || !ts.isObjectLiteralExpression(declaration.parent)) {
|
|
1226
|
+
return undefined;
|
|
1227
|
+
}
|
|
1228
|
+
const implemented = this.implementedMember(declaration);
|
|
1229
|
+
if (implemented) {
|
|
1230
|
+
return implemented;
|
|
1231
|
+
}
|
|
1232
|
+
const name = this.checker.getSymbolAtLocation(declaration.name)?.getName();
|
|
1233
|
+
return name === undefined ? undefined : {
|
|
1234
|
+
owner: this.sourceModuleType(declaration.getSourceFile()),
|
|
1235
|
+
name: escapeLoneSurrogates(name)
|
|
1236
|
+
};
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
private memberFunctionName(fn: ts.FunctionExpression | ts.ArrowFunction): string | undefined {
|
|
1240
|
+
const node = this.castTarget(fn);
|
|
1241
|
+
const parent = node.parent;
|
|
1242
|
+
if (ts.isPropertyAssignment(parent) && parent.initializer === node) {
|
|
1243
|
+
return ts.isIdentifier(parent.name) || ts.isStringLiteralLike(parent.name) || ts.isNumericLiteral(parent.name) ?
|
|
1244
|
+
parent.name.text : undefined;
|
|
1245
|
+
}
|
|
1246
|
+
if (ts.isBinaryExpression(parent) && parent.right === node && parent.operatorToken.kind === ts.SyntaxKind.EqualsToken &&
|
|
1247
|
+
(ts.isPropertyAccessExpression(parent.left) || ts.isElementAccessExpression(parent.left)) &&
|
|
1248
|
+
!this.isPrototype(parent.left.expression) &&
|
|
1249
|
+
this.isAnonymousReceiver(this.checker.getTypeAtLocation(parent.left.expression))) {
|
|
1250
|
+
const target = parent.left;
|
|
1251
|
+
if (ts.isPropertyAccessExpression(target)) {
|
|
1252
|
+
return target.name.text;
|
|
1253
|
+
}
|
|
1254
|
+
if (ts.isElementAccessExpression(target) && ts.isStringLiteralLike(target.argumentExpression)) {
|
|
1255
|
+
return target.argumentExpression.text;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
return undefined;
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
private isPrototype(node: ts.Expression): boolean {
|
|
1262
|
+
return ts.isPropertyAccessExpression(node) && node.name.text === 'prototype';
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
private isAnonymousReceiver(type: ts.Type): boolean {
|
|
1266
|
+
return !((type.getSymbol()?.flags ?? 0) & (ts.SymbolFlags.Class | ts.SymbolFlags.Interface | ts.SymbolFlags.Enum));
|
|
892
1267
|
}
|
|
893
1268
|
|
|
894
1269
|
/** The namespace whose body holds a declaration, as `namespace moment` holds `function utc`. */
|
|
@@ -925,6 +1300,9 @@ export class JavaScriptTypeMapping {
|
|
|
925
1300
|
if (ts.isFunctionDeclaration(declaration)) {
|
|
926
1301
|
return ts.isSourceFile(declaration.parent) || ts.isModuleBlock(declaration.parent);
|
|
927
1302
|
}
|
|
1303
|
+
if (ts.isArrowFunction(declaration) || ts.isFunctionExpression(declaration)) {
|
|
1304
|
+
return this.moduleBindingName(declaration) !== undefined;
|
|
1305
|
+
}
|
|
928
1306
|
return (ts.isMethodDeclaration(declaration) || ts.isAccessor(declaration)) &&
|
|
929
1307
|
!!(ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Static);
|
|
930
1308
|
}
|
|
@@ -966,17 +1344,17 @@ export class JavaScriptTypeMapping {
|
|
|
966
1344
|
*/
|
|
967
1345
|
private createMethodType(
|
|
968
1346
|
signature: ts.Signature,
|
|
969
|
-
node: ts.MethodSignature | ts.FunctionDeclaration | ts.MethodDeclaration | ts.AccessorDeclaration | ts.ConstructorDeclaration | ts.FunctionExpression | ts.CallExpression | ts.NewExpression,
|
|
1347
|
+
node: ts.MethodSignature | ts.FunctionDeclaration | ts.MethodDeclaration | ts.AccessorDeclaration | ts.ConstructorDeclaration | ts.FunctionExpression | ts.ArrowFunction | ts.CallExpression | ts.NewExpression | ts.JsxOpeningLikeElement,
|
|
970
1348
|
declaringType: Type.FullyQualified,
|
|
971
1349
|
name: string,
|
|
972
|
-
declaredFormalTypeNames: string[] = []
|
|
1350
|
+
declaredFormalTypeNames: string[] = [],
|
|
1351
|
+
isStatic: boolean = this.isStatic(signature)
|
|
973
1352
|
): Type.Method {
|
|
974
1353
|
// Create composite cache key: declaring type + method name + signature
|
|
975
1354
|
// This prevents cache collisions when methods with identical signatures have different names
|
|
976
1355
|
// (e.g., util.isString(): any vs util.isArray(): any)
|
|
977
1356
|
const declaringTypeSig = Type.signature(declaringType);
|
|
978
1357
|
const signatureStr = this.checker.signatureToString(signature);
|
|
979
|
-
const isStatic = this.isStatic(signature);
|
|
980
1358
|
const cacheKey = `${isStatic ? 'static ' : ''}${declaringTypeSig}#${name}${signatureStr}`;
|
|
981
1359
|
const cached = this.methodCache.get(cacheKey);
|
|
982
1360
|
if (cached) {
|
|
@@ -1035,20 +1413,90 @@ export class JavaScriptTypeMapping {
|
|
|
1035
1413
|
* A union's member has one only when every member type inherits it from the same class.
|
|
1036
1414
|
*/
|
|
1037
1415
|
private declaringClassOf(member: ts.Symbol): Type.FullyQualified | undefined {
|
|
1038
|
-
let owner:
|
|
1416
|
+
let owner: Type.FullyQualified | undefined;
|
|
1417
|
+
let ownerSymbol: ts.Symbol | undefined;
|
|
1039
1418
|
for (const declaration of member.declarations ?? []) {
|
|
1040
1419
|
const parent = declaration.parent;
|
|
1041
|
-
|
|
1420
|
+
const type = ts.isClassLike(parent) || ts.isInterfaceDeclaration(parent) ?
|
|
1421
|
+
this.checker.getTypeAtLocation(parent) :
|
|
1422
|
+
undefined;
|
|
1423
|
+
const declaringType = type ? this.declaringType(type) : this.typeLiteralOwner(parent);
|
|
1424
|
+
if (!declaringType || declaringType === Type.unknownType) {
|
|
1042
1425
|
return undefined;
|
|
1043
1426
|
}
|
|
1044
|
-
|
|
1045
|
-
if (owner && owner.symbol !== type.symbol) {
|
|
1427
|
+
if (owner && (type ? ownerSymbol !== type.symbol : !this.sameType(owner, declaringType))) {
|
|
1046
1428
|
return undefined;
|
|
1047
1429
|
}
|
|
1048
|
-
owner
|
|
1430
|
+
owner ??= declaringType;
|
|
1431
|
+
ownerSymbol ??= type?.symbol;
|
|
1432
|
+
}
|
|
1433
|
+
return owner;
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
private sameType(a: Type.FullyQualified, b: Type.FullyQualified): boolean {
|
|
1437
|
+
return Type.FullyQualified.getFullyQualifiedName(a) === Type.FullyQualified.getFullyQualifiedName(b);
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
private memberOwner(parent: ts.Node): Type.FullyQualified {
|
|
1441
|
+
const type = ts.isClassLike(parent) || ts.isInterfaceDeclaration(parent) || ts.isObjectLiteralExpression(parent) ?
|
|
1442
|
+
this.checker.getTypeAtLocation(parent) :
|
|
1443
|
+
undefined;
|
|
1444
|
+
return type ? this.declaringType(type) : this.typeLiteralOwner(parent) ?? Type.unknownType as Type.FullyQualified;
|
|
1445
|
+
}
|
|
1446
|
+
|
|
1447
|
+
/**
|
|
1448
|
+
* The type that declares the members of a type literal, as an interface declares its own: the type
|
|
1449
|
+
* alias whose body it is. In parsed source that is also the alias whose conditional body has it as
|
|
1450
|
+
* a branch, as `type StorePersist<S> = S extends … ? {…} : never` does, and a literal that types a
|
|
1451
|
+
* property of a type that declares its members is named after that type and the property, as
|
|
1452
|
+
* `persist` in `interface Store { persist: { rehydrate(): void } }` names `Store.persist`.
|
|
1453
|
+
*/
|
|
1454
|
+
private typeLiteralOwner(node: ts.Node): Type.FullyQualified | undefined {
|
|
1455
|
+
const aliased = this.aliasedTypeLiteral(node);
|
|
1456
|
+
if (aliased) {
|
|
1457
|
+
return this.declaringType(aliased);
|
|
1458
|
+
}
|
|
1459
|
+
if (!ts.isTypeLiteralNode(node) || !this.isParsedSource(node)) {
|
|
1460
|
+
return undefined;
|
|
1461
|
+
}
|
|
1462
|
+
const alias = this.conditionalAlias(node);
|
|
1463
|
+
if (alias) {
|
|
1464
|
+
const name = this.getFullyQualifiedNameFromSymbol(alias);
|
|
1465
|
+
return name === 'unknown' ? undefined : {...moduleClass(name), classKind: Type.Class.Kind.Interface} as Type.Class;
|
|
1466
|
+
}
|
|
1467
|
+
const property = node.parent;
|
|
1468
|
+
if (!ts.isPropertySignature(property) || property.type !== node ||
|
|
1469
|
+
!(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name))) {
|
|
1470
|
+
return undefined;
|
|
1471
|
+
}
|
|
1472
|
+
const enclosing = property.parent;
|
|
1473
|
+
const owner = ts.isInterfaceDeclaration(enclosing) ?
|
|
1474
|
+
this.declaringType(this.checker.getTypeAtLocation(enclosing)) :
|
|
1475
|
+
this.typeLiteralOwner(enclosing);
|
|
1476
|
+
return owner && owner !== Type.unknownType ?
|
|
1477
|
+
moduleClass(`${Type.FullyQualified.getFullyQualifiedName(owner)}.${property.name.text}`) :
|
|
1478
|
+
undefined;
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
private conditionalAlias(literal: ts.TypeLiteralNode): ts.Symbol | undefined {
|
|
1482
|
+
let node: ts.Node = literal;
|
|
1483
|
+
let branch = false;
|
|
1484
|
+
while (ts.isParenthesizedTypeNode(node.parent) ||
|
|
1485
|
+
(ts.isConditionalTypeNode(node.parent) && (node.parent.trueType === node || node.parent.falseType === node))) {
|
|
1486
|
+
branch ||= ts.isConditionalTypeNode(node.parent);
|
|
1487
|
+
node = node.parent;
|
|
1049
1488
|
}
|
|
1050
|
-
|
|
1051
|
-
|
|
1489
|
+
return branch && ts.isTypeAliasDeclaration(node.parent) && node.parent.type === node ?
|
|
1490
|
+
this.checker.getSymbolAtLocation(node.parent.name) : undefined;
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
/** The type of a type literal that is the body of a type alias, which declares its members as an interface does. */
|
|
1494
|
+
private aliasedTypeLiteral(node: ts.Node | undefined): ts.Type | undefined {
|
|
1495
|
+
if (!node || !ts.isTypeLiteralNode(node)) {
|
|
1496
|
+
return undefined;
|
|
1497
|
+
}
|
|
1498
|
+
const type = this.checker.getTypeAtLocation(node);
|
|
1499
|
+
return type.aliasSymbol ? type : undefined;
|
|
1052
1500
|
}
|
|
1053
1501
|
|
|
1054
1502
|
private wrapperType(declaringType: (Type.FullyQualified & Type.Primitive) | Type.FullyQualified) {
|
|
@@ -1091,30 +1539,40 @@ export class JavaScriptTypeMapping {
|
|
|
1091
1539
|
let methodName: string;
|
|
1092
1540
|
let declaringType: Type.FullyQualified;
|
|
1093
1541
|
let declaredFormalTypeNames: string[] = [];
|
|
1542
|
+
let isStatic: boolean | undefined;
|
|
1094
1543
|
|
|
1095
1544
|
if (ts.isCallExpression(node) && this.isImportDefer(node.expression)) {
|
|
1096
1545
|
return undefined;
|
|
1097
1546
|
}
|
|
1098
1547
|
|
|
1099
1548
|
// Handle different kinds of nodes that represent methods or method invocations
|
|
1100
|
-
if (ts.isCallOrNewExpression(node)) {
|
|
1549
|
+
if (ts.isCallOrNewExpression(node) || this.isJsxComponent(node)) {
|
|
1101
1550
|
// For method invocations (e.g., _.map(...))
|
|
1102
1551
|
signature = this.checker.getResolvedSignature(node);
|
|
1103
1552
|
if (!signature) {
|
|
1104
1553
|
return undefined;
|
|
1105
1554
|
}
|
|
1555
|
+
const calleeExpression = ts.isCallOrNewExpression(node) ? node.expression : node.tagName;
|
|
1556
|
+
|
|
1557
|
+
if (ts.isJsxOpeningLikeElement(node) && this.checker.getTypeAtLocation(calleeExpression).getConstructSignatures().length) {
|
|
1558
|
+
const component = this.declaringType(signature.getReturnType());
|
|
1559
|
+
return component === Type.unknownType ? undefined : this.createMethodType(signature, node, component, '<constructor>');
|
|
1560
|
+
}
|
|
1106
1561
|
|
|
1107
1562
|
// Calling a module itself, as in `require('m')()`, calls its default export.
|
|
1108
1563
|
const calledModule = ts.isCallExpression(node) ? this.loadedModule(node.expression) : undefined;
|
|
1109
1564
|
if (calledModule) {
|
|
1110
|
-
|
|
1565
|
+
const callee = this.parsedCallee(undefined, signature, true);
|
|
1566
|
+
return callee ?
|
|
1567
|
+
this.createMethodType(signature, node, callee.declaringType, callee.name, [], callee.isStatic) :
|
|
1568
|
+
this.createMethodType(signature, node, moduleClass(calledModule), '<default>');
|
|
1111
1569
|
}
|
|
1112
1570
|
|
|
1113
|
-
let symbol = this.checker.getSymbolAtLocation(
|
|
1571
|
+
let symbol = this.checker.getSymbolAtLocation(calleeExpression);
|
|
1114
1572
|
const declaringNamespace = this.enclosingNamespace(signature.getDeclaration());
|
|
1115
1573
|
|
|
1116
1574
|
|
|
1117
|
-
if (!symbol && ts.isPropertyAccessExpression(
|
|
1575
|
+
if (!symbol && ts.isPropertyAccessExpression(calleeExpression)) {
|
|
1118
1576
|
// For property access expressions where we couldn't get a symbol,
|
|
1119
1577
|
// try to get the symbol from the signature's declaration
|
|
1120
1578
|
const declaration = signature?.getDeclaration();
|
|
@@ -1126,14 +1584,14 @@ export class JavaScriptTypeMapping {
|
|
|
1126
1584
|
if (!symbol && signature) {
|
|
1127
1585
|
// For cases like util.isArray where the module is 'any' type
|
|
1128
1586
|
// We'll construct a basic method type from the signature
|
|
1129
|
-
methodName =
|
|
1587
|
+
methodName = calleeExpression.name.getText();
|
|
1130
1588
|
|
|
1131
1589
|
// When there's no symbol but we have a signature, we need to work harder
|
|
1132
1590
|
// to find the declaring type. This happens with CommonJS require() calls
|
|
1133
1591
|
// where the module is typed as 'any' but methods still have signatures
|
|
1134
1592
|
|
|
1135
1593
|
// Trace back through the AST to the require() call or import binding the receiver comes from
|
|
1136
|
-
const moduleName = this.moduleOfExpression(
|
|
1594
|
+
const moduleName = this.moduleOfExpression(calleeExpression.expression);
|
|
1137
1595
|
|
|
1138
1596
|
// Use the inferred type or fall back to unknown
|
|
1139
1597
|
declaringType = moduleName ? moduleClass(moduleName) : Type.unknownType as Type.FullyQualified;
|
|
@@ -1148,11 +1606,11 @@ export class JavaScriptTypeMapping {
|
|
|
1148
1606
|
}
|
|
1149
1607
|
|
|
1150
1608
|
// Get the method name
|
|
1151
|
-
if (ts.isPropertyAccessExpression(
|
|
1152
|
-
methodName =
|
|
1609
|
+
if (ts.isPropertyAccessExpression(calleeExpression)) {
|
|
1610
|
+
methodName = calleeExpression.name.getText();
|
|
1153
1611
|
|
|
1154
1612
|
// Check if the object is an imported symbol
|
|
1155
|
-
const objSymbol = this.checker.getSymbolAtLocation(
|
|
1613
|
+
const objSymbol = this.checker.getSymbolAtLocation(calleeExpression.expression);
|
|
1156
1614
|
let isImport = false;
|
|
1157
1615
|
if (objSymbol) {
|
|
1158
1616
|
// Only call getAliasedSymbol if the symbol is actually an alias
|
|
@@ -1162,7 +1620,7 @@ export class JavaScriptTypeMapping {
|
|
|
1162
1620
|
}
|
|
1163
1621
|
}
|
|
1164
1622
|
|
|
1165
|
-
const exprType = this.checker.getTypeAtLocation(
|
|
1623
|
+
const exprType = this.checker.getTypeAtLocation(calleeExpression.expression);
|
|
1166
1624
|
const mappedType = this.getType(exprType);
|
|
1167
1625
|
|
|
1168
1626
|
// A namespace import of an ES module is typed as its anonymous namespace object, so the
|
|
@@ -1170,14 +1628,20 @@ export class JavaScriptTypeMapping {
|
|
|
1170
1628
|
// declared as a TypeScript namespace (`export = React`) keeps that namespace's name.
|
|
1171
1629
|
// An installed module's object is named after its package, so where the receiver is an
|
|
1172
1630
|
// import's alias, which resolves to the module its specifier names, the specifier wins.
|
|
1173
|
-
const receiverBinding = this.bindingOf(
|
|
1631
|
+
const receiverBinding = this.bindingOf(calleeExpression.expression);
|
|
1174
1632
|
const anonymous = !mappedType || mappedType.kind !== Type.Kind.Class ||
|
|
1175
1633
|
(mappedType as Type.Class).fullyQualifiedName.startsWith('{') ||
|
|
1176
1634
|
(isImport && this.isInstalledModuleObject(exprType));
|
|
1177
1635
|
|
|
1178
1636
|
const declaringClass = this.declaringClassOf(symbol);
|
|
1637
|
+
const memberFunction = !declaringClass && !declaringNamespace &&
|
|
1638
|
+
(!receiverBinding || receiverBinding.kind === 'named') && this.isAnonymousReceiver(exprType) ?
|
|
1639
|
+
this.parsedMemberFunction(signature.getDeclaration()) : undefined;
|
|
1179
1640
|
if (declaringClass) {
|
|
1180
1641
|
declaringType = declaringClass;
|
|
1642
|
+
} else if (memberFunction) {
|
|
1643
|
+
declaringType = memberFunction.owner;
|
|
1644
|
+
methodName = memberFunction.name;
|
|
1181
1645
|
} else if (declaringNamespace) {
|
|
1182
1646
|
// A callable module's receiver is typed as its function, which names no declaration
|
|
1183
1647
|
declaringType = moduleClass(this.namespaceName(declaringNamespace));
|
|
@@ -1222,12 +1686,12 @@ export class JavaScriptTypeMapping {
|
|
|
1222
1686
|
}
|
|
1223
1687
|
}
|
|
1224
1688
|
|
|
1225
|
-
} else if (ts.isIdentifier(
|
|
1226
|
-
methodName =
|
|
1689
|
+
} else if (ts.isIdentifier(calleeExpression)) {
|
|
1690
|
+
methodName = calleeExpression.getText();
|
|
1227
1691
|
|
|
1228
1692
|
// Check if this is an import first
|
|
1229
|
-
const exprSymbol = this.checker.getSymbolAtLocation(
|
|
1230
|
-
const binding = this.moduleBinding(exprSymbol?.declarations?.[0],
|
|
1693
|
+
const exprSymbol = this.checker.getSymbolAtLocation(calleeExpression);
|
|
1694
|
+
const binding = this.moduleBinding(exprSymbol?.declarations?.[0], calleeExpression);
|
|
1231
1695
|
let moduleSpecifier: string | undefined;
|
|
1232
1696
|
let aliasedSymbol: ts.Symbol | undefined;
|
|
1233
1697
|
|
|
@@ -1252,10 +1716,11 @@ export class JavaScriptTypeMapping {
|
|
|
1252
1716
|
}
|
|
1253
1717
|
moduleSpecifier ??= binding?.module;
|
|
1254
1718
|
|
|
1255
|
-
const
|
|
1256
|
-
if (
|
|
1257
|
-
declaringType =
|
|
1258
|
-
methodName =
|
|
1719
|
+
const callee = this.parsedCallee(aliasedSymbol ?? symbol, signature, !!moduleSpecifier);
|
|
1720
|
+
if (callee) {
|
|
1721
|
+
declaringType = callee.declaringType;
|
|
1722
|
+
methodName = callee.name;
|
|
1723
|
+
isStatic = callee.isStatic;
|
|
1259
1724
|
} else if (moduleSpecifier) {
|
|
1260
1725
|
// This is an imported function - use the module specifier as declaring type
|
|
1261
1726
|
declaringType = moduleClass(moduleSpecifier);
|
|
@@ -1278,7 +1743,7 @@ export class JavaScriptTypeMapping {
|
|
|
1278
1743
|
methodName = ts.getNameOfDeclaration(signature.getDeclaration())?.getText() ?? methodName;
|
|
1279
1744
|
} else {
|
|
1280
1745
|
// Fall back to the original logic for non-imported functions
|
|
1281
|
-
const exprType = this.checker.getTypeAtLocation(
|
|
1746
|
+
const exprType = this.checker.getTypeAtLocation(calleeExpression);
|
|
1282
1747
|
const funcType = this.getType(exprType);
|
|
1283
1748
|
|
|
1284
1749
|
if (funcType && funcType.kind === Type.Kind.Class) {
|
|
@@ -1329,14 +1794,9 @@ export class JavaScriptTypeMapping {
|
|
|
1329
1794
|
|
|
1330
1795
|
methodName = escapeLoneSurrogates(symbol.getName());
|
|
1331
1796
|
|
|
1332
|
-
// The declaring type: the class, whether declared or an expression, the interface or the
|
|
1333
|
-
const
|
|
1334
|
-
|
|
1335
|
-
const parentType = this.checker.getTypeAtLocation(parent);
|
|
1336
|
-
declaringType = this.declaringType(parentType);
|
|
1337
|
-
} else {
|
|
1338
|
-
declaringType = Type.unknownType as Type.FullyQualified;
|
|
1339
|
-
}
|
|
1797
|
+
// The declaring type: the class, whether declared or an expression, the interface, the object literal or the aliased type literal around it
|
|
1798
|
+
const memberFunction = this.parsedMemberFunction(node);
|
|
1799
|
+
declaringType = memberFunction ? memberFunction.owner : this.memberOwner(node.parent);
|
|
1340
1800
|
|
|
1341
1801
|
// Get type parameters from node
|
|
1342
1802
|
if (node.typeParameters) {
|
|
@@ -1353,10 +1813,7 @@ export class JavaScriptTypeMapping {
|
|
|
1353
1813
|
|
|
1354
1814
|
methodName = escapeLoneSurrogates(symbol.getName());
|
|
1355
1815
|
|
|
1356
|
-
|
|
1357
|
-
declaringType = ts.isClassLike(parent) || ts.isInterfaceDeclaration(parent) || ts.isObjectLiteralExpression(parent) ?
|
|
1358
|
-
this.declaringType(this.checker.getTypeAtLocation(parent)) :
|
|
1359
|
-
Type.unknownType as Type.FullyQualified;
|
|
1816
|
+
declaringType = this.memberOwner(node.parent);
|
|
1360
1817
|
} else if (ts.isConstructorDeclaration(node)) {
|
|
1361
1818
|
// For constructor declarations
|
|
1362
1819
|
signature = this.checker.getSignatureFromDeclaration(node);
|
|
@@ -1380,11 +1837,12 @@ export class JavaScriptTypeMapping {
|
|
|
1380
1837
|
return undefined;
|
|
1381
1838
|
}
|
|
1382
1839
|
|
|
1383
|
-
|
|
1840
|
+
const identity = ts.isFunctionExpression(node) ? this.functionIdentity(node) : undefined;
|
|
1841
|
+
methodName = ts.isFunctionDeclaration(node) ? this.functionDeclarationName(node) : identity?.name ?? "<anonymous>";
|
|
1384
1842
|
|
|
1385
1843
|
declaringType = ts.isFunctionDeclaration(node) ?
|
|
1386
1844
|
this.functionOwner(node) :
|
|
1387
|
-
this.sourceModuleType(node.getSourceFile());
|
|
1845
|
+
identity?.owner ?? this.sourceModuleType(node.getSourceFile());
|
|
1388
1846
|
|
|
1389
1847
|
// Get type parameters from node
|
|
1390
1848
|
if (node.typeParameters) {
|
|
@@ -1392,13 +1850,24 @@ export class JavaScriptTypeMapping {
|
|
|
1392
1850
|
declaredFormalTypeNames.push(tp.name.getText());
|
|
1393
1851
|
}
|
|
1394
1852
|
}
|
|
1853
|
+
} else if (ts.isArrowFunction(node)) {
|
|
1854
|
+
const identity = this.functionIdentity(node);
|
|
1855
|
+
signature = identity ? this.checker.getSignatureFromDeclaration(node) : undefined;
|
|
1856
|
+
if (!identity || !signature) {
|
|
1857
|
+
return undefined;
|
|
1858
|
+
}
|
|
1859
|
+
methodName = identity.name;
|
|
1860
|
+
declaringType = identity.owner;
|
|
1861
|
+
for (const tp of node.typeParameters ?? []) {
|
|
1862
|
+
declaredFormalTypeNames.push(tp.name.getText());
|
|
1863
|
+
}
|
|
1395
1864
|
} else {
|
|
1396
1865
|
// For other node types, return undefined
|
|
1397
1866
|
return undefined;
|
|
1398
1867
|
}
|
|
1399
1868
|
|
|
1400
1869
|
// Create the method type using the helper
|
|
1401
|
-
return this.createMethodType(signature, node, declaringType, methodName, declaredFormalTypeNames);
|
|
1870
|
+
return this.createMethodType(signature, node, declaringType, methodName, declaredFormalTypeNames, isStatic);
|
|
1402
1871
|
}
|
|
1403
1872
|
|
|
1404
1873
|
|
|
@@ -1630,12 +2099,47 @@ export class JavaScriptTypeMapping {
|
|
|
1630
2099
|
} as Type.Class;
|
|
1631
2100
|
}
|
|
1632
2101
|
|
|
1633
|
-
private
|
|
1634
|
-
const
|
|
1635
|
-
|
|
2102
|
+
private bases(classType: Type.Class, baseType: ts.Type, apply: (heritage: Heritage) => void): void {
|
|
2103
|
+
const base = asClass(this.getType(baseType));
|
|
2104
|
+
if (!base) {
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
const inherit = () => apply(this.heritageFrom(classType, base, new Set()));
|
|
2108
|
+
const waiting = base.fullyQualifiedName === classType.fullyQualifiedName ?
|
|
2109
|
+
this.heritageInProgress.get(base) : undefined;
|
|
2110
|
+
if (waiting) {
|
|
2111
|
+
waiting.push(inherit);
|
|
2112
|
+
} else {
|
|
2113
|
+
inherit();
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
|
|
2117
|
+
private heritageFrom(classType: Type.Class, base: Type.Class, seen: Set<Type.Class>): Heritage {
|
|
1636
2118
|
// TypeScript accepts heritage cycles (`class A implements A`). Whichever edge closes one,
|
|
1637
2119
|
// in mapping order, is dropped.
|
|
1638
|
-
|
|
2120
|
+
if (this.inheritsFrom(base, classType, new Set())) {
|
|
2121
|
+
return {interfaces: []};
|
|
2122
|
+
}
|
|
2123
|
+
if (base.fullyQualifiedName !== classType.fullyQualifiedName) {
|
|
2124
|
+
return {supertype: base, interfaces: []};
|
|
2125
|
+
}
|
|
2126
|
+
// Another type of the class's own name, such as the class that `export =` gives in
|
|
2127
|
+
// @types/supertest's lib/test.d.ts, which its index.d.ts interface `Test` extends. A store
|
|
2128
|
+
// that keys types by name keeps one of the two, and reads this edge as the class extending
|
|
2129
|
+
// itself. Its own bases stand in for it.
|
|
2130
|
+
if (seen.has(base)) {
|
|
2131
|
+
return {interfaces: []};
|
|
2132
|
+
}
|
|
2133
|
+
seen.add(base);
|
|
2134
|
+
const superclass = base.supertype && asClass(base.supertype);
|
|
2135
|
+
const inherited: Heritage = superclass ? this.heritageFrom(classType, superclass, seen) : {interfaces: []};
|
|
2136
|
+
const interfaces = [...inherited.interfaces];
|
|
2137
|
+
for (const iface of base.interfaces) {
|
|
2138
|
+
const baseInterface = asClass(iface);
|
|
2139
|
+
const h: Heritage = baseInterface ? this.heritageFrom(classType, baseInterface, seen) : {interfaces: []};
|
|
2140
|
+
interfaces.push(...(h.supertype ? [h.supertype] : []), ...h.interfaces);
|
|
2141
|
+
}
|
|
2142
|
+
return {supertype: inherited.supertype, interfaces};
|
|
1639
2143
|
}
|
|
1640
2144
|
|
|
1641
2145
|
private inheritsFrom(type: Type.Class, ancestor: Type.Class, seen: Set<Type.Class>): boolean {
|
|
@@ -1651,9 +2155,15 @@ export class JavaScriptTypeMapping {
|
|
|
1651
2155
|
}
|
|
1652
2156
|
|
|
1653
2157
|
private addInterface(classType: Type.Class, baseType: ts.Type): void {
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
2158
|
+
this.bases(classType, baseType, ({supertype, interfaces}) =>
|
|
2159
|
+
this.addInterfaces(classType, supertype ? [supertype, ...interfaces] : interfaces));
|
|
2160
|
+
}
|
|
2161
|
+
|
|
2162
|
+
private addInterfaces(classType: Type.Class, interfaces: Type.Class[]): void {
|
|
2163
|
+
for (const iface of interfaces) {
|
|
2164
|
+
if (!classType.interfaces.includes(iface)) {
|
|
2165
|
+
classType.interfaces.push(iface);
|
|
2166
|
+
}
|
|
1657
2167
|
}
|
|
1658
2168
|
}
|
|
1659
2169
|
|
|
@@ -1662,6 +2172,18 @@ export class JavaScriptTypeMapping {
|
|
|
1662
2172
|
* Since the shell is already in the cache, any recursive references will find it
|
|
1663
2173
|
*/
|
|
1664
2174
|
private populateClassType(classType: Type.Class, type: ts.Type): void {
|
|
2175
|
+
this.heritageInProgress.set(classType, []);
|
|
2176
|
+
try {
|
|
2177
|
+
this.populateHeritage(classType, type);
|
|
2178
|
+
} finally {
|
|
2179
|
+
const waiting = this.heritageInProgress.get(classType) ?? [];
|
|
2180
|
+
this.heritageInProgress.delete(classType);
|
|
2181
|
+
waiting.forEach(inherit => inherit());
|
|
2182
|
+
}
|
|
2183
|
+
this.populateMembers(classType, type);
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
private populateHeritage(classType: Type.Class, type: ts.Type): void {
|
|
1665
2187
|
const symbol = type.getSymbol?.();
|
|
1666
2188
|
const classSymbol = symbol && symbol.flags & ts.SymbolFlags.Alias ?
|
|
1667
2189
|
this.checker.getAliasedSymbol(symbol) : symbol;
|
|
@@ -1712,10 +2234,12 @@ export class JavaScriptTypeMapping {
|
|
|
1712
2234
|
let interfaces = baseTypes;
|
|
1713
2235
|
if (classType.classKind === Type.Class.Kind.Class && (!classDeclaration ||
|
|
1714
2236
|
classDeclaration.heritageClauses?.some(c => c.token === ts.SyntaxKind.ExtendsKeyword))) {
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
2237
|
+
this.bases(classType, baseTypes[0], ({supertype, interfaces}) => {
|
|
2238
|
+
if (supertype) {
|
|
2239
|
+
classType.supertype = supertype;
|
|
2240
|
+
}
|
|
2241
|
+
this.addInterfaces(classType, interfaces);
|
|
2242
|
+
});
|
|
1719
2243
|
interfaces = baseTypes.slice(1);
|
|
1720
2244
|
}
|
|
1721
2245
|
for (const baseType of interfaces) {
|
|
@@ -1732,7 +2256,10 @@ export class JavaScriptTypeMapping {
|
|
|
1732
2256
|
}
|
|
1733
2257
|
}
|
|
1734
2258
|
}
|
|
2259
|
+
}
|
|
1735
2260
|
|
|
2261
|
+
private populateMembers(classType: Type.Class, type: ts.Type): void {
|
|
2262
|
+
const symbol = type.getSymbol?.();
|
|
1736
2263
|
// Extract type parameters from declarations (not provided by getBaseTypes)
|
|
1737
2264
|
if (symbol?.declarations) {
|
|
1738
2265
|
for (const declaration of symbol.declarations) {
|