66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { AST , CssSelector , DomElementSchemaRegistry , ExternalExpr , LiteralPrimitive , ParseSourceSpan , PropertyRead , SafePropertyRead , TmplAstElement , TmplAstNode , TmplAstReference , TmplAstTemplate , TmplAstTextAttribute , WrappedNodeExpr } from '@angular/compiler' ;
9+ import { AST , CssSelector , DomElementSchemaRegistry , ExternalExpr , LiteralPrimitive , ParseSourceSpan , PropertyRead , SafePropertyRead , TmplAstElement , TmplAstNode , TmplAstTemplate , TmplAstTextAttribute , WrappedNodeExpr } from '@angular/compiler' ;
1010import ts from 'typescript' ;
1111
1212import { ErrorCode , ngErrorCode } from '../../diagnostics' ;
13- import { absoluteFrom , absoluteFromSourceFile , AbsoluteFsPath , getSourceFileOrError } from '../../file_system' ;
13+ import { absoluteFromSourceFile , AbsoluteFsPath , getSourceFileOrError } from '../../file_system' ;
1414import { Reference , ReferenceEmitKind , ReferenceEmitter } from '../../imports' ;
1515import { IncrementalBuild } from '../../incremental/api' ;
16- import { DirectiveMeta , MetadataReader , MetadataReaderWithIndex , MetaKind , NgModuleIndex , PipeMeta } from '../../metadata' ;
16+ import { DirectiveMeta , MetadataReader , MetadataReaderWithIndex , MetaKind , NgModuleIndex , NgModuleMeta , PipeMeta } from '../../metadata' ;
1717import { PerfCheckpoint , PerfEvent , PerfPhase , PerfRecorder } from '../../perf' ;
1818import { ProgramDriver , UpdateMode } from '../../program_driver' ;
1919import { ClassDeclaration , DeclarationNode , isNamedClassDeclaration , ReflectionHost } from '../../reflection' ;
2020import { ComponentScopeKind , ComponentScopeReader , TypeCheckScopeRegistry } from '../../scope' ;
2121import { isShim } from '../../shims' ;
2222import { getSourceFileOrNull , isSymbolWithValueDeclaration } from '../../util/src/typescript' ;
23- import { ElementSymbol , FullTemplateMapping , GlobalCompletion , NgTemplateDiagnostic , OptimizeFor , PotentialDirective , PotentialImport , PotentialImportKind , PotentialPipe , ProgramTypeCheckAdapter , Symbol , TcbLocation , TemplateDiagnostic , TemplateId , TemplateSymbol , TemplateTypeChecker , TypeCheckableDirectiveMeta , TypeCheckingConfig } from '../api' ;
23+ import { ElementSymbol , FullTemplateMapping , GlobalCompletion , NgTemplateDiagnostic , OptimizeFor , PotentialDirective , PotentialImport , PotentialImportKind , PotentialImportMode , PotentialPipe , ProgramTypeCheckAdapter , Symbol , TcbLocation , TemplateDiagnostic , TemplateId , TemplateSymbol , TemplateTypeChecker , TypeCheckableDirectiveMeta , TypeCheckingConfig } from '../api' ;
2424import { makeTemplateDiagnostic } from '../diagnostics' ;
2525
2626import { CompletionEngine } from './completion' ;
@@ -99,6 +99,14 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
9999 return data . template ;
100100 }
101101
102+ getUsedDirectives ( component : ts . ClassDeclaration ) : TypeCheckableDirectiveMeta [ ] | null {
103+ return this . getLatestComponentState ( component ) . data ?. boundTarget . getUsedDirectives ( ) || null ;
104+ }
105+
106+ getUsedPipes ( component : ts . ClassDeclaration ) : string [ ] | null {
107+ return this . getLatestComponentState ( component ) . data ?. boundTarget . getUsedPipes ( ) || null ;
108+ }
109+
102110 private getLatestComponentState ( component : ts . ClassDeclaration ) :
103111 { data : TemplateData | null , tcb : ts . Node | null , tcbPath : AbsoluteFsPath , tcbIsShim : boolean } {
104112 this . ensureShimForComponent ( component ) ;
@@ -598,6 +606,20 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
598606 return this . typeCheckScopeRegistry . getTypeCheckDirectiveMetadata ( new Reference ( dir ) ) ;
599607 }
600608
609+ getNgModuleMetadata ( module : ts . ClassDeclaration ) : NgModuleMeta | null {
610+ if ( ! isNamedClassDeclaration ( module ) ) {
611+ return null ;
612+ }
613+ return this . metaReader . getNgModuleMetadata ( new Reference ( module ) ) ;
614+ }
615+
616+ getPipeMetadata ( pipe : ts . ClassDeclaration ) : PipeMeta | null {
617+ if ( ! isNamedClassDeclaration ( pipe ) ) {
618+ return null ;
619+ }
620+ return this . metaReader . getPipeMetadata ( new Reference ( pipe ) ) ;
621+ }
622+
601623 getPotentialElementTags ( component : ts . ClassDeclaration ) : Map < string , PotentialDirective | null > {
602624 if ( this . elementTagCache . has ( component ) ) {
603625 return this . elementTagCache . get ( component ) ! ;
@@ -712,18 +734,18 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
712734 }
713735
714736 getPotentialImportsFor (
715- toImport : PotentialDirective | PotentialPipe ,
716- inContext : ts . ClassDeclaration ) : ReadonlyArray < PotentialImport > {
737+ toImport : Reference < ClassDeclaration > , inContext : ts . ClassDeclaration ,
738+ importMode : PotentialImportMode ) : ReadonlyArray < PotentialImport > {
717739 const imports : PotentialImport [ ] = [ ] ;
718740
719- const meta = this . metaReader . getDirectiveMetadata ( toImport . ref ) ??
720- this . metaReader . getPipeMetadata ( toImport . ref ) ;
741+ const meta =
742+ this . metaReader . getDirectiveMetadata ( toImport ) ?? this . metaReader . getPipeMetadata ( toImport ) ;
721743 if ( meta === null ) {
722744 return imports ;
723745 }
724746
725- if ( meta . isStandalone ) {
726- const emitted = this . emit ( PotentialImportKind . Standalone , toImport . ref , inContext ) ;
747+ if ( meta . isStandalone || importMode === PotentialImportMode . ForceDirect ) {
748+ const emitted = this . emit ( PotentialImportKind . Standalone , toImport , inContext ) ;
727749 if ( emitted !== null ) {
728750 imports . push ( emitted ) ;
729751 }
@@ -732,7 +754,7 @@ export class TemplateTypeCheckerImpl implements TemplateTypeChecker {
732754 const exportingNgModules = this . ngModuleIndex . getNgModulesExporting ( meta . ref . node ) ;
733755 if ( exportingNgModules !== null ) {
734756 for ( const exporter of exportingNgModules ) {
735- const emittedRef = this . emit ( PotentialImportKind . Standalone , exporter , inContext ) ;
757+ const emittedRef = this . emit ( PotentialImportKind . NgModule , exporter , inContext ) ;
736758 if ( emittedRef !== null ) {
737759 imports . push ( emittedRef ) ;
738760 }
0 commit comments