Skip to content

Commit

Permalink
fix(bindAll): guard against getters that aren't functions
Browse files Browse the repository at this point in the history
  • Loading branch information
steelsojka committed May 19, 2017
1 parent e37e7b0 commit 08fa50b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
18 changes: 18 additions & 0 deletions src/bindAll.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,22 @@ describe('bindAll', () => {
myClass.fn2.call(null);
expect(context).to.equal(null);
});

it('should work with getters', () => {
@BindAll()
class MyClass {
get prop(): string {
return 'blorg';
}

fn() {
return this.prop;
}
}

const myClass = new MyClass();

myClass.fn.call(null);
expect(myClass.fn()).to.equal('blorg');
});
});
17 changes: 11 additions & 6 deletions src/bindAll.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import isFunction = require('lodash/isFunction');
import { copyMetadata } from './utils';

/**
Expand Down Expand Up @@ -46,12 +47,16 @@ function bindAllMethods(target: Function, instance: any, methods: string[] = [])
const descriptor = Object.getOwnPropertyDescriptor(proto, key);

if (include && key !== 'constructor' && !instance.hasOwnProperty(key)) {
Object.defineProperty(instance, key, {
configurable: true,
enumerable: descriptor.enumerable,
value: copyMetadata(instance[key].bind(instance), instance[key]),
writable: descriptor.writable
});
const value = instance[key];

if (isFunction(value)) {
Object.defineProperty(instance, key, {
configurable: true,
enumerable: descriptor.enumerable,
value: copyMetadata(value.bind(instance), value),
writable: descriptor.writable
});
}
}
}

Expand Down

0 comments on commit 08fa50b

Please sign in to comment.