refactor(core): clean up support for ES2015 constructor delegation (#30368)
This commit moves the delegated constructor detection to a helper function and also adds more test coverage. The original code for this came from https://github.com/angular/angular/pull/24156 thanks to @ts2do. Closes #24156 Closes #27267 // FW-1310 PR Close #30368
This commit is contained in:

committed by
Alex Rickabaugh

parent
9abf114fbb
commit
b68850215a
@ -26,6 +26,19 @@ export const INHERITED_CLASS_WITH_CTOR =
|
||||
export const INHERITED_CLASS_WITH_DELEGATE_CTOR =
|
||||
/^class\s+[A-Za-z\d$_]*\s*extends\s+[^{]+{[\s\S]*constructor\s*\(\)\s*{\s+super\(\.\.\.arguments\)/;
|
||||
|
||||
/**
|
||||
* Determine whether a stringified type is a class which delegates its constructor
|
||||
* to its parent.
|
||||
*
|
||||
* This is not trivial since compiled code can actually contain a constructor function
|
||||
* even if the original source code did not. For instance, when the child class contains
|
||||
* an initialized instance property.
|
||||
*/
|
||||
export function isDelegateCtor(typeStr: string): boolean {
|
||||
return DELEGATE_CTOR.test(typeStr) || INHERITED_CLASS_WITH_DELEGATE_CTOR.test(typeStr) ||
|
||||
(INHERITED_CLASS.test(typeStr) && !INHERITED_CLASS_WITH_CTOR.test(typeStr));
|
||||
}
|
||||
|
||||
export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
||||
private _reflect: any;
|
||||
|
||||
@ -72,8 +85,7 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
|
||||
// This also helps to work around for https://github.com/Microsoft/TypeScript/issues/12439
|
||||
// that sets 'design:paramtypes' to []
|
||||
// if a class inherits from another class but has no ctor declared itself.
|
||||
if (DELEGATE_CTOR.exec(typeStr) || INHERITED_CLASS_WITH_DELEGATE_CTOR.exec(typeStr) ||
|
||||
(INHERITED_CLASS.exec(typeStr) && !INHERITED_CLASS_WITH_CTOR.exec(typeStr))) {
|
||||
if (isDelegateCtor(typeStr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user