From 511f67bcb42b59c9a3a3efab8fed578db100afe1 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 25 Oct 2019 12:48:14 -0700 Subject: tools: update ESLint to 6.6.0 Update ESLint to 6.6.0 PR-URL: https://github.com/nodejs/node/pull/30123 Reviewed-By: Luigi Pinca Reviewed-By: Rich Trott Reviewed-By: Yongsheng Zhang Reviewed-By: Trivikram Kamat --- .../eslint/node_modules/onetime/index.js | 41 ++++++++++++++-------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'tools/node_modules/eslint/node_modules/onetime/index.js') diff --git a/tools/node_modules/eslint/node_modules/onetime/index.js b/tools/node_modules/eslint/node_modules/onetime/index.js index 0d76476b0b..a3f412af1b 100644 --- a/tools/node_modules/eslint/node_modules/onetime/index.js +++ b/tools/node_modules/eslint/node_modules/onetime/index.js @@ -1,39 +1,50 @@ 'use strict'; const mimicFn = require('mimic-fn'); -module.exports = (fn, opts) => { - // TODO: Remove this in v3 - if (opts === true) { - throw new TypeError('The second argument is now an options object'); - } +const calledFunctions = new WeakMap(); +const oneTime = (fn, options = {}) => { if (typeof fn !== 'function') { throw new TypeError('Expected a function'); } - opts = opts || {}; - let ret; - let called = false; - const fnName = fn.displayName || fn.name || ''; + let isCalled = false; + let callCount = 0; + const functionName = fn.displayName || fn.name || ''; - const onetime = function () { - if (called) { - if (opts.throw === true) { - throw new Error(`Function \`${fnName}\` can only be called once`); + const onetime = function (...args) { + calledFunctions.set(onetime, ++callCount); + + if (isCalled) { + if (options.throw === true) { + throw new Error(`Function \`${functionName}\` can only be called once`); } return ret; } - called = true; - ret = fn.apply(this, arguments); + isCalled = true; + ret = fn.apply(this, args); fn = null; return ret; }; mimicFn(onetime, fn); + calledFunctions.set(onetime, callCount); return onetime; }; + +module.exports = oneTime; +// TODO: Remove this for the next major release +module.exports.default = oneTime; + +module.exports.callCount = fn => { + if (!calledFunctions.has(fn)) { + throw new Error(`The given function \`${fn.name}\` is not wrapped by the \`onetime\` package`); + } + + return calledFunctions.get(fn); +}; -- cgit v1.2.3