|
1 | | -import { MethodDefinition, TSTypeLiteral, Identifier, TSIndexSignature } from '@typescript-eslint/types/dist/ts-estree'; |
| 1 | +import { MethodDefinition, TSTypeLiteral, Identifier, TSIndexSignature, |
| 2 | + TSTypeAnnotation, TypeNode, TSTypeReference } from '@typescript-eslint/types/dist/ts-estree'; |
2 | 3 | import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree'; |
3 | 4 | import { ClassReference, InterfaceLoaded } from '../../lib/parse/ClassIndex'; |
4 | 5 | import { ClassLoader } from '../../lib/parse/ClassLoader'; |
@@ -962,6 +963,39 @@ export interface A{ |
962 | 963 | expect(() => parameterLoader.getFieldRange(field, {})) |
963 | 964 | .toThrow(new Error('Found untyped generic field type at field fieldA in A at file')); |
964 | 965 | }); |
| 966 | + |
| 967 | + it('should get the range of a Record', async() => { |
| 968 | + expect(await getFieldRange('fieldA: Record<string, number>', {})) |
| 969 | + .toMatchObject({ |
| 970 | + type: 'hash', |
| 971 | + value: { |
| 972 | + members: [ |
| 973 | + { |
| 974 | + parameters: [ |
| 975 | + { |
| 976 | + name: 'key', |
| 977 | + type: 'Identifier', |
| 978 | + typeAnnotation: { |
| 979 | + type: 'TSTypeAnnotation', |
| 980 | + typeAnnotation: { |
| 981 | + type: 'TSStringKeyword', |
| 982 | + }, |
| 983 | + }, |
| 984 | + }, |
| 985 | + ], |
| 986 | + type: 'TSIndexSignature', |
| 987 | + typeAnnotation: { |
| 988 | + type: 'TSTypeAnnotation', |
| 989 | + typeAnnotation: { |
| 990 | + type: 'TSNumberKeyword', |
| 991 | + }, |
| 992 | + }, |
| 993 | + }, |
| 994 | + ], |
| 995 | + type: 'TSTypeLiteral', |
| 996 | + }, |
| 997 | + }); |
| 998 | + }); |
965 | 999 | }); |
966 | 1000 |
|
967 | 1001 | describe('getFieldDefault', () => { |
@@ -1077,4 +1111,58 @@ export interface A{ |
1077 | 1111 | .rejects.toThrow(new Error('Missing field type on an index signature in A at file')); |
1078 | 1112 | }); |
1079 | 1113 | }); |
| 1114 | + |
| 1115 | + describe('handleTypeOverride', () => { |
| 1116 | + const clazz: ClassReference = { localName: 'A', fileName: 'file' }; |
| 1117 | + |
| 1118 | + async function handleTypeOverride(type: string): Promise<ParameterRangeUnresolved | undefined> { |
| 1119 | + resolutionContext.contentsOverrides = { |
| 1120 | + 'file.d.ts': `export class A{ |
| 1121 | + constructor(a: ${type}) {} |
| 1122 | +}`, |
| 1123 | + }; |
| 1124 | + const classLoaded = await classLoader.loadClassDeclaration(clazz, false); |
| 1125 | + const field: Identifier = <any> (<MethodDefinition> constructorLoader.getConstructor(classLoaded)) |
| 1126 | + .value.params[0]; |
| 1127 | + const parameterLoader = new ParameterLoader({ classLoaded }); |
| 1128 | + const typeNode: TypeNode = (<TSTypeAnnotation> field.typeAnnotation).typeAnnotation; |
| 1129 | + return parameterLoader.handleTypeOverride(<TSTypeReference> typeNode); |
| 1130 | + } |
| 1131 | + |
| 1132 | + it('should do nothing on an unsupported type', async() => { |
| 1133 | + expect(await handleTypeOverride('String')).toBeUndefined(); |
| 1134 | + }); |
| 1135 | + |
| 1136 | + it('handle a Record type alias', async() => { |
| 1137 | + expect(await handleTypeOverride('Record<string, number>')).toMatchObject({ |
| 1138 | + type: 'hash', |
| 1139 | + value: { |
| 1140 | + members: [ |
| 1141 | + { |
| 1142 | + parameters: [ |
| 1143 | + { |
| 1144 | + name: 'key', |
| 1145 | + type: 'Identifier', |
| 1146 | + typeAnnotation: { |
| 1147 | + type: 'TSTypeAnnotation', |
| 1148 | + typeAnnotation: { |
| 1149 | + type: 'TSStringKeyword', |
| 1150 | + }, |
| 1151 | + }, |
| 1152 | + }, |
| 1153 | + ], |
| 1154 | + type: 'TSIndexSignature', |
| 1155 | + typeAnnotation: { |
| 1156 | + type: 'TSTypeAnnotation', |
| 1157 | + typeAnnotation: { |
| 1158 | + type: 'TSNumberKeyword', |
| 1159 | + }, |
| 1160 | + }, |
| 1161 | + }, |
| 1162 | + ], |
| 1163 | + type: 'TSTypeLiteral', |
| 1164 | + }, |
| 1165 | + }); |
| 1166 | + }); |
| 1167 | + }); |
1080 | 1168 | }); |
0 commit comments