mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(compiler-cli): update emit signature to support for strongly typed emitCallback (#47893)
Currently `ngc-wrapped` mostly relies on any casts/or disabled strictness checks to be able to use `tsickle`'s emit callback and emit result merging for ngtsc. We should change this so that supertypes of `ts.EmitResult` can be used in these optional callbacks- allowing us to enable strictness checks in `packages/bazel/...` too. PR Close #47893
This commit is contained in:
parent
37ba610449
commit
39b898d0cd
5 changed files with 30 additions and 40 deletions
|
|
@ -7,7 +7,7 @@
|
|||
*/
|
||||
|
||||
import ts from 'typescript';
|
||||
import type {TsickleHost} from 'tsickle';
|
||||
import type {TsickleHost, EmitResult as TsickleEmitResult} from 'tsickle';
|
||||
import yargs from 'yargs';
|
||||
import {exitCodeFromResult, formatDiagnostics, ParsedConfiguration, performCompilation, readConfiguration} from './perform_compile';
|
||||
import {createPerformWatchHost, performWatchCompilation} from './perform_watch';
|
||||
|
|
@ -91,8 +91,8 @@ export function mainDiagnosticsForTest(
|
|||
};
|
||||
}
|
||||
|
||||
function createEmitCallback(
|
||||
options: api.CompilerOptions, tsickle?: TsickleModule): api.TsEmitCallback|undefined {
|
||||
function createEmitCallback(options: api.CompilerOptions, tsickle?: TsickleModule):
|
||||
api.TsEmitCallback<TsickleEmitResult>|undefined {
|
||||
if (!options.annotateForClosureCompiler) {
|
||||
return undefined;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -231,14 +231,8 @@ export class NgtscProgram implements api.Program {
|
|||
this.options, ctx, resolve);
|
||||
}
|
||||
|
||||
emit(opts?: {
|
||||
emitFlags?: api.EmitFlags|undefined;
|
||||
forceEmit?: boolean;
|
||||
cancellationToken?: ts.CancellationToken | undefined;
|
||||
customTransformers?: api.CustomTransformers | undefined;
|
||||
emitCallback?: api.TsEmitCallback | undefined;
|
||||
mergeEmitResultsCallback?: api.TsMergeEmitResultsCallback | undefined;
|
||||
}|undefined): ts.EmitResult {
|
||||
emit<CbEmitRes extends ts.EmitResult>(opts?: api.EmitOptions<CbEmitRes>|
|
||||
undefined): ts.EmitResult {
|
||||
// Check if emission of the i18n messages bundle was requested.
|
||||
if (opts !== undefined && opts.emitFlags !== undefined &&
|
||||
opts.emitFlags & api.EmitFlags.I18nBundle) {
|
||||
|
|
@ -263,7 +257,8 @@ export class NgtscProgram implements api.Program {
|
|||
const res = this.compiler.perfRecorder.inPhase(PerfPhase.TypeScriptEmit, () => {
|
||||
const {transformers} = this.compiler.prepareEmit();
|
||||
const ignoreFiles = this.compiler.ignoreForEmit;
|
||||
const emitCallback = opts && opts.emitCallback || defaultEmitCallback;
|
||||
const emitCallback =
|
||||
(opts?.emitCallback ?? defaultEmitCallback) as api.TsEmitCallback<CbEmitRes>;
|
||||
|
||||
const writeFile: ts.WriteFileCallback =
|
||||
(fileName: string, data: string, writeByteOrderMark: boolean,
|
||||
|
|
@ -291,7 +286,7 @@ export class NgtscProgram implements api.Program {
|
|||
beforeTransforms.push(...customTransforms.beforeTs);
|
||||
}
|
||||
|
||||
const emitResults: ts.EmitResult[] = [];
|
||||
const emitResults: CbEmitRes[] = [];
|
||||
|
||||
for (const targetSourceFile of this.tsProgram.getSourceFiles()) {
|
||||
if (targetSourceFile.isDeclarationFile || ignoreFiles.has(targetSourceFile)) {
|
||||
|
|
@ -345,7 +340,7 @@ export class NgtscProgram implements api.Program {
|
|||
}
|
||||
}
|
||||
|
||||
const defaultEmitCallback: api.TsEmitCallback = ({
|
||||
const defaultEmitCallback: api.TsEmitCallback<ts.EmitResult> = ({
|
||||
program,
|
||||
targetSourceFile,
|
||||
writeFile,
|
||||
|
|
|
|||
|
|
@ -208,7 +208,7 @@ export function exitCodeFromResult(diags: ReadonlyArray<ts.Diagnostic>|undefined
|
|||
return diags.some(d => d.source === 'angular' && d.code === api.UNKNOWN_ERROR_CODE) ? 2 : 1;
|
||||
}
|
||||
|
||||
export function performCompilation({
|
||||
export function performCompilation<CbEmitRes extends ts.EmitResult = ts.EmitResult>({
|
||||
rootNames,
|
||||
options,
|
||||
host,
|
||||
|
|
@ -225,8 +225,8 @@ export function performCompilation({
|
|||
options: api.CompilerOptions,
|
||||
host?: api.CompilerHost,
|
||||
oldProgram?: api.Program,
|
||||
emitCallback?: api.TsEmitCallback,
|
||||
mergeEmitResultsCallback?: api.TsMergeEmitResultsCallback,
|
||||
emitCallback?: api.TsEmitCallback<CbEmitRes>,
|
||||
mergeEmitResultsCallback?: api.TsMergeEmitResultsCallback<CbEmitRes>,
|
||||
gatherDiagnostics?: (program: api.Program) => ReadonlyArray<ts.Diagnostic>,
|
||||
customTransformers?: api.CustomTransformers,
|
||||
emitFlags?: api.EmitFlags,
|
||||
|
|
|
|||
|
|
@ -39,11 +39,11 @@ export enum FileChangeEvent {
|
|||
CreateDeleteDir,
|
||||
}
|
||||
|
||||
export interface PerformWatchHost {
|
||||
export interface PerformWatchHost<CbEmitRes extends ts.EmitResult = ts.EmitResult> {
|
||||
reportDiagnostics(diagnostics: ReadonlyArray<ts.Diagnostic>): void;
|
||||
readConfiguration(): ParsedConfiguration;
|
||||
createCompilerHost(options: api.CompilerOptions): api.CompilerHost;
|
||||
createEmitCallback(options: api.CompilerOptions): api.TsEmitCallback|undefined;
|
||||
createEmitCallback(options: api.CompilerOptions): api.TsEmitCallback<CbEmitRes>|undefined;
|
||||
onFileChange(
|
||||
options: api.CompilerOptions, listener: (event: FileChangeEvent, fileName: string) => void,
|
||||
ready: () => void): {close: () => void};
|
||||
|
|
@ -51,11 +51,11 @@ export interface PerformWatchHost {
|
|||
clearTimeout(timeoutId: any): void;
|
||||
}
|
||||
|
||||
export function createPerformWatchHost(
|
||||
export function createPerformWatchHost<CbEmitRes extends ts.EmitResult = ts.EmitResult>(
|
||||
configFileName: string, reportDiagnostics: (diagnostics: ReadonlyArray<ts.Diagnostic>) => void,
|
||||
existingOptions?: ts.CompilerOptions,
|
||||
createEmitCallback?: (options: api.CompilerOptions) =>
|
||||
api.TsEmitCallback | undefined): PerformWatchHost {
|
||||
api.TsEmitCallback<CbEmitRes>| undefined): PerformWatchHost {
|
||||
return {
|
||||
reportDiagnostics: reportDiagnostics,
|
||||
createCompilerHost: options => createCompilerHost({options}),
|
||||
|
|
|
|||
|
|
@ -162,11 +162,11 @@ export interface TsEmitArguments {
|
|||
customTransformers?: ts.CustomTransformers;
|
||||
}
|
||||
|
||||
export interface TsEmitCallback {
|
||||
(args: TsEmitArguments): ts.EmitResult;
|
||||
export interface TsEmitCallback<T extends ts.EmitResult> {
|
||||
(args: TsEmitArguments): T;
|
||||
}
|
||||
export interface TsMergeEmitResultsCallback {
|
||||
(results: ts.EmitResult[]): ts.EmitResult;
|
||||
export interface TsMergeEmitResultsCallback<T extends ts.EmitResult> {
|
||||
(results: T[]): T;
|
||||
}
|
||||
|
||||
export interface LazyRoute {
|
||||
|
|
@ -175,6 +175,15 @@ export interface LazyRoute {
|
|||
referencedModule: {name: string, filePath: string};
|
||||
}
|
||||
|
||||
export interface EmitOptions<CbEmitRes extends ts.EmitResult> {
|
||||
emitFlags?: EmitFlags;
|
||||
forceEmit?: boolean;
|
||||
cancellationToken?: ts.CancellationToken;
|
||||
customTransformers?: CustomTransformers;
|
||||
emitCallback?: TsEmitCallback<CbEmitRes>;
|
||||
mergeEmitResultsCallback?: TsMergeEmitResultsCallback<CbEmitRes>;
|
||||
}
|
||||
|
||||
export interface Program {
|
||||
/**
|
||||
* Retrieve the TypeScript program used to produce semantic diagnostics and emit the sources.
|
||||
|
|
@ -250,21 +259,7 @@ export interface Program {
|
|||
*
|
||||
* Angular structural information is required to emit files.
|
||||
*/
|
||||
emit({
|
||||
emitFlags,
|
||||
forceEmit,
|
||||
cancellationToken,
|
||||
customTransformers,
|
||||
emitCallback,
|
||||
mergeEmitResultsCallback,
|
||||
}?: {
|
||||
emitFlags?: EmitFlags,
|
||||
forceEmit?: boolean,
|
||||
cancellationToken?: ts.CancellationToken,
|
||||
customTransformers?: CustomTransformers,
|
||||
emitCallback?: TsEmitCallback,
|
||||
mergeEmitResultsCallback?: TsMergeEmitResultsCallback
|
||||
}): ts.EmitResult;
|
||||
emit<CbEmitRes extends ts.EmitResult>(opts?: EmitOptions<CbEmitRes>|undefined): ts.EmitResult;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
|
|
|
|||
Loading…
Reference in a new issue