Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add standalone function deprecation helper #16782

Merged
merged 1 commit into from Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/common/api/deprecate.js
Expand Up @@ -35,6 +35,14 @@ const deprecate = {
}
},

function: (fn, newName) => {
const warn = warnOnce(fn.name, newName)
return function () {
warn()
fn.apply(this, arguments)
}
},

event: (emitter, oldName, newName) => {
const warn = newName.startsWith('-') /* internal event */
? warnOnce(`${oldName} event`)
Expand Down
32 changes: 29 additions & 3 deletions spec/api-deprecations-spec.js
Expand Up @@ -82,9 +82,35 @@ describe('deprecations', () => {
expect(msg).to.include(prop)
})

it('warns exactly once when a function is deprecated with no replacement', () => {
let msg
deprecations.setHandler(m => { msg = m })

function oldFn () { return 'hello' }
const deprecatedFn = deprecate.function(oldFn)
deprecatedFn()

expect(msg).to.be.a('string')
expect(msg).to.include('oldFn')
})

it('warns exactly once when a function is deprecated with a replacement', () => {
let msg
deprecations.setHandler(m => { msg = m })

function oldFn () { return 'hello' }
function newFn () { return 'goodbye' }
const deprecatedFn = deprecate.function(oldFn, newFn)
deprecatedFn()

expect(msg).to.be.a('string')
expect(msg).to.include('oldFn')
expect(msg).to.include('newFn')
})

it('warns only once per item', () => {
const messages = []
deprecations.setHandler(message => { messages.push(message) })
deprecations.setHandler(message => messages.push(message))

const key = 'foo'
const val = 'bar'
Expand Down Expand Up @@ -125,15 +151,15 @@ describe('deprecations', () => {

const enableCallbackWarnings = () => {
warnings = []
deprecations.setHandler(warning => { warnings.push(warning) })
deprecations.setHandler(warning => warnings.push(warning))
process.enablePromiseAPIs = true
}

beforeEach(() => {
deprecations.setHandler(null)
process.throwDeprecation = true

promiseFunc = param => new Promise((resolve, reject) => { resolve(param) })
promiseFunc = param => new Promise((resolve, reject) => resolve(param))
})

it('acts as a pass-through for promise-based invocations', async () => {
Expand Down