refactor(ivy): mark synthetic decorators explicitly (#33362)

In ngcc's migration system, synthetic decorators can be injected into a
compilation to ensure that certain classes are compiled with Angular
logic, where the original library code did not include the necessary
decorators. Prior to this change, synthesized decorators would have a
fake AST structure as associated node and a made-up identifier. In
theory, this may introduce issues downstream:

1) a decorator's node is used for diagnostics, so it must have position
information. Having fake AST nodes without a position is therefore a
problem. Note that this is currently not a problem in practice, as
injected synthesized decorators would not produce any diagnostics.

2) the decorator's identifier should refer to an imported symbol.
Therefore, it is required that the symbol is actually imported.
Moreover, bundle formats such as UMD and CommonJS use namespaces for
imports, so a bare `ts.Identifier` would not be suitable to use as
identifier. This was also not a problem in practice, as the identifier
is only used in the `setClassMetadata` generated code, which is omitted
for synthetically injected decorators.

To remedy these potential issues, this commit makes a decorator's
identifier optional and switches its node over from a fake AST structure
to the class' name.

PR Close #33362
This commit is contained in:
JoostK
2019-10-20 22:45:28 +02:00
committed by Andrew Kushnir
parent 31b9492951
commit 3858b26211
18 changed files with 131 additions and 78 deletions

View File

@ -9,9 +9,12 @@
import * as ts from 'typescript';
/**
* Metadata extracted from an instance of a decorator on another declaration.
* Metadata extracted from an instance of a decorator on another declaration, or synthesized from
* other information about a class.
*/
export interface Decorator {
export type Decorator = ConcreteDecorator | SyntheticDecorator;
export interface BaseDecorator {
/**
* Name by which the decorator was invoked in the user's code.
*
@ -23,7 +26,7 @@ export interface Decorator {
/**
* Identifier which refers to the decorator in the user's code.
*/
identifier: DecoratorIdentifier;
identifier: DecoratorIdentifier|null;
/**
* `Import` by which the decorator was brought into the module in which it was invoked, or `null`
@ -32,16 +35,54 @@ export interface Decorator {
import : Import | null;
/**
* TypeScript reference to the decorator itself.
* TypeScript reference to the decorator itself, or `null` if the decorator is synthesized (e.g.
* in ngcc).
*/
node: ts.Node;
node: ts.Node|null;
/**
* Arguments of the invocation of the decorator, if the decorator is invoked, or `null` otherwise.
* Arguments of the invocation of the decorator, if the decorator is invoked, or `null`
* otherwise.
*/
args: ts.Expression[]|null;
}
/**
* Metadata extracted from an instance of a decorator on another declaration, which was actually
* present in a file.
*
* Concrete decorators always have an `identifier` and a `node`.
*/
export interface ConcreteDecorator extends BaseDecorator {
identifier: DecoratorIdentifier;
node: ts.Node;
}
/**
* Synthetic decorators never have an `identifier` or a `node`, but know the node for which they
* were synthesized.
*/
export interface SyntheticDecorator extends BaseDecorator {
identifier: null;
node: null;
/**
* The `ts.Node` for which this decorator was created.
*/
synthesizedFor: ts.Node;
}
export const Decorator = {
nodeForError: (decorator: Decorator): ts.Node => {
if (decorator.node !== null) {
return decorator.node;
} else {
// TODO(alxhub): we can't rely on narrowing until TS 3.6 is in g3.
return (decorator as SyntheticDecorator).synthesizedFor;
}
},
};
/**
* A decorator is identified by either a simple identifier (e.g. `Decorator`) or, in some cases,
* a namespaced property access (e.g. `core.Decorator`).