feat(ivy): support @Injectable on already decorated classes (#28523)
Previously, ngtsc would throw an error if two decorators were matched on the same class simultaneously. However, @Injectable is a special case, and it appears frequently on component, directive, and pipe classes. For pipes in particular, it's a common pattern to treat the pipe class also as an injectable service. ngtsc actually lacked the capability to compile multiple matching decorators on a class, so this commit adds support for that. Decorator handlers (and thus the decorators they match) are classified into three categories: PRIMARY, SHARED, and WEAK. PRIMARY handlers compile decorators that cannot coexist with other primary decorators. The handlers for Component, Directive, Pipe, and NgModule are marked as PRIMARY. A class may only have one decorator from this group. SHARED handlers compile decorators that can coexist with others. Injectable is the only decorator in this category, meaning it's valid to put an @Injectable decorator on a previously decorated class. WEAK handlers behave like SHARED, but are dropped if any non-WEAK handler matches a class. The handler which compiles ngBaseDef is WEAK, since ngBaseDef is only needed if a class doesn't otherwise have a decorator. Tests are added to validate that @Injectable can coexist with the other decorators and that an error is generated when mixing the primaries. PR Close #28523
This commit is contained in:

committed by
Misko Hevery

parent
d2742cf473
commit
99d8582882
@ -26,3 +26,11 @@ export function isFromDtsFile(node: ts.Node): boolean {
|
||||
}
|
||||
return sf !== undefined && D_TS.test(sf.fileName);
|
||||
}
|
||||
|
||||
export function getSourceFile(node: ts.Node): ts.SourceFile {
|
||||
// In certain transformation contexts, `ts.Node.getSourceFile()` can actually return `undefined`,
|
||||
// despite the type signature not allowing it. In that event, get the `ts.SourceFile` via the
|
||||
// original node instead (which works).
|
||||
const directSf = node.getSourceFile() as ts.SourceFile | undefined;
|
||||
return directSf !== undefined ? directSf : ts.getOriginalNode(node).getSourceFile();
|
||||
}
|
||||
|
Reference in New Issue
Block a user