Skip to content

Commit

Permalink
feat: advanced deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
pooya parsa committed Feb 25, 2020
1 parent 75d900a commit 5b88628
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
21 changes: 17 additions & 4 deletions src/hable.js
Expand Up @@ -16,11 +16,24 @@ export default class Hookable {
return
}

if (this._deprecatedHooks[name]) {
if (process.env.NODE_ENV !== 'production') {
this._logger.warn(`${name} hook has been deprecated, please use ${this._deprecatedHooks[name]}`)
const originalName = name
let deprecatedHook
while (this._deprecatedHooks[name]) {
deprecatedHook = this._deprecatedHooks[name]
if (typeof deprecatedHook === 'string') {
deprecatedHook = { to: deprecatedHook }
}
name = deprecatedHook.newName
}
if (deprecatedHook) {
if (!deprecatedHook.message) {
this._logger.warn(
`${originalName} hook has been deprecated` +
(deprecatedHook.to ? `, please use ${deprecatedHook.to}` : '')
)
} else {
this._logger.warn(deprecatedHook.message)
}
name = this._deprecatedHooks[name]
}

this._hooks[name] = this._hooks[name] || []
Expand Down
24 changes: 16 additions & 8 deletions test/hookable.test.js
Expand Up @@ -46,15 +46,23 @@ describe('core: hookable', () => {
expect(hook._hooks['test:hook']).toBeUndefined()
})

test('should convert and display deprecated hook', () => {
test('should convert and display deprecated hooks', () => {
const hook = new Hookable()
hook.deprecateHook('test:hook', 'test:before')

hook.hook('test:hook', () => { })

expect(console.warn).toBeCalledWith('test:hook hook has been deprecated, please use test:before')
expect(hook._hooks['test:hook']).toBeUndefined()
expect(hook._hooks['test:before']).toEqual([expect.any(Function)])
hook.deprecateHook('a', 'b')
hook.deprecateHook('b', { to: 'c' })
hook.deprecateHook('x', { to: 'y', message: 'Custom' })

hook.hook('a', () => { })
hook.hook('b', () => { })
hook.hook('c', () => { })
hook.hook('x', () => { })

expect(console.warn).toBeCalledWith('a hook has been deprecated, please use c')
expect(console.warn).toBeCalledWith('b hook has been deprecated, please use c')
expect(console.warn).toBeCalledWith('Custom')
expect(hook._hooks.a).toBeUndefined()
expect(hook._hooks.b).toBeUndefined()
expect(hook._hooks.c).toEqual([expect.any(Function), expect.any(Function), expect.any(Function)])
})

test('deprecateHooks', () => {
Expand Down

0 comments on commit 5b88628

Please sign in to comment.