Skip to content

Commit 5c89d6b

Browse files
trotylvicb
authored andcommitted
feat(core): support metadata reflection for native class types (angular#22356)
closes angular#21731 PR Close angular#22356
1 parent 3e6a86f commit 5c89d6b

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

Diff for: packages/core/src/reflection/reflection_capabilities.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ import {GetterFn, MethodFn, SetterFn} from './types';
1515

1616

1717
/**
18-
* Attention: This regex has to hold even if the code is minified!
18+
* Attention: These regex has to hold even if the code is minified!
1919
*/
2020
export const DELEGATE_CTOR = /^function\s+\S+\(\)\s*{[\s\S]+\.apply\(this,\s*arguments\)/;
21+
export const INHERITED_CLASS = /^class\s+[A-Za-z\d$_]*\s*extends\s+[A-Za-z\d$_]+\s*{/;
22+
export const INHERITED_CLASS_WITH_CTOR =
23+
/^class\s+[A-Za-z\d$_]*\s*extends\s+[A-Za-z\d$_]+\s*{[\s\S]*constructor\s*\(/;
2124

2225
export class ReflectionCapabilities implements PlatformReflectionCapabilities {
2326
private _reflect: any;
@@ -57,14 +60,16 @@ export class ReflectionCapabilities implements PlatformReflectionCapabilities {
5760
}
5861

5962
private _ownParameters(type: Type<any>, parentCtor: any): any[][]|null {
63+
const typeStr = type.toString();
6064
// If we have no decorators, we only have function.length as metadata.
6165
// In that case, to detect whether a child class declared an own constructor or not,
6266
// we need to look inside of that constructor to check whether it is
6367
// just calling the parent.
6468
// This also helps to work around for https://github.com/Microsoft/TypeScript/issues/12439
6569
// that sets 'design:paramtypes' to []
6670
// if a class inherits from another class but has no ctor declared itself.
67-
if (DELEGATE_CTOR.exec(type.toString())) {
71+
if (DELEGATE_CTOR.exec(typeStr) ||
72+
(INHERITED_CLASS.exec(typeStr) && !INHERITED_CLASS_WITH_CTOR.exec(typeStr))) {
6873
return null;
6974
}
7075

Diff for: packages/core/test/reflection/reflector_spec.ts

+36-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {Reflector} from '@angular/core/src/reflection/reflection';
10-
import {DELEGATE_CTOR, ReflectionCapabilities} from '@angular/core/src/reflection/reflection_capabilities';
10+
import {DELEGATE_CTOR, INHERITED_CLASS, INHERITED_CLASS_WITH_CTOR, ReflectionCapabilities} from '@angular/core/src/reflection/reflection_capabilities';
1111
import {global} from '@angular/core/src/util';
1212
import {makeDecorator, makeParamDecorator, makePropDecorator} from '@angular/core/src/util/decorators';
1313

@@ -188,6 +188,41 @@ class TestObj {
188188
Object.defineProperty(dummyArrowFn, 'prototype', {value: undefined});
189189
expect(() => reflector.annotations(dummyArrowFn as any)).not.toThrow();
190190
});
191+
192+
it('should support native class', () => {
193+
const ChildNoCtor = `class ChildNoCtor extends Parent {}\n`;
194+
const ChildWithCtor = `class ChildWithCtor extends Parent {\n` +
195+
` constructor() { super(); }` +
196+
`}\n`;
197+
const ChildNoCtorPrivateProps = `class ChildNoCtorPrivateProps extends Parent {\n` +
198+
` private x = 10;\n` +
199+
`}\n`;
200+
201+
const checkNoOwnMetadata = (str: string) =>
202+
INHERITED_CLASS.exec(str) && !INHERITED_CLASS_WITH_CTOR.exec(str);
203+
204+
expect(checkNoOwnMetadata(ChildNoCtor)).toBeTruthy();
205+
expect(checkNoOwnMetadata(ChildNoCtorPrivateProps)).toBeTruthy();
206+
expect(checkNoOwnMetadata(ChildWithCtor)).toBeFalsy();
207+
});
208+
209+
it('should properly handle all class forms', () => {
210+
const ctor = (str: string) => expect(INHERITED_CLASS.exec(str)).toBeTruthy() &&
211+
expect(INHERITED_CLASS_WITH_CTOR.exec(str)).toBeTruthy();
212+
const noCtor = (str: string) => expect(INHERITED_CLASS.exec(str)).toBeTruthy() &&
213+
expect(INHERITED_CLASS_WITH_CTOR.exec(str)).toBeFalsy();
214+
215+
ctor(`class Bar extends Foo {constructor(){}}`);
216+
ctor(`class Bar extends Foo { constructor ( ) {} }`);
217+
ctor(`class Bar extends Foo { other(){}; constructor(){} }`);
218+
219+
noCtor(`class extends Foo{}`);
220+
noCtor(`class extends Foo {}`);
221+
noCtor(`class Bar extends Foo {}`);
222+
noCtor(`class $Bar1_ extends $Fo0_ {}`);
223+
noCtor(`class Bar extends Foo { other(){} }`);
224+
});
225+
191226
});
192227

193228
describe('inheritance with decorators', () => {

0 commit comments

Comments
 (0)