Skip to content

Commit

Permalink
feat: support indexes in interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 27, 2022
1 parent a575911 commit 0f99fe8
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 21 deletions.
3 changes: 3 additions & 0 deletions packages/api/src/localizedTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ function getChildren(
baseType,
implementsTypes,
constructSignatures,
indexInfos,
} = info
return [
...(baseType
Expand Down Expand Up @@ -432,6 +433,8 @@ function getChildren(
),
]
: []),
...(indexInfos?.map((info) => getLocalizedIndex(info)) ??
[]),
...properties.map(localize),
]
}
Expand Down
52 changes: 31 additions & 21 deletions packages/api/src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ function _generateTypeTree(
}
}

const indexInfos = getIndexInfos(tsCtx, type) //.map((indexInfo) => getIndexInfo(indexInfo))

if (isArrayType(tsCtx, type)) {
return {
kind: "array",
Expand All @@ -339,15 +341,17 @@ function _generateTypeTree(
symbol &&
symbol.flags & SymbolFlags.Class)
) {
const interfaceKind = isClassType(tsCtx, type)
? "class"
: isInterfaceType(tsCtx, type)
? "interface"
: (assert(
false,
"Should be class or interface type"
) as never)

return {
kind: isClassType(tsCtx, type)
? "class"
: isInterfaceType(tsCtx, type)
? "interface"
: (assert(
false,
"Should be class or interface type"
) as never),
kind: interfaceKind,
properties: parseSymbols(type.getProperties(), {
insideClassOrInterface: true,
}),
Expand All @@ -365,6 +369,10 @@ function _generateTypeTree(
})
),
classSymbol: wrapSafe(getSymbolInfo)(classSymbol),
...(interfaceKind === "interface" &&
isNonEmpty(indexInfos) && {
indexInfos: indexInfos.map(getIndexInfo),
}),
}
} else if (signatures.length > 0) {
return {
Expand All @@ -381,9 +389,7 @@ function _generateTypeTree(
return {
kind: "object",
properties: parseSymbols(type.getProperties()),
indexInfos: getIndexInfos(tsCtx, type).map((indexInfo) =>
getIndexInfo(indexInfo)
),
indexInfos: indexInfos.map(getIndexInfo),
objectClass: wrapSafe(parseSymbol)(classSymbol),
}
}
Expand Down Expand Up @@ -694,6 +700,12 @@ export function getTypeInfoChildren(info: TypeInfo): TypeInfo[] {
...(signature.typeParameters ?? []),
]

const mapIndexInfo = (x: IndexInfo): (TypeInfo | undefined)[] => [
x.type,
x.keyType,
x.parameterType,
]

return [
...(info.typeParameters ?? []),
...(info.typeArguments ?? []),
Expand All @@ -705,11 +717,7 @@ export function getTypeInfoChildren(info: TypeInfo): TypeInfo[] {
case "object": {
return [
...info.properties,
...(info.indexInfos?.flatMap((x) => [
x.type,
x.keyType,
x.parameterType,
]) ?? []),
...(info.indexInfos?.flatMap(mapIndexInfo) ?? []),
// ...wrapSafe(getTypeInfoChildren)(info.objectClass) ?? [],
info.objectClass,
]
Expand Down Expand Up @@ -776,6 +784,7 @@ export function getTypeInfoChildren(info: TypeInfo): TypeInfo[] {
[]),
info.baseType,
...(info.implementsTypes ?? []),
...(info.indexInfos?.flatMap(mapIndexInfo) ?? []),
]
}

Expand All @@ -789,6 +798,10 @@ export function getTypeInfoChildren(info: TypeInfo): TypeInfo[] {
}

export function getTypeInfoSymbols(info: TypeInfo): SymbolInfo[] {
const mapIndexInfo = ({
parameterSymbol,
}: IndexInfo): SymbolInfo | undefined => parameterSymbol

return removeDuplicates(
filterUndefined([
info.symbolMeta,
Expand All @@ -800,17 +813,14 @@ export function getTypeInfoSymbols(info: TypeInfo): SymbolInfo[] {
function _getTypeInfoSymbols(info: TypeInfo): (SymbolInfo | undefined)[] {
switch (info.kind) {
case "object": {
return [
...(info.indexInfos?.map(
({ parameterSymbol }) => parameterSymbol
) ?? []),
]
return [...(info.indexInfos?.map(mapIndexInfo) ?? [])]
}

case "class": {
return [
...(info.constructSignatures?.flatMap(mapSignature) ?? []),
info.classSymbol,
...(info.indexInfos?.map(mapIndexInfo) ?? []),
]
}

Expand Down
1 change: 1 addition & 0 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export type TypeInfoNoId = {
// typeParameters?: TypeInfo[],
constructSignatures?: SignatureInfo[]
classSymbol?: SymbolInfo
indexInfos?: IndexInfo[]
}
| { kind: "function"; signatures: SignatureInfo[] }
| { kind: "array"; type: TypeInfo }
Expand Down
214 changes: 214 additions & 0 deletions tests/baselines/reference/interfaceWithIndex.localized.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
=== interfaceWithIndex.ts ===

interface InterfaceWithIndex {
[index: string]: string;
a: "asd"
}
> InterfaceWithIndex --- {
"kindText": "interface",
"kind": "interface",
"symbol": {
"name": "InterfaceWithIndex",
"locations": [
{
"fileName": "cases/interfaceWithIndex.ts",
"range": {
"start": {
"line": 0,
"character": 10
},
"end": {
"line": 0,
"character": 28
}
}
}
]
},
"locations": [
{
"fileName": "cases/interfaceWithIndex.ts",
"range": {
"start": {
"line": 0,
"character": 10
},
"end": {
"line": 0,
"character": 28
}
}
}
],
"children": [
{
"kindText": "index",
"kind": "index_info",
"symbol": {
"name": "index",
"isArgument": true,
"locations": [
{
"fileName": "/Users/maxstoumen/Projects/ts-type-explorer/tests/cases/interfaceWithIndex.ts",
"range": {
"start": {
"line": 1,
"character": 3
},
"end": {
"line": 1,
"character": 8
}
}
}
]
},
"children": [
{
"kindText": "string",
"kind": "primitive",
"primitiveKind": "string",
"purpose": "index_parameter_type",
"children": [],
"_id": "1"
},
{
"reference": "1"
},
{
"reference": "1"
}
]
},
{
"kindText": "\"asd\"",
"kind": "string_literal",
"symbol": {
"name": "a",
"insideClassOrInterface": true,
"property": true,
"locations": [
{
"fileName": "cases/interfaceWithIndex.ts",
"range": {
"start": {
"line": 2,
"character": 2
},
"end": {
"line": 2,
"character": 3
}
}
}
]
},
"locations": [
{
"fileName": "cases/interfaceWithIndex.ts",
"range": {
"start": {
"line": 2,
"character": 2
},
"end": {
"line": 2,
"character": 3
}
}
}
],
"children": [],
"_id": "2"
}
],
"_id": "0"
}
> [index: string]: string;
a: "asd"
> [index: string]: string;
> index: string
> index: string
> index --- {
"kindText": "string",
"kind": "primitive",
"primitiveKind": "string",
"symbol": {
"name": "index",
"isArgument": true,
"locations": [
{
"fileName": "cases/interfaceWithIndex.ts",
"range": {
"start": {
"line": 1,
"character": 3
},
"end": {
"line": 1,
"character": 8
}
}
}
]
},
"locations": [
{
"fileName": "cases/interfaceWithIndex.ts",
"range": {
"start": {
"line": 1,
"character": 3
},
"end": {
"line": 1,
"character": 8
}
}
}
],
"children": [],
"_id": "0"
}
> a: "asd"
> a --- {
"kindText": "\"asd\"",
"kind": "string_literal",
"symbol": {
"name": "a",
"insideClassOrInterface": true,
"property": true,
"locations": [
{
"fileName": "cases/interfaceWithIndex.ts",
"range": {
"start": {
"line": 2,
"character": 2
},
"end": {
"line": 2,
"character": 3
}
}
}
]
},
"locations": [
{
"fileName": "cases/interfaceWithIndex.ts",
"range": {
"start": {
"line": 2,
"character": 2
},
"end": {
"line": 2,
"character": 3
}
}
}
],
"children": [],
"_id": "0"
}
Loading

0 comments on commit 0f99fe8

Please sign in to comment.