Skip to content

Commit ea971f7

Browse files
esugoikara
authored andcommitted
fix(core): inheritance delegate ctor regex updated to work on minified code (#36962)
If one component Parent inherit another component Base like the following: @component(...) class Base { constructor(@Inject(InjectionToken) injector: Injector) { } } @component(...) class Parent extends Base { // no constructor } When creating Component Parent, the dependency injection should work on delegating ctors like above. The code Parent code above will be compiled into something like: class Parent extends Base { constructor() { super(...arguments); } } The angular core isDelegateCtor function will identify the delegation ctor to the base class. But when the code above is minified (using terser), the minified code will compress the spaces, resulting in something like: class Parent extends Base{constructor(){super(...arguments)}} The regex will stop working, since it wasn't aware of this case. So this fix will allow this to work in minified code cases. PR Close #36962
1 parent ddaa124 commit ea971f7

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/core/src/reflection/reflection_capabilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const INHERITED_CLASS = /^class\s+[A-Za-z\d$_]*\s*extends\s+[^{]+{/;
2525
export const INHERITED_CLASS_WITH_CTOR =
2626
/^class\s+[A-Za-z\d$_]*\s*extends\s+[^{]+{[\s\S]*constructor\s*\(/;
2727
export const INHERITED_CLASS_WITH_DELEGATE_CTOR =
28-
/^class\s+[A-Za-z\d$_]*\s*extends\s+[^{]+{[\s\S]*constructor\s*\(\)\s*{\s+super\(\.\.\.arguments\)/;
28+
/^class\s+[A-Za-z\d$_]*\s*extends\s+[^{]+{[\s\S]*constructor\s*\(\)\s*{\s*super\(\.\.\.arguments\)/;
2929

3030
/**
3131
* Determine whether a stringified type is a class which delegates its constructor

packages/core/test/reflection/reflector_spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ class TestObj {
201201
expect(isDelegateCtor(ChildWithCtor.toString())).toBe(false);
202202
});
203203

204+
it('should support ES2015 classes when minified', () => {
205+
// These classes are ES2015 in minified form
206+
const ChildNoCtorMinified = 'class ChildNoCtor extends Parent{}';
207+
const ChildWithCtorMinified = 'class ChildWithCtor extends Parent{constructor(){super()}}';
208+
const ChildNoCtorPrivatePropsMinified =
209+
'class ChildNoCtorPrivateProps extends Parent{constructor(){super(...arguments);this.x=10}}';
210+
211+
expect(isDelegateCtor(ChildNoCtorMinified)).toBe(true);
212+
expect(isDelegateCtor(ChildNoCtorPrivatePropsMinified)).toBe(true);
213+
expect(isDelegateCtor(ChildWithCtorMinified)).toBe(false);
214+
});
215+
204216
it('should not throw when no prototype on type', () => {
205217
// Cannot test arrow function here due to the compilation
206218
const dummyArrowFn = function() {};

0 commit comments

Comments
 (0)