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:
@ -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') !;
|
||||
|
||||
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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)),
|
||||
|
@ -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`);
|
||||
}
|
||||
|
||||
|
@ -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)) {
|
||||
|
@ -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.`);
|
||||
}
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user