Skip to content

Commit

Permalink
fix(compiler-cli): support reflecting namespace declarations (#42728)
Browse files Browse the repository at this point in the history
DTS bundling, will cause originally namespaced imports become namespace declarations within the same file. Example:

Before bundling
```ts
import * as i1 from './router';

export declare class RouterModule {
    constructor(guard: any, router: Router);

    static ɵmod: i0.ɵɵNgModuleDeclaration<RouterModule, [typeof i1.RouterOutlet...]>;
}
```

After bundling
```
declare namespace i1 {
  export {
    RouterOutletContract,
    RouterOutlet
  }
}

export declare class RouterModule {
    constructor(guard: any, router: Router);

    static ɵmod: i0.ɵɵNgModuleDeclaration<RouterModule, [typeof i1.RouterOutlet...]>;
}
```

And therefore this commit adds support for reflecting types that are defined in such namespace declarations.

Closes #42064

PR Close #42728
  • Loading branch information
alan-agius4 authored and atscott committed Jul 2, 2021
1 parent 4a8ab4f commit 4511609
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/compiler-cli/src/ngtsc/reflection/src/typescript.ts
Expand Up @@ -548,6 +548,8 @@ export function reflectTypeEntityToDeclaration(
throw new Error(`Module specifier is not a string`);
}
return {node, from: importDecl.moduleSpecifier.text};
} else if (ts.isModuleDeclaration(decl)) {
return {node, from: null};
} else {
throw new Error(`Unknown import type?`);
}
Expand Down
24 changes: 24 additions & 0 deletions packages/compiler-cli/src/ngtsc/reflection/test/ts_host_spec.ts
Expand Up @@ -153,6 +153,30 @@ runInEachFileSystem(() => {
expectParameter(args[0], 'bar', {moduleName: './bar', name: 'Bar'});
});

it('should reflect an argument from a namespace declarations', () => {
const {program} = makeProgram([{
name: _('/entry.ts'),
contents: `
export declare class Bar {}
declare namespace i1 {
export {
Bar,
}
}
class Foo {
constructor(bar: i1.Bar) {}
}
`
}]);
const clazz = getDeclaration(program, _('/entry.ts'), 'Foo', isNamedClassDeclaration);
const checker = program.getTypeChecker();
const host = new TypeScriptReflectionHost(checker);
const args = host.getConstructorParameters(clazz)!;
expect(args.length).toBe(1);
expectParameter(args[0], 'bar', 'i1.Bar');
});

it('should reflect an argument from a default import', () => {
const {program} = makeProgram([
{
Expand Down

0 comments on commit 4511609

Please sign in to comment.