From ec3b53bf83abe3d175ac1c44a1b14e2396eb37d7 Mon Sep 17 00:00:00 2001 From: charburgx Date: Sun, 6 Nov 2022 20:45:57 -0600 Subject: [PATCH] fix(api): find nearest signature recursively fixes #9 --- packages/api/src/tree.ts | 6 +- packages/api/src/util.ts | 14 +- .../reference/mapArray.localized.tree | 1144 +++++++++++++++++ tests/baselines/reference/mapArray.tree | 1096 ++++++++++++++++ tests/cases/mapArray.ts | 1 + tests/other/map.spec.ts | 26 +- 6 files changed, 2280 insertions(+), 7 deletions(-) create mode 100644 tests/baselines/reference/mapArray.localized.tree create mode 100644 tests/baselines/reference/mapArray.tree create mode 100644 tests/cases/mapArray.ts diff --git a/packages/api/src/tree.ts b/packages/api/src/tree.ts index 5b7d7e1..3741300 100644 --- a/packages/api/src/tree.ts +++ b/packages/api/src/tree.ts @@ -56,6 +56,7 @@ import { isReadonlyTupleType, getSymbolExports, isNamespace, + getSignaturesOfType, } from "./util" const maxDepthExceeded: TypeInfo = { kind: "max_depth", id: getEmptyTypeId() } @@ -693,7 +694,10 @@ function resolveSignature( const isConstructCallExpression = node?.parent.kind === ts.SyntaxKind.NewExpression - const signature = getResolvedSignature(ctx, node) + const signature = + getSignaturesOfType(ctx, type).length > 0 + ? getResolvedSignature(ctx, node) + : undefined const signatureTypeArguments = signature ? getSignatureTypeArguments(ctx, signature, node) diff --git a/packages/api/src/util.ts b/packages/api/src/util.ts index 801430a..0368950 100644 --- a/packages/api/src/util.ts +++ b/packages/api/src/util.ts @@ -673,11 +673,15 @@ export function getCallLikeExpression( { ts }: TypescriptContext, node: ts.Node ) { - return ts.isCallLikeExpression(node) - ? node - : ts.isCallLikeExpression(node.parent) - ? node.parent - : undefined + while (node && !ts.isSourceFile(node)) { + if (ts.isCallLikeExpression(node)) { + return node + } + + node = node.parent + } + + return undefined } /** diff --git a/tests/baselines/reference/mapArray.localized.tree b/tests/baselines/reference/mapArray.localized.tree new file mode 100644 index 0000000..f9b3dcd --- /dev/null +++ b/tests/baselines/reference/mapArray.localized.tree @@ -0,0 +1,1144 @@ +=== mapArray.ts === + +const mapped = [1].map(x => x.toString()) +> const mapped = [1].map(x => x.toString()) +> mapped = [1].map(x => x.toString()) +> mapped = [1].map(x => x.toString()) +> mapped --- { + "kindText": "string", + "kind": "primitive", + "primitiveKind": "string", + "symbol": { + "name": "mapped", + "locations": [ + { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 6 + }, + "end": { + "line": 0, + "character": 12 + } + } + } + ] + }, + "dimension": 1, + "locations": [ + { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 6 + }, + "end": { + "line": 0, + "character": 12 + } + } + } + ], + "children": [], + "_id": "0" +} +> [1].map(x => x.toString()) +> [1].map --- { + "kindText": "method", + "kind": "function", + "symbol": { + "name": "map", + "insideClassOrInterface": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 4 + }, + "end": { + "line": 1427, + "character": 7 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 4 + }, + "end": { + "line": 1427, + "character": 7 + } + } + } + ], + "children": [ + { + "purpose": "type_parameter_list", + "children": [ + { + "kindText": "type parameter", + "kind": "type_parameter", + "symbol": { + "name": "U", + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 8 + }, + "end": { + "line": 1427, + "character": 9 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 8 + }, + "end": { + "line": 1427, + "character": 9 + } + } + } + ], + "children": [ + { + "kindText": "string", + "kind": "primitive", + "primitiveKind": "string", + "purpose": "parameter_value", + "children": [], + "_id": "2" + } + ], + "_id": "1" + } + ] + }, + { + "kindText": "function", + "kind": "function", + "symbol": { + "name": "callbackfn", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 11 + }, + "end": { + "line": 1427, + "character": 21 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 11 + }, + "end": { + "line": 1427, + "character": 21 + } + } + } + ], + "children": [ + { + "kindText": "number", + "kind": "primitive", + "primitiveKind": "number", + "symbol": { + "name": "value", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + ], + "children": [], + "_id": "4" + }, + { + "reference": "4" + }, + { + "kindText": "number", + "kind": "primitive", + "primitiveKind": "number", + "alias": "value", + "symbol": { + "name": "array", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 49 + }, + "end": { + "line": 1427, + "character": 54 + } + } + } + ] + }, + "dimension": 1, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + ], + "children": [], + "_id": "5" + }, + { + "reference": "2" + } + ], + "_id": "3" + }, + { + "kindText": "any", + "kind": "primitive", + "primitiveKind": "any", + "symbol": { + "name": "thisArg", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 67 + }, + "end": { + "line": 1427, + "character": 74 + } + } + } + ] + }, + "optional": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 67 + }, + "end": { + "line": 1427, + "character": 74 + } + } + } + ], + "children": [], + "_id": "6" + }, + { + "kindText": "string", + "kind": "primitive", + "primitiveKind": "string", + "purpose": "return", + "dimension": 1, + "children": [], + "_id": "7" + } + ], + "_id": "0" +} +> map --- { + "kindText": "method", + "kind": "function", + "symbol": { + "name": "map", + "insideClassOrInterface": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 4 + }, + "end": { + "line": 1427, + "character": 7 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 4 + }, + "end": { + "line": 1427, + "character": 7 + } + } + } + ], + "children": [ + { + "purpose": "type_parameter_list", + "children": [ + { + "kindText": "type parameter", + "kind": "type_parameter", + "symbol": { + "name": "U", + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 8 + }, + "end": { + "line": 1427, + "character": 9 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 8 + }, + "end": { + "line": 1427, + "character": 9 + } + } + } + ], + "children": [ + { + "kindText": "string", + "kind": "primitive", + "primitiveKind": "string", + "purpose": "parameter_value", + "children": [], + "_id": "2" + } + ], + "_id": "1" + } + ] + }, + { + "kindText": "function", + "kind": "function", + "symbol": { + "name": "callbackfn", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 11 + }, + "end": { + "line": 1427, + "character": 21 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 11 + }, + "end": { + "line": 1427, + "character": 21 + } + } + } + ], + "children": [ + { + "kindText": "number", + "kind": "primitive", + "primitiveKind": "number", + "symbol": { + "name": "value", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + ], + "children": [], + "_id": "4" + }, + { + "reference": "4" + }, + { + "kindText": "number", + "kind": "primitive", + "primitiveKind": "number", + "alias": "value", + "symbol": { + "name": "array", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 49 + }, + "end": { + "line": 1427, + "character": 54 + } + } + } + ] + }, + "dimension": 1, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + ], + "children": [], + "_id": "5" + }, + { + "reference": "2" + } + ], + "_id": "3" + }, + { + "kindText": "any", + "kind": "primitive", + "primitiveKind": "any", + "symbol": { + "name": "thisArg", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 67 + }, + "end": { + "line": 1427, + "character": 74 + } + } + } + ] + }, + "optional": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 67 + }, + "end": { + "line": 1427, + "character": 74 + } + } + } + ], + "children": [], + "_id": "6" + }, + { + "kindText": "string", + "kind": "primitive", + "primitiveKind": "string", + "purpose": "return", + "dimension": 1, + "children": [], + "_id": "7" + } + ], + "_id": "0" +} +> x => x.toString() +> x => x.toString() +> x +> x +> x --- { + "kindText": "number", + "kind": "primitive", + "primitiveKind": "number", + "symbol": { + "name": "x", + "isArgument": true, + "locations": [ + { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 23 + }, + "end": { + "line": 0, + "character": 24 + } + } + } + ] + }, + "locations": [ + { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 23 + }, + "end": { + "line": 0, + "character": 24 + } + } + } + ], + "children": [], + "_id": "0" +} +> => --- { + "kindText": "function", + "kind": "function", + "symbol": { + "name": "__function", + "locations": [ + { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 23 + }, + "end": { + "line": 0, + "character": 40 + } + } + } + ] + }, + "locations": [ + { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 23 + }, + "end": { + "line": 0, + "character": 40 + } + } + } + ], + "children": [ + { + "purpose": "type_parameter_list", + "children": [ + { + "kindText": "type parameter", + "kind": "type_parameter", + "symbol": { + "name": "U", + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 8 + }, + "end": { + "line": 1427, + "character": 9 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 8 + }, + "end": { + "line": 1427, + "character": 9 + } + } + } + ], + "children": [ + { + "kindText": "string", + "kind": "primitive", + "primitiveKind": "string", + "purpose": "parameter_value", + "children": [], + "_id": "2" + } + ], + "_id": "1" + } + ] + }, + { + "kindText": "function", + "kind": "function", + "symbol": { + "name": "callbackfn", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 11 + }, + "end": { + "line": 1427, + "character": 21 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 11 + }, + "end": { + "line": 1427, + "character": 21 + } + } + } + ], + "children": [ + { + "kindText": "number", + "kind": "primitive", + "primitiveKind": "number", + "symbol": { + "name": "value", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + ], + "children": [], + "_id": "4" + }, + { + "reference": "4" + }, + { + "kindText": "number", + "kind": "primitive", + "primitiveKind": "number", + "alias": "value", + "symbol": { + "name": "array", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 49 + }, + "end": { + "line": 1427, + "character": 54 + } + } + } + ] + }, + "dimension": 1, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + ], + "children": [], + "_id": "5" + }, + { + "reference": "2" + } + ], + "_id": "3" + }, + { + "kindText": "any", + "kind": "primitive", + "primitiveKind": "any", + "symbol": { + "name": "thisArg", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 67 + }, + "end": { + "line": 1427, + "character": 74 + } + } + } + ] + }, + "optional": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 67 + }, + "end": { + "line": 1427, + "character": 74 + } + } + } + ], + "children": [], + "_id": "6" + }, + { + "kindText": "string", + "kind": "primitive", + "primitiveKind": "string", + "purpose": "return", + "dimension": 1, + "children": [], + "_id": "7" + } + ], + "_id": "0" +} +> x.toString() +> x.toString --- { + "kindText": "method", + "kind": "function", + "symbol": { + "name": "toString", + "insideClassOrInterface": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 4 + }, + "end": { + "line": 555, + "character": 12 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 4 + }, + "end": { + "line": 555, + "character": 12 + } + } + } + ], + "children": [ + { + "kindText": "number", + "kind": "primitive", + "primitiveKind": "number", + "symbol": { + "name": "radix", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 13 + }, + "end": { + "line": 555, + "character": 18 + } + } + } + ] + }, + "optional": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 13 + }, + "end": { + "line": 555, + "character": 18 + } + } + } + ], + "children": [], + "_id": "1" + }, + { + "kindText": "string", + "kind": "primitive", + "primitiveKind": "string", + "purpose": "return", + "children": [], + "_id": "2" + } + ], + "_id": "0" +} +> x --- { + "kindText": "number", + "kind": "primitive", + "primitiveKind": "number", + "symbol": { + "name": "x", + "isArgument": true, + "locations": [ + { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 23 + }, + "end": { + "line": 0, + "character": 24 + } + } + } + ] + }, + "locations": [ + { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 23 + }, + "end": { + "line": 0, + "character": 24 + } + } + } + ], + "children": [], + "_id": "0" +} +> toString --- { + "kindText": "method", + "kind": "function", + "symbol": { + "name": "toString", + "insideClassOrInterface": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 4 + }, + "end": { + "line": 555, + "character": 12 + } + } + } + ] + }, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 4 + }, + "end": { + "line": 555, + "character": 12 + } + } + } + ], + "children": [ + { + "kindText": "number", + "kind": "primitive", + "primitiveKind": "number", + "symbol": { + "name": "radix", + "isArgument": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 13 + }, + "end": { + "line": 555, + "character": 18 + } + } + } + ] + }, + "optional": true, + "locations": [ + { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 13 + }, + "end": { + "line": 555, + "character": 18 + } + } + } + ], + "children": [], + "_id": "1" + }, + { + "kindText": "string", + "kind": "primitive", + "primitiveKind": "string", + "purpose": "return", + "children": [], + "_id": "2" + } + ], + "_id": "0" +} \ No newline at end of file diff --git a/tests/baselines/reference/mapArray.tree b/tests/baselines/reference/mapArray.tree new file mode 100644 index 0000000..90a96e5 --- /dev/null +++ b/tests/baselines/reference/mapArray.tree @@ -0,0 +1,1096 @@ +=== mapArray.ts === + +const mapped = [1].map(x => x.toString()) +> const mapped = [1].map(x => x.toString()) +> mapped = [1].map(x => x.toString()) +> mapped = [1].map(x => x.toString()) +> mapped --- { + "kind": "array", + "type": { + "kind": "primitive", + "primitive": "string", + "id": "1" + }, + "symbolMeta": { + "name": "mapped", + "flags": 2, + "declarations": [ + { + "location": { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 6 + }, + "end": { + "line": 0, + "character": 12 + } + } + } + } + ] + }, + "id": "0" +} +> [1].map(x => x.toString()) +> [1].map --- { + "kind": "function", + "signatures": [ + { + "symbolMeta": { + "name": "map", + "flags": 8192, + "insideClassOrInterface": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 4 + }, + "end": { + "line": 1427, + "character": 7 + } + } + } + } + ] + }, + "parameters": [ + { + "kind": "function", + "signatures": [ + { + "symbolMeta": { + "name": "__type", + "flags": 2048, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 23 + }, + "end": { + "line": 1427, + "character": 65 + } + } + } + } + ] + }, + "parameters": [ + { + "kind": "primitive", + "primitive": "number", + "symbolMeta": { + "name": "value", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + } + ] + }, + "id": "4" + }, + { + "kind": "reference", + "symbolMeta": { + "name": "index", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 34 + }, + "end": { + "line": 1427, + "character": 39 + } + } + } + } + ] + }, + "id": "4" + }, + { + "kind": "array", + "type": { + "kind": "reference", + "id": "4" + }, + "symbolMeta": { + "name": "array", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 49 + }, + "end": { + "line": 1427, + "character": 54 + } + } + } + } + ] + }, + "id": "5" + } + ], + "returnType": { + "kind": "primitive", + "primitive": "string", + "id": "2" + } + } + ], + "symbolMeta": { + "name": "callbackfn", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 11 + }, + "end": { + "line": 1427, + "character": 21 + } + } + } + } + ] + }, + "id": "3" + }, + { + "kind": "primitive", + "primitive": "any", + "symbolMeta": { + "name": "thisArg", + "flags": 33554433, + "optional": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 67 + }, + "end": { + "line": 1427, + "character": 74 + } + } + } + } + ] + }, + "id": "6" + } + ], + "returnType": { + "kind": "array", + "type": { + "kind": "reference", + "id": "2" + }, + "id": "7" + }, + "typeParameters": [ + { + "kind": "type_parameter", + "symbolMeta": { + "name": "U", + "flags": 262144, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 8 + }, + "end": { + "line": 1427, + "character": 9 + } + } + } + } + ] + }, + "id": "8" + } + ] + } + ], + "symbolMeta": { + "name": "map", + "flags": 33562624, + "insideClassOrInterface": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 4 + }, + "end": { + "line": 1427, + "character": 7 + } + } + } + } + ] + }, + "typeParameters": [ + { + "kind": "type_parameter", + "symbolMeta": { + "name": "U", + "flags": 262144, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 8 + }, + "end": { + "line": 1427, + "character": 9 + } + } + } + } + ] + }, + "id": "1" + } + ], + "typeArguments": [ + { + "kind": "reference", + "id": "2" + } + ], + "id": "0" +} +> map --- { + "kind": "function", + "signatures": [ + { + "symbolMeta": { + "name": "map", + "flags": 8192, + "insideClassOrInterface": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 4 + }, + "end": { + "line": 1427, + "character": 7 + } + } + } + } + ] + }, + "parameters": [ + { + "kind": "function", + "signatures": [ + { + "symbolMeta": { + "name": "__type", + "flags": 2048, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 23 + }, + "end": { + "line": 1427, + "character": 65 + } + } + } + } + ] + }, + "parameters": [ + { + "kind": "primitive", + "primitive": "number", + "symbolMeta": { + "name": "value", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + } + ] + }, + "id": "4" + }, + { + "kind": "reference", + "symbolMeta": { + "name": "index", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 34 + }, + "end": { + "line": 1427, + "character": 39 + } + } + } + } + ] + }, + "id": "4" + }, + { + "kind": "array", + "type": { + "kind": "reference", + "id": "4" + }, + "symbolMeta": { + "name": "array", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 49 + }, + "end": { + "line": 1427, + "character": 54 + } + } + } + } + ] + }, + "id": "5" + } + ], + "returnType": { + "kind": "primitive", + "primitive": "string", + "id": "2" + } + } + ], + "symbolMeta": { + "name": "callbackfn", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 11 + }, + "end": { + "line": 1427, + "character": 21 + } + } + } + } + ] + }, + "id": "3" + }, + { + "kind": "primitive", + "primitive": "any", + "symbolMeta": { + "name": "thisArg", + "flags": 33554433, + "optional": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 67 + }, + "end": { + "line": 1427, + "character": 74 + } + } + } + } + ] + }, + "id": "6" + } + ], + "returnType": { + "kind": "array", + "type": { + "kind": "reference", + "id": "2" + }, + "id": "7" + }, + "typeParameters": [ + { + "kind": "type_parameter", + "symbolMeta": { + "name": "U", + "flags": 262144, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 8 + }, + "end": { + "line": 1427, + "character": 9 + } + } + } + } + ] + }, + "id": "8" + } + ] + } + ], + "symbolMeta": { + "name": "map", + "flags": 33562624, + "insideClassOrInterface": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 4 + }, + "end": { + "line": 1427, + "character": 7 + } + } + } + } + ] + }, + "typeParameters": [ + { + "kind": "type_parameter", + "symbolMeta": { + "name": "U", + "flags": 262144, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 8 + }, + "end": { + "line": 1427, + "character": 9 + } + } + } + } + ] + }, + "id": "1" + } + ], + "typeArguments": [ + { + "kind": "reference", + "id": "2" + } + ], + "id": "0" +} +> x => x.toString() +> x => x.toString() +> x +> x +> x --- { + "kind": "primitive", + "primitive": "number", + "symbolMeta": { + "name": "x", + "flags": 1, + "declarations": [ + { + "location": { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 23 + }, + "end": { + "line": 0, + "character": 24 + } + } + } + } + ] + }, + "id": "0" +} +> => --- { + "kind": "function", + "signatures": [ + { + "symbolMeta": { + "name": "map", + "flags": 8192, + "insideClassOrInterface": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 4 + }, + "end": { + "line": 1427, + "character": 7 + } + } + } + } + ] + }, + "parameters": [ + { + "kind": "function", + "signatures": [ + { + "symbolMeta": { + "name": "__type", + "flags": 2048, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 23 + }, + "end": { + "line": 1427, + "character": 65 + } + } + } + } + ] + }, + "parameters": [ + { + "kind": "primitive", + "primitive": "number", + "symbolMeta": { + "name": "value", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 24 + }, + "end": { + "line": 1427, + "character": 29 + } + } + } + } + ] + }, + "id": "3" + }, + { + "kind": "reference", + "symbolMeta": { + "name": "index", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 34 + }, + "end": { + "line": 1427, + "character": 39 + } + } + } + } + ] + }, + "id": "3" + }, + { + "kind": "array", + "type": { + "kind": "reference", + "id": "3" + }, + "symbolMeta": { + "name": "array", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 49 + }, + "end": { + "line": 1427, + "character": 54 + } + } + } + } + ] + }, + "id": "4" + } + ], + "returnType": { + "kind": "primitive", + "primitive": "string", + "id": "1" + } + } + ], + "symbolMeta": { + "name": "callbackfn", + "flags": 33554433, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 11 + }, + "end": { + "line": 1427, + "character": 21 + } + } + } + } + ] + }, + "id": "2" + }, + { + "kind": "primitive", + "primitive": "any", + "symbolMeta": { + "name": "thisArg", + "flags": 33554433, + "optional": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 67 + }, + "end": { + "line": 1427, + "character": 74 + } + } + } + } + ] + }, + "id": "5" + } + ], + "returnType": { + "kind": "array", + "type": { + "kind": "reference", + "id": "1" + }, + "id": "6" + }, + "typeParameters": [ + { + "kind": "type_parameter", + "symbolMeta": { + "name": "U", + "flags": 262144, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 1427, + "character": 8 + }, + "end": { + "line": 1427, + "character": 9 + } + } + } + } + ] + }, + "id": "7" + } + ] + } + ], + "symbolMeta": { + "name": "__function", + "flags": 16, + "declarations": [ + { + "location": { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 23 + }, + "end": { + "line": 0, + "character": 40 + } + } + } + } + ] + }, + "typeArguments": [ + { + "kind": "reference", + "id": "1" + } + ], + "id": "0" +} +> x.toString() +> x.toString --- { + "kind": "function", + "signatures": [ + { + "symbolMeta": { + "name": "toString", + "flags": 8192, + "insideClassOrInterface": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 4 + }, + "end": { + "line": 555, + "character": 12 + } + } + } + } + ] + }, + "parameters": [ + { + "kind": "primitive", + "primitive": "number", + "symbolMeta": { + "name": "radix", + "flags": 1, + "optional": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 13 + }, + "end": { + "line": 555, + "character": 18 + } + } + } + } + ] + }, + "id": "1" + } + ], + "returnType": { + "kind": "primitive", + "primitive": "string", + "id": "2" + } + } + ], + "symbolMeta": { + "name": "toString", + "flags": 8192, + "insideClassOrInterface": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 4 + }, + "end": { + "line": 555, + "character": 12 + } + } + } + } + ] + }, + "id": "0" +} +> x --- { + "kind": "primitive", + "primitive": "number", + "symbolMeta": { + "name": "x", + "flags": 1, + "declarations": [ + { + "location": { + "fileName": "cases/mapArray.ts", + "range": { + "start": { + "line": 0, + "character": 23 + }, + "end": { + "line": 0, + "character": 24 + } + } + } + } + ] + }, + "id": "0" +} +> toString --- { + "kind": "function", + "signatures": [ + { + "symbolMeta": { + "name": "toString", + "flags": 8192, + "insideClassOrInterface": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 4 + }, + "end": { + "line": 555, + "character": 12 + } + } + } + } + ] + }, + "parameters": [ + { + "kind": "primitive", + "primitive": "number", + "symbolMeta": { + "name": "radix", + "flags": 1, + "optional": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 13 + }, + "end": { + "line": 555, + "character": 18 + } + } + } + } + ] + }, + "id": "1" + } + ], + "returnType": { + "kind": "primitive", + "primitive": "string", + "id": "2" + } + } + ], + "symbolMeta": { + "name": "toString", + "flags": 8192, + "insideClassOrInterface": true, + "declarations": [ + { + "location": { + "fileName": "../node_modules/typescript/lib/lib.es5.d.ts", + "range": { + "start": { + "line": 555, + "character": 4 + }, + "end": { + "line": 555, + "character": 12 + } + } + } + } + ] + }, + "id": "0" +} \ No newline at end of file diff --git a/tests/cases/mapArray.ts b/tests/cases/mapArray.ts new file mode 100644 index 0000000..4aa615b --- /dev/null +++ b/tests/cases/mapArray.ts @@ -0,0 +1 @@ +const mapped = [1].map(x => x.toString()) \ No newline at end of file diff --git a/tests/other/map.spec.ts b/tests/other/map.spec.ts index 43db262..ccd0a02 100644 --- a/tests/other/map.spec.ts +++ b/tests/other/map.spec.ts @@ -1,4 +1,5 @@ import { APIConfig, getTypeInfoAtRange } from "@ts-type-explorer/api" +import assert from "assert" import path from "path" import { createTsContext } from "../lib/tsUtil" @@ -28,7 +29,7 @@ describe("map.ts", () => { const fileName2 = path.join(__dirname, "map2.ts") describe("map2.ts", () => { - it("deosn't error when retrieving outside", () => { + it("doesn't error when retrieving outside", () => { const ctx = createTsContext(fileName2) const pos = { line: 3, character: 0 } @@ -45,4 +46,27 @@ describe("map2.ts", () => { apiConfig ) }) + + it("has proper return", () => { + const ctx = createTsContext(fileName2) + + const pos = { line: 1, character: 47 } + + const info = getTypeInfoAtRange(ctx, { + fileName: fileName2, + range: { + start: pos, + end: pos, + }, + }) + + assert(info && info.kind === "function") + + const returnType = info.signatures[0].returnType + assert( + returnType && + returnType.kind === "array" && + returnType.type.aliasSymbolMeta?.name === "Type" + ) + }) })