Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/compiler-cli/ngcc/src/host/commonjs_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export class CommonJsReflectionHost extends Esm5ReflectionHost {
}

getImportOfIdentifier(id: ts.Identifier): Import|null {
const superImport = super.getImportOfIdentifier(id);
if (superImport !== null) {
return superImport;
}

const requireCall = this.findCommonJsImport(id);
if (requireCall === null) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-cli/ngcc/src/host/esm2015_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
*/
protected getStaticProperty(symbol: NgccClassSymbol, propertyName: ts.__String): ts.Symbol
|undefined {
return symbol.implementation.exports && symbol.implementation.exports.get(propertyName);
return symbol.declaration.exports && symbol.declaration.exports.get(propertyName);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/compiler-cli/ngcc/src/host/umd_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export class UmdReflectionHost extends Esm5ReflectionHost {
}

getImportOfIdentifier(id: ts.Identifier): Import|null {
const superImport = super.getImportOfIdentifier(id);
if (superImport !== null) {
return superImport;
}

const importParameter = this.findUmdImportParameter(id);
const from = importParameter && this.getUmdImportPath(importParameter);
return from !== null ? {from, name: id.text} : null;
Expand Down
26 changes: 25 additions & 1 deletion packages/compiler-cli/ngcc/test/host/commonjs_host_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as ts from 'typescript';

import {absoluteFrom, getFileSystem, getSourceFileOrError} from '../../../src/ngtsc/file_system';
import {TestFile, runInEachFileSystem} from '../../../src/ngtsc/file_system/testing';
import {ClassMemberKind, CtorParameter, Import, InlineDeclaration, isNamedClassDeclaration, isNamedFunctionDeclaration, isNamedVariableDeclaration} from '../../../src/ngtsc/reflection';
import {ClassMemberKind, CtorParameter, InlineDeclaration, isNamedClassDeclaration, isNamedFunctionDeclaration, isNamedVariableDeclaration} from '../../../src/ngtsc/reflection';
import {getDeclaration} from '../../../src/ngtsc/testing';
import {loadFakeCore, loadTestFiles} from '../../../test/helpers';
import {CommonJsReflectionHost} from '../../src/host/commonjs_host';
Expand Down Expand Up @@ -1558,6 +1558,30 @@ exports.ExternalModule = ExternalModule;
expect(importOfIdent).toEqual({name: 'a', from: './file_a'});
});

it('should find the import of an identifier in a declaration file', () => {
loadTestFiles([
{
name: _('/index.d.ts'),
contents: `
import {MyClass} from './myclass.d.ts';
export declare const a: MyClass;`
},
{
name: _('/myclass.d.ts'),
contents: `export declare class MyClass {}`,
}
]);
const {program, host: compilerHost} = makeTestBundleProgram(_('/index.d.ts'));
const host = new CommonJsReflectionHost(new MockLogger(), false, program, compilerHost);
const variableNode =
getDeclaration(program, _('/index.d.ts'), 'a', isNamedVariableDeclaration);
const identifier =
((variableNode.type as ts.TypeReferenceNode).typeName as ts.Identifier);

const importOfIdent = host.getImportOfIdentifier(identifier !);
expect(importOfIdent).toEqual({name: 'MyClass', from: './myclass.d.ts'});
});

it('should return null if the identifier was not imported', () => {
loadTestFiles(IMPORTS_FILES);
const {program, host: compilerHost} = makeTestBundleProgram(_('/index.js'));
Expand Down
23 changes: 23 additions & 0 deletions packages/compiler-cli/ngcc/test/host/esm2015_host_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,13 @@ runInEachFileSystem(() => {
CLASS_EXPRESSION_FILE = {
name: _('/class_expression.js'),
contents: `
import {Directive} from '@angular/core';
var AliasedClass_1;
let EmptyClass = class EmptyClass {};
let AliasedClass = AliasedClass_1 = class AliasedClass {}
AliasedClass.decorators = [
{ type: Directive, args: [{ selector: '[someDirective]' },] }
];
let usageOfAliasedClass = AliasedClass_1;
`,
};
Expand Down Expand Up @@ -708,6 +712,25 @@ runInEachFileSystem(() => {
]);
});

it('should find the decorators on an aliased class', () => {
loadTestFiles([CLASS_EXPRESSION_FILE]);
const {program} = makeTestBundleProgram(CLASS_EXPRESSION_FILE.name);
const host = new Esm2015ReflectionHost(new MockLogger(), false, program.getTypeChecker());
const classNode = getDeclaration(
program, CLASS_EXPRESSION_FILE.name, 'AliasedClass', isNamedVariableDeclaration);
const decorators = host.getDecoratorsOfDeclaration(classNode) !;

expect(decorators).toBeDefined();
expect(decorators.length).toEqual(1);

const decorator = decorators[0];
expect(decorator.name).toEqual('Directive');
expect(decorator.import).toEqual({name: 'Directive', from: '@angular/core'});
expect(decorator.args !.map(arg => arg.getText())).toEqual([
'{ selector: \'[someDirective]\' }',
]);
});

it('should return null if the symbol is not a class', () => {
loadTestFiles([FOO_FUNCTION_FILE]);
const {program} = makeTestBundleProgram(FOO_FUNCTION_FILE.name);
Expand Down
25 changes: 24 additions & 1 deletion packages/compiler-cli/ngcc/test/host/umd_host_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {TestFile, runInEachFileSystem} from '../../../src/ngtsc/file_system/test
import {ClassMemberKind, CtorParameter, Import, isNamedClassDeclaration, isNamedFunctionDeclaration, isNamedVariableDeclaration} from '../../../src/ngtsc/reflection';
import {getDeclaration} from '../../../src/ngtsc/testing';
import {loadFakeCore, loadTestFiles} from '../../../test/helpers';
import {Esm2015ReflectionHost} from '../../src/host/esm2015_host';
import {getIifeBody} from '../../src/host/esm5_host';
import {UmdReflectionHost} from '../../src/host/umd_host';
import {MockLogger} from '../helpers/mock_logger';
Expand Down Expand Up @@ -1658,6 +1657,30 @@ runInEachFileSystem(() => {
expect(importOfIdent).toEqual({name: 'a', from: './file_a'});
});

it('should find the import of an identifier in a declaration file', () => {
loadTestFiles([
{
name: _('/index.d.ts'),
contents: `
import {MyClass} from './myclass.d.ts';
export declare const a: MyClass;`
},
{
name: _('/myclass.d.ts'),
contents: `export declare class MyClass {}`,
}
]);
const {program, host: compilerHost} = makeTestBundleProgram(_('/index.d.ts'));
const host = new UmdReflectionHost(new MockLogger(), false, program, compilerHost);
const variableNode =
getDeclaration(program, _('/index.d.ts'), 'a', isNamedVariableDeclaration);
const identifier =
((variableNode.type as ts.TypeReferenceNode).typeName as ts.Identifier);

const importOfIdent = host.getImportOfIdentifier(identifier !);
expect(importOfIdent).toEqual({name: 'MyClass', from: './myclass.d.ts'});
});

it('should return null if the identifier was not imported', () => {
loadTestFiles(IMPORTS_FILES);
const {program, host: compilerHost} = makeTestBundleProgram(_('/index.js'));
Expand Down