[Telemetry Tools] Merge array of constraints (#79654)

This commit is contained in:
Alejandro Fernández Haro 2020-10-06 16:41:40 +01:00 committed by GitHub
parent 6b80eb20ab
commit ed825398c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View file

@ -148,6 +148,17 @@ describe('getDescriptor', () => {
});
});
it('serializes RecordWithKnownAllProps', () => {
const usageInterface = usageInterfaces.get('RecordWithKnownAllProps')!;
const descriptor = getDescriptor(usageInterface, tsProgram);
expect(descriptor).toEqual({
prop1: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' },
prop2: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' },
prop3: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' },
prop4: { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' },
});
});
it('serializes IndexedAccessType', () => {
const usageInterface = usageInterfaces.get('IndexedAccessType')!;
const descriptor = getDescriptor(usageInterface, tsProgram);

View file

@ -88,7 +88,11 @@ export function getConstraints(node: ts.Node, program: ts.Program): any {
if (ts.isUnionTypeNode(node)) {
const types = node.types.filter(discardNullOrUndefined);
return types.map((typeNode) => getConstraints(typeNode, program));
return types.reduce<any>((acc, typeNode) => {
const constraints = getConstraints(typeNode, program);
const contraintsArray = Array.isArray(constraints) ? constraints : [constraints];
return [...acc, ...contraintsArray];
}, []);
}
if (ts.isLiteralTypeNode(node) && ts.isLiteralExpression(node.literal)) {

View file

@ -58,6 +58,10 @@ export type TypeAliasWithRecord = Usage & Record<string, number>;
export type MappedTypeProps = 'prop1' | 'prop2';
export type MappedTypeExtraProps = 'prop3' | 'prop4';
export type MappedTypeAllProps = MappedTypeProps | MappedTypeExtraProps;
export interface MappedTypes {
mappedTypeWithExternallyDefinedProps: {
[key in MappedTypeProps]: number;
@ -68,5 +72,6 @@ export interface MappedTypes {
}
export type RecordWithKnownProps = Record<MappedTypeProps, number>;
export type RecordWithKnownAllProps = Record<MappedTypeAllProps, number>;
export type IndexedAccessType = Pick<WithUnion, 'prop1' | 'prop2'>;