@openrewrite/rewrite 8.117.0 → 8.119.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.d.ts.map +1 -1
- package/dist/yaml/parser.js +193 -254
- 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 +206 -252
|
@@ -76,6 +76,10 @@ function sortBySignature(bounds) {
|
|
|
76
76
|
function typeSignatureToJSON() {
|
|
77
77
|
return java_1.Type.signature(this);
|
|
78
78
|
}
|
|
79
|
+
function asClass(type) {
|
|
80
|
+
const unwrapped = java_1.Type.isParameterized(type) ? type.type : type;
|
|
81
|
+
return java_1.Type.isClass(unwrapped) ? unwrapped : undefined;
|
|
82
|
+
}
|
|
79
83
|
function isGlobalAugmentation(declaration) {
|
|
80
84
|
return typescript_1.default.isModuleDeclaration(declaration) && (declaration.flags & typescript_1.default.NodeFlags.GlobalAugmentation) !== 0;
|
|
81
85
|
}
|
|
@@ -114,6 +118,10 @@ class JavaScriptTypeMapping {
|
|
|
114
118
|
// unknown, since `type Rec<T> = (x: T) => Rec<T[]>` instantiates without end and each
|
|
115
119
|
// instantiation has a type id of its own that the type cache cannot match.
|
|
116
120
|
this.functionTypesInProgress = new Set();
|
|
121
|
+
// Classes whose bases are being mapped, each with what waits to read them. A namesake base
|
|
122
|
+
// gives its own bases in its place, and has none to give until it is done.
|
|
123
|
+
this.heritageInProgress = new Map();
|
|
124
|
+
this.referencedImplementations = new WeakMap();
|
|
117
125
|
this.regExpSymbol = checker.resolveName("RegExp", undefined, typescript_1.default.SymbolFlags.Type, false);
|
|
118
126
|
// Resolve global wrapper types for primitives from TypeScript's lib
|
|
119
127
|
const stringSymbol = checker.resolveName("String", undefined, typescript_1.default.SymbolFlags.Type, false);
|
|
@@ -545,7 +553,7 @@ class JavaScriptTypeMapping {
|
|
|
545
553
|
return java_1.Type.isPrimitive(type) ? type : java_1.Type.Primitive.None;
|
|
546
554
|
}
|
|
547
555
|
variableType(node) {
|
|
548
|
-
var _a, _b, _c;
|
|
556
|
+
var _a, _b, _c, _d;
|
|
549
557
|
let symbol;
|
|
550
558
|
let location = node;
|
|
551
559
|
// Get the symbol depending on node type
|
|
@@ -611,7 +619,11 @@ class JavaScriptTypeMapping {
|
|
|
611
619
|
else {
|
|
612
620
|
// For non-imported variables, check if they belong to a class/interface/namespace
|
|
613
621
|
const parentSymbol = actualSymbol.parent;
|
|
614
|
-
|
|
622
|
+
const typeLiteral = this.aliasedTypeLiteral((_b = actualSymbol.valueDeclaration) === null || _b === void 0 ? void 0 : _b.parent);
|
|
623
|
+
if (typeLiteral) {
|
|
624
|
+
ownerType = this.getType(typeLiteral);
|
|
625
|
+
}
|
|
626
|
+
else if (parentSymbol) {
|
|
615
627
|
const parentType = this.checker.getDeclaredTypeOfSymbol(parentSymbol);
|
|
616
628
|
if (parentType) {
|
|
617
629
|
ownerType = this.getType(parentType);
|
|
@@ -662,7 +674,7 @@ class JavaScriptTypeMapping {
|
|
|
662
674
|
const variable = {
|
|
663
675
|
kind: java_1.Type.Kind.Variable,
|
|
664
676
|
flags: 0,
|
|
665
|
-
name: (
|
|
677
|
+
name: (_d = (symbol.flags & typescript_1.default.SymbolFlags.Alias && ((_c = actualSymbol.declarations) === null || _c === void 0 ? void 0 : _c.length) ? undefined : binding === null || binding === void 0 ? void 0 : binding.exportName)) !== null && _d !== void 0 ? _d : (0, parser_utils_1.escapeLoneSurrogates)(actualSymbol.getName()),
|
|
666
678
|
owner: ownerType,
|
|
667
679
|
type: mappedType,
|
|
668
680
|
annotations: [],
|
|
@@ -687,6 +699,21 @@ class JavaScriptTypeMapping {
|
|
|
687
699
|
return typescript_1.default.isCallExpression(node) && typescript_1.default.isIdentifier(node.expression) && node.expression.text === 'require' ?
|
|
688
700
|
this.moduleSpecifierArgument(node) : undefined;
|
|
689
701
|
}
|
|
702
|
+
/**
|
|
703
|
+
* TypeScript tells an intrinsic element (`<div>`, `<my-el>`, `<svg:rect>`) from a component by the tag's spelling,
|
|
704
|
+
* and a tag holding a string, as `const Tag = 'h1'; <Tag/>`, renders an intrinsic element too.
|
|
705
|
+
*/
|
|
706
|
+
isJsxComponent(node) {
|
|
707
|
+
if (!typescript_1.default.isJsxOpeningLikeElement(node) || typescript_1.default.isJsxNamespacedName(node.tagName)) {
|
|
708
|
+
return false;
|
|
709
|
+
}
|
|
710
|
+
const tagName = node.tagName;
|
|
711
|
+
if (typescript_1.default.isIdentifier(tagName) && (/^[a-z]/.test(tagName.text) || tagName.text.includes('-'))) {
|
|
712
|
+
return false;
|
|
713
|
+
}
|
|
714
|
+
const tagType = this.checker.getTypeAtLocation(tagName);
|
|
715
|
+
return !(tagType.isUnion() ? tagType.types : [tagType]).some(t => t.flags & typescript_1.default.TypeFlags.StringLike);
|
|
716
|
+
}
|
|
690
717
|
isImportDefer(node) {
|
|
691
718
|
return typescript_1.default.isMetaProperty(node) && node.keywordToken === typescript_1.default.SyntaxKind.ImportKeyword && node.name.text === 'defer';
|
|
692
719
|
}
|
|
@@ -820,14 +847,335 @@ class JavaScriptTypeMapping {
|
|
|
820
847
|
const fileName = sourceFile.fileName;
|
|
821
848
|
return (0, package_name_1.moduleOfFile)(this.sourceRoot && path.isAbsolute(fileName) ? path.relative(this.sourceRoot, fileName) : fileName);
|
|
822
849
|
}
|
|
850
|
+
/** Whether a declaration is in a parsed source file, which a `.d.ts` file or an installed package is not. */
|
|
851
|
+
isParsedSource(declaration) {
|
|
852
|
+
const sourceFile = declaration.getSourceFile();
|
|
853
|
+
return !!sourceFile && !sourceFile.isDeclarationFile && !this.isInstalled(sourceFile.fileName);
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* What a call into a parsed source file calls, as that file's declaration names it: the function
|
|
857
|
+
* the callee is declared as, or the top-level variable holding it, as `const f = () => …` or
|
|
858
|
+
* `const ky = createInstance()` does, where the signature it calls is declared in parsed source too.
|
|
859
|
+
* A local variable holding a function names it on its file, as a nested function declaration is.
|
|
860
|
+
* One destructured from an object, as in `const {setState} = createStore()`, calls the member it is
|
|
861
|
+
* destructured from, as `store.setState()` does, where a named type in parsed source declares it.
|
|
862
|
+
* A callee bound to a module, by an import or a `require`, calls the declaration its signature
|
|
863
|
+
* resolves to, which may be one a re-export such as `module.exports = require('./lib/express')` leads to.
|
|
864
|
+
*/
|
|
865
|
+
parsedCallee(symbol, signature, boundToModule) {
|
|
866
|
+
const target = signature.getDeclaration();
|
|
867
|
+
if (!target || !this.isParsedSource(target)) {
|
|
868
|
+
return undefined;
|
|
869
|
+
}
|
|
870
|
+
const declaration = symbol === null || symbol === void 0 ? void 0 : symbol.valueDeclaration;
|
|
871
|
+
if (declaration && this.isParsedSource(declaration)) {
|
|
872
|
+
if (typescript_1.default.isFunctionDeclaration(declaration)) {
|
|
873
|
+
return { declaringType: this.functionOwner(declaration), name: this.functionDeclarationName(declaration) };
|
|
874
|
+
}
|
|
875
|
+
if (this.isTopLevelVariable(declaration) && !this.moduleBinding(declaration, declaration)) {
|
|
876
|
+
return { declaringType: this.sourceModuleType(declaration.getSourceFile()), name: declaration.name.text, isStatic: true };
|
|
877
|
+
}
|
|
878
|
+
const destructured = typescript_1.default.isBindingElement(declaration) ? this.destructuredMember(declaration) : undefined;
|
|
879
|
+
if (destructured) {
|
|
880
|
+
return { declaringType: destructured.owner, name: destructured.name };
|
|
881
|
+
}
|
|
882
|
+
const fn = typescript_1.default.isVariableDeclaration(declaration) && declaration.initializer ?
|
|
883
|
+
this.heldFunction(declaration.initializer) : undefined;
|
|
884
|
+
if (fn && typescript_1.default.isVariableDeclaration(declaration) && typescript_1.default.isIdentifier(declaration.name)) {
|
|
885
|
+
const local = this.implementedMember(fn);
|
|
886
|
+
return local ?
|
|
887
|
+
{ declaringType: local.owner, name: local.name } :
|
|
888
|
+
{ declaringType: this.sourceModuleType(declaration.getSourceFile()), name: declaration.name.text };
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
if (!boundToModule) {
|
|
892
|
+
return undefined;
|
|
893
|
+
}
|
|
894
|
+
if (typescript_1.default.isFunctionDeclaration(target)) {
|
|
895
|
+
return { declaringType: this.functionOwner(target), name: this.functionDeclarationName(target) };
|
|
896
|
+
}
|
|
897
|
+
const identity = (typescript_1.default.isArrowFunction(target) || typescript_1.default.isFunctionExpression(target)) ? this.functionIdentity(target) : undefined;
|
|
898
|
+
return identity && { declaringType: identity.owner, name: identity.name };
|
|
899
|
+
}
|
|
900
|
+
castTarget(fn) {
|
|
901
|
+
let node = fn;
|
|
902
|
+
while (this.isCast(node.parent)) {
|
|
903
|
+
node = node.parent;
|
|
904
|
+
}
|
|
905
|
+
return node;
|
|
906
|
+
}
|
|
907
|
+
isCast(node) {
|
|
908
|
+
return typescript_1.default.isParenthesizedExpression(node) || typescript_1.default.isAsExpression(node) ||
|
|
909
|
+
typescript_1.default.isSatisfiesExpression(node) || typescript_1.default.isTypeAssertionExpression(node);
|
|
910
|
+
}
|
|
911
|
+
isTopLevelVariable(declaration) {
|
|
912
|
+
return typescript_1.default.isVariableDeclaration(declaration) && typescript_1.default.isIdentifier(declaration.name) &&
|
|
913
|
+
typescript_1.default.isVariableStatement(declaration.parent.parent) && typescript_1.default.isSourceFile(declaration.parent.parent.parent);
|
|
914
|
+
}
|
|
823
915
|
/**
|
|
824
|
-
*
|
|
825
|
-
*
|
|
916
|
+
* What declares a function expression or arrow function: the member of a named type it implements,
|
|
917
|
+
* where no module binding names it, or else its file, under its own name, or else the one a top-level
|
|
918
|
+
* `const f = …`, an `exports.f = …` assignment, the property it is assigned to or the local
|
|
919
|
+
* variable it initializes gives it.
|
|
826
920
|
*/
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
921
|
+
functionIdentity(fn) {
|
|
922
|
+
var _a, _b, _c, _d;
|
|
923
|
+
const bound = this.moduleBindingName(fn);
|
|
924
|
+
const implemented = bound === undefined ? this.implementedMember(fn) : undefined;
|
|
925
|
+
if (implemented) {
|
|
926
|
+
return implemented;
|
|
927
|
+
}
|
|
928
|
+
const name = (_d = (_c = (_b = (_a = fn.name) === null || _a === void 0 ? void 0 : _a.text) !== null && _b !== void 0 ? _b : bound) !== null && _c !== void 0 ? _c : this.memberFunctionName(fn)) !== null && _d !== void 0 ? _d : this.localBindingName(fn);
|
|
929
|
+
return name === undefined ? undefined : { owner: this.sourceModuleType(fn.getSourceFile()), name };
|
|
930
|
+
}
|
|
931
|
+
localBindingName(fn) {
|
|
932
|
+
const node = this.castTarget(fn);
|
|
933
|
+
const parent = node.parent;
|
|
934
|
+
return typescript_1.default.isVariableDeclaration(parent) && parent.initializer === node && typescript_1.default.isIdentifier(parent.name) ?
|
|
935
|
+
parent.name.text : undefined;
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* The member of a named type declared in parsed source that a function implements, which is
|
|
939
|
+
* where a call through that type is declared, as in Java a call is declared on the interface it is
|
|
940
|
+
* made through. That is the member of an object literal's contextual type, as in
|
|
941
|
+
* `const api: Store = {setState() {}}`, or of the receiver a function is assigned to a property of,
|
|
942
|
+
* as in `inst.create = () => …`. A local variable holding a function implements the member its
|
|
943
|
+
* annotation indexes, as in `const setState: Store['setState'] = …`, or the one an object literal
|
|
944
|
+
* holding it as a property implements, as `{setState}` does.
|
|
945
|
+
*/
|
|
946
|
+
implementedMember(fn) {
|
|
947
|
+
var _a;
|
|
948
|
+
if (!this.isParsedSource(fn)) {
|
|
949
|
+
return undefined;
|
|
950
|
+
}
|
|
951
|
+
if (typescript_1.default.isMethodDeclaration(fn)) {
|
|
952
|
+
return typescript_1.default.isObjectLiteralExpression(fn.parent) ? this.memberOfContextualType(fn.parent, fn.name) : undefined;
|
|
953
|
+
}
|
|
954
|
+
const node = this.castTarget(fn);
|
|
955
|
+
const parent = node.parent;
|
|
956
|
+
if (typescript_1.default.isPropertyAssignment(parent) && parent.initializer === node) {
|
|
957
|
+
return this.memberOfContextualType(parent.parent, parent.name);
|
|
958
|
+
}
|
|
959
|
+
if (typescript_1.default.isBinaryExpression(parent) && parent.right === node) {
|
|
960
|
+
return this.assignedMember(parent);
|
|
961
|
+
}
|
|
962
|
+
if (typescript_1.default.isVariableDeclaration(parent) && parent.initializer === node && typescript_1.default.isIdentifier(parent.name) &&
|
|
963
|
+
!this.isTopLevelVariable(parent)) {
|
|
964
|
+
return (_a = this.indexedMember(parent.type)) !== null && _a !== void 0 ? _a : this.referencedImplementation(parent);
|
|
965
|
+
}
|
|
966
|
+
return undefined;
|
|
967
|
+
}
|
|
968
|
+
assignedMember(assignment) {
|
|
969
|
+
const target = assignment.left;
|
|
970
|
+
if (assignment.operatorToken.kind !== typescript_1.default.SyntaxKind.EqualsToken ||
|
|
971
|
+
!(typescript_1.default.isPropertyAccessExpression(target) || typescript_1.default.isElementAccessExpression(target)) ||
|
|
972
|
+
this.isPrototype(target.expression)) {
|
|
973
|
+
return undefined;
|
|
974
|
+
}
|
|
975
|
+
const name = typescript_1.default.isPropertyAccessExpression(target) ? target.name :
|
|
976
|
+
typescript_1.default.isStringLiteralLike(target.argumentExpression) ? target.argumentExpression : undefined;
|
|
977
|
+
return name && this.receiverTypes(target.expression)
|
|
978
|
+
.map(type => this.memberOf(type, name.text))
|
|
979
|
+
.find(member => member !== undefined);
|
|
980
|
+
}
|
|
981
|
+
destructuredMember(element) {
|
|
982
|
+
var _a;
|
|
983
|
+
const name = (_a = element.propertyName) !== null && _a !== void 0 ? _a : element.name;
|
|
984
|
+
if (!typescript_1.default.isObjectBindingPattern(element.parent) || element.dotDotDotToken ||
|
|
985
|
+
!(typescript_1.default.isIdentifier(name) || typescript_1.default.isStringLiteralLike(name))) {
|
|
986
|
+
return undefined;
|
|
987
|
+
}
|
|
988
|
+
return this.memberOf(this.checker.getTypeAtLocation(element.parent), name.text);
|
|
989
|
+
}
|
|
990
|
+
memberOfContextualType(literal, name) {
|
|
991
|
+
if (!typescript_1.default.isIdentifier(name) && !typescript_1.default.isStringLiteralLike(name) && !typescript_1.default.isNumericLiteral(name)) {
|
|
992
|
+
return undefined;
|
|
993
|
+
}
|
|
994
|
+
const type = this.checker.getContextualType(literal);
|
|
995
|
+
return type && this.memberOf(type, name.text);
|
|
996
|
+
}
|
|
997
|
+
/**
|
|
998
|
+
* The types a receiver may be written through: its own, or where that is `any`, as with
|
|
999
|
+
* `const ky: Partial<KyInstance> & any`, each part of the intersection its declaration is annotated with.
|
|
1000
|
+
*/
|
|
1001
|
+
receiverTypes(receiver) {
|
|
1002
|
+
var _a;
|
|
1003
|
+
const type = this.checker.getTypeAtLocation(receiver);
|
|
1004
|
+
if (!(type.flags & typescript_1.default.TypeFlags.Any)) {
|
|
1005
|
+
return [type];
|
|
1006
|
+
}
|
|
1007
|
+
const declaration = typescript_1.default.isIdentifier(receiver) ? (_a = this.checker.getSymbolAtLocation(receiver)) === null || _a === void 0 ? void 0 : _a.valueDeclaration : undefined;
|
|
1008
|
+
const annotation = declaration && (typescript_1.default.isVariableDeclaration(declaration) || typescript_1.default.isParameter(declaration)) ? declaration.type : undefined;
|
|
1009
|
+
const parts = annotation && typescript_1.default.isIntersectionTypeNode(annotation) ? annotation.types : [];
|
|
1010
|
+
return parts.map(part => this.checker.getTypeFromTypeNode(part));
|
|
1011
|
+
}
|
|
1012
|
+
/** The member an indexed access type with a literal index names, as `Store['setState']` names `setState` of `Store`. */
|
|
1013
|
+
indexedMember(annotation) {
|
|
1014
|
+
if (!annotation || !typescript_1.default.isIndexedAccessTypeNode(annotation) || !typescript_1.default.isLiteralTypeNode(annotation.indexType) ||
|
|
1015
|
+
!typescript_1.default.isStringLiteral(annotation.indexType.literal)) {
|
|
1016
|
+
return undefined;
|
|
1017
|
+
}
|
|
1018
|
+
return this.memberOf(this.checker.getTypeFromTypeNode(annotation.objectType), annotation.indexType.literal.text);
|
|
1019
|
+
}
|
|
1020
|
+
memberOf(type, name) {
|
|
1021
|
+
var _a;
|
|
1022
|
+
if (type.flags & (typescript_1.default.TypeFlags.Any | typescript_1.default.TypeFlags.Unknown)) {
|
|
1023
|
+
return undefined;
|
|
1024
|
+
}
|
|
1025
|
+
const member = this.checker.getPropertyOfType(this.checker.getApparentType(this.checker.getNonNullableType(type)), name);
|
|
1026
|
+
if (!((_a = member === null || member === void 0 ? void 0 : member.declarations) === null || _a === void 0 ? void 0 : _a.length) || !member.declarations.every(declaration => this.isParsedSource(declaration))) {
|
|
1027
|
+
return undefined;
|
|
1028
|
+
}
|
|
1029
|
+
const owner = this.declaringClassOf(member);
|
|
1030
|
+
return owner && { owner, name: (0, parser_utils_1.escapeLoneSurrogates)(name) };
|
|
1031
|
+
}
|
|
1032
|
+
/**
|
|
1033
|
+
* The member a local variable holding a function implements, where an object literal in its file
|
|
1034
|
+
* holds it as a property, as `{setState}` or `{setState: set}` does, and that literal's contextual
|
|
1035
|
+
* type has that member, or where it is assigned to such a member, as in `inst.setState = set`.
|
|
1036
|
+
* A variable held as members of different types implements none.
|
|
1037
|
+
*/
|
|
1038
|
+
referencedImplementation(variable) {
|
|
1039
|
+
var _a;
|
|
1040
|
+
const sourceFile = variable.getSourceFile();
|
|
1041
|
+
let implementations = this.referencedImplementations.get(sourceFile);
|
|
1042
|
+
if (!implementations) {
|
|
1043
|
+
implementations = new Map();
|
|
1044
|
+
this.referencedImplementations.set(sourceFile, implementations);
|
|
1045
|
+
const found = implementations;
|
|
1046
|
+
const hold = (symbol, member) => {
|
|
1047
|
+
const declaration = symbol === null || symbol === void 0 ? void 0 : symbol.valueDeclaration;
|
|
1048
|
+
if (!declaration || !typescript_1.default.isVariableDeclaration(declaration) || declaration.getSourceFile() !== sourceFile ||
|
|
1049
|
+
!declaration.initializer || !this.heldFunction(declaration.initializer) || found.get(declaration) === null) {
|
|
1050
|
+
return;
|
|
1051
|
+
}
|
|
1052
|
+
const implemented = member();
|
|
1053
|
+
if (implemented) {
|
|
1054
|
+
const previous = found.get(declaration);
|
|
1055
|
+
found.set(declaration, !previous || this.sameMember(previous, implemented) ? implemented : null);
|
|
1056
|
+
}
|
|
1057
|
+
};
|
|
1058
|
+
const visit = (node) => {
|
|
1059
|
+
if (typescript_1.default.isShorthandPropertyAssignment(node)) {
|
|
1060
|
+
hold(this.checker.getShorthandAssignmentValueSymbol(node), () => this.memberOfContextualType(node.parent, node.name));
|
|
1061
|
+
}
|
|
1062
|
+
else if (typescript_1.default.isPropertyAssignment(node) && typescript_1.default.isIdentifier(node.initializer)) {
|
|
1063
|
+
hold(this.checker.getSymbolAtLocation(node.initializer), () => this.memberOfContextualType(node.parent, node.name));
|
|
1064
|
+
}
|
|
1065
|
+
else if (typescript_1.default.isBinaryExpression(node) && node.operatorToken.kind === typescript_1.default.SyntaxKind.EqualsToken &&
|
|
1066
|
+
typescript_1.default.isIdentifier(node.right) && !typescript_1.default.isIdentifier(node.left)) {
|
|
1067
|
+
hold(this.checker.getSymbolAtLocation(node.right), () => this.assignedMember(node));
|
|
1068
|
+
}
|
|
1069
|
+
typescript_1.default.forEachChild(node, visit);
|
|
1070
|
+
};
|
|
1071
|
+
visit(sourceFile);
|
|
1072
|
+
}
|
|
1073
|
+
return (_a = implementations.get(variable)) !== null && _a !== void 0 ? _a : undefined;
|
|
1074
|
+
}
|
|
1075
|
+
/** The function an initializer is, also through a cast, as in `(() => …) as F`. */
|
|
1076
|
+
heldFunction(initializer) {
|
|
1077
|
+
let node = initializer;
|
|
1078
|
+
while (this.isCast(node)) {
|
|
1079
|
+
node = node.expression;
|
|
1080
|
+
}
|
|
1081
|
+
return typescript_1.default.isArrowFunction(node) || typescript_1.default.isFunctionExpression(node) ? node : undefined;
|
|
1082
|
+
}
|
|
1083
|
+
sameMember(a, b) {
|
|
1084
|
+
return a.name === b.name && this.sameType(a.owner, b.owner);
|
|
1085
|
+
}
|
|
1086
|
+
/** Only a default export, as in `export default function () {}`, declares a function without a name. */
|
|
1087
|
+
functionDeclarationName(declaration) {
|
|
1088
|
+
var _a, _b;
|
|
1089
|
+
return (_b = (_a = declaration.name) === null || _a === void 0 ? void 0 : _a.text) !== null && _b !== void 0 ? _b : '<default>';
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* The name a top-level `const f = …` or an `exports.f = …` assignment gives the function it holds,
|
|
1093
|
+
* also through a cast, as in `const f = (() => …) as F`. One a module exports as its default, by
|
|
1094
|
+
* `export default`, `export =` or `module.exports =`, is named `<default>`, as a call of the module is.
|
|
1095
|
+
*/
|
|
1096
|
+
moduleBindingName(fn) {
|
|
1097
|
+
const node = this.castTarget(fn);
|
|
1098
|
+
const parent = node.parent;
|
|
1099
|
+
if (this.isTopLevelVariable(parent) && parent.initializer === node) {
|
|
1100
|
+
return parent.name.text;
|
|
1101
|
+
}
|
|
1102
|
+
if (typescript_1.default.isExportAssignment(parent)) {
|
|
1103
|
+
return '<default>';
|
|
1104
|
+
}
|
|
1105
|
+
if (!typescript_1.default.isBinaryExpression(parent) || parent.right !== node ||
|
|
1106
|
+
parent.operatorToken.kind !== typescript_1.default.SyntaxKind.EqualsToken || !typescript_1.default.isPropertyAccessExpression(parent.left)) {
|
|
1107
|
+
return undefined;
|
|
1108
|
+
}
|
|
1109
|
+
if (this.isModuleExports(parent.left)) {
|
|
1110
|
+
return '<default>';
|
|
1111
|
+
}
|
|
1112
|
+
return this.isExportsObject(parent.left.expression) ? parent.left.name.text : undefined;
|
|
1113
|
+
}
|
|
1114
|
+
isExportsObject(node) {
|
|
1115
|
+
return typescript_1.default.isIdentifier(node) ? node.text === 'exports' : this.isModuleExports(node);
|
|
1116
|
+
}
|
|
1117
|
+
isModuleExports(node) {
|
|
1118
|
+
return typescript_1.default.isPropertyAccessExpression(node) && typescript_1.default.isIdentifier(node.expression) &&
|
|
1119
|
+
node.expression.text === 'module' && node.name.text === 'exports';
|
|
1120
|
+
}
|
|
1121
|
+
/**
|
|
1122
|
+
* A function that an object of no class holds as a member, in a parsed source file: a function
|
|
1123
|
+
* expression or arrow assigned to a property, as in `app.use = function () {}` or
|
|
1124
|
+
* `{use: function () {}}`, or a method of an object literal. It is declared on its file, as a
|
|
1125
|
+
* function declaration is, under the member's name, unless it implements a member of a named type.
|
|
1126
|
+
*/
|
|
1127
|
+
parsedMemberFunction(declaration) {
|
|
1128
|
+
var _a;
|
|
1129
|
+
if (!declaration || !this.isParsedSource(declaration)) {
|
|
1130
|
+
return undefined;
|
|
1131
|
+
}
|
|
1132
|
+
if (typescript_1.default.isFunctionDeclaration(declaration)) {
|
|
1133
|
+
return { owner: this.functionOwner(declaration), name: (0, parser_utils_1.escapeLoneSurrogates)(this.functionDeclarationName(declaration)) };
|
|
1134
|
+
}
|
|
1135
|
+
if (typescript_1.default.isFunctionExpression(declaration) || typescript_1.default.isArrowFunction(declaration)) {
|
|
1136
|
+
const identity = this.functionIdentity(declaration);
|
|
1137
|
+
return identity && { owner: identity.owner, name: (0, parser_utils_1.escapeLoneSurrogates)(identity.name) };
|
|
1138
|
+
}
|
|
1139
|
+
if (!typescript_1.default.isMethodDeclaration(declaration) || !typescript_1.default.isObjectLiteralExpression(declaration.parent)) {
|
|
1140
|
+
return undefined;
|
|
1141
|
+
}
|
|
1142
|
+
const implemented = this.implementedMember(declaration);
|
|
1143
|
+
if (implemented) {
|
|
1144
|
+
return implemented;
|
|
1145
|
+
}
|
|
1146
|
+
const name = (_a = this.checker.getSymbolAtLocation(declaration.name)) === null || _a === void 0 ? void 0 : _a.getName();
|
|
1147
|
+
return name === undefined ? undefined : {
|
|
1148
|
+
owner: this.sourceModuleType(declaration.getSourceFile()),
|
|
1149
|
+
name: (0, parser_utils_1.escapeLoneSurrogates)(name)
|
|
1150
|
+
};
|
|
1151
|
+
}
|
|
1152
|
+
memberFunctionName(fn) {
|
|
1153
|
+
const node = this.castTarget(fn);
|
|
1154
|
+
const parent = node.parent;
|
|
1155
|
+
if (typescript_1.default.isPropertyAssignment(parent) && parent.initializer === node) {
|
|
1156
|
+
return typescript_1.default.isIdentifier(parent.name) || typescript_1.default.isStringLiteralLike(parent.name) || typescript_1.default.isNumericLiteral(parent.name) ?
|
|
1157
|
+
parent.name.text : undefined;
|
|
1158
|
+
}
|
|
1159
|
+
if (typescript_1.default.isBinaryExpression(parent) && parent.right === node && parent.operatorToken.kind === typescript_1.default.SyntaxKind.EqualsToken &&
|
|
1160
|
+
(typescript_1.default.isPropertyAccessExpression(parent.left) || typescript_1.default.isElementAccessExpression(parent.left)) &&
|
|
1161
|
+
!this.isPrototype(parent.left.expression) &&
|
|
1162
|
+
this.isAnonymousReceiver(this.checker.getTypeAtLocation(parent.left.expression))) {
|
|
1163
|
+
const target = parent.left;
|
|
1164
|
+
if (typescript_1.default.isPropertyAccessExpression(target)) {
|
|
1165
|
+
return target.name.text;
|
|
1166
|
+
}
|
|
1167
|
+
if (typescript_1.default.isElementAccessExpression(target) && typescript_1.default.isStringLiteralLike(target.argumentExpression)) {
|
|
1168
|
+
return target.argumentExpression.text;
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
return undefined;
|
|
1172
|
+
}
|
|
1173
|
+
isPrototype(node) {
|
|
1174
|
+
return typescript_1.default.isPropertyAccessExpression(node) && node.name.text === 'prototype';
|
|
1175
|
+
}
|
|
1176
|
+
isAnonymousReceiver(type) {
|
|
1177
|
+
var _a, _b;
|
|
1178
|
+
return !(((_b = (_a = type.getSymbol()) === null || _a === void 0 ? void 0 : _a.flags) !== null && _b !== void 0 ? _b : 0) & (typescript_1.default.SymbolFlags.Class | typescript_1.default.SymbolFlags.Interface | typescript_1.default.SymbolFlags.Enum));
|
|
831
1179
|
}
|
|
832
1180
|
/** The namespace whose body holds a declaration, as `namespace moment` holds `function utc`. */
|
|
833
1181
|
enclosingNamespace(declaration) {
|
|
@@ -861,6 +1209,9 @@ class JavaScriptTypeMapping {
|
|
|
861
1209
|
if (typescript_1.default.isFunctionDeclaration(declaration)) {
|
|
862
1210
|
return typescript_1.default.isSourceFile(declaration.parent) || typescript_1.default.isModuleBlock(declaration.parent);
|
|
863
1211
|
}
|
|
1212
|
+
if (typescript_1.default.isArrowFunction(declaration) || typescript_1.default.isFunctionExpression(declaration)) {
|
|
1213
|
+
return this.moduleBindingName(declaration) !== undefined;
|
|
1214
|
+
}
|
|
864
1215
|
return (typescript_1.default.isMethodDeclaration(declaration) || typescript_1.default.isAccessor(declaration)) &&
|
|
865
1216
|
!!(typescript_1.default.getCombinedModifierFlags(declaration) & typescript_1.default.ModifierFlags.Static);
|
|
866
1217
|
}
|
|
@@ -894,13 +1245,12 @@ class JavaScriptTypeMapping {
|
|
|
894
1245
|
/**
|
|
895
1246
|
* Helper to create a Type.Method object from common parameters
|
|
896
1247
|
*/
|
|
897
|
-
createMethodType(signature, node, declaringType, name, declaredFormalTypeNames = []) {
|
|
1248
|
+
createMethodType(signature, node, declaringType, name, declaredFormalTypeNames = [], isStatic = this.isStatic(signature)) {
|
|
898
1249
|
// Create composite cache key: declaring type + method name + signature
|
|
899
1250
|
// This prevents cache collisions when methods with identical signatures have different names
|
|
900
1251
|
// (e.g., util.isString(): any vs util.isArray(): any)
|
|
901
1252
|
const declaringTypeSig = java_1.Type.signature(declaringType);
|
|
902
1253
|
const signatureStr = this.checker.signatureToString(signature);
|
|
903
|
-
const isStatic = this.isStatic(signature);
|
|
904
1254
|
const cacheKey = `${isStatic ? 'static ' : ''}${declaringTypeSig}#${name}${signatureStr}`;
|
|
905
1255
|
const cached = this.methodCache.get(cacheKey);
|
|
906
1256
|
if (cached) {
|
|
@@ -955,19 +1305,85 @@ class JavaScriptTypeMapping {
|
|
|
955
1305
|
declaringClassOf(member) {
|
|
956
1306
|
var _a;
|
|
957
1307
|
let owner;
|
|
1308
|
+
let ownerSymbol;
|
|
958
1309
|
for (const declaration of (_a = member.declarations) !== null && _a !== void 0 ? _a : []) {
|
|
959
1310
|
const parent = declaration.parent;
|
|
960
|
-
|
|
1311
|
+
const type = typescript_1.default.isClassLike(parent) || typescript_1.default.isInterfaceDeclaration(parent) ?
|
|
1312
|
+
this.checker.getTypeAtLocation(parent) :
|
|
1313
|
+
undefined;
|
|
1314
|
+
const declaringType = type ? this.declaringType(type) : this.typeLiteralOwner(parent);
|
|
1315
|
+
if (!declaringType || declaringType === java_1.Type.unknownType) {
|
|
961
1316
|
return undefined;
|
|
962
1317
|
}
|
|
963
|
-
|
|
964
|
-
if (owner && owner.symbol !== type.symbol) {
|
|
1318
|
+
if (owner && (type ? ownerSymbol !== type.symbol : !this.sameType(owner, declaringType))) {
|
|
965
1319
|
return undefined;
|
|
966
1320
|
}
|
|
967
|
-
owner =
|
|
1321
|
+
owner !== null && owner !== void 0 ? owner : (owner = declaringType);
|
|
1322
|
+
ownerSymbol !== null && ownerSymbol !== void 0 ? ownerSymbol : (ownerSymbol = type === null || type === void 0 ? void 0 : type.symbol);
|
|
1323
|
+
}
|
|
1324
|
+
return owner;
|
|
1325
|
+
}
|
|
1326
|
+
sameType(a, b) {
|
|
1327
|
+
return java_1.Type.FullyQualified.getFullyQualifiedName(a) === java_1.Type.FullyQualified.getFullyQualifiedName(b);
|
|
1328
|
+
}
|
|
1329
|
+
memberOwner(parent) {
|
|
1330
|
+
var _a;
|
|
1331
|
+
const type = typescript_1.default.isClassLike(parent) || typescript_1.default.isInterfaceDeclaration(parent) || typescript_1.default.isObjectLiteralExpression(parent) ?
|
|
1332
|
+
this.checker.getTypeAtLocation(parent) :
|
|
1333
|
+
undefined;
|
|
1334
|
+
return type ? this.declaringType(type) : (_a = this.typeLiteralOwner(parent)) !== null && _a !== void 0 ? _a : java_1.Type.unknownType;
|
|
1335
|
+
}
|
|
1336
|
+
/**
|
|
1337
|
+
* The type that declares the members of a type literal, as an interface declares its own: the type
|
|
1338
|
+
* alias whose body it is. In parsed source that is also the alias whose conditional body has it as
|
|
1339
|
+
* a branch, as `type StorePersist<S> = S extends … ? {…} : never` does, and a literal that types a
|
|
1340
|
+
* property of a type that declares its members is named after that type and the property, as
|
|
1341
|
+
* `persist` in `interface Store { persist: { rehydrate(): void } }` names `Store.persist`.
|
|
1342
|
+
*/
|
|
1343
|
+
typeLiteralOwner(node) {
|
|
1344
|
+
const aliased = this.aliasedTypeLiteral(node);
|
|
1345
|
+
if (aliased) {
|
|
1346
|
+
return this.declaringType(aliased);
|
|
968
1347
|
}
|
|
969
|
-
|
|
970
|
-
|
|
1348
|
+
if (!typescript_1.default.isTypeLiteralNode(node) || !this.isParsedSource(node)) {
|
|
1349
|
+
return undefined;
|
|
1350
|
+
}
|
|
1351
|
+
const alias = this.conditionalAlias(node);
|
|
1352
|
+
if (alias) {
|
|
1353
|
+
const name = this.getFullyQualifiedNameFromSymbol(alias);
|
|
1354
|
+
return name === 'unknown' ? undefined : Object.assign(Object.assign({}, moduleClass(name)), { classKind: java_1.Type.Class.Kind.Interface });
|
|
1355
|
+
}
|
|
1356
|
+
const property = node.parent;
|
|
1357
|
+
if (!typescript_1.default.isPropertySignature(property) || property.type !== node ||
|
|
1358
|
+
!(typescript_1.default.isIdentifier(property.name) || typescript_1.default.isStringLiteral(property.name))) {
|
|
1359
|
+
return undefined;
|
|
1360
|
+
}
|
|
1361
|
+
const enclosing = property.parent;
|
|
1362
|
+
const owner = typescript_1.default.isInterfaceDeclaration(enclosing) ?
|
|
1363
|
+
this.declaringType(this.checker.getTypeAtLocation(enclosing)) :
|
|
1364
|
+
this.typeLiteralOwner(enclosing);
|
|
1365
|
+
return owner && owner !== java_1.Type.unknownType ?
|
|
1366
|
+
moduleClass(`${java_1.Type.FullyQualified.getFullyQualifiedName(owner)}.${property.name.text}`) :
|
|
1367
|
+
undefined;
|
|
1368
|
+
}
|
|
1369
|
+
conditionalAlias(literal) {
|
|
1370
|
+
let node = literal;
|
|
1371
|
+
let branch = false;
|
|
1372
|
+
while (typescript_1.default.isParenthesizedTypeNode(node.parent) ||
|
|
1373
|
+
(typescript_1.default.isConditionalTypeNode(node.parent) && (node.parent.trueType === node || node.parent.falseType === node))) {
|
|
1374
|
+
branch || (branch = typescript_1.default.isConditionalTypeNode(node.parent));
|
|
1375
|
+
node = node.parent;
|
|
1376
|
+
}
|
|
1377
|
+
return branch && typescript_1.default.isTypeAliasDeclaration(node.parent) && node.parent.type === node ?
|
|
1378
|
+
this.checker.getSymbolAtLocation(node.parent.name) : undefined;
|
|
1379
|
+
}
|
|
1380
|
+
/** The type of a type literal that is the body of a type alias, which declares its members as an interface does. */
|
|
1381
|
+
aliasedTypeLiteral(node) {
|
|
1382
|
+
if (!node || !typescript_1.default.isTypeLiteralNode(node)) {
|
|
1383
|
+
return undefined;
|
|
1384
|
+
}
|
|
1385
|
+
const type = this.checker.getTypeAtLocation(node);
|
|
1386
|
+
return type.aliasSymbol ? type : undefined;
|
|
971
1387
|
}
|
|
972
1388
|
wrapperType(declaringType) {
|
|
973
1389
|
if (declaringType === java_1.Type.Primitive.String && this.stringWrapperType) {
|
|
@@ -985,7 +1401,7 @@ class JavaScriptTypeMapping {
|
|
|
985
1401
|
}
|
|
986
1402
|
}
|
|
987
1403
|
methodType(node) {
|
|
988
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
1404
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
989
1405
|
// An object literal is a `J.NewClass`, and `J.NewClass.getType()` reads
|
|
990
1406
|
// `constructorType.returnType`, as it does for a Java anonymous class.
|
|
991
1407
|
if (typescript_1.default.isObjectLiteralExpression(node)) {
|
|
@@ -1011,24 +1427,33 @@ class JavaScriptTypeMapping {
|
|
|
1011
1427
|
let methodName;
|
|
1012
1428
|
let declaringType;
|
|
1013
1429
|
let declaredFormalTypeNames = [];
|
|
1430
|
+
let isStatic;
|
|
1014
1431
|
if (typescript_1.default.isCallExpression(node) && this.isImportDefer(node.expression)) {
|
|
1015
1432
|
return undefined;
|
|
1016
1433
|
}
|
|
1017
1434
|
// Handle different kinds of nodes that represent methods or method invocations
|
|
1018
|
-
if (typescript_1.default.isCallOrNewExpression(node)) {
|
|
1435
|
+
if (typescript_1.default.isCallOrNewExpression(node) || this.isJsxComponent(node)) {
|
|
1019
1436
|
// For method invocations (e.g., _.map(...))
|
|
1020
1437
|
signature = this.checker.getResolvedSignature(node);
|
|
1021
1438
|
if (!signature) {
|
|
1022
1439
|
return undefined;
|
|
1023
1440
|
}
|
|
1441
|
+
const calleeExpression = typescript_1.default.isCallOrNewExpression(node) ? node.expression : node.tagName;
|
|
1442
|
+
if (typescript_1.default.isJsxOpeningLikeElement(node) && this.checker.getTypeAtLocation(calleeExpression).getConstructSignatures().length) {
|
|
1443
|
+
const component = this.declaringType(signature.getReturnType());
|
|
1444
|
+
return component === java_1.Type.unknownType ? undefined : this.createMethodType(signature, node, component, '<constructor>');
|
|
1445
|
+
}
|
|
1024
1446
|
// Calling a module itself, as in `require('m')()`, calls its default export.
|
|
1025
1447
|
const calledModule = typescript_1.default.isCallExpression(node) ? this.loadedModule(node.expression) : undefined;
|
|
1026
1448
|
if (calledModule) {
|
|
1027
|
-
|
|
1449
|
+
const callee = this.parsedCallee(undefined, signature, true);
|
|
1450
|
+
return callee ?
|
|
1451
|
+
this.createMethodType(signature, node, callee.declaringType, callee.name, [], callee.isStatic) :
|
|
1452
|
+
this.createMethodType(signature, node, moduleClass(calledModule), '<default>');
|
|
1028
1453
|
}
|
|
1029
|
-
let symbol = this.checker.getSymbolAtLocation(
|
|
1454
|
+
let symbol = this.checker.getSymbolAtLocation(calleeExpression);
|
|
1030
1455
|
const declaringNamespace = this.enclosingNamespace(signature.getDeclaration());
|
|
1031
|
-
if (!symbol && typescript_1.default.isPropertyAccessExpression(
|
|
1456
|
+
if (!symbol && typescript_1.default.isPropertyAccessExpression(calleeExpression)) {
|
|
1032
1457
|
// For property access expressions where we couldn't get a symbol,
|
|
1033
1458
|
// try to get the symbol from the signature's declaration
|
|
1034
1459
|
const declaration = signature === null || signature === void 0 ? void 0 : signature.getDeclaration();
|
|
@@ -1039,12 +1464,12 @@ class JavaScriptTypeMapping {
|
|
|
1039
1464
|
if (!symbol && signature) {
|
|
1040
1465
|
// For cases like util.isArray where the module is 'any' type
|
|
1041
1466
|
// We'll construct a basic method type from the signature
|
|
1042
|
-
methodName =
|
|
1467
|
+
methodName = calleeExpression.name.getText();
|
|
1043
1468
|
// When there's no symbol but we have a signature, we need to work harder
|
|
1044
1469
|
// to find the declaring type. This happens with CommonJS require() calls
|
|
1045
1470
|
// where the module is typed as 'any' but methods still have signatures
|
|
1046
1471
|
// Trace back through the AST to the require() call or import binding the receiver comes from
|
|
1047
|
-
const moduleName = this.moduleOfExpression(
|
|
1472
|
+
const moduleName = this.moduleOfExpression(calleeExpression.expression);
|
|
1048
1473
|
// Use the inferred type or fall back to unknown
|
|
1049
1474
|
declaringType = moduleName ? moduleClass(moduleName) : java_1.Type.unknownType;
|
|
1050
1475
|
// Create the method type using the helper
|
|
@@ -1055,10 +1480,10 @@ class JavaScriptTypeMapping {
|
|
|
1055
1480
|
return undefined;
|
|
1056
1481
|
}
|
|
1057
1482
|
// Get the method name
|
|
1058
|
-
if (typescript_1.default.isPropertyAccessExpression(
|
|
1059
|
-
methodName =
|
|
1483
|
+
if (typescript_1.default.isPropertyAccessExpression(calleeExpression)) {
|
|
1484
|
+
methodName = calleeExpression.name.getText();
|
|
1060
1485
|
// Check if the object is an imported symbol
|
|
1061
|
-
const objSymbol = this.checker.getSymbolAtLocation(
|
|
1486
|
+
const objSymbol = this.checker.getSymbolAtLocation(calleeExpression.expression);
|
|
1062
1487
|
let isImport = false;
|
|
1063
1488
|
if (objSymbol) {
|
|
1064
1489
|
// Only call getAliasedSymbol if the symbol is actually an alias
|
|
@@ -1067,21 +1492,28 @@ class JavaScriptTypeMapping {
|
|
|
1067
1492
|
isImport = aliasedSymbol && aliasedSymbol !== objSymbol;
|
|
1068
1493
|
}
|
|
1069
1494
|
}
|
|
1070
|
-
const exprType = this.checker.getTypeAtLocation(
|
|
1495
|
+
const exprType = this.checker.getTypeAtLocation(calleeExpression.expression);
|
|
1071
1496
|
const mappedType = this.getType(exprType);
|
|
1072
1497
|
// A namespace import of an ES module is typed as its anonymous namespace object, so the
|
|
1073
1498
|
// module is what declares its members, and its `default` is the default export. One
|
|
1074
1499
|
// declared as a TypeScript namespace (`export = React`) keeps that namespace's name.
|
|
1075
1500
|
// An installed module's object is named after its package, so where the receiver is an
|
|
1076
1501
|
// import's alias, which resolves to the module its specifier names, the specifier wins.
|
|
1077
|
-
const receiverBinding = this.bindingOf(
|
|
1502
|
+
const receiverBinding = this.bindingOf(calleeExpression.expression);
|
|
1078
1503
|
const anonymous = !mappedType || mappedType.kind !== java_1.Type.Kind.Class ||
|
|
1079
1504
|
mappedType.fullyQualifiedName.startsWith('{') ||
|
|
1080
1505
|
(isImport && this.isInstalledModuleObject(exprType));
|
|
1081
1506
|
const declaringClass = this.declaringClassOf(symbol);
|
|
1507
|
+
const memberFunction = !declaringClass && !declaringNamespace &&
|
|
1508
|
+
(!receiverBinding || receiverBinding.kind === 'named') && this.isAnonymousReceiver(exprType) ?
|
|
1509
|
+
this.parsedMemberFunction(signature.getDeclaration()) : undefined;
|
|
1082
1510
|
if (declaringClass) {
|
|
1083
1511
|
declaringType = declaringClass;
|
|
1084
1512
|
}
|
|
1513
|
+
else if (memberFunction) {
|
|
1514
|
+
declaringType = memberFunction.owner;
|
|
1515
|
+
methodName = memberFunction.name;
|
|
1516
|
+
}
|
|
1085
1517
|
else if (declaringNamespace) {
|
|
1086
1518
|
// A callable module's receiver is typed as its function, which names no declaration
|
|
1087
1519
|
declaringType = moduleClass(this.namespaceName(declaringNamespace));
|
|
@@ -1132,11 +1564,11 @@ class JavaScriptTypeMapping {
|
|
|
1132
1564
|
}
|
|
1133
1565
|
}
|
|
1134
1566
|
}
|
|
1135
|
-
else if (typescript_1.default.isIdentifier(
|
|
1136
|
-
methodName =
|
|
1567
|
+
else if (typescript_1.default.isIdentifier(calleeExpression)) {
|
|
1568
|
+
methodName = calleeExpression.getText();
|
|
1137
1569
|
// Check if this is an import first
|
|
1138
|
-
const exprSymbol = this.checker.getSymbolAtLocation(
|
|
1139
|
-
const binding = this.moduleBinding((_a = exprSymbol === null || exprSymbol === void 0 ? void 0 : exprSymbol.declarations) === null || _a === void 0 ? void 0 : _a[0],
|
|
1570
|
+
const exprSymbol = this.checker.getSymbolAtLocation(calleeExpression);
|
|
1571
|
+
const binding = this.moduleBinding((_a = exprSymbol === null || exprSymbol === void 0 ? void 0 : exprSymbol.declarations) === null || _a === void 0 ? void 0 : _a[0], calleeExpression);
|
|
1140
1572
|
let moduleSpecifier;
|
|
1141
1573
|
let aliasedSymbol;
|
|
1142
1574
|
if (exprSymbol) {
|
|
@@ -1157,10 +1589,11 @@ class JavaScriptTypeMapping {
|
|
|
1157
1589
|
}
|
|
1158
1590
|
}
|
|
1159
1591
|
moduleSpecifier !== null && moduleSpecifier !== void 0 ? moduleSpecifier : (moduleSpecifier = binding === null || binding === void 0 ? void 0 : binding.module);
|
|
1160
|
-
const
|
|
1161
|
-
if (
|
|
1162
|
-
declaringType =
|
|
1163
|
-
methodName =
|
|
1592
|
+
const callee = this.parsedCallee(aliasedSymbol !== null && aliasedSymbol !== void 0 ? aliasedSymbol : symbol, signature, !!moduleSpecifier);
|
|
1593
|
+
if (callee) {
|
|
1594
|
+
declaringType = callee.declaringType;
|
|
1595
|
+
methodName = callee.name;
|
|
1596
|
+
isStatic = callee.isStatic;
|
|
1164
1597
|
}
|
|
1165
1598
|
else if (moduleSpecifier) {
|
|
1166
1599
|
// This is an imported function - use the module specifier as declaring type
|
|
@@ -1188,7 +1621,7 @@ class JavaScriptTypeMapping {
|
|
|
1188
1621
|
}
|
|
1189
1622
|
else {
|
|
1190
1623
|
// Fall back to the original logic for non-imported functions
|
|
1191
|
-
const exprType = this.checker.getTypeAtLocation(
|
|
1624
|
+
const exprType = this.checker.getTypeAtLocation(calleeExpression);
|
|
1192
1625
|
const funcType = this.getType(exprType);
|
|
1193
1626
|
if (funcType && funcType.kind === java_1.Type.Kind.Class) {
|
|
1194
1627
|
const fqn = funcType.fullyQualifiedName;
|
|
@@ -1238,15 +1671,9 @@ class JavaScriptTypeMapping {
|
|
|
1238
1671
|
return undefined;
|
|
1239
1672
|
}
|
|
1240
1673
|
methodName = (0, parser_utils_1.escapeLoneSurrogates)(symbol.getName());
|
|
1241
|
-
// The declaring type: the class, whether declared or an expression, the interface or the
|
|
1242
|
-
const
|
|
1243
|
-
|
|
1244
|
-
const parentType = this.checker.getTypeAtLocation(parent);
|
|
1245
|
-
declaringType = this.declaringType(parentType);
|
|
1246
|
-
}
|
|
1247
|
-
else {
|
|
1248
|
-
declaringType = java_1.Type.unknownType;
|
|
1249
|
-
}
|
|
1674
|
+
// The declaring type: the class, whether declared or an expression, the interface, the object literal or the aliased type literal around it
|
|
1675
|
+
const memberFunction = this.parsedMemberFunction(node);
|
|
1676
|
+
declaringType = memberFunction ? memberFunction.owner : this.memberOwner(node.parent);
|
|
1250
1677
|
// Get type parameters from node
|
|
1251
1678
|
if (node.typeParameters) {
|
|
1252
1679
|
for (const tp of node.typeParameters) {
|
|
@@ -1261,10 +1688,7 @@ class JavaScriptTypeMapping {
|
|
|
1261
1688
|
return undefined;
|
|
1262
1689
|
}
|
|
1263
1690
|
methodName = (0, parser_utils_1.escapeLoneSurrogates)(symbol.getName());
|
|
1264
|
-
|
|
1265
|
-
declaringType = typescript_1.default.isClassLike(parent) || typescript_1.default.isInterfaceDeclaration(parent) || typescript_1.default.isObjectLiteralExpression(parent) ?
|
|
1266
|
-
this.declaringType(this.checker.getTypeAtLocation(parent)) :
|
|
1267
|
-
java_1.Type.unknownType;
|
|
1691
|
+
declaringType = this.memberOwner(node.parent);
|
|
1268
1692
|
}
|
|
1269
1693
|
else if (typescript_1.default.isConstructorDeclaration(node)) {
|
|
1270
1694
|
// For constructor declarations
|
|
@@ -1288,10 +1712,11 @@ class JavaScriptTypeMapping {
|
|
|
1288
1712
|
if (!signature) {
|
|
1289
1713
|
return undefined;
|
|
1290
1714
|
}
|
|
1291
|
-
|
|
1715
|
+
const identity = typescript_1.default.isFunctionExpression(node) ? this.functionIdentity(node) : undefined;
|
|
1716
|
+
methodName = typescript_1.default.isFunctionDeclaration(node) ? this.functionDeclarationName(node) : (_h = identity === null || identity === void 0 ? void 0 : identity.name) !== null && _h !== void 0 ? _h : "<anonymous>";
|
|
1292
1717
|
declaringType = typescript_1.default.isFunctionDeclaration(node) ?
|
|
1293
1718
|
this.functionOwner(node) :
|
|
1294
|
-
this.sourceModuleType(node.getSourceFile());
|
|
1719
|
+
(_j = identity === null || identity === void 0 ? void 0 : identity.owner) !== null && _j !== void 0 ? _j : this.sourceModuleType(node.getSourceFile());
|
|
1295
1720
|
// Get type parameters from node
|
|
1296
1721
|
if (node.typeParameters) {
|
|
1297
1722
|
for (const tp of node.typeParameters) {
|
|
@@ -1299,12 +1724,24 @@ class JavaScriptTypeMapping {
|
|
|
1299
1724
|
}
|
|
1300
1725
|
}
|
|
1301
1726
|
}
|
|
1727
|
+
else if (typescript_1.default.isArrowFunction(node)) {
|
|
1728
|
+
const identity = this.functionIdentity(node);
|
|
1729
|
+
signature = identity ? this.checker.getSignatureFromDeclaration(node) : undefined;
|
|
1730
|
+
if (!identity || !signature) {
|
|
1731
|
+
return undefined;
|
|
1732
|
+
}
|
|
1733
|
+
methodName = identity.name;
|
|
1734
|
+
declaringType = identity.owner;
|
|
1735
|
+
for (const tp of (_k = node.typeParameters) !== null && _k !== void 0 ? _k : []) {
|
|
1736
|
+
declaredFormalTypeNames.push(tp.name.getText());
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1302
1739
|
else {
|
|
1303
1740
|
// For other node types, return undefined
|
|
1304
1741
|
return undefined;
|
|
1305
1742
|
}
|
|
1306
1743
|
// Create the method type using the helper
|
|
1307
|
-
return this.createMethodType(signature, node, declaringType, methodName, declaredFormalTypeNames);
|
|
1744
|
+
return this.createMethodType(signature, node, declaringType, methodName, declaredFormalTypeNames, isStatic);
|
|
1308
1745
|
}
|
|
1309
1746
|
/**
|
|
1310
1747
|
* Get the fully qualified name for a TypeScript type.
|
|
@@ -1528,12 +1965,47 @@ class JavaScriptTypeMapping {
|
|
|
1528
1965
|
toJSON: typeSignatureToJSON
|
|
1529
1966
|
};
|
|
1530
1967
|
}
|
|
1531
|
-
|
|
1532
|
-
const
|
|
1533
|
-
|
|
1968
|
+
bases(classType, baseType, apply) {
|
|
1969
|
+
const base = asClass(this.getType(baseType));
|
|
1970
|
+
if (!base) {
|
|
1971
|
+
return;
|
|
1972
|
+
}
|
|
1973
|
+
const inherit = () => apply(this.heritageFrom(classType, base, new Set()));
|
|
1974
|
+
const waiting = base.fullyQualifiedName === classType.fullyQualifiedName ?
|
|
1975
|
+
this.heritageInProgress.get(base) : undefined;
|
|
1976
|
+
if (waiting) {
|
|
1977
|
+
waiting.push(inherit);
|
|
1978
|
+
}
|
|
1979
|
+
else {
|
|
1980
|
+
inherit();
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
heritageFrom(classType, base, seen) {
|
|
1534
1984
|
// TypeScript accepts heritage cycles (`class A implements A`). Whichever edge closes one,
|
|
1535
1985
|
// in mapping order, is dropped.
|
|
1536
|
-
|
|
1986
|
+
if (this.inheritsFrom(base, classType, new Set())) {
|
|
1987
|
+
return { interfaces: [] };
|
|
1988
|
+
}
|
|
1989
|
+
if (base.fullyQualifiedName !== classType.fullyQualifiedName) {
|
|
1990
|
+
return { supertype: base, interfaces: [] };
|
|
1991
|
+
}
|
|
1992
|
+
// Another type of the class's own name, such as the class that `export =` gives in
|
|
1993
|
+
// @types/supertest's lib/test.d.ts, which its index.d.ts interface `Test` extends. A store
|
|
1994
|
+
// that keys types by name keeps one of the two, and reads this edge as the class extending
|
|
1995
|
+
// itself. Its own bases stand in for it.
|
|
1996
|
+
if (seen.has(base)) {
|
|
1997
|
+
return { interfaces: [] };
|
|
1998
|
+
}
|
|
1999
|
+
seen.add(base);
|
|
2000
|
+
const superclass = base.supertype && asClass(base.supertype);
|
|
2001
|
+
const inherited = superclass ? this.heritageFrom(classType, superclass, seen) : { interfaces: [] };
|
|
2002
|
+
const interfaces = [...inherited.interfaces];
|
|
2003
|
+
for (const iface of base.interfaces) {
|
|
2004
|
+
const baseInterface = asClass(iface);
|
|
2005
|
+
const h = baseInterface ? this.heritageFrom(classType, baseInterface, seen) : { interfaces: [] };
|
|
2006
|
+
interfaces.push(...(h.supertype ? [h.supertype] : []), ...h.interfaces);
|
|
2007
|
+
}
|
|
2008
|
+
return { supertype: inherited.supertype, interfaces };
|
|
1537
2009
|
}
|
|
1538
2010
|
inheritsFrom(type, ancestor, seen) {
|
|
1539
2011
|
if (type === ancestor) {
|
|
@@ -1547,9 +2019,13 @@ class JavaScriptTypeMapping {
|
|
|
1547
2019
|
type.interfaces.some(i => this.inheritsFrom(i, ancestor, seen));
|
|
1548
2020
|
}
|
|
1549
2021
|
addInterface(classType, baseType) {
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
2022
|
+
this.bases(classType, baseType, ({ supertype, interfaces }) => this.addInterfaces(classType, supertype ? [supertype, ...interfaces] : interfaces));
|
|
2023
|
+
}
|
|
2024
|
+
addInterfaces(classType, interfaces) {
|
|
2025
|
+
for (const iface of interfaces) {
|
|
2026
|
+
if (!classType.interfaces.includes(iface)) {
|
|
2027
|
+
classType.interfaces.push(iface);
|
|
2028
|
+
}
|
|
1553
2029
|
}
|
|
1554
2030
|
}
|
|
1555
2031
|
/**
|
|
@@ -1557,7 +2033,20 @@ class JavaScriptTypeMapping {
|
|
|
1557
2033
|
* Since the shell is already in the cache, any recursive references will find it
|
|
1558
2034
|
*/
|
|
1559
2035
|
populateClassType(classType, type) {
|
|
1560
|
-
var _a
|
|
2036
|
+
var _a;
|
|
2037
|
+
this.heritageInProgress.set(classType, []);
|
|
2038
|
+
try {
|
|
2039
|
+
this.populateHeritage(classType, type);
|
|
2040
|
+
}
|
|
2041
|
+
finally {
|
|
2042
|
+
const waiting = (_a = this.heritageInProgress.get(classType)) !== null && _a !== void 0 ? _a : [];
|
|
2043
|
+
this.heritageInProgress.delete(classType);
|
|
2044
|
+
waiting.forEach(inherit => inherit());
|
|
2045
|
+
}
|
|
2046
|
+
this.populateMembers(classType, type);
|
|
2047
|
+
}
|
|
2048
|
+
populateHeritage(classType, type) {
|
|
2049
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
1561
2050
|
const symbol = (_a = type.getSymbol) === null || _a === void 0 ? void 0 : _a.call(type);
|
|
1562
2051
|
const classSymbol = symbol && symbol.flags & typescript_1.default.SymbolFlags.Alias ?
|
|
1563
2052
|
this.checker.getAliasedSymbol(symbol) : symbol;
|
|
@@ -1610,10 +2099,12 @@ class JavaScriptTypeMapping {
|
|
|
1610
2099
|
let interfaces = baseTypes;
|
|
1611
2100
|
if (classType.classKind === java_1.Type.Class.Kind.Class && (!classDeclaration ||
|
|
1612
2101
|
((_j = classDeclaration.heritageClauses) === null || _j === void 0 ? void 0 : _j.some(c => c.token === typescript_1.default.SyntaxKind.ExtendsKeyword)))) {
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
2102
|
+
this.bases(classType, baseTypes[0], ({ supertype, interfaces }) => {
|
|
2103
|
+
if (supertype) {
|
|
2104
|
+
classType.supertype = supertype;
|
|
2105
|
+
}
|
|
2106
|
+
this.addInterfaces(classType, interfaces);
|
|
2107
|
+
});
|
|
1617
2108
|
interfaces = baseTypes.slice(1);
|
|
1618
2109
|
}
|
|
1619
2110
|
for (const baseType of interfaces) {
|
|
@@ -1629,6 +2120,10 @@ class JavaScriptTypeMapping {
|
|
|
1629
2120
|
}
|
|
1630
2121
|
}
|
|
1631
2122
|
}
|
|
2123
|
+
}
|
|
2124
|
+
populateMembers(classType, type) {
|
|
2125
|
+
var _a, _b;
|
|
2126
|
+
const symbol = (_a = type.getSymbol) === null || _a === void 0 ? void 0 : _a.call(type);
|
|
1632
2127
|
// Extract type parameters from declarations (not provided by getBaseTypes)
|
|
1633
2128
|
if (symbol === null || symbol === void 0 ? void 0 : symbol.declarations) {
|
|
1634
2129
|
for (const declaration of symbol.declarations) {
|
|
@@ -1647,7 +2142,7 @@ class JavaScriptTypeMapping {
|
|
|
1647
2142
|
// Get properties and methods
|
|
1648
2143
|
const properties = this.checker.getPropertiesOfType(type);
|
|
1649
2144
|
for (const prop of properties) {
|
|
1650
|
-
const declaration = prop.valueDeclaration || ((
|
|
2145
|
+
const declaration = prop.valueDeclaration || ((_b = prop.declarations) === null || _b === void 0 ? void 0 : _b[0]);
|
|
1651
2146
|
if (!declaration) {
|
|
1652
2147
|
// Skip properties without declarations (synthetic/built-in properties)
|
|
1653
2148
|
continue;
|