From 7c161e167907cea92f25df192e0dde789e0cfd43 Mon Sep 17 00:00:00 2001 From: JoostK Date: Sun, 8 Nov 2020 16:13:42 +0100 Subject: [PATCH] refactor(compiler-cli): define type alias for the required delegation pattern (#39604) The type alias allows for this pattern to be more easily used in other areas of the compiler code. The current usages of this pattern have been updated to use the type alias. PR Close #39604 --- packages/compiler-cli/src/ngtsc/core/src/host.ts | 15 +++------------ .../compiler-cli/src/ngtsc/typecheck/src/host.ts | 12 ++---------- .../compiler-cli/src/ngtsc/util/src/typescript.ts | 8 ++++++++ 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/packages/compiler-cli/src/ngtsc/core/src/host.ts b/packages/compiler-cli/src/ngtsc/core/src/host.ts index b7f49060e6..24683b131b 100644 --- a/packages/compiler-cli/src/ngtsc/core/src/host.ts +++ b/packages/compiler-cli/src/ngtsc/core/src/host.ts @@ -15,7 +15,7 @@ import {FactoryGenerator, isShim, ShimAdapter, ShimReferenceTagger, SummaryGener import {FactoryTracker, PerFileShimGenerator, TopLevelShimGenerator} from '../../shims/api'; import {TypeCheckShimGenerator} from '../../typecheck'; import {normalizeSeparators} from '../../util/src/path'; -import {getRootDirs, isDtsPath, isNonDeclarationTsPath} from '../../util/src/typescript'; +import {getRootDirs, isNonDeclarationTsPath, RequiredDelegations} from '../../util/src/typescript'; import {ExtendedTsCompilerHost, NgCompilerAdapter, NgCompilerOptions, UnifiedModulesHost} from '../api'; // A persistent source of bugs in CompilerHost delegation has been the addition by TS of new, @@ -23,15 +23,6 @@ import {ExtendedTsCompilerHost, NgCompilerAdapter, NgCompilerOptions, UnifiedMod // the delegating host doesn't implement or delegate them. This causes subtle runtime failures. No // more. This infrastructure ensures that failing to delegate a method is a compile-time error. -/** - * Represents the `ExtendedTsCompilerHost` interface, with a transformation applied that turns all - * methods (even optional ones) into required fields (which may be `undefined`, if the method was - * optional). - */ -export type RequiredCompilerHostDelegations = { - [M in keyof Required]: ExtendedTsCompilerHost[M]; -}; - /** * Delegates all methods of `ExtendedTsCompilerHost` to a delegate, with the exception of * `getSourceFile` and `fileExists` which are implemented in `NgCompilerHost`. @@ -40,7 +31,7 @@ export type RequiredCompilerHostDelegations = { * generated for this class. */ export class DelegatingCompilerHost implements - Omit { + Omit, 'getSourceFile'|'fileExists'> { constructor(protected delegate: ExtendedTsCompilerHost) {} private delegateMethod(name: M): @@ -89,7 +80,7 @@ export class DelegatingCompilerHost implements * `ExtendedTsCompilerHost` methods whenever present. */ export class NgCompilerHost extends DelegatingCompilerHost implements - RequiredCompilerHostDelegations, ExtendedTsCompilerHost, NgCompilerAdapter { + RequiredDelegations, ExtendedTsCompilerHost, NgCompilerAdapter { readonly factoryTracker: FactoryTracker|null = null; readonly entryPoint: AbsoluteFsPath|null = null; readonly constructionDiagnostics: ts.Diagnostic[]; diff --git a/packages/compiler-cli/src/ngtsc/typecheck/src/host.ts b/packages/compiler-cli/src/ngtsc/typecheck/src/host.ts index d7283890c1..41b6d9dfdc 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/src/host.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/src/host.ts @@ -9,15 +9,7 @@ import * as ts from 'typescript'; import {copyFileShimData, ShimReferenceTagger} from '../../shims'; - -/** - * Represents the `ts.CompilerHost` interface, with a transformation applied that turns all - * methods (even optional ones) into required fields (which may be `undefined`, if the method was - * optional). - */ -export type RequiredCompilerHostDelegations = { - [M in keyof Required]: ts.CompilerHost[M]; -}; +import {RequiredDelegations} from '../../util/src/typescript'; /** * Delegates all methods of `ts.CompilerHost` to a delegate, with the exception of @@ -27,7 +19,7 @@ export type RequiredCompilerHostDelegations = { * generated for this class. */ export class DelegatingCompilerHost implements - Omit { + Omit, 'getSourceFile'|'fileExists'|'writeFile'> { constructor(protected delegate: ts.CompilerHost) {} private delegateMethod(name: M): ts.CompilerHost[M] { diff --git a/packages/compiler-cli/src/ngtsc/util/src/typescript.ts b/packages/compiler-cli/src/ngtsc/util/src/typescript.ts index c7bf5eed6b..f8ca1fb619 100644 --- a/packages/compiler-cli/src/ngtsc/util/src/typescript.ts +++ b/packages/compiler-cli/src/ngtsc/util/src/typescript.ts @@ -149,3 +149,11 @@ export function isAssignment(node: ts.Node): node is ts.BinaryExpression { * Asserts that the keys `K` form a subset of the keys of `T`. */ export type SubsetOfKeys = K; + +/** + * Represents the type `T`, with a transformation applied that turns all methods (even optional + * ones) into required fields (which may be `undefined`, if the method was optional). + */ +export type RequiredDelegations = { + [M in keyof Required]: T[M]; +};