Skip to content

Commit

Permalink
feat: mapped type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 18, 2022
1 parent 811b2f8 commit 7688059
Show file tree
Hide file tree
Showing 14 changed files with 770 additions and 652 deletions.
1 change: 1 addition & 0 deletions packages/api/src/localization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export function localizePurpose(purpose: TypePurpose): string {
object_class: "class",
type_parameter_list: "type parameters",
type_argument_list: "type arguments",
index_parameter_type: "parameter",
}

return nameByPurpose[purpose]
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/localizedTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function generateTypeInfoMap(tree: TypeInfo, cache?: TypeInfoMap): TypeInfoMap {
return cache
}

export type TypePurpose = 'return'|'index_type'|'index_value_type'|'conditional_check'|'conditional_extends'|'conditional_true'|'conditional_false'|'keyof'|'indexed_access_index'|'indexed_access_base'|'parameter_default'|'parameter_base_constraint'|'class_constructor'|'class_base_type'|'class_implementations'|'object_class'|'type_parameter_list'|'type_argument_list'|'parameter_value'
export type TypePurpose = 'return'|'index_type'|'index_value_type'|'index_parameter_type'|'conditional_check'|'conditional_extends'|'conditional_true'|'conditional_false'|'keyof'|'indexed_access_index'|'indexed_access_base'|'parameter_default'|'parameter_base_constraint'|'class_constructor'|'class_base_type'|'class_implementations'|'object_class'|'type_parameter_list'|'type_argument_list'|'parameter_value'

type ResolvedTypeInfo = Exclude<TypeInfo, {kind: 'reference'}>
type LocalizedSymbolInfo = { name: string, anonymous?: boolean, insideClassOrInterface?: boolean, property?: boolean, locations?: SourceFileLocation[], isArgument?: boolean }
Expand Down Expand Up @@ -327,6 +327,7 @@ function getChildren(info: ResolvedTypeInfo, { typeArguments: contextualTypeArgu
kind: "index_info",
symbol: wrapSafe(localizeSymbol)(indexInfo.parameterSymbol),
children: [
...indexInfo.parameterType ? [localizeOpts(indexInfo.parameterType, { purpose: 'index_parameter_type' })] : [],
...indexInfo.keyType ? [localizeOpts(indexInfo.keyType, { purpose: 'index_type'})] : [],
...indexInfo.type ? [localizeOpts(indexInfo.type, { purpose: 'index_value_type'})] : [],
]
Expand Down
7 changes: 5 additions & 2 deletions packages/api/src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,19 @@ function _generateTypeTree({ symbol, type, node }: SymbolOrType, ctx: TypeTreeCo
function getIndexInfo(indexInfo: TSIndexInfoMerged): IndexInfo {
const { typeChecker } = ctx

const parameterSymbol =
const parameterSymbol: ts.Symbol =
// @ts-expect-error
indexInfo?.declaration?.parameters?.[0]?.symbol
?? indexInfo?.parameterType?.getSymbol()

const parameterType = indexInfo?.parameterType ?? (parameterSymbol && getSymbolType(typeChecker, parameterSymbol))

return {
...indexInfo.keyType && { keyType: parseType(indexInfo.keyType) },
...indexInfo.type && { type: parseType(indexInfo.type) },
// parameterSymbol: wrapSafe(getSymbolInfo)(wrapSafe(typeChecker.getSymbolAtLocation)(indexInfo?.declaration?.parameters?.[0]))
parameterSymbol: wrapSafe(getSymbolInfo)(parameterSymbol),
...parameterType && { parameterType: parseType(parameterType) },
}
}

Expand Down Expand Up @@ -394,7 +397,7 @@ export function getTypeInfoChildren(info: TypeInfo): TypeInfo[] {
case 'object': {
return [
...info.properties,
...info.indexInfos?.flatMap(x => [ x.type, x.keyType ]) ?? [],
...info.indexInfos?.flatMap(x => [ x.type, x.keyType, x.parameterType ]) ?? [],
// ...wrapSafe(getTypeInfoChildren)(info.objectClass) ?? [],
info.objectClass,
]
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 @@ -28,6 +28,7 @@ export type IndexInfo = {
keyType?: TypeInfo,
type?: TypeInfo,
parameterSymbol?: SymbolInfo
parameterType?: TypeInfo,
// parameterName?: string
}

Expand Down
491 changes: 250 additions & 241 deletions tests/baselines/reference/consoleLog.localized.tree

Large diffs are not rendered by default.

550 changes: 281 additions & 269 deletions tests/baselines/reference/consoleLog.tree

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion tests/baselines/reference/mapped.localized.tree
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ type mapped = { [index: string]: number }
"kindText": "string",
"kind": "primitive",
"primitiveKind": "string",
"purpose": "index_type",
"purpose": "index_parameter_type",
"children": [],
"_id": "2"
},
{
"reference": "2"
},
{
"kindText": "number",
"kind": "primitive",
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/mapped.tree
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type mapped = { [index: string]: number }
}
}
]
},
"parameterType": {
"kind": "reference",
"id": "2"
}
}
],
Expand Down
59 changes: 31 additions & 28 deletions tests/baselines/reference/mappedParam.localized.tree
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,6 @@ type mappedType2 = { [P in 'a'|'b'|'c']: P}
]
},
"children": [
{
"kindText": "union",
"kind": "union",
"purpose": "index_type",
"children": [
{
"kindText": "\"a\"",
"kind": "string_literal",
"children": [],
"_id": "1"
},
{
"kindText": "\"b\"",
"kind": "string_literal",
"children": [],
"_id": "2"
},
{
"kindText": "\"c\"",
"kind": "string_literal",
"children": [],
"_id": "3"
}
],
"_id": "5"
},
{
"kindText": "type parameter",
"kind": "type_parameter",
Expand All @@ -107,7 +81,7 @@ type mappedType2 = { [P in 'a'|'b'|'c']: P}
}
]
},
"purpose": "index_value_type",
"purpose": "index_parameter_type",
"locations": [
{
"fileName": "cases/mappedParam.ts",
Expand All @@ -125,10 +99,39 @@ type mappedType2 = { [P in 'a'|'b'|'c']: P}
],
"children": [
{
"reference": "5"
"kindText": "union",
"kind": "union",
"purpose": "parameter_base_constraint",
"children": [
{
"kindText": "\"a\"",
"kind": "string_literal",
"children": [],
"_id": "1"
},
{
"kindText": "\"b\"",
"kind": "string_literal",
"children": [],
"_id": "2"
},
{
"kindText": "\"c\"",
"kind": "string_literal",
"children": [],
"_id": "3"
}
],
"_id": "5"
}
],
"_id": "4"
},
{
"reference": "5"
},
{
"reference": "4"
}
]
},
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/mappedParam.tree
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,31 @@ type mappedType2 = { [P in 'a'|'b'|'c']: P}
}
}
]
},
"parameterType": {
"kind": "reference",
"symbolMeta": {
"name": "P",
"flags": 262144,
"declarations": [
{
"location": {
"fileName": "cases/mappedParam.ts",
"range": {
"start": {
"line": 0,
"character": 22
},
"end": {
"line": 0,
"character": 38
}
}
}
}
]
},
"id": "4"
}
}
],
Expand Down
139 changes: 71 additions & 68 deletions tests/baselines/reference/partial.localized.tree
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,76 @@ type partialUnion = Partial<{a: string}|{b: string}>
]
},
"children": [
{
"kindText": "type parameter",
"kind": "type_parameter",
"symbol": {
"name": "P",
"locations": [
{
"fileName": "../node_modules/typescript/lib/lib.es5.d.ts",
"range": {
"start": {
"line": 1549,
"character": 5
},
"end": {
"line": 1549,
"character": 17
}
}
}
]
},
"purpose": "index_parameter_type",
"locations": [
{
"fileName": "../node_modules/typescript/lib/lib.es5.d.ts",
"range": {
"start": {
"line": 1549,
"character": 5
},
"end": {
"line": 1549,
"character": 17
}
}
}
],
"children": [
{
"kindText": "union",
"kind": "union",
"purpose": "parameter_base_constraint",
"children": [
{
"kindText": "string",
"kind": "primitive",
"primitiveKind": "string",
"children": [],
"_id": "10"
},
{
"kindText": "number",
"kind": "primitive",
"primitiveKind": "number",
"children": [],
"_id": "11"
},
{
"kindText": "symbol",
"kind": "primitive",
"primitiveKind": "essymbol",
"children": [],
"_id": "12"
}
],
"_id": "9"
}
],
"_id": "8"
},
{
"kindText": "keyof",
"kind": "index",
Expand All @@ -467,74 +537,7 @@ type partialUnion = Partial<{a: string}|{b: string}>
"reference": "1"
},
{
"kindText": "type parameter",
"kind": "type_parameter",
"symbol": {
"name": "P",
"locations": [
{
"fileName": "../node_modules/typescript/lib/lib.es5.d.ts",
"range": {
"start": {
"line": 1549,
"character": 5
},
"end": {
"line": 1549,
"character": 17
}
}
}
]
},
"purpose": "indexed_access_index",
"locations": [
{
"fileName": "../node_modules/typescript/lib/lib.es5.d.ts",
"range": {
"start": {
"line": 1549,
"character": 5
},
"end": {
"line": 1549,
"character": 17
}
}
}
],
"children": [
{
"kindText": "union",
"kind": "union",
"purpose": "parameter_base_constraint",
"children": [
{
"kindText": "string",
"kind": "primitive",
"primitiveKind": "string",
"children": [],
"_id": "10"
},
{
"kindText": "number",
"kind": "primitive",
"primitiveKind": "number",
"children": [],
"_id": "11"
},
{
"kindText": "symbol",
"kind": "primitive",
"primitiveKind": "essymbol",
"children": [],
"_id": "12"
}
],
"_id": "9"
}
],
"_id": "8"
"reference": "8"
}
],
"_id": "7"
Expand Down
25 changes: 25 additions & 0 deletions tests/baselines/reference/partial.tree
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,31 @@ type partialUnion = Partial<{a: string}|{b: string}>
}
}
]
},
"parameterType": {
"kind": "reference",
"symbolMeta": {
"name": "P",
"flags": 262144,
"declarations": [
{
"location": {
"fileName": "../node_modules/typescript/lib/lib.es5.d.ts",
"range": {
"start": {
"line": 1549,
"character": 5
},
"end": {
"line": 1549,
"character": 17
}
}
}
}
]
},
"id": "8"
}
}
],
Expand Down
Loading

0 comments on commit 7688059

Please sign in to comment.