Skip to content

Commit

Permalink
make deprecations consistent and DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Feb 21, 2022
1 parent ba00e7f commit e2fd37f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
30 changes: 15 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@ const perf = typeof performance === 'object' && performance &&
typeof performance.now === 'function' ? performance : Date

const warned = new Set()
const deprecatedOption = (opt, msg) => {
const deprecatedOption = (opt, instead) => {
const code = `LRU_CACHE_OPTION_${opt}`
if (shouldWarn(code)) {
warn(code, `The ${opt} option is deprecated. ${msg}`, LRUCache)
warn(code, `${opt} option`, `options.${instead}`, LRUCache)
}
}
const deprecatedMethod = (method, msg) => {
const deprecatedMethod = (method, instead) => {
const code = `LRU_CACHE_METHOD_${method}`
if (shouldWarn(code)) {
const { prototype } = LRUCache
const { get } = Object.getOwnPropertyDescriptor(prototype, method)
warn(code, `The ${method} method is deprecated. ${msg}`, get)
warn(code, `${method} method`, `cache.${instead}()`, get)
}
}
const deprecatedProperty = (field, msg) => {
const deprecatedProperty = (field, instead) => {
const code = `LRU_CACHE_PROPERTY_${field}`
if (shouldWarn(code)) {
const { prototype } = LRUCache
const { get } = Object.getOwnPropertyDescriptor(prototype, field)
warn(code, `The ${field} property is deprecated. ${msg}`, get)
warn(code, `${field} property`, `cache.${instead}`, get)
}
}
const shouldWarn = (code) => !(process.noDeprecation || warned.has(code))
const warn = (code, msg, fn) => {
const warn = (code, what, instead, fn) => {
warned.add(code)
process.emitWarning(msg, 'DeprecationWarning', code, fn)
process.emitWarning(`The ${what} is deprecated. Please use ${instead} instead.`, 'DeprecationWarning', code, fn)
}

const isPosInt = n => n && n === Math.floor(n) && n > 0 && isFinite(n)
Expand Down Expand Up @@ -156,13 +156,13 @@ class LRUCache {
}

if (stale) {
deprecatedOption('stale', 'please use options.allowStale instead')
deprecatedOption('stale', 'allowStale')
}
if (maxAge) {
deprecatedOption('maxAge', 'please use options.ttl instead')
deprecatedOption('maxAge', 'ttl')
}
if (length) {
deprecatedOption('length', 'please use options.sizeCalculation instead')
deprecatedOption('length', 'sizeCalculation')
}
}

Expand Down Expand Up @@ -305,7 +305,7 @@ class LRUCache {
}

get prune () {
deprecatedMethod('prune', 'Please use cache.purgeStale() instead.')
deprecatedMethod('prune', 'purgeStale')
return this.purgeStale
}

Expand Down Expand Up @@ -493,7 +493,7 @@ class LRUCache {
}

get del () {
deprecatedMethod('del', 'Please use cache.delete() instead.')
deprecatedMethod('del', 'delete')
return this.delete
}
delete (k) {
Expand Down Expand Up @@ -568,12 +568,12 @@ class LRUCache {
}
}
get reset () {
deprecatedMethod('reset', 'Please use cache.clear() instead.')
deprecatedMethod('reset', 'clear')
return this.clear
}

get length () {
deprecatedProperty('length', 'Please use cache.size instead.')
deprecatedProperty('length', 'size')
return this.size
}
}
Expand Down
6 changes: 3 additions & 3 deletions tap-snapshots/test/deprecations.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
exports[`test/deprecations.js TAP > must match snapshot 1`] = `
Array [
Array [
"The stale option is deprecated. please use options.allowStale instead",
"The stale option is deprecated. Please use options.allowStale instead.",
"DeprecationWarning",
"LRU_CACHE_OPTION_stale",
Function LRUCache(classLRUCache),
],
Array [
"The maxAge option is deprecated. please use options.ttl instead",
"The maxAge option is deprecated. Please use options.ttl instead.",
"DeprecationWarning",
"LRU_CACHE_OPTION_maxAge",
Function LRUCache(classLRUCache),
],
Array [
"The length option is deprecated. please use options.sizeCalculation instead",
"The length option is deprecated. Please use options.sizeCalculation instead.",
"DeprecationWarning",
"LRU_CACHE_OPTION_length",
Function LRUCache(classLRUCache),
Expand Down

0 comments on commit e2fd37f

Please sign in to comment.