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:
Pete Bacon Darwin
2018-05-27 20:50:37 -05:00
committed by Alex Rickabaugh
parent 9abf114fbb
commit b68850215a
2 changed files with 37 additions and 21 deletions

View File

@ -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;
}