-
Notifications
You must be signed in to change notification settings - Fork 27.2k
feat(language-service): Allow auto-imports of a pipe via quick fix when its selector is used. #48354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
feat(language-service): Allow auto-imports of a pipe via quick fix when its selector is used. #48354
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8ed0178
refactor(language-service): Improve the quick fix auto-import tests.
dylhunn f7bb7b6
refactor(compiler-cli): Make `getKnown` return an array of nodes.
dylhunn 9f5ece8
feat(language-service): Introduce a new NgModuleIndex, and use it to …
dylhunn 18b0681
feat(language-service): Allow auto-imports of a pipe via quick fix wh…
dylhunn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/compiler-cli/src/ngtsc/metadata/src/ng_module_index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.io/license | ||
| */ | ||
|
|
||
| import {Reference} from '../../imports'; | ||
| import {ClassDeclaration} from '../../reflection'; | ||
|
|
||
| import {MetadataReader, MetadataReaderWithIndex, MetaKind, NgModuleIndex} from './api'; | ||
|
|
||
| /** | ||
| * An index of all NgModules that export or re-export a given trait. | ||
| */ | ||
| export class NgModuleIndexImpl implements NgModuleIndex { | ||
| constructor(private metaReader: MetadataReader, private localReader: MetadataReaderWithIndex) {} | ||
|
|
||
| // A map from an NgModule's Class Declaration to the "main" reference to that module, aka the one | ||
| // present in the reader metadata object | ||
| private ngModuleAuthoritativeReference = new Map<ClassDeclaration, Reference<ClassDeclaration>>(); | ||
| // A map from a Directive/Pipe's class declaration to the class declarations of all re-exporting | ||
| // NgModules | ||
| private typeToExportingModules = new Map<ClassDeclaration, Set<ClassDeclaration>>(); | ||
|
|
||
| private indexed = false; | ||
|
|
||
| private updateWith<K, V>(cache: Map<K, Set<V>>, key: K, elem: V) { | ||
| if (cache.has(key)) { | ||
| cache.get(key)!.add(elem); | ||
| } else { | ||
| const set = new Set<V>(); | ||
| set.add(elem); | ||
| cache.set(key, set); | ||
| } | ||
| } | ||
|
|
||
| private index(): void { | ||
| const seenTypesWithReexports = new Map<ClassDeclaration, Set<ClassDeclaration>>(); | ||
| const locallyDeclaredDirsAndNgModules = [ | ||
| ...this.localReader.getKnown(MetaKind.NgModule), | ||
| ...this.localReader.getKnown(MetaKind.Directive), | ||
| ]; | ||
| for (const decl of locallyDeclaredDirsAndNgModules) { | ||
| // Here it's safe to create a new Reference because these are known local types. | ||
| this.indexTrait(new Reference(decl), seenTypesWithReexports); | ||
| } | ||
| this.indexed = true; | ||
| } | ||
|
|
||
| private indexTrait( | ||
| ref: Reference<ClassDeclaration>, | ||
| seenTypesWithReexports: Map<ClassDeclaration, Set<ClassDeclaration>>): void { | ||
| if (seenTypesWithReexports.has(ref.node)) { | ||
| // We've processed this type before. | ||
| return; | ||
| } | ||
| seenTypesWithReexports.set(ref.node, new Set()); | ||
|
|
||
| const meta = | ||
| this.metaReader.getDirectiveMetadata(ref) ?? this.metaReader.getNgModuleMetadata(ref); | ||
| if (meta === null) { | ||
| return; | ||
| } | ||
|
|
||
| // Component + NgModule: recurse into imports | ||
| if (meta.imports !== null) { | ||
| for (const childRef of meta.imports) { | ||
| this.indexTrait(childRef, seenTypesWithReexports); | ||
| } | ||
| } | ||
|
|
||
| if (meta.kind === MetaKind.NgModule) { | ||
| if (!this.ngModuleAuthoritativeReference.has(ref.node)) { | ||
| this.ngModuleAuthoritativeReference.set(ref.node, ref); | ||
| } | ||
|
|
||
| for (const childRef of meta.exports) { | ||
| this.indexTrait(childRef, seenTypesWithReexports); | ||
|
|
||
| const childMeta = this.metaReader.getDirectiveMetadata(childRef) ?? | ||
| this.metaReader.getPipeMetadata(childRef) ?? | ||
| this.metaReader.getNgModuleMetadata(childRef); | ||
| if (childMeta === null) { | ||
| continue; | ||
| } | ||
|
|
||
| switch (childMeta.kind) { | ||
| case MetaKind.Directive: | ||
| case MetaKind.Pipe: | ||
| this.updateWith(this.typeToExportingModules, childRef.node, ref.node); | ||
| this.updateWith(seenTypesWithReexports, ref.node, childRef.node); | ||
| break; | ||
| case MetaKind.NgModule: | ||
| if (seenTypesWithReexports.has(childRef.node)) { | ||
| for (const reexported of seenTypesWithReexports.get(childRef.node)!) { | ||
| this.updateWith(this.typeToExportingModules, reexported, ref.node); | ||
| this.updateWith(seenTypesWithReexports, ref.node, reexported); | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| getNgModulesExporting(directiveOrPipe: ClassDeclaration): Array<Reference<ClassDeclaration>> { | ||
| if (!this.indexed) { | ||
| this.index(); | ||
| } | ||
|
|
||
| if (!this.typeToExportingModules.has(directiveOrPipe)) { | ||
| return []; | ||
| } | ||
|
|
||
| const refs: Array<Reference<ClassDeclaration>> = []; | ||
| for (const ngModule of this.typeToExportingModules.get(directiveOrPipe)!) { | ||
| if (this.ngModuleAuthoritativeReference.has(ngModule)) { | ||
| refs.push(this.ngModuleAuthoritativeReference.get(ngModule)!); | ||
| } | ||
| } | ||
| return refs; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.