Skip to content

Commit 9eefc32

Browse files
committed
Initialize descriptor without always setting 'writable'.
1 parent 9fa770b commit 9eefc32

File tree

2 files changed

+94
-11
lines changed

2 files changed

+94
-11
lines changed

src/index.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,14 @@ const buildInitializerDefineProperty = template(`
4545

4646
const buildApplyDecoratedDescriptor = template(`
4747
function NAME(target, property, decorators, descriptor, context){
48-
var desc = {
49-
enumerable: !!descriptor.enumerable,
50-
configurable: !!descriptor.configurable,
51-
};
52-
if ('value' in descriptor || 'initializer' in descriptor){
48+
var desc = {};
49+
Object.keys(descriptor).forEach(function(key){
50+
desc[key] = descriptor[key];
51+
});
52+
desc.enumerable = !!desc.enumerable;
53+
desc.configurable = !!desc.configurable;
54+
if ('value' in desc || desc.initializer){
5355
desc.writable = true;
54-
desc.initializer = descriptor.initializer;
55-
desc.value = descriptor.value;
56-
} else {
57-
desc.get = descriptor.get;
58-
desc.set = descriptor.set;
5956
}
6057
6158
desc = decorators.slice().reverse().reduce(function(desc, decorator){

test/index.js

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('decorators', function(){
3333
expect(calls).to.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
3434
});
3535

36-
it('should call descriptors in reverse order per-method', function(){
36+
it('should call decorators in reverse order per-method', function(){
3737
const calls = [];
3838
function dec(id){
3939
return function(){
@@ -639,6 +639,36 @@ describe('decorators', function(){
639639
expect(inst.prop).to.be.undefined;
640640
});
641641

642+
it('should support mutating an initialzer into an accessor', function(){
643+
function dec(target, name, descriptor){
644+
expect(target).to.be.ok;
645+
expect(name).to.eql("prop");
646+
expect(descriptor).to.be.an('object');
647+
648+
let {initializer} = descriptor;
649+
delete descriptor.initializer;
650+
delete descriptor.writable;
651+
652+
let value;
653+
descriptor.get = function(){
654+
if (initializer){
655+
value = '__' + initializer.call(this) + '__';
656+
initializer = null;
657+
}
658+
return value;
659+
};
660+
}
661+
662+
class Example {
663+
@dec
664+
prop = 3;
665+
}
666+
667+
let inst = new Example();
668+
669+
expect(inst.prop).to.eql('__3__');
670+
});
671+
642672
it('should allow returning a descriptor', function(){
643673
function dec(target, name, descriptor){
644674
expect(target).to.be.ok;
@@ -854,6 +884,34 @@ describe('decorators', function(){
854884
expect(Example.prop).to.be.undefined;
855885
});
856886

887+
it('should support mutating an initialzer into an accessor', function(){
888+
function dec(target, name, descriptor){
889+
expect(target).to.be.ok;
890+
expect(name).to.eql("prop");
891+
expect(descriptor).to.be.an('object');
892+
893+
let {initializer} = descriptor;
894+
delete descriptor.initializer;
895+
delete descriptor.writable;
896+
897+
let value;
898+
descriptor.get = function(){
899+
if (initializer){
900+
value = '__' + initializer.call(this) + '__';
901+
initializer = null;
902+
}
903+
return value;
904+
};
905+
}
906+
907+
class Example {
908+
@dec
909+
static prop = 3;
910+
}
911+
912+
expect(Example.prop).to.eql('__3__');
913+
});
914+
857915
it('should allow returning a descriptor', function(){
858916
function dec(target, name, descriptor){
859917
expect(target).to.be.ok;
@@ -1405,6 +1463,34 @@ describe('decorators', function(){
14051463
};
14061464
});
14071465

1466+
it('should support mutating an initialzer into an accessor', function(){
1467+
function dec(target, name, descriptor){
1468+
expect(target).to.be.ok;
1469+
expect(name).to.eql("prop");
1470+
expect(descriptor).to.be.an('object');
1471+
1472+
let {initializer} = descriptor;
1473+
delete descriptor.initializer;
1474+
delete descriptor.writable;
1475+
1476+
let value;
1477+
descriptor.get = function(){
1478+
if (initializer){
1479+
value = '__' + initializer.call(this) + '__';
1480+
initializer = null;
1481+
}
1482+
return value;
1483+
};
1484+
}
1485+
1486+
let inst = {
1487+
@dec
1488+
prop: 3
1489+
};
1490+
1491+
expect(inst.prop).to.eql('__3__');
1492+
});
1493+
14081494
it('should allow returning a descriptor', function(){
14091495
function dec(target, name, descriptor){
14101496
expect(target).to.be.ok;

0 commit comments

Comments
 (0)