2018-11-13 09:40:54 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. 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 * as ts from 'typescript';
|
|
|
|
|
2018-12-18 14:09:21 -05:00
|
|
|
import {Reference, TsReferenceResolver} from '../../../ngtsc/imports';
|
2018-12-18 12:48:15 -05:00
|
|
|
import {PartialEvaluator} from '../../../ngtsc/partial_evaluator';
|
|
|
|
import {TypeScriptReflectionHost} from '../../../ngtsc/reflection';
|
2018-11-13 09:40:54 -05:00
|
|
|
import {getDeclaration, makeProgram} from '../../../ngtsc/testing/in_memory_typescript';
|
|
|
|
import {NgccReferencesRegistry} from '../../src/analysis/ngcc_references_registry';
|
|
|
|
|
|
|
|
describe('NgccReferencesRegistry', () => {
|
|
|
|
it('should return a mapping from resolved reference identifiers to their declarations', () => {
|
2018-12-18 14:09:21 -05:00
|
|
|
const {program, options, host} = makeProgram([{
|
2018-11-13 09:40:54 -05:00
|
|
|
name: 'index.ts',
|
|
|
|
contents: `
|
|
|
|
export class SomeClass {}
|
|
|
|
export function someFunction() {}
|
|
|
|
export const someVariable = 42;
|
|
|
|
|
|
|
|
export const testArray = [SomeClass, someFunction, someVariable];
|
|
|
|
`
|
|
|
|
}]);
|
|
|
|
|
|
|
|
const checker = program.getTypeChecker();
|
|
|
|
|
|
|
|
const testArrayDeclaration =
|
|
|
|
getDeclaration(program, 'index.ts', 'testArray', ts.isVariableDeclaration);
|
|
|
|
const someClassDecl = getDeclaration(program, 'index.ts', 'SomeClass', ts.isClassDeclaration);
|
|
|
|
const someFunctionDecl =
|
|
|
|
getDeclaration(program, 'index.ts', 'someFunction', ts.isFunctionDeclaration);
|
|
|
|
const someVariableDecl =
|
|
|
|
getDeclaration(program, 'index.ts', 'someVariable', ts.isVariableDeclaration);
|
|
|
|
const testArrayExpression = testArrayDeclaration.initializer !;
|
|
|
|
|
2018-12-18 14:09:21 -05:00
|
|
|
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);
|
2018-11-13 09:40:54 -05:00
|
|
|
|
2018-12-18 12:48:15 -05:00
|
|
|
const references = evaluator.evaluate(testArrayExpression) as Reference<ts.Declaration>[];
|
2018-12-13 14:52:20 -05:00
|
|
|
registry.add(null !, ...references);
|
2018-11-13 09:40:54 -05:00
|
|
|
|
|
|
|
const map = registry.getDeclarationMap();
|
|
|
|
expect(map.size).toEqual(2);
|
|
|
|
expect(map.get(someClassDecl.name !) !.node).toBe(someClassDecl);
|
|
|
|
expect(map.get(someFunctionDecl.name !) !.node).toBe(someFunctionDecl);
|
|
|
|
expect(map.has(someVariableDecl.name as ts.Identifier)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|