From 29e2bc7d919f2efe533f60459f6ed1263c34e0cf Mon Sep 17 00:00:00 2001 From: Daniel Trevino <23410540+danieltre23@users.noreply.github.com> Date: Tue, 27 Jul 2021 16:12:25 +0000 Subject: [PATCH] refactor(compiler-cli): move typecheck test utils into its own package (#42984) This commit moves the test utils used in the typechecking tests into its own package. This makes them available to be used in the tests of a different package. Refs #42966 PR Close #42984 --- .../src/ngtsc/typecheck/test/BUILD.bazel | 1 + .../ngtsc/typecheck/test/diagnostics_spec.ts | 2 +- .../src/ngtsc/typecheck/test/program_spec.ts | 2 +- .../typecheck/test/span_comments_spec.ts | 2 +- .../typecheck/test/type_check_block_spec.ts | 2 +- .../test/type_checker__completion_spec.ts | 2 +- ...ecker__get_symbol_of_template_node_spec.ts | 2 +- .../ngtsc/typecheck/test/type_checker_spec.ts | 2 +- .../typecheck/test/type_constructor_spec.ts | 2 +- .../test/type_parameter_emitter_spec.ts | 2 +- .../src/ngtsc/typecheck/testing/BUILD.bazel | 28 +++ .../{test/test_utils.ts => testing/index.ts} | 174 +++++++++--------- 12 files changed, 125 insertions(+), 96 deletions(-) create mode 100644 packages/compiler-cli/src/ngtsc/typecheck/testing/BUILD.bazel rename packages/compiler-cli/src/ngtsc/typecheck/{test/test_utils.ts => testing/index.ts} (88%) diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/BUILD.bazel b/packages/compiler-cli/src/ngtsc/typecheck/test/BUILD.bazel index f84d17a25d..7e3036f547 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/BUILD.bazel +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/BUILD.bazel @@ -26,6 +26,7 @@ ts_library( "//packages/compiler-cli/src/ngtsc/typecheck", "//packages/compiler-cli/src/ngtsc/typecheck/api", "//packages/compiler-cli/src/ngtsc/typecheck/diagnostics", + "//packages/compiler-cli/src/ngtsc/typecheck/testing", "//packages/compiler-cli/src/ngtsc/util", "@npm//typescript", ], diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/diagnostics_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/diagnostics_spec.ts index 42914425a5..69906794b2 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/diagnostics_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/diagnostics_spec.ts @@ -12,7 +12,7 @@ import {absoluteFrom, getSourceFileOrError} from '../../file_system'; import {runInEachFileSystem, TestFile} from '../../file_system/testing'; import {OptimizeFor, TypeCheckingConfig} from '../api'; -import {ngForDeclaration, ngForDts, setup, TestDeclaration} from './test_utils'; +import {ngForDeclaration, ngForDts, setup, TestDeclaration} from '../testing'; runInEachFileSystem(() => { describe('template diagnostics', () => { diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/program_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/program_spec.ts index 39027c315a..263a9a3b31 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/program_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/program_spec.ts @@ -15,7 +15,7 @@ import {sfExtensionData, ShimReferenceTagger} from '../../shims'; import {expectCompleteReuse, makeProgram} from '../../testing'; import {OptimizeFor} from '../api'; -import {setup} from './test_utils'; +import {setup} from '../testing'; runInEachFileSystem(() => { describe('template type-checking program', () => { diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/span_comments_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/span_comments_spec.ts index 3507d8952c..13d31be748 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/span_comments_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/span_comments_spec.ts @@ -7,7 +7,7 @@ */ import {initMockFileSystem} from '../../file_system/testing'; -import {tcb, TestDeclaration} from './test_utils'; +import {tcb, TestDeclaration} from '../testing'; describe('type check blocks diagnostics', () => { beforeEach(() => initMockFileSystem('Native')); diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts index d55df81b3f..e7e9be1df9 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/type_check_block_spec.ts @@ -9,7 +9,7 @@ import {initMockFileSystem} from '../../file_system/testing'; import {TypeCheckingConfig} from '../api'; -import {ALL_ENABLED_CONFIG, tcb, TestDeclaration, TestDirective} from './test_utils'; +import {ALL_ENABLED_CONFIG, tcb, TestDeclaration, TestDirective} from '../testing'; describe('type check blocks', () => { diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker__completion_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker__completion_spec.ts index d86e0bc8ac..cf0e523533 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker__completion_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker__completion_spec.ts @@ -14,7 +14,7 @@ import {runInEachFileSystem} from '../../file_system/testing'; import {getTokenAtPosition} from '../../util/src/typescript'; import {CompletionKind, GlobalCompletion, TemplateTypeChecker, TypeCheckingConfig} from '../api'; -import {getClass, setup, TypeCheckingTarget} from './test_utils'; +import {getClass, setup, TypeCheckingTarget} from '../testing'; runInEachFileSystem(() => { describe('TemplateTypeChecker.getGlobalCompletions()', () => { diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker__get_symbol_of_template_node_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker__get_symbol_of_template_node_spec.ts index d510ecf5b0..86760f818e 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker__get_symbol_of_template_node_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker__get_symbol_of_template_node_spec.ts @@ -15,7 +15,7 @@ import {runInEachFileSystem} from '../../file_system/testing'; import {ClassDeclaration} from '../../reflection'; import {DirectiveSymbol, DomBindingSymbol, ElementSymbol, ExpressionSymbol, InputBindingSymbol, OutputBindingSymbol, PipeSymbol, ReferenceSymbol, Symbol, SymbolKind, TemplateSymbol, TemplateTypeChecker, TypeCheckingConfig, VariableSymbol} from '../api'; -import {getClass, ngForDeclaration, ngForTypeCheckTarget, setup as baseTestSetup, TypeCheckingTarget} from './test_utils'; +import {getClass, ngForDeclaration, ngForTypeCheckTarget, setup as baseTestSetup, TypeCheckingTarget} from '../testing'; runInEachFileSystem(() => { describe('TemplateTypeChecker.getSymbolOfNode', () => { diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker_spec.ts index 3774e4f9ee..d181d7e954 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/type_checker_spec.ts @@ -11,7 +11,7 @@ import {absoluteFrom, absoluteFromSourceFile, getSourceFileOrError} from '../../ import {runInEachFileSystem} from '../../file_system/testing'; import {OptimizeFor} from '../api'; -import {getClass, setup, TestDeclaration} from './test_utils'; +import {getClass, setup, TestDeclaration} from '../testing'; runInEachFileSystem(() => { describe('TemplateTypeChecker', () => { diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/type_constructor_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/type_constructor_spec.ts index 1fa41d2150..1239d23eef 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/type_constructor_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/type_constructor_spec.ts @@ -19,7 +19,7 @@ import {InliningMode, PendingFileTypeCheckingData, TypeCheckContextImpl, TypeChe import {TemplateSourceManager} from '../src/source'; import {TypeCheckFile} from '../src/type_check_file'; -import {ALL_ENABLED_CONFIG} from './test_utils'; +import {ALL_ENABLED_CONFIG} from '../testing'; runInEachFileSystem(() => { describe('ngtsc typechecking', () => { diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/type_parameter_emitter_spec.ts b/packages/compiler-cli/src/ngtsc/typecheck/test/type_parameter_emitter_spec.ts index 27172b32e8..cec382c25e 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/type_parameter_emitter_spec.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/test/type_parameter_emitter_spec.ts @@ -13,7 +13,7 @@ import {isNamedClassDeclaration, TypeScriptReflectionHost} from '../../reflectio import {getDeclaration, makeProgram} from '../../testing'; import {TypeParameterEmitter} from '../src/type_parameter_emitter'; -import {angularCoreDts} from './test_utils'; +import {angularCoreDts} from '../testing'; runInEachFileSystem(() => { diff --git a/packages/compiler-cli/src/ngtsc/typecheck/testing/BUILD.bazel b/packages/compiler-cli/src/ngtsc/typecheck/testing/BUILD.bazel new file mode 100644 index 0000000000..4e0633bfbd --- /dev/null +++ b/packages/compiler-cli/src/ngtsc/typecheck/testing/BUILD.bazel @@ -0,0 +1,28 @@ +load("//tools:defaults.bzl", "ts_library") + +ts_library( + name = "testing", + testonly = True, + srcs = glob([ + "**/*.ts", + ]), + visibility = ["//packages/compiler-cli/src/ngtsc:__subpackages__"], + deps = [ + "//packages/compiler", + "//packages/compiler-cli/src/ngtsc/file_system", + "//packages/compiler-cli/src/ngtsc/file_system/testing", + "//packages/compiler-cli/src/ngtsc/imports", + "//packages/compiler-cli/src/ngtsc/incremental", + "//packages/compiler-cli/src/ngtsc/metadata", + "//packages/compiler-cli/src/ngtsc/perf", + "//packages/compiler-cli/src/ngtsc/program_driver", + "//packages/compiler-cli/src/ngtsc/reflection", + "//packages/compiler-cli/src/ngtsc/scope", + "//packages/compiler-cli/src/ngtsc/testing", + "//packages/compiler-cli/src/ngtsc/typecheck", + "//packages/compiler-cli/src/ngtsc/typecheck/api", + "//packages/compiler-cli/src/ngtsc/typecheck/diagnostics", + "//packages/compiler-cli/src/ngtsc/util", + "@npm//typescript", + ], +) diff --git a/packages/compiler-cli/src/ngtsc/typecheck/test/test_utils.ts b/packages/compiler-cli/src/ngtsc/typecheck/testing/index.ts similarity index 88% rename from packages/compiler-cli/src/ngtsc/typecheck/test/test_utils.ts rename to packages/compiler-cli/src/ngtsc/typecheck/testing/index.ts index 860b0aafad..5a556c914b 100644 --- a/packages/compiler-cli/src/ngtsc/typecheck/test/test_utils.ts +++ b/packages/compiler-cli/src/ngtsc/typecheck/testing/index.ts @@ -35,58 +35,58 @@ export function typescriptLibDts(): TestFile { return { name: absoluteFrom('/lib.d.ts'), contents: ` - type Partial = { [P in keyof T]?: T[P]; }; - type Pick = { [P in K]: T[P]; }; - type NonNullable = T extends null | undefined ? never : T; - - // The following native type declarations are required for proper type inference - declare interface Function { - call(...args: any[]): any; - } - declare interface Array { - [index: number]: T; - length: number; - } - declare interface String { - length: number; - } - - declare interface Event { - preventDefault(): void; - } - declare interface MouseEvent extends Event { - readonly x: number; - readonly y: number; - } - - declare interface HTMLElementEventMap { - "click": MouseEvent; - } - declare interface HTMLElement { - addEventListener(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any): void; - addEventListener(type: string, listener: (evt: Event): void;): void; - } - declare interface HTMLDivElement extends HTMLElement {} - declare interface HTMLImageElement extends HTMLElement { - src: string; - alt: string; - width: number; - height: number; - } - declare interface HTMLQuoteElement extends HTMLElement { - cite: string; - } - declare interface HTMLElementTagNameMap { - "blockquote": HTMLQuoteElement; - "div": HTMLDivElement; - "img": HTMLImageElement; - } - declare interface Document { - createElement(tagName: K): HTMLElementTagNameMap[K]; - createElement(tagName: string): HTMLElement; - } - declare const document: Document; - ` + type Partial = { [P in keyof T]?: T[P]; }; + type Pick = { [P in K]: T[P]; }; + type NonNullable = T extends null | undefined ? never : T; + + // The following native type declarations are required for proper type inference + declare interface Function { + call(...args: any[]): any; + } + declare interface Array { + [index: number]: T; + length: number; + } + declare interface String { + length: number; + } + + declare interface Event { + preventDefault(): void; + } + declare interface MouseEvent extends Event { + readonly x: number; + readonly y: number; + } + + declare interface HTMLElementEventMap { + "click": MouseEvent; + } + declare interface HTMLElement { + addEventListener(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any): void; + addEventListener(type: string, listener: (evt: Event): void;): void; + } + declare interface HTMLDivElement extends HTMLElement {} + declare interface HTMLImageElement extends HTMLElement { + src: string; + alt: string; + width: number; + height: number; + } + declare interface HTMLQuoteElement extends HTMLElement { + cite: string; + } + declare interface HTMLElementTagNameMap { + "blockquote": HTMLQuoteElement; + "div": HTMLDivElement; + "img": HTMLImageElement; + } + declare interface Document { + createElement(tagName: K): HTMLElementTagNameMap[K]; + createElement(tagName: string): HTMLElement; + } + declare const document: Document; + ` }; } @@ -94,18 +94,18 @@ export function angularCoreDts(): TestFile { return { name: absoluteFrom('/node_modules/@angular/core/index.d.ts'), contents: ` - export declare class TemplateRef { - abstract readonly elementRef: unknown; - abstract createEmbeddedView(context: C): unknown; - } - - export declare class EventEmitter { - subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): unknown; - subscribe(observerOrNext?: any, error?: any, complete?: any): unknown; - } - - export declare type NgIterable = Array | Iterable; - ` + export declare class TemplateRef { + abstract readonly elementRef: unknown; + abstract createEmbeddedView(context: C): unknown; + } + + export declare class EventEmitter { + subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): unknown; + subscribe(observerOrNext?: any, error?: any, complete?: any): unknown; + } + + export declare type NgIterable = Array | Iterable; + ` }; } @@ -113,10 +113,10 @@ export function angularAnimationsDts(): TestFile { return { name: absoluteFrom('/node_modules/@angular/animations/index.d.ts'), contents: ` - export declare class AnimationEvent { - element: any; - } - ` + export declare class AnimationEvent { + element: any; + } + ` }; } @@ -136,25 +136,25 @@ export function ngForDts(): TestFile { return { name: absoluteFrom('/ngfor.d.ts'), contents: ` - export declare class NgForOf { - ngForOf: T[]; - ngForTrackBy: TrackByFunction; - static ngTemplateContextGuard(dir: NgForOf, ctx: any): ctx is NgForOfContext; - } - - export interface TrackByFunction { - (index: number, item: T): any; - } - - export declare class NgForOfContext { - $implicit: T; - index: number; - count: number; - readonly odd: boolean; - readonly even: boolean; - readonly first: boolean; - readonly last: boolean; - }`, + export declare class NgForOf { + ngForOf: T[]; + ngForTrackBy: TrackByFunction; + static ngTemplateContextGuard(dir: NgForOf, ctx: any): ctx is NgForOfContext; + } + + export interface TrackByFunction { + (index: number, item: T): any; + } + + export declare class NgForOfContext { + $implicit: T; + index: number; + count: number; + readonly odd: boolean; + readonly even: boolean; + readonly first: boolean; + readonly last: boolean; + }`, }; }