Skip to content

Commit

Permalink
fixup! fix(ngcc): use aliased exported types correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Sep 5, 2020
1 parent b35f1f0 commit 9a5b522
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 69 deletions.
37 changes: 3 additions & 34 deletions packages/compiler-cli/ngcc/src/host/esm2015_host.ts
Expand Up @@ -1605,17 +1605,6 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
});
}

private getImportOfTypeExpression(typeExpression: ts.Expression): Import|null {
if (ts.isIdentifier(typeExpression)) {
return this.getImportOfIdentifier(typeExpression);
}
if (ts.isPropertyAccessExpression(typeExpression) &&
ts.isIdentifier(typeExpression.expression)) {
return this.getImportOfIdentifier(typeExpression.expression);
}
return null;
}

/**
* `typeExpression` is an expression in a "type" context. Resolve it to a declared value.
*
Expand All @@ -1629,30 +1618,10 @@ export class Esm2015ReflectionHost extends TypeScriptReflectionHost implements N
};
}

const imp = this.getImportOfTypeExpression(typeExpression);
if (imp === null) {
return {
kind: TypeValueReferenceKind.LOCAL,
expression: typeExpression,
defaultImportStatement: null,
};
}

const decl = this.getDeclarationOfExpression(typeExpression);
if (decl === null || decl.node === null) {
return {
kind: TypeValueReferenceKind.LOCAL,
expression: typeExpression,
defaultImportStatement: null,
};
}

return {
kind: TypeValueReferenceKind.IMPORTED,
valueDeclaration: decl.node,
moduleName: imp.from,
importedName: imp.name,
nestedPath: null,
kind: TypeValueReferenceKind.LOCAL,
expression: typeExpression,
defaultImportStatement: null,
};
}

Expand Down
66 changes: 31 additions & 35 deletions packages/compiler-cli/ngcc/test/host/esm2015_host_spec.ts
Expand Up @@ -1140,33 +1140,32 @@ runInEachFileSystem(() => {
});

describe('getConstructorParameters()', () => {
describe('getConstructorParameters()', () => {
it('should find the correct imported identifier for decorated constructor parameter types',
() => {
const files = [
{
name: _('/node_modules/shared-lib/foo.d.ts'),
contents: `
it('should always specify LOCAL type value references for decorated constructor parameter types',
() => {
const files = [
{
name: _('/node_modules/shared-lib/foo.d.ts'),
contents: `
declare class Foo {}
export {Foo as Bar};
`,
},
{
name: _('/node_modules/shared-lib/index.d.ts'),
contents: `
},
{
name: _('/node_modules/shared-lib/index.d.ts'),
contents: `
export {Bar as Baz} from './foo';
`,
},
{
name: _('/local.js'),
contents: `
},
{
name: _('/local.js'),
contents: `
class Internal {}
export {Internal as External};
`
},
{
name: _('/main.js'),
contents: `
},
{
name: _('/main.js'),
contents: `
import {Baz} from 'shared-lib';
import {External} from './local';
export class SameFile {}
Expand All @@ -1176,23 +1175,21 @@ runInEachFileSystem(() => {
}
SomeClass.ctorParameters = [{ type: Baz }, { type: External }, { type: SameFile }];
`,
},
];
},
];

loadTestFiles(files);
const bundle = makeTestBundleProgram(_('/main.js'));
const host =
createHost(bundle, new Esm2015ReflectionHost(new MockLogger(), false, bundle));
const classNode = getDeclaration(
bundle.program, _('/main.js'), 'SomeClass', isNamedClassDeclaration);
loadTestFiles(files);
const bundle = makeTestBundleProgram(_('/main.js'));
const host =
createHost(bundle, new Esm2015ReflectionHost(new MockLogger(), false, bundle));
const classNode =
getDeclaration(bundle.program, _('/main.js'), 'SomeClass', isNamedClassDeclaration);

const parameters = host.getConstructorParameters(classNode)!;
const parameters = host.getConstructorParameters(classNode)!;

expect(parameters.map(p => p.name)).toEqual(['arg1', 'arg2', 'arg3']);
expectTypeValueReferencesForParameters(
parameters, ['Baz', 'External', 'SameFile'], ['shared-lib', './local', null]);
});
});
expect(parameters.map(p => p.name)).toEqual(['arg1', 'arg2', 'arg3']);
expectTypeValueReferencesForParameters(parameters, ['Baz', 'External', 'SameFile']);
});

it('should find the decorated constructor parameters', () => {
loadFakeCore(getFileSystem());
Expand All @@ -1208,8 +1205,7 @@ runInEachFileSystem(() => {
'_viewContainer', '_template', 'injected'
]);
expectTypeValueReferencesForParameters(
parameters, ['ViewContainerRef', 'TemplateRef', null],
['@angular/core', '@angular/core', null]);
parameters, ['ViewContainerRef', 'TemplateRef', null]);
});

it('should accept `ctorParameters` as an array', () => {
Expand Down

0 comments on commit 9a5b522

Please sign in to comment.