@@ -30,7 +30,6 @@ import {ivySwitchTransform} from '../../switch';
3030import { aliasTransformFactory , CompilationMode , declarationTransformFactory , DecoratorHandler , DtsTransformRegistry , ivyTransformFactory , TraitCompiler } from '../../transform' ;
3131import { TemplateTypeCheckerImpl } from '../../typecheck' ;
3232import { OptimizeFor , TemplateTypeChecker , TypeCheckingConfig , TypeCheckingProgramStrategy } from '../../typecheck/api' ;
33- import { isTemplateDiagnostic } from '../../typecheck/diagnostics' ;
3433import { getSourceFileOrNull , isDtsPath , resolveModuleName } from '../../util/src/typescript' ;
3534import { LazyRoute , NgCompilerAdapter , NgCompilerOptions } from '../api' ;
3635
@@ -84,11 +83,12 @@ export class NgCompiler {
8483 private constructionDiagnostics : ts . Diagnostic [ ] = [ ] ;
8584
8685 /**
87- * Semantic diagnostics related to the program itself.
86+ * Non-template diagnostics related to the program itself. Does not include template
87+ * diagnostics because the template type checker memoizes them itself.
8888 *
89- * This is set by (and memoizes) `getDiagnostics `.
89+ * This is set by (and memoizes) `getNonTemplateDiagnostics `.
9090 */
91- private diagnostics : ts . Diagnostic [ ] | null = null ;
91+ private nonTemplateDiagnostics : ts . Diagnostic [ ] | null = null ;
9292
9393 private closureCompilerEnabled : boolean ;
9494 private nextProgram : ts . Program ;
@@ -175,38 +175,23 @@ export class NgCompiler {
175175 return this . incrementalDriver . depGraph . getResourceDependencies ( file ) ;
176176 }
177177
178+ /**
179+ * Get all Angular-related diagnostics for this compilation.
180+ */
181+ getDiagnostics ( ) : ts . Diagnostic [ ] {
182+ return [ ...this . getNonTemplateDiagnostics ( ) , ...this . getTemplateDiagnostics ( ) ] ;
183+ }
184+
178185 /**
179186 * Get all Angular-related diagnostics for this compilation.
180187 *
181188 * If a `ts.SourceFile` is passed, only diagnostics related to that file are returned.
182189 */
183- getDiagnostics ( file ?: ts . SourceFile ) : ts . Diagnostic [ ] {
184- if ( this . diagnostics === null ) {
185- const compilation = this . ensureAnalyzed ( ) ;
186- this . diagnostics =
187- [ ...compilation . traitCompiler . diagnostics , ...this . getTemplateDiagnostics ( ) ] ;
188- if ( this . entryPoint !== null && compilation . exportReferenceGraph !== null ) {
189- this . diagnostics . push ( ...checkForPrivateExports (
190- this . entryPoint , this . tsProgram . getTypeChecker ( ) , compilation . exportReferenceGraph ) ) ;
191- }
192- }
193-
194- if ( file === undefined ) {
195- return this . diagnostics ;
196- } else {
197- return this . diagnostics . filter ( diag => {
198- if ( diag . file === file ) {
199- return true ;
200- } else if ( isTemplateDiagnostic ( diag ) && diag . componentFile === file ) {
201- // Template diagnostics are reported when diagnostics for the component file are
202- // requested (since no consumer of `getDiagnostics` would ever ask for diagnostics from
203- // the fake ts.SourceFile for templates).
204- return true ;
205- } else {
206- return false ;
207- }
208- } ) ;
209- }
190+ getDiagnosticsForFile ( file : ts . SourceFile , optimizeFor : OptimizeFor ) : ts . Diagnostic [ ] {
191+ return [
192+ ...this . getNonTemplateDiagnostics ( ) . filter ( diag => diag . file === file ) ,
193+ ...this . getTemplateDiagnosticsForFile ( file , optimizeFor )
194+ ] ;
210195 }
211196
212197 /**
@@ -582,6 +567,37 @@ export class NgCompiler {
582567 return diagnostics ;
583568 }
584569
570+ private getTemplateDiagnosticsForFile ( sf : ts . SourceFile , optimizeFor : OptimizeFor ) :
571+ ReadonlyArray < ts . Diagnostic > {
572+ const compilation = this . ensureAnalyzed ( ) ;
573+
574+ // Get the diagnostics.
575+ const typeCheckSpan = this . perfRecorder . start ( 'typeCheckDiagnostics' ) ;
576+ const diagnostics : ts . Diagnostic [ ] = [ ] ;
577+ if ( ! sf . isDeclarationFile && ! this . adapter . isShim ( sf ) ) {
578+ diagnostics . push ( ...compilation . templateTypeChecker . getDiagnosticsForFile ( sf , optimizeFor ) ) ;
579+ }
580+
581+ const program = this . typeCheckingProgramStrategy . getProgram ( ) ;
582+ this . perfRecorder . stop ( typeCheckSpan ) ;
583+ this . incrementalStrategy . setIncrementalDriver ( this . incrementalDriver , program ) ;
584+ this . nextProgram = program ;
585+
586+ return diagnostics ;
587+ }
588+
589+ private getNonTemplateDiagnostics ( ) : ts . Diagnostic [ ] {
590+ if ( this . nonTemplateDiagnostics === null ) {
591+ const compilation = this . ensureAnalyzed ( ) ;
592+ this . nonTemplateDiagnostics = [ ...compilation . traitCompiler . diagnostics ] ;
593+ if ( this . entryPoint !== null && compilation . exportReferenceGraph !== null ) {
594+ this . nonTemplateDiagnostics . push ( ...checkForPrivateExports (
595+ this . entryPoint , this . tsProgram . getTypeChecker ( ) , compilation . exportReferenceGraph ) ) ;
596+ }
597+ }
598+ return this . nonTemplateDiagnostics ;
599+ }
600+
585601 /**
586602 * Reifies the inter-dependencies of NgModules and the components within their compilation scopes
587603 * into the `IncrementalDriver`'s dependency graph.
0 commit comments