From e8326e600dc1dc8f7f60b09a182933a242b2d1ed Mon Sep 17 00:00:00 2001 From: Alan Date: Fri, 9 Mar 2018 15:45:11 +0100 Subject: [PATCH] fix: overloading a function doesn't generate all of the signatures (#22569) PR Close #22569 --- tools/public_api_guard/common/common.d.ts | 2 +- tools/public_api_guard/core/core.d.ts | 1 + tools/public_api_guard/core/testing.d.ts | 1 + tools/public_api_guard/router/testing.d.ts | 1 + tools/ts-api-guardian/lib/serializer.ts | 12 ++++++++++-- tools/ts-api-guardian/test/unit_test.ts | 18 ++++++++++++++++++ 6 files changed, 32 insertions(+), 3 deletions(-) diff --git a/tools/public_api_guard/common/common.d.ts b/tools/public_api_guard/common/common.d.ts index 9d07132c20..cebf029d7b 100644 --- a/tools/public_api_guard/common/common.d.ts +++ b/tools/public_api_guard/common/common.d.ts @@ -303,7 +303,7 @@ export declare class NgLocaleLocalization extends NgLocalization { /** @deprecated */ protected deprecatedPluralFn: ((locale: string, value: string | number) => Plural) | null | undefined; protected locale: string; constructor(locale: string, - deprecatedPluralFn?: ((locale: string, value: string | number) => Plural) | null | undefined); + /** @deprecated */ deprecatedPluralFn?: ((locale: string, value: string | number) => Plural) | null | undefined); getPluralCategory(value: any, locale?: string): string; } diff --git a/tools/public_api_guard/core/core.d.ts b/tools/public_api_guard/core/core.d.ts index 835512649c..83d605e4ec 100644 --- a/tools/public_api_guard/core/core.d.ts +++ b/tools/public_api_guard/core/core.d.ts @@ -449,6 +449,7 @@ export declare const HostListener: HostListenerDecorator; /** @experimental */ export declare function inject(token: Type | InjectionToken, notFoundValue?: undefined, flags?: InjectFlags): T; +export declare function inject(token: Type | InjectionToken, notFoundValue: T | null, flags?: InjectFlags): T | null; /** @stable */ export declare const Inject: InjectDecorator; diff --git a/tools/public_api_guard/core/testing.d.ts b/tools/public_api_guard/core/testing.d.ts index d4c5452bad..b5fcd428c9 100644 --- a/tools/public_api_guard/core/testing.d.ts +++ b/tools/public_api_guard/core/testing.d.ts @@ -156,3 +156,4 @@ export declare function withBody(html: string, blockFn: T): T; /** @experimental */ export declare function withModule(moduleDef: TestModuleMetadata): InjectSetupWrapper; +export declare function withModule(moduleDef: TestModuleMetadata, fn: Function): () => any; diff --git a/tools/public_api_guard/router/testing.d.ts b/tools/public_api_guard/router/testing.d.ts index d14731865e..e7877cdc2d 100644 --- a/tools/public_api_guard/router/testing.d.ts +++ b/tools/public_api_guard/router/testing.d.ts @@ -5,6 +5,7 @@ export declare class RouterTestingModule { /** @stable */ export declare function setupTestingRouter(urlSerializer: UrlSerializer, contexts: ChildrenOutletContexts, location: Location, loader: NgModuleFactoryLoader, compiler: Compiler, injector: Injector, routes: Route[][], opts?: ExtraOptions, urlHandlingStrategy?: UrlHandlingStrategy): Router; +export declare function setupTestingRouter(urlSerializer: UrlSerializer, contexts: ChildrenOutletContexts, location: Location, loader: NgModuleFactoryLoader, compiler: Compiler, injector: Injector, routes: Route[][], urlHandlingStrategy?: UrlHandlingStrategy): Router; /** @stable */ export declare class SpyNgModuleFactoryLoader implements NgModuleFactoryLoader { diff --git a/tools/ts-api-guardian/lib/serializer.ts b/tools/ts-api-guardian/lib/serializer.ts index 2f8be4ef0e..222568b118 100644 --- a/tools/ts-api-guardian/lib/serializer.ts +++ b/tools/ts-api-guardian/lib/serializer.ts @@ -215,7 +215,15 @@ class ResolvedDeclarationEmitter { } } - let children = node.getChildren(); + let children: ts.Node[] = []; + if (ts.isFunctionDeclaration(node)) { + // Used ts.isFunctionDeclaration instead of node.kind because this is a type guard + const symbol = this.typeChecker.getSymbolAtLocation(node.name); + symbol.declarations.forEach(x => children = children.concat(x.getChildren())); + } else { + children = node.getChildren(); + } + const sourceText = node.getSourceFile().text; if (children.length) { // Sort declarations under a class or an interface @@ -252,7 +260,7 @@ class ResolvedDeclarationEmitter { .join(''); // Print stability annotation for fields - if (node.kind in memberDeclarationOrder) { + if (ts.isParameter(node) || node.kind in memberDeclarationOrder) { const trivia = sourceText.substr(node.pos, node.getLeadingTriviaWidth()); const match = stabilityAnnotationPattern.exec(trivia); if (match) { diff --git a/tools/ts-api-guardian/test/unit_test.ts b/tools/ts-api-guardian/test/unit_test.ts index 2cfa89c0a2..b968eda329 100644 --- a/tools/ts-api-guardian/test/unit_test.ts +++ b/tools/ts-api-guardian/test/unit_test.ts @@ -59,6 +59,24 @@ describe('unit test', () => { check({'file.d.ts': input}, expected); }); + it('should support overloads functions', () => { + const input = ` + export declare function group(steps: AnimationMetadata[], options?: AnimationOptions | null): AnimationGroupMetadata; + + export declare function registerLocaleData(data: any, extraData?: any): void; + export declare function registerLocaleData(data: any, localeId?: string, extraData?: any): void; + `; + + const expected = ` + export declare function group(steps: AnimationMetadata[], options?: AnimationOptions | null): AnimationGroupMetadata; + + export declare function registerLocaleData(data: any, extraData?: any): void; + export declare function registerLocaleData(data: any, localeId?: string, extraData?: any): void; + `; + + check({'file.d.ts': input}, expected); + }); + it('should ignore private props', () => { const input = ` export declare class A {