Skip to content

Commit

Permalink
fix: fix applyAspect correctly being apply to whole class methods inc…
Browse files Browse the repository at this point in the history
…luding constructor
  • Loading branch information
k1r0s committed Dec 13, 2017
1 parent fd38797 commit a561a3d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
16 changes: 7 additions & 9 deletions src/apply-aspect.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
export function applyAspect (advicePool: any) {
export function applyAspect ({ constructor = [], ...methodAdvices }: any) {
return function (target) {
for (let key in advicePool) {
if (key === "constructor") {
advicePool[key].forEach(advice => target = advice(target))
} else {
advicePool[key].forEach(advice => {
Object.defineProperty(target.prototype, key, advice(target, key, Object.getOwnPropertyDescriptor(target.prototype, key)))
})
}
for (let key in methodAdvices) {
methodAdvices[key].forEach(advice =>
Object.defineProperty(target.prototype, key,
advice(target, key, Object.getOwnPropertyDescriptor(target.prototype, key))))
}

constructor.forEach(advice => target = advice(target))
return target
}
}
2 changes: 1 addition & 1 deletion src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function applyReflect (target, advices, methodName, keyJoinPoint, original) {
const keyBeforeInstance = generateKey(KEY_BEFORE_INSTANCE, methodName)
const keyAfterInstance = generateKey(KEY_AFTER_INSTANCE, methodName)
// this is only needed to avoid babel's TypeError https://github.com/babel/babel/issues/682
// and should be removed in the future when babel isn't no longer needed to transpile ES6 classes
// and should be removed when ES6 classes will be fully supported
const result = wrapMethod(target, methodName, keyBeforeInstance, keyAfterInstance)
result.prototype = target.prototype
return result
Expand Down
2 changes: 1 addition & 1 deletion test/aspect.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { applyAspect, beforeMethod, afterMethod } from "../src"
import { applyAspect, afterInstance, beforeInstance, beforeMethod, afterMethod } from "../src"

const methodSpy = jest.fn()

Expand Down

0 comments on commit a561a3d

Please sign in to comment.