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

@ -37,20 +37,14 @@ export function createDirectiveDecorator(clazz: ClassDeclaration): Decorator {
// TODO: At the moment ngtsc does not accept a directive with no selector
ts.createPropertyAssignment('selector', ts.createStringLiteral('NGCC_DUMMY')),
]);
const decoratorType = ts.createIdentifier('Directive');
const decoratorNode = ts.createObjectLiteral([
ts.createPropertyAssignment('type', decoratorType),
ts.createPropertyAssignment('args', ts.createArrayLiteral([selectorArg])),
]);
setParentPointers(clazz.getSourceFile(), decoratorNode);
return {
name: 'Directive',
identifier: decoratorType,
identifier: null,
import: {name: 'Directive', from: '@angular/core'},
node: decoratorNode,
args: [selectorArg],
node: null,
synthesizedFor: clazz.name,
args: [reifySourceFile(selectorArg)],
};
}
@ -58,27 +52,33 @@ export function createDirectiveDecorator(clazz: ClassDeclaration): Decorator {
* Create an empty `Injectable` decorator that will be associated with the `clazz`.
*/
export function createInjectableDecorator(clazz: ClassDeclaration): Decorator {
const decoratorType = ts.createIdentifier('Injectable');
const decoratorNode = ts.createObjectLiteral([
ts.createPropertyAssignment('type', decoratorType),
ts.createPropertyAssignment('args', ts.createArrayLiteral([])),
]);
setParentPointers(clazz.getSourceFile(), decoratorNode);
return {
name: 'Injectable',
identifier: decoratorType,
identifier: null,
import: {name: 'Injectable', from: '@angular/core'},
node: decoratorNode,
node: null,
synthesizedFor: clazz.name,
args: [],
};
}
const EMPTY_SF = ts.createSourceFile('(empty)', '', ts.ScriptTarget.Latest);
/**
* Ensure that a tree of AST nodes have their parents wired up.
* Takes a `ts.Expression` and returns the same `ts.Expression`, but with an associated
* `ts.SourceFile`.
*
* This transformation is necessary to use synthetic `ts.Expression`s with the `PartialEvaluator`,
* and many decorator arguments are interpreted in this way.
*/
export function setParentPointers(parent: ts.Node, child: ts.Node): void {
child.parent = parent;
ts.forEachChild(child, grandchild => setParentPointers(child, grandchild));
function reifySourceFile(expr: ts.Expression): ts.Expression {
const printer = ts.createPrinter();
const exprText = printer.printNode(ts.EmitHint.Unspecified, expr, EMPTY_SF);
const sf = ts.createSourceFile(
'(synthetic)', `const expr = ${exprText};`, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS);
const stmt = sf.statements[0];
if (!ts.isVariableStatement(stmt)) {
throw new Error(`Expected VariableStatement, got ${ts.SyntaxKind[stmt.kind]}`);
}
return stmt.declarationList.declarations[0].initializer !;
}

View File

@ -130,6 +130,9 @@ export class Renderer {
}
clazz.decorators.forEach(dec => {
if (dec.node === null) {
return;
}
const decoratorArray = dec.node.parent !;
if (!decoratorsToRemove.has(decoratorArray)) {
decoratorsToRemove.set(decoratorArray, [dec.node]);

View File

@ -86,7 +86,7 @@ exports.OtherDirective = OtherDirective;
const decorator = decorators[0];
expect(decorator.name).toEqual('Directive');
expect(decorator.identifier.getText()).toEqual('core.Directive');
expect(decorator.identifier !.getText()).toEqual('core.Directive');
expect(decorator.import).toEqual({name: 'Directive', from: '@angular/core'});
expect(decorator.args !.map(arg => arg.getText())).toEqual([
'{ selector: \'[someDirective]\' }',

View File

@ -175,7 +175,7 @@ runInEachFileSystem(() => {
const decorator = decorators[0];
expect(decorator.name).toEqual('Directive');
expect(decorator.identifier.getText()).toEqual('Directive');
expect(decorator.identifier !.getText()).toEqual('Directive');
expect(decorator.import).toEqual({name: 'Directive', from: '@angular/core'});
expect(decorator.args !.map(arg => arg.getText())).toEqual([
'{ selector: \'[someDirective]\' }',
@ -197,7 +197,7 @@ runInEachFileSystem(() => {
const decorator = decorators[0];
expect(decorator.name).toEqual('Directive');
expect(decorator.identifier.getText()).toEqual('Directive');
expect(decorator.identifier !.getText()).toEqual('Directive');
expect(decorator.import).toEqual({name: 'Directive', from: '@angular/core'});
expect(decorator.args !.map(arg => arg.getText())).toEqual([
'{ selector: \'[someDirective]\' }',
@ -219,7 +219,7 @@ runInEachFileSystem(() => {
const decorator = decorators[0];
expect(decorator.name).toEqual('Directive');
expect(decorator.identifier.getText()).toEqual('Directive');
expect(decorator.identifier !.getText()).toEqual('Directive');
expect(decorator.import).toEqual({name: 'Directive', from: './directives'});
expect(decorator.args !.map(arg => arg.getText())).toEqual([
'{ selector: \'[someDirective]\' }',
@ -433,7 +433,7 @@ runInEachFileSystem(() => {
const classNode = getDeclaration(
program, _('/some_directive.js'), 'SomeDirective', isNamedVariableDeclaration);
const classDecorators = host.getDecoratorsOfDeclaration(classNode) !;
const decoratorNode = classDecorators[0].node;
const decoratorNode = classDecorators[0].node !;
const identifierOfDirective =
ts.isCallExpression(decoratorNode) && ts.isIdentifier(decoratorNode.expression) ?
decoratorNode.expression :

View File

@ -202,7 +202,7 @@ export { SomeDirective };
const decorator = decorators[0];
expect(decorator.name).toEqual('Directive');
expect(decorator.identifier.getText()).toEqual('Directive');
expect(decorator.identifier !.getText()).toEqual('Directive');
expect(decorator.import).toEqual({name: 'Directive', from: '@angular/core'});
expect(decorator.args !.map(arg => arg.getText())).toEqual([
'{ selector: \'[someDirective]\' }',
@ -224,7 +224,7 @@ export { SomeDirective };
const decorator = decorators[0];
expect(decorator.name).toEqual('Directive');
expect(decorator.identifier.getText()).toEqual('Directive');
expect(decorator.identifier !.getText()).toEqual('Directive');
expect(decorator.import).toEqual({name: 'Directive', from: '@angular/core'});
expect(decorator.args !.map(arg => arg.getText())).toEqual([
'{ selector: \'[someDirective]\' }',
@ -245,7 +245,7 @@ export { SomeDirective };
const decorator = decorators[0];
expect(decorator.name).toEqual('Directive');
expect(decorator.identifier.getText()).toEqual('Directive');
expect(decorator.identifier !.getText()).toEqual('Directive');
expect(decorator.import).toEqual({name: 'Directive', from: './directives'});
expect(decorator.args !.map(arg => arg.getText())).toEqual([
'{ selector: \'[someDirective]\' }',
@ -470,7 +470,7 @@ export { SomeDirective };
const classNode = getDeclaration(
program, _('/some_directive.js'), 'SomeDirective', isNamedVariableDeclaration);
const classDecorators = host.getDecoratorsOfDeclaration(classNode) !;
const decoratorNode = classDecorators[0].node;
const decoratorNode = classDecorators[0].node !;
const identifierOfDirective =
ts.isCallExpression(decoratorNode) && ts.isIdentifier(decoratorNode.expression) ?

View File

@ -79,7 +79,7 @@ runInEachFileSystem(() => {
const decorator = decorators[0];
expect(decorator.name).toEqual('Directive');
expect(decorator.identifier.getText()).toEqual('core.Directive');
expect(decorator.identifier !.getText()).toEqual('core.Directive');
expect(decorator.import).toEqual({name: 'Directive', from: '@angular/core'});
expect(decorator.args !.map(arg => arg.getText())).toEqual([
'{ selector: \'[someDirective]\' }',

View File

@ -356,7 +356,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString())
.not.toContain(`{ type: core.Directive, args: [{ selector: '[a]' }] },`);
@ -377,7 +377,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'B') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString())
.toContain(`{ type: core.Directive, args: [{ selector: '[a]' }] },`);
@ -398,7 +398,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'C') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
renderer.addDefinitions(output, compiledClass, 'SOME DEFINITION TEXT');
expect(output.toString())
@ -423,7 +423,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).not.toContain(`Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);
@ -440,7 +440,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'B') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).toContain(`Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);
@ -458,7 +458,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'C') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).toContain(`Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);

View File

@ -345,7 +345,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString())
.not.toContain(`{ type: Directive, args: [{ selector: '[a]' }] },`);
@ -364,7 +364,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'B') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[a]' }] },`);
expect(output.toString()).toContain(`{ type: OtherA }`);
@ -383,7 +383,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'C') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
renderer.addDefinitions(output, compiledClass, 'SOME DEFINITION TEXT');
expect(output.toString()).toContain(`{ type: Directive, args: [{ selector: '[a]' }] },`);
@ -406,7 +406,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).not.toContain(`Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);
@ -423,7 +423,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'B') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).toContain(`Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);
@ -441,7 +441,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'C') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).toContain(`Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);

View File

@ -255,7 +255,7 @@ A.decorators = [
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString())
.not.toContain(`{ type: Directive, args: [{ selector: '[a]' }] },`);
@ -276,7 +276,7 @@ A.decorators = [
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'B') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString())
.toContain(`{ type: Directive, args: [{ selector: '[a]' }] },`);
@ -304,7 +304,7 @@ A.decorators = [
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
// The decorator should have been removed correctly.
expect(output.toString()).toContain('A.decorators = [ { type: OtherA }');
@ -319,7 +319,7 @@ A.decorators = [
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'C') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString())
.toContain(`{ type: Directive, args: [{ selector: '[a]' }] },`);
@ -385,7 +385,7 @@ export { D };
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).not.toContain(`Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);
@ -402,7 +402,7 @@ export { D };
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'B') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).toContain(`Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);
@ -420,7 +420,7 @@ export { D };
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'C') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).toContain(`Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);

View File

@ -425,7 +425,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString())
.not.toContain(`{ type: core.Directive, args: [{ selector: '[a]' }] },`);
@ -446,7 +446,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'B') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString())
.toContain(`{ type: core.Directive, args: [{ selector: '[a]' }] },`);
@ -467,7 +467,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'C') !;
const decorator = compiledClass.decorators ![0];
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
renderer.addDefinitions(output, compiledClass, 'SOME DEFINITION TEXT');
expect(output.toString())
@ -490,7 +490,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'A') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).not.toContain(`core.Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);
@ -507,7 +507,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'B') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).toContain(`core.Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);
@ -525,7 +525,7 @@ SOME DEFINITION TEXT
decorationAnalyses.get(sourceFile) !.compiledClasses.find(c => c.name === 'C') !;
const decorator = compiledClass.decorators !.find(d => d.name === 'Directive') !;
const decoratorsToRemove = new Map<ts.Node, ts.Node[]>();
decoratorsToRemove.set(decorator.node.parent !, [decorator.node]);
decoratorsToRemove.set(decorator.node !.parent !, [decorator.node !]);
renderer.removeDecorators(output, decoratorsToRemove);
expect(output.toString()).toContain(`core.Directive({ selector: '[a]' }),`);
expect(output.toString()).toContain(`OtherA()`);

View File

@ -509,7 +509,7 @@ export class ComponentDecoratorHandler implements
}
if (decorator.args === null || decorator.args.length !== 1) {
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_ARITY_WRONG, decorator.node,
ErrorCode.DECORATOR_ARITY_WRONG, Decorator.nodeForError(decorator),
`Incorrect number of arguments to @Component decorator`);
}
const meta = unwrapExpression(decorator.args[0]);
@ -624,7 +624,8 @@ export class ComponentDecoratorHandler implements
} {
if (!component.has('template')) {
throw new FatalDiagnosticError(
ErrorCode.COMPONENT_MISSING_TEMPLATE, decorator.node, 'component is missing a template');
ErrorCode.COMPONENT_MISSING_TEMPLATE, Decorator.nodeForError(decorator),
'component is missing a template');
}
const templateExpr = component.get('template') !;

View File

@ -121,7 +121,7 @@ export function extractDirectiveMetadata(
directive = new Map<string, ts.Expression>();
} else if (decorator.args.length !== 1) {
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_ARITY_WRONG, decorator.node,
ErrorCode.DECORATOR_ARITY_WRONG, Decorator.nodeForError(decorator),
`Incorrect number of arguments to @${decorator.name} decorator`);
} else {
const meta = unwrapExpression(decorator.args[0]);
@ -447,7 +447,7 @@ export function queriesFromFields(
evaluator: PartialEvaluator): R3QueryMetadata[] {
return fields.map(({member, decorators}) => {
const decorator = decorators[0];
const node = member.node || decorator.node;
const node = member.node || Decorator.nodeForError(decorator);
// Throw in case of `@Input() @ContentChild('foo') foo: any`, which is not supported in Ivy
if (member.decorators !.some(v => v.name === 'Input')) {
@ -465,7 +465,7 @@ export function queriesFromFields(
'Query decorator must go on a property-type member');
}
return extractQueryMetadata(
decorator.node, decorator.name, decorator.args || [], member.name, reflector, evaluator);
node, decorator.name, decorator.args || [], member.name, reflector, evaluator);
});
}

View File

@ -116,7 +116,8 @@ function extractInjectableMetadata(
const typeArgumentCount = reflector.getGenericArityOfClass(clazz) || 0;
if (decorator.args === null) {
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_NOT_CALLED, decorator.node, '@Injectable must be called');
ErrorCode.DECORATOR_NOT_CALLED, Decorator.nodeForError(decorator),
'@Injectable must be called');
}
if (decorator.args.length === 0) {
return {
@ -202,7 +203,8 @@ function extractInjectableCtorDeps(
strictCtorDeps: boolean) {
if (decorator.args === null) {
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_NOT_CALLED, decorator.node, '@Injectable must be called');
ErrorCode.DECORATOR_NOT_CALLED, Decorator.nodeForError(decorator),
'@Injectable must be called');
}
let ctorDeps: R3DependencyMetadata[]|'invalid'|null = null;

View File

@ -122,6 +122,9 @@ function classMemberToMetadata(
* Convert a reflected decorator to metadata.
*/
function decoratorToMetadata(decorator: Decorator): ts.ObjectLiteralExpression {
if (decorator.identifier === null) {
throw new Error('Illegal state: synthesized decorator cannot be emitted in class metadata.');
}
// Decorators have a type.
const properties: ts.ObjectLiteralElementLike[] = [
ts.createPropertyAssignment('type', ts.getMutableClone(decorator.identifier)),

View File

@ -67,7 +67,7 @@ export class NgModuleDecoratorHandler implements DecoratorHandler<NgModuleAnalys
const name = node.name.text;
if (decorator.args === null || decorator.args.length > 1) {
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_ARITY_WRONG, decorator.node,
ErrorCode.DECORATOR_ARITY_WRONG, Decorator.nodeForError(decorator),
`Incorrect number of arguments to @NgModule decorator`);
}

View File

@ -53,11 +53,13 @@ export class PipeDecoratorHandler implements DecoratorHandler<PipeHandlerData, D
const type = new WrappedNodeExpr(clazz.name);
if (decorator.args === null) {
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_NOT_CALLED, decorator.node, `@Pipe must be called`);
ErrorCode.DECORATOR_NOT_CALLED, Decorator.nodeForError(decorator),
`@Pipe must be called`);
}
if (decorator.args.length !== 1) {
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_ARITY_WRONG, decorator.node, '@Pipe must have exactly one argument');
ErrorCode.DECORATOR_ARITY_WRONG, Decorator.nodeForError(decorator),
'@Pipe must have exactly one argument');
}
const meta = unwrapExpression(decorator.args[0]);
if (!ts.isObjectLiteralExpression(meta)) {

View File

@ -55,7 +55,7 @@ export function getConstructorDependencies(
if (name === 'Inject') {
if (dec.args === null || dec.args.length !== 1) {
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_ARITY_WRONG, dec.node,
ErrorCode.DECORATOR_ARITY_WRONG, Decorator.nodeForError(dec),
`Unexpected number of arguments to @Inject().`);
}
token = new WrappedNodeExpr(dec.args[0]);
@ -70,14 +70,15 @@ export function getConstructorDependencies(
} else if (name === 'Attribute') {
if (dec.args === null || dec.args.length !== 1) {
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_ARITY_WRONG, dec.node,
ErrorCode.DECORATOR_ARITY_WRONG, Decorator.nodeForError(dec),
`Unexpected number of arguments to @Attribute().`);
}
token = new WrappedNodeExpr(dec.args[0]);
resolved = R3ResolvedDependencyType.Attribute;
} else {
throw new FatalDiagnosticError(
ErrorCode.DECORATOR_UNEXPECTED, dec.node, `Unexpected decorator ${name} on parameter.`);
ErrorCode.DECORATOR_UNEXPECTED, Decorator.nodeForError(dec),
`Unexpected decorator ${name} on parameter.`);
}
});

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`).