refactor(ngcc): simplify the `detectKnownDeclaration()` signature (#36989)

A number of overloads were added to `detectKnownDeclaration()` to
allow it to support `null` being passed through. In practice this could
easily be avoided, which allows the overloads to be removed and the
method signature and implementations to be simplified.

PR Close #36989
This commit is contained in:
Pete Bacon Darwin 2020-05-12 08:19:59 +01:00 committed by Kara Erickson
parent 13ba84731f
commit 491da99abe
4 changed files with 15 additions and 19 deletions

View File

@ -32,7 +32,8 @@ export class DelegatingReflectionHost implements NgccReflectionHost {
getDeclarationOfIdentifier(id: ts.Identifier): Declaration|null { getDeclarationOfIdentifier(id: ts.Identifier): Declaration|null {
if (isFromDtsFile(id)) { if (isFromDtsFile(id)) {
return this.detectKnownDeclaration(this.tsHost.getDeclarationOfIdentifier(id)); const declaration = this.tsHost.getDeclarationOfIdentifier(id);
return declaration !== null ? this.detectKnownDeclaration(declaration) : null;
} }
return this.ngccHost.getDeclarationOfIdentifier(id); return this.ngccHost.getDeclarationOfIdentifier(id);
} }
@ -157,10 +158,7 @@ export class DelegatingReflectionHost implements NgccReflectionHost {
return this.ngccHost.getEndOfClass(classSymbol); return this.ngccHost.getEndOfClass(classSymbol);
} }
detectKnownDeclaration(decl: null): null; detectKnownDeclaration<T extends Declaration>(decl: T): T {
detectKnownDeclaration<T extends Declaration>(decl: T): T;
detectKnownDeclaration<T extends Declaration>(decl: T|null): T|null;
detectKnownDeclaration<T extends Declaration>(decl: T|null): T|null {
return this.ngccHost.detectKnownDeclaration(decl); return this.ngccHost.detectKnownDeclaration(decl);
} }
} }

View File

@ -604,16 +604,12 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
* @param decl The `Declaration` to check. * @param decl The `Declaration` to check.
* @return The passed in `Declaration` (potentially enhanced with a `KnownDeclaration`). * @return The passed in `Declaration` (potentially enhanced with a `KnownDeclaration`).
*/ */
detectKnownDeclaration(decl: null): null; detectKnownDeclaration<T extends Declaration>(decl: T): T {
detectKnownDeclaration<T extends Declaration>(decl: T): T; if (decl.known === null && this.isJavaScriptObjectDeclaration(decl)) {
detectKnownDeclaration<T extends Declaration>(decl: T|null): T|null;
detectKnownDeclaration<T extends Declaration>(decl: T|null): T|null {
if (decl !== null && decl.known === null && this.isJavaScriptObjectDeclaration(decl)) {
// If the identifier resolves to the global JavaScript `Object`, update the declaration to // If the identifier resolves to the global JavaScript `Object`, update the declaration to
// denote it as the known `JsGlobalObject` declaration. // denote it as the known `JsGlobalObject` declaration.
decl.known = KnownDeclaration.JsGlobalObject; decl.known = KnownDeclaration.JsGlobalObject;
} }
return decl; return decl;
} }
@ -626,7 +622,11 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
*/ */
protected getDeclarationOfSymbol(symbol: ts.Symbol, originalId: ts.Identifier|null): Declaration protected getDeclarationOfSymbol(symbol: ts.Symbol, originalId: ts.Identifier|null): Declaration
|null { |null {
return this.detectKnownDeclaration(super.getDeclarationOfSymbol(symbol, originalId)); const declaration = super.getDeclarationOfSymbol(symbol, originalId);
if (declaration === null) {
return null;
}
return this.detectKnownDeclaration(declaration);
} }
/** /**

View File

@ -283,13 +283,11 @@ export class Esm5ReflectionHost extends Esm2015ReflectionHost {
* @param decl The `Declaration` to check. * @param decl The `Declaration` to check.
* @return The passed in `Declaration` (potentially enhanced with a `KnownDeclaration`). * @return The passed in `Declaration` (potentially enhanced with a `KnownDeclaration`).
*/ */
detectKnownDeclaration(decl: null): null; detectKnownDeclaration<T extends Declaration>(decl: T): T {
detectKnownDeclaration<T extends Declaration>(decl: T): T;
detectKnownDeclaration<T extends Declaration>(decl: T|null): T|null;
detectKnownDeclaration<T extends Declaration>(decl: T|null): T|null {
decl = super.detectKnownDeclaration(decl); decl = super.detectKnownDeclaration(decl);
if (decl !== null && decl.known === null && decl.node !== null) { // Also check for TS helpers
if (decl.known === null && decl.node !== null) {
decl.known = getTsHelperFnFromDeclaration(decl.node); decl.known = getTsHelperFnFromDeclaration(decl.node);
} }

View File

@ -100,8 +100,8 @@ export interface NgccReflectionHost extends ReflectionHost {
* Check whether a `Declaration` corresponds with a known declaration and set its `known` property * Check whether a `Declaration` corresponds with a known declaration and set its `known` property
* to the appropriate `KnownDeclaration`. * to the appropriate `KnownDeclaration`.
* *
* @param decl The `Declaration` to check or `null` if there is no declaration. * @param decl The `Declaration` to check.
* @return The passed in `Declaration` (potentially enhanced with a `KnownDeclaration`). * @return The passed in `Declaration` (potentially enhanced with a `KnownDeclaration`).
*/ */
detectKnownDeclaration<T extends Declaration>(decl: T|null): T|null; detectKnownDeclaration<T extends Declaration>(decl: T): T;
} }