From 7dfa44e71a72e858b35ea2fa1ccbcca5d9bb22d6 Mon Sep 17 00:00:00 2001 From: Doug Parker Date: Mon, 6 Dec 2021 21:41:31 -0800 Subject: [PATCH] refactor(compiler-cli): add initial compiler options definitions for extended template diagnostics (#44391) Refs #42966. This includes a mapping of extended template diagnostics to their associated diagnostic category. It is generated from the list of diagnostic names, so the list should always be implicitly kept up to date. Usage looks like: ```json { "angularCompilerOptions": { "extendedDiagnostics": { "checks": { "invalidBananaInBox": "error", "nullishCoalescingNotNullable": "suppress" } } } } ``` PR Close #44391 --- .../compiler-cli/compiler_options.md | 16 +++++++++ .../compiler-cli/src/ngtsc/core/BUILD.bazel | 1 + .../src/ngtsc/core/api/src/options.ts | 14 +++++--- .../src/ngtsc/core/api/src/public_options.ts | 36 +++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/goldens/public-api/compiler-cli/compiler_options.md b/goldens/public-api/compiler-cli/compiler_options.md index 106c43325ab..2125d3d7e9a 100644 --- a/goldens/public-api/compiler-cli/compiler_options.md +++ b/goldens/public-api/compiler-cli/compiler_options.md @@ -10,6 +10,22 @@ export interface BazelAndG3Options { generateDeepReexports?: boolean; } +// @public +export enum DiagnosticCategoryLabel { + Error = "error", + Suppress = "suppress", + Warning = "warning" +} + +// @public +export interface DiagnosticOptions { + extendedDiagnostics?: { + checks?: { + [Name in ExtendedTemplateDiagnosticName]?: DiagnosticCategoryLabel; + }; + }; +} + // @public export interface I18nOptions { enableI18nLegacyMessageIdFormat?: boolean; diff --git a/packages/compiler-cli/src/ngtsc/core/BUILD.bazel b/packages/compiler-cli/src/ngtsc/core/BUILD.bazel index 7f00fa4479a..0dc379f3a5d 100644 --- a/packages/compiler-cli/src/ngtsc/core/BUILD.bazel +++ b/packages/compiler-cli/src/ngtsc/core/BUILD.bazel @@ -47,6 +47,7 @@ ts_library( name = "api", srcs = glob(["api/**/*.ts"]), deps = [ + "//packages/compiler-cli/src/ngtsc/diagnostics", "//packages/compiler-cli/src/ngtsc/file_system", "//packages/compiler-cli/src/ngtsc/shims:api", "@npm//typescript", diff --git a/packages/compiler-cli/src/ngtsc/core/api/src/options.ts b/packages/compiler-cli/src/ngtsc/core/api/src/options.ts index 39f4d141ce2..923b0972272 100644 --- a/packages/compiler-cli/src/ngtsc/core/api/src/options.ts +++ b/packages/compiler-cli/src/ngtsc/core/api/src/options.ts @@ -8,7 +8,7 @@ import ts from 'typescript'; -import {BazelAndG3Options, I18nOptions, LegacyNgcOptions, MiscOptions, NgcCompatibilityOptions, StrictTemplateOptions, TargetOptions} from './public_options'; +import {BazelAndG3Options, DiagnosticOptions, I18nOptions, LegacyNgcOptions, MiscOptions, NgcCompatibilityOptions, StrictTemplateOptions, TargetOptions} from './public_options'; /** @@ -53,6 +53,12 @@ export interface InternalOptions { * Also includes a few miscellaneous options. */ export interface NgCompilerOptions extends ts.CompilerOptions, LegacyNgcOptions, BazelAndG3Options, - NgcCompatibilityOptions, StrictTemplateOptions, - TestOnlyOptions, I18nOptions, TargetOptions, - InternalOptions, MiscOptions {} + DiagnosticOptions, NgcCompatibilityOptions, + StrictTemplateOptions, TestOnlyOptions, I18nOptions, + TargetOptions, InternalOptions, MiscOptions { + // Replace the index signature type from `ts.CompilerOptions` as it is more strict than it needs + // to be and would conflict with some types from the other interfaces. This is ok because Angular + // compiler options are actually separate from TS compiler options in the `tsconfig.json` and we + // have full control over the structure of Angular's compiler options. + [prop: string]: any; +} diff --git a/packages/compiler-cli/src/ngtsc/core/api/src/public_options.ts b/packages/compiler-cli/src/ngtsc/core/api/src/public_options.ts index 91da97d649a..765718eae74 100644 --- a/packages/compiler-cli/src/ngtsc/core/api/src/public_options.ts +++ b/packages/compiler-cli/src/ngtsc/core/api/src/public_options.ts @@ -6,6 +6,8 @@ * found in the LICENSE file at https://angular.io/license */ +import {ExtendedTemplateDiagnosticName} from '../../../../ngtsc/diagnostics'; + /** * Options supported by the legacy View Engine compiler, which are still consumed by the Angular Ivy * compiler for backwards compatibility. @@ -258,6 +260,40 @@ export interface StrictTemplateOptions { strictLiteralTypes?: boolean; } +/** + * A label referring to a `ts.DiagnosticCategory` or `'suppress'`, meaning the associated diagnostic + * should not be displayed at all. + * + * @publicApi + */ +export enum DiagnosticCategoryLabel { + /** Treat the diagnostic as a warning, don't fail the compilation. */ + Warning = 'warning', + + /** Treat the diagnostic as a hard error, fail the compilation. */ + Error = 'error', + + /** Ignore the diagnostic altogether. */ + Suppress = 'suppress', +} + +/** + * Options which control how diagnostics are emitted from the compiler. + * + * @publicApi + */ +export interface DiagnosticOptions { + /** Options which control how diagnostics are emitted from the compiler. */ + extendedDiagnostics?: { + /** + * A map of each extended template diagnostic's name to its category. This can be expanded in + * the future with more information for each check or for additional diagnostics not part of the + * extended template diagnostics system. + */ + checks?: {[Name in ExtendedTemplateDiagnosticName]?: DiagnosticCategoryLabel}; + }; +} + /** * Options which control behavior useful for "monorepo" build cases using Bazel (such as the * internal Google monorepo, g3).