feat(tsc-wrapped): record arity of generic classes (#14104)

This commit is contained in:
Chuck Jazdzewski 2017-01-25 13:40:55 -08:00 committed by Miško Hevery
parent 7670cc1a86
commit 1079b9381c
3 changed files with 37 additions and 1 deletions

View File

@ -128,6 +128,12 @@ export class MetadataCollector {
}); });
} }
// Add arity if the type is generic
const typeParameters = classDeclaration.typeParameters;
if (typeParameters && typeParameters.length) {
result.arity = typeParameters.length;
}
// Add class decorators // Add class decorators
if (classDeclaration.decorators) { if (classDeclaration.decorators) {
result.decorators = getDecorators(classDeclaration.decorators); result.decorators = getDecorators(classDeclaration.decorators);

View File

@ -37,6 +37,7 @@ export interface ModuleExportMetadata {
export interface ClassMetadata { export interface ClassMetadata {
__symbolic: 'class'; __symbolic: 'class';
extends?: MetadataSymbolicExpression|MetadataError; extends?: MetadataSymbolicExpression|MetadataError;
arity?: number;
decorators?: (MetadataSymbolicExpression|MetadataError)[]; decorators?: (MetadataSymbolicExpression|MetadataError)[];
members?: MetadataMap; members?: MetadataMap;
statics?: {[name: string]: MetadataValue | FunctionMetadata}; statics?: {[name: string]: MetadataValue | FunctionMetadata};

View File

@ -9,7 +9,7 @@
import * as ts from 'typescript'; import * as ts from 'typescript';
import {MetadataCollector} from '../src/collector'; import {MetadataCollector} from '../src/collector';
import {ClassMetadata, ConstructorMetadata, ModuleMetadata} from '../src/schema'; import {ClassMetadata, ConstructorMetadata, MetadataEntry, ModuleMetadata, isClassMetadata} from '../src/schema';
import {Directory, Host, expectValidSources} from './typescript.mocks'; import {Directory, Host, expectValidSources} from './typescript.mocks';
@ -28,6 +28,7 @@ describe('Collector', () => {
'/promise.ts', '/promise.ts',
'/unsupported-1.ts', '/unsupported-1.ts',
'/unsupported-2.ts', '/unsupported-2.ts',
'class-arity.ts',
'import-star.ts', 'import-star.ts',
'exported-classes.ts', 'exported-classes.ts',
'exported-functions.ts', 'exported-functions.ts',
@ -666,6 +667,27 @@ describe('Collector', () => {
} }
}); });
}); });
function expectClass(entry: MetadataEntry): entry is ClassMetadata {
const result = isClassMetadata(entry);
expect(result).toBeTruthy();
return result;
}
it('should collect the correct arity for a class', () => {
const metadata = collector.getMetadata(program.getSourceFile('/class-arity.ts'));
const zero = metadata.metadata['Zero'];
if (expectClass(zero)) expect(zero.arity).toBeUndefined();
const one = metadata.metadata['One'];
if (expectClass(one)) expect(one.arity).toBe(1);
const two = metadata.metadata['Two'];
if (expectClass(two)) expect(two.arity).toBe(2);
const three = metadata.metadata['Three'];
if (expectClass(three)) expect(three.arity).toBe(3);
const nine = metadata.metadata['Nine'];
if (expectClass(nine)) expect(nine.arity).toBe(9);
});
}); });
function override(fileName: string, content: string) { function override(fileName: string, content: string) {
@ -867,6 +889,13 @@ const FILES: Directory = {
declare var Promise: PromiseConstructor; declare var Promise: PromiseConstructor;
`, `,
'class-arity.ts': `
export class Zero {}
export class One<T> {}
export class Two<T, V> {}
export class Three<T1, T2, T3> {}
export class Nine<T1, T2, T3, T4, T5, T6, T7, T8, T9> {}
`,
'unsupported-1.ts': ` 'unsupported-1.ts': `
export let {a, b} = {a: 1, b: 2}; export let {a, b} = {a: 1, b: 2};
export let [c, d] = [1, 2]; export let [c, d] = [1, 2];