fix(compiler-cli): support reflecting namespace declarations (#42728)

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
This commit is contained in:
Alan Agius 2021-07-02 12:33:51 +02:00 committed by atscott
parent 7e04116d15
commit 4c78984ad2
2 changed files with 26 additions and 0 deletions

View File

@ -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?`);
}

View File

@ -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([
{