refactor(ivy): split type into type, internalType and adjacentType (#33533)

When compiling an Angular decorator (e.g. Directive), @angular/compiler
generates an 'expression' to be added as a static definition field
on the class, a 'type' which will be added for that field to the .d.ts
file, and a statement adjacent to the class that calls `setClassMetadata()`.

Previously, the same WrappedNodeExpr of the class' ts.Identifier was used
within each of this situations.

In the ngtsc case, this is proper. In the ngcc case, if the class being
compiled is within an ES5 IIFE, the outer name of the class may have
changed. Thus, the class has both an inner and outer name. The outer name
should continue to be used elsewhere in the compiler and in 'type'.

The 'expression' will live within the IIFE, the `internalType` should be used.
The adjacent statement will also live within the IIFE, the `adjacentType` should be used.

This commit introduces `ReflectionHost.getInternalNameOfClass()` and
`ReflectionHost.getAdjacentNameOfClass()`, which the compiler can use to
query for the correct name to use.

PR Close #33533
This commit is contained in:
Alex Rickabaugh
2019-11-01 16:55:09 +00:00
committed by atscott
parent d9a38928f5
commit 8d0de89ece
14 changed files with 116 additions and 25 deletions

View File

@ -622,4 +622,24 @@ export interface ReflectionHost {
* `ts.Program` as the input declaration.
*/
getDtsDeclaration(declaration: ts.Declaration): ts.Declaration|null;
/**
* Get a `ts.Identifier` for a given `ClassDeclaration` which can be used to refer to the class
* within its definition (such as in static fields).
*
* This can differ from `clazz.name` when ngcc runs over ES5 code, since the class may have a
* different name within its IIFE wrapper than it does externally.
*/
getInternalNameOfClass(clazz: ClassDeclaration): ts.Identifier;
/**
* Get a `ts.Identifier` for a given `ClassDeclaration` which can be used to refer to the class
* from statements that are "adjacent", and conceptually tightly bound, to the class but not
* actually inside it.
*
* Similar to `getInternalNameOfClass()`, this name can differ from `clazz.name` when ngcc runs
* over ES5 code, since these "adjacent" statements need to exist in the IIFE where the class may
* have a different name than it does externally.
*/
getAdjacentNameOfClass(clazz: ClassDeclaration): ts.Identifier;
}

View File

@ -182,6 +182,9 @@ export class TypeScriptReflectionHost implements ReflectionHost {
getDtsDeclaration(_: ts.Declaration): ts.Declaration|null { return null; }
getInternalNameOfClass(clazz: ClassDeclaration): ts.Identifier { return clazz.name; }
getAdjacentNameOfClass(clazz: ClassDeclaration): ts.Identifier { return clazz.name; }
protected getDirectImportOfIdentifier(id: ts.Identifier): Import|null {
const symbol = this.checker.getSymbolAtLocation(id);