Skip to content

Commit

Permalink
fix(api): simple index info has parameter info
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 27, 2022
1 parent 0f99fe8 commit 3b477b5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 26 deletions.
9 changes: 5 additions & 4 deletions packages/api/src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
getIntersectionTypesFlat,
getSymbolType,
getTypeId,
TSIndexInfoMerged,
isPureObject,
wrapSafe,
isArrayType,
Expand All @@ -42,6 +41,7 @@ import {
TypescriptContext,
removeDuplicates,
narrowDeclarationForLocation,
TSIndexInfo,
} from "./util"

const maxDepthExceeded: TypeInfo = { kind: "max_depth", id: getEmptyTypeId() }
Expand Down Expand Up @@ -556,7 +556,7 @@ function _generateTypeTree(
})
}

function getIndexInfo(indexInfo: TSIndexInfoMerged): IndexInfo {
function getIndexInfo({ info: indexInfo, type }: TSIndexInfo): IndexInfo {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const parameterSymbol: ts.Symbol =
// @ts-expect-error This info exists on the object but is not publicly exposed by type info
Expand All @@ -565,8 +565,9 @@ function _generateTypeTree(
indexInfo?.parameterType?.getSymbol()

const parameterType =
indexInfo?.parameterType ??
(parameterSymbol && getSymbolType(tsCtx, parameterSymbol))
type === "parameter" &&
(indexInfo?.parameterType ??
(parameterSymbol && getSymbolType(tsCtx, parameterSymbol)))

return {
...(indexInfo.keyType && { keyType: parseType(indexInfo.keyType) }),
Expand Down
22 changes: 16 additions & 6 deletions packages/api/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ export type TSIndexInfoMerged = {
type?: ts.Type
parameterType?: ts.Type
}

export type TSIndexInfoType = "simple" | "parameter"

interface MappedType extends ts.Type {
declaration: ts.MappedTypeNode
typeParameter?: ts.TypeParameter
Expand All @@ -229,12 +232,16 @@ interface MappedType extends ts.Type {
// containsError?: boolean;
}

export type TSIndexInfo = { type: TSIndexInfoType; info: TSIndexInfoMerged }

export function getIndexInfos(
{ typeChecker, ts }: TypescriptContext,
type: ts.Type
) {
const indexInfos: TSIndexInfoMerged[] = [
...typeChecker.getIndexInfosOfType(type),
const indexInfos: TSIndexInfo[] = [
...typeChecker
.getIndexInfosOfType(type)
.map((info) => ({ type: "simple", info } as const)),
]

if (
Expand All @@ -247,10 +254,13 @@ export function getIndexInfos(

if (mappedType.typeParameter) {
indexInfos.push({
keyType: mappedType.constraintType,
type: mappedType.templateType,
parameterType: mappedType.typeParameter,
declaration: mappedType.declaration,
type: "parameter",
info: {
keyType: mappedType.constraintType,
type: mappedType.templateType,
parameterType: mappedType.typeParameter,
declaration: mappedType.declaration,
},
})
}
}
Expand Down
5 changes: 1 addition & 4 deletions tests/baselines/reference/interfaceWithIndex.localized.tree
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,10 @@ interface InterfaceWithIndex {
"kindText": "string",
"kind": "primitive",
"primitiveKind": "string",
"purpose": "index_parameter_type",
"purpose": "index_type",
"children": [],
"_id": "1"
},
{
"reference": "1"
},
{
"reference": "1"
}
Expand Down
4 changes: 0 additions & 4 deletions tests/baselines/reference/interfaceWithIndex.tree
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ interface InterfaceWithIndex {
}
}
]
},
"parameterType": {
"kind": "reference",
"id": "2"
}
}
],
Expand Down
5 changes: 1 addition & 4 deletions tests/baselines/reference/mapped.localized.tree
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,10 @@ type mapped = { [index: string]: number }
"kindText": "string",
"kind": "primitive",
"primitiveKind": "string",
"purpose": "index_parameter_type",
"purpose": "index_type",
"children": [],
"_id": "1"
},
{
"reference": "1"
},
{
"kindText": "number",
"kind": "primitive",
Expand Down
4 changes: 0 additions & 4 deletions tests/baselines/reference/mapped.tree
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ type mapped = { [index: string]: number }
}
}
]
},
"parameterType": {
"kind": "reference",
"id": "2"
}
}
],
Expand Down

0 comments on commit 3b477b5

Please sign in to comment.