Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit e422fb1

Browse files
JiaLiPassionmhevery
authored andcommitted
fix(patch): fix #707, should not try to patch non configurable property (#717)
1 parent 160531b commit e422fb1

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

lib/common/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ export const isMix: boolean = typeof process !== 'undefined' &&
6161

6262
export function patchProperty(obj: any, prop: string) {
6363
const desc = Object.getOwnPropertyDescriptor(obj, prop) || {enumerable: true, configurable: true};
64+
// if the descriptor is not configurable
65+
// just return
66+
if (!desc.configurable) {
67+
return;
68+
}
6469

6570
const originalDesc = Object.getOwnPropertyDescriptor(obj, 'original' + prop);
6671
if (!originalDesc && desc.get) {

test/common/util.spec.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {patchMethod, zoneSymbol} from '../../lib/common/utils';
9+
import {patchMethod, patchProperty, zoneSymbol} from '../../lib/common/utils';
1010

1111
describe('utils', function() {
1212

@@ -61,6 +61,25 @@ describe('utils', function() {
6161
expect(pMethod).toBe(Type.prototype.method);
6262
});
6363

64+
it('should not patch property which is not configurable', () => {
65+
const TestType = function() {};
66+
const originalDefineProperty = (Object as any)[zoneSymbol('defineProperty')];
67+
if (originalDefineProperty) {
68+
originalDefineProperty(
69+
TestType.prototype, 'nonConfigurableProperty',
70+
{configurable: false, writable: true, value: 'test'});
71+
} else {
72+
Object.defineProperty(
73+
TestType.prototype, 'nonConfigurableProperty',
74+
{configurable: false, writable: true, value: 'test'});
75+
}
76+
patchProperty(TestType.prototype, 'nonConfigurableProperty');
77+
const desc = Object.getOwnPropertyDescriptor(TestType.prototype, 'nonConfigurableProperty');
78+
expect(desc.writable).toBeTruthy();
79+
expect(!desc.get).toBeTruthy();
80+
});
81+
82+
6483
it('should have a method name in the stacktrace', () => {
6584
const fn = function someOtherName() {
6685
throw new Error('MyError');

0 commit comments

Comments
 (0)