Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts-api-guardian: overloading a function doesn't generate all of the signatures #22569

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/public_api_guard/common/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/core/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ export declare const HostListener: HostListenerDecorator;

/** @experimental */
export declare function inject<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: undefined, flags?: InjectFlags): T;
export declare function inject<T>(token: Type<T> | InjectionToken<T>, notFoundValue: T | null, flags?: InjectFlags): T | null;

/** @stable */
export declare const Inject: InjectDecorator;
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/core/testing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,4 @@ export declare function withBody<T>(html: string, blockFn: T): T;

/** @experimental */
export declare function withModule(moduleDef: TestModuleMetadata): InjectSetupWrapper;
export declare function withModule(moduleDef: TestModuleMetadata, fn: Function): () => any;
1 change: 1 addition & 0 deletions tools/public_api_guard/router/testing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions tools/ts-api-guardian/lib/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,15 @@ class ResolvedDeclarationEmitter {
}
}

let children = node.getChildren();
let children: ts.Node[] = [];
if (ts.isFunctionDeclaration(node)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used ts.isFunctionDeclaration instead of node.kind because this is a type guard.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put this kind of thing as a comment in the source code - many people will read that later, no one will see this thread

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// 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
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions tools/ts-api-guardian/test/unit_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down