From 54b70a7f2beebddf5d7fdc0428808393aa975c26 Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman Date: Sat, 1 Feb 2020 11:04:38 -0500 Subject: [PATCH] Use optional catch bindings --- lib/copy/__tests__/copy-permissions.test.js | 4 ++-- lib/empty/index.js | 2 +- lib/ensure/file.js | 2 +- lib/ensure/symlink-type.js | 2 +- lib/mkdirs/__tests__/issue-209.test.js | 3 +-- lib/mkdirs/mkdirs-sync.js | 12 ++++++------ lib/move-sync/__tests__/move-sync.test.js | 2 +- lib/move/__tests__/move.test.js | 2 +- lib/remove/rimraf.js | 2 +- lib/util/buffer.js | 2 +- 10 files changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/copy/__tests__/copy-permissions.test.js b/lib/copy/__tests__/copy-permissions.test.js index f14bf023..dafa7d05 100644 --- a/lib/copy/__tests__/copy-permissions.test.js +++ b/lib/copy/__tests__/copy-permissions.test.js @@ -32,13 +32,13 @@ describe('copy', () => { try { gidWheel = process.getgid() // userid.gid('wheel') - } catch (err) { + } catch { gidWheel = process.getgid() } try { gidStaff = process.getgid() // userid.gid('staff') - } catch (err) { + } catch { gidStaff = process.getgid() } diff --git a/lib/empty/index.js b/lib/empty/index.js index 204d53ba..90fb4699 100644 --- a/lib/empty/index.js +++ b/lib/empty/index.js @@ -30,7 +30,7 @@ function emptyDirSync (dir) { let items try { items = fs.readdirSync(dir) - } catch (err) { + } catch { return mkdir.mkdirsSync(dir) } diff --git a/lib/ensure/file.js b/lib/ensure/file.js index b8b2495c..15cc473c 100644 --- a/lib/ensure/file.js +++ b/lib/ensure/file.js @@ -44,7 +44,7 @@ function createFileSync (file) { let stats try { stats = fs.statSync(file) - } catch (e) {} + } catch {} if (stats && stats.isFile()) return const dir = path.dirname(file) diff --git a/lib/ensure/symlink-type.js b/lib/ensure/symlink-type.js index 4f8787c2..42dc0ce7 100644 --- a/lib/ensure/symlink-type.js +++ b/lib/ensure/symlink-type.js @@ -19,7 +19,7 @@ function symlinkTypeSync (srcpath, type) { if (type) return type try { stats = fs.lstatSync(srcpath) - } catch (e) { + } catch { return 'file' } return (stats && stats.isDirectory()) ? 'dir' : 'file' diff --git a/lib/mkdirs/__tests__/issue-209.test.js b/lib/mkdirs/__tests__/issue-209.test.js index 7b16b816..bfe40031 100644 --- a/lib/mkdirs/__tests__/issue-209.test.js +++ b/lib/mkdirs/__tests__/issue-209.test.js @@ -30,8 +30,7 @@ describe('mkdirp: issue-209, win32, when bad path, should return a cleaner error try { const file = 'c:\\tmp\foo:moo' fse.mkdirpSync(file) - } catch (err) { - // console.error(err) + } catch { didErr = true } assert(didErr) diff --git a/lib/mkdirs/mkdirs-sync.js b/lib/mkdirs/mkdirs-sync.js index 25d794e2..decba61a 100644 --- a/lib/mkdirs/mkdirs-sync.js +++ b/lib/mkdirs/mkdirs-sync.js @@ -28,9 +28,9 @@ function mkdirsSync (p, opts, made) { try { xfs.mkdirSync(p, mode) made = made || p - } catch (err0) { - if (err0.code === 'ENOENT') { - if (path.dirname(p) === p) throw err0 + } catch (err) { + if (err.code === 'ENOENT') { + if (path.dirname(p) === p) throw err made = mkdirsSync(path.dirname(p), opts, made) mkdirsSync(p, opts, made) } else { @@ -39,10 +39,10 @@ function mkdirsSync (p, opts, made) { let stat try { stat = xfs.statSync(p) - } catch (err1) { - throw err0 + } catch { + throw err } - if (!stat.isDirectory()) throw err0 + if (!stat.isDirectory()) throw err } } diff --git a/lib/move-sync/__tests__/move-sync.test.js b/lib/move-sync/__tests__/move-sync.test.js index 8ba0ede3..dca83aab 100644 --- a/lib/move-sync/__tests__/move-sync.test.js +++ b/lib/move-sync/__tests__/move-sync.test.js @@ -281,7 +281,7 @@ describe('moveSync()', () => { // make sure we have permission on device try { fs.writeFileSync(path.join(differentDevice, 'file'), 'hi') - } catch (err) { + } catch { console.log("Can't write to device. Skipping moveSync test.") __skipTests = true } diff --git a/lib/move/__tests__/move.test.js b/lib/move/__tests__/move.test.js index cb39b373..af41b93f 100644 --- a/lib/move/__tests__/move.test.js +++ b/lib/move/__tests__/move.test.js @@ -324,7 +324,7 @@ describe('+ move()', () => { // make sure we have permission on device try { fs.writeFileSync(path.join(differentDevice, 'file'), 'hi') - } catch (err) { + } catch { console.log("Can't write to device. Skipping move test.") __skipTests = true } diff --git a/lib/remove/rimraf.js b/lib/remove/rimraf.js index f287e4e1..1e44c128 100644 --- a/lib/remove/rimraf.js +++ b/lib/remove/rimraf.js @@ -302,7 +302,7 @@ function rmkidsSync (p, options) { try { const ret = options.rmdirSync(p, options) return ret - } catch (er) { } + } catch {} } while (Date.now() - startTime < 500) // give up after 500ms } else { const ret = options.rmdirSync(p, options) diff --git a/lib/util/buffer.js b/lib/util/buffer.js index dabf2885..352164da 100644 --- a/lib/util/buffer.js +++ b/lib/util/buffer.js @@ -4,7 +4,7 @@ module.exports = function (size) { if (typeof Buffer.allocUnsafe === 'function') { try { return Buffer.allocUnsafe(size) - } catch (e) { + } catch { return new Buffer(size) } }