feat(ivy): reference external classes by their exported name (#27743)
Previously, ngtsc would assume that a given directive/pipe being imported from an external package was importable using the same name by which it was declared. This isn't always true; sometimes a package will export a directive under a different name. For example, Angular frequently prefixes directive names with the 'ɵ' character to indicate that they're part of the package's private API, and not for public consumption. This commit introduces the TsReferenceResolver class which, given a declaration to import and a module name to import it from, can determine the exported name of the declared class within the module. This allows ngtsc to pick the correct name by which to import the class instead of making assumptions about how it was exported. This resolver is used to select a correct symbol name when creating an AbsoluteReference. FW-517 #resolve FW-536 #resolve PR Close #27743
This commit is contained in:

committed by
Kara Erickson

parent
0b9094ec63
commit
1c39ad38d3
@ -6,6 +6,7 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
import {ConstantPool} from '@angular/compiler';
|
||||
import {TsReferenceResolver} from '@angular/compiler-cli/src/ngtsc/imports';
|
||||
import {PartialEvaluator} from '@angular/compiler-cli/src/ngtsc/partial_evaluator';
|
||||
import * as path from 'canonical-path';
|
||||
import * as fs from 'fs';
|
||||
@ -59,8 +60,9 @@ export class FileResourceLoader implements ResourceLoader {
|
||||
*/
|
||||
export class DecorationAnalyzer {
|
||||
resourceLoader = new FileResourceLoader();
|
||||
scopeRegistry = new SelectorScopeRegistry(this.typeChecker, this.reflectionHost);
|
||||
evaluator = new PartialEvaluator(this.reflectionHost, this.typeChecker);
|
||||
resolver = new TsReferenceResolver(this.program, this.typeChecker, this.options, this.host);
|
||||
scopeRegistry = new SelectorScopeRegistry(this.typeChecker, this.reflectionHost, this.resolver);
|
||||
evaluator = new PartialEvaluator(this.reflectionHost, this.typeChecker, this.resolver);
|
||||
handlers: DecoratorHandler<any, any>[] = [
|
||||
new BaseDefDecoratorHandler(this.reflectionHost, this.evaluator),
|
||||
new ComponentDecoratorHandler(
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
import * as ts from 'typescript';
|
||||
|
||||
import {Reference} from '../../../ngtsc/imports';
|
||||
import {Reference, TsReferenceResolver} from '../../../ngtsc/imports';
|
||||
import {PartialEvaluator} from '../../../ngtsc/partial_evaluator';
|
||||
import {TypeScriptReflectionHost} from '../../../ngtsc/reflection';
|
||||
import {getDeclaration, makeProgram} from '../../../ngtsc/testing/in_memory_typescript';
|
||||
@ -16,7 +16,7 @@ import {NgccReferencesRegistry} from '../../src/analysis/ngcc_references_registr
|
||||
|
||||
describe('NgccReferencesRegistry', () => {
|
||||
it('should return a mapping from resolved reference identifiers to their declarations', () => {
|
||||
const {program} = makeProgram([{
|
||||
const {program, options, host} = makeProgram([{
|
||||
name: 'index.ts',
|
||||
contents: `
|
||||
export class SomeClass {}
|
||||
@ -38,9 +38,10 @@ describe('NgccReferencesRegistry', () => {
|
||||
getDeclaration(program, 'index.ts', 'someVariable', ts.isVariableDeclaration);
|
||||
const testArrayExpression = testArrayDeclaration.initializer !;
|
||||
|
||||
const host = new TypeScriptReflectionHost(checker);
|
||||
const evaluator = new PartialEvaluator(host, checker);
|
||||
const registry = new NgccReferencesRegistry(host);
|
||||
const reflectionHost = new TypeScriptReflectionHost(checker);
|
||||
const resolver = new TsReferenceResolver(program, checker, options, host);
|
||||
const evaluator = new PartialEvaluator(reflectionHost, checker, resolver);
|
||||
const registry = new NgccReferencesRegistry(reflectionHost);
|
||||
|
||||
const references = evaluator.evaluate(testArrayExpression) as Reference<ts.Declaration>[];
|
||||
registry.add(null !, ...references);
|
||||
|
Reference in New Issue
Block a user