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';
|
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
import {absoluteFrom} from '../../../src/ngtsc/file_system';
|
|
|
|
import {TestFile, runInEachFileSystem} from '../../../src/ngtsc/file_system/testing';
|
2019-03-20 09:47:58 -04:00
|
|
|
import {Reference} from '../../../src/ngtsc/imports';
|
|
|
|
import {PartialEvaluator} from '../../../src/ngtsc/partial_evaluator';
|
|
|
|
import {TypeScriptReflectionHost} from '../../../src/ngtsc/reflection';
|
2019-06-06 15:22:32 -04:00
|
|
|
import {getDeclaration} from '../../../src/ngtsc/testing';
|
|
|
|
import {loadTestFiles} from '../../../test/helpers';
|
2018-11-13 09:40:54 -05:00
|
|
|
import {NgccReferencesRegistry} from '../../src/analysis/ngcc_references_registry';
|
2019-06-06 15:22:32 -04:00
|
|
|
import {makeTestBundleProgram} from '../helpers/utils';
|
2018-11-13 09:40:54 -05:00
|
|
|
|
2019-06-06 15:22:32 -04:00
|
|
|
runInEachFileSystem(() => {
|
|
|
|
describe('NgccReferencesRegistry', () => {
|
|
|
|
it('should return a mapping from resolved reference identifiers to their declarations', () => {
|
|
|
|
const _ = absoluteFrom;
|
|
|
|
const TEST_FILES: TestFile[] = [{
|
|
|
|
name: _('/index.ts'),
|
|
|
|
contents: `
|
2018-11-13 09:40:54 -05:00
|
|
|
export class SomeClass {}
|
|
|
|
export function someFunction() {}
|
|
|
|
export const someVariable = 42;
|
|
|
|
|
|
|
|
export const testArray = [SomeClass, someFunction, someVariable];
|
|
|
|
`
|
2019-06-06 15:22:32 -04:00
|
|
|
}];
|
|
|
|
loadTestFiles(TEST_FILES);
|
|
|
|
const {program} = makeTestBundleProgram(TEST_FILES[0].name);
|
|
|
|
|
|
|
|
const checker = program.getTypeChecker();
|
|
|
|
|
|
|
|
const indexPath = _('/index.ts');
|
|
|
|
const testArrayDeclaration =
|
|
|
|
getDeclaration(program, indexPath, 'testArray', ts.isVariableDeclaration);
|
|
|
|
const someClassDecl = getDeclaration(program, indexPath, 'SomeClass', ts.isClassDeclaration);
|
|
|
|
const someFunctionDecl =
|
|
|
|
getDeclaration(program, indexPath, 'someFunction', ts.isFunctionDeclaration);
|
|
|
|
const someVariableDecl =
|
|
|
|
getDeclaration(program, indexPath, 'someVariable', ts.isVariableDeclaration);
|
|
|
|
const testArrayExpression = testArrayDeclaration.initializer !;
|
|
|
|
|
|
|
|
const reflectionHost = new TypeScriptReflectionHost(checker);
|
|
|
|
const evaluator = new PartialEvaluator(reflectionHost, checker);
|
|
|
|
const registry = new NgccReferencesRegistry(reflectionHost);
|
|
|
|
|
|
|
|
const references = (evaluator.evaluate(testArrayExpression) as any[]).filter(isReference);
|
|
|
|
registry.add(null !, ...references);
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2018-11-13 09:40:54 -05:00
|
|
|
});
|
2019-06-06 15:22:32 -04:00
|
|
|
|
|
|
|
function isReference(ref: any): ref is Reference<ts.Declaration> {
|
|
|
|
return ref instanceof Reference;
|
|
|
|
}
|
2018-11-13 09:40:54 -05:00
|
|
|
});
|