From c4cab1f4085b456ca2d05198c8cfd610777a0427 Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Mon, 18 Jan 2021 19:39:25 +0800 Subject: [PATCH] lib: refactor to use validateBoolean MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/36983 Reviewed-By: Michaƫl Zasso Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Luigi Pinca --- lib/fs.js | 7 +++---- lib/internal/fs/promises.js | 4 ++-- lib/internal/process/report.js | 9 +++------ lib/internal/vm/module.js | 6 ++---- lib/vm.js | 5 +---- 5 files changed, 11 insertions(+), 20 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index b873593e2307a4..ff55d76d7b9fd2 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -123,6 +123,7 @@ const { const { isUint32, parseFileMode, + validateBoolean, validateBuffer, validateCallback, validateInteger, @@ -1002,8 +1003,7 @@ function mkdir(path, options, callback) { callback = makeCallback(callback); path = getValidatedPath(path); - if (typeof recursive !== 'boolean') - throw new ERR_INVALID_ARG_TYPE('options.recursive', 'boolean', recursive); + validateBoolean(recursive, 'options.recursive'); const req = new FSReqCallback(); req.oncomplete = callback; @@ -1023,8 +1023,7 @@ function mkdirSync(path, options) { mode = options.mode; } path = getValidatedPath(path); - if (typeof recursive !== 'boolean') - throw new ERR_INVALID_ARG_TYPE('options.recursive', 'boolean', recursive); + validateBoolean(recursive, 'options.recursive'); const ctx = { path }; const result = binding.mkdir(pathModule.toNamespacedPath(path), diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index d805f5ca359d05..86e6924ca2104e 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -66,6 +66,7 @@ const { const { opendir } = require('internal/fs/dir'); const { parseFileMode, + validateBoolean, validateBuffer, validateInteger, validateUint32 @@ -509,8 +510,7 @@ async function mkdir(path, options) { mode = 0o777 } = options || {}; path = getValidatedPath(path); - if (typeof recursive !== 'boolean') - throw new ERR_INVALID_ARG_TYPE('options.recursive', 'boolean', recursive); + validateBoolean(recursive, 'options.recursive'); return binding.mkdir(pathModule.toNamespacedPath(path), parseFileMode(mode, 'mode', 0o777), recursive, diff --git a/lib/internal/process/report.js b/lib/internal/process/report.js index 114e35ecf2bd06..dcb8f8ed0cd221 100644 --- a/lib/internal/process/report.js +++ b/lib/internal/process/report.js @@ -69,8 +69,7 @@ const report = { return nr.shouldReportOnFatalError(); }, set reportOnFatalError(trigger) { - if (typeof trigger !== 'boolean') - throw new ERR_INVALID_ARG_TYPE('trigger', 'boolean', trigger); + validateBoolean(trigger, 'trigger'); nr.setReportOnFatalError(trigger); }, @@ -78,8 +77,7 @@ const report = { return nr.shouldReportOnSignal(); }, set reportOnSignal(trigger) { - if (typeof trigger !== 'boolean') - throw new ERR_INVALID_ARG_TYPE('trigger', 'boolean', trigger); + validateBoolean(trigger, 'trigger'); nr.setReportOnSignal(trigger); removeSignalHandler(); @@ -89,8 +87,7 @@ const report = { return nr.shouldReportOnUncaughtException(); }, set reportOnUncaughtException(trigger) { - if (typeof trigger !== 'boolean') - throw new ERR_INVALID_ARG_TYPE('trigger', 'boolean', trigger); + validateBoolean(trigger, 'trigger'); nr.setReportOnUncaughtException(trigger); } diff --git a/lib/internal/vm/module.js b/lib/internal/vm/module.js index f847f2404f47e0..0d49aed62b05e1 100644 --- a/lib/internal/vm/module.js +++ b/lib/internal/vm/module.js @@ -39,6 +39,7 @@ const { ERR_VM_MODULE_STATUS, } = require('internal/errors').codes; const { + validateBoolean, validateInt32, validateUint32, validateString, @@ -215,10 +216,7 @@ class Module { validateUint32(timeout, 'options.timeout', true); } const { breakOnSigint = false } = options; - if (typeof breakOnSigint !== 'boolean') { - throw new ERR_INVALID_ARG_TYPE('options.breakOnSigint', 'boolean', - breakOnSigint); - } + validateBoolean(breakOnSigint, 'options.breakOnSigint'); const status = this[kWrap].getStatus(); if (status !== kInstantiated && status !== kEvaluated && diff --git a/lib/vm.js b/lib/vm.js index 79c97f3af3ff02..565b444dc697cd 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -90,10 +90,7 @@ class Script extends ContextifyScript { cachedData ); } - if (typeof produceCachedData !== 'boolean') { - throw new ERR_INVALID_ARG_TYPE('options.produceCachedData', 'boolean', - produceCachedData); - } + validateBoolean(produceCachedData, 'options.produceCachedData'); // Calling `ReThrow()` on a native TryCatch does not generate a new // abort-on-uncaught-exception check. A dummy try/catch in JS land