From 64a3906571ae2d71c97d7765976b9c4b4f6bbcbe Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 25 Feb 2021 18:28:55 +0800 Subject: [PATCH] Enable `unicorn/prefer-optional-catch-binding` (#9464) --- .eslintrc.yml | 1 + scripts/build/build.js | 2 +- scripts/build/bundler.js | 11 +++++++---- scripts/build/cache.js | 4 ++-- src/cli/format.js | 2 +- src/common/load-plugins.js | 4 ++-- src/language-css/parser-postcss.js | 6 +++--- src/language-html/conditional-comment.js | 2 +- src/language-html/printer-html.js | 2 +- src/main/options.js | 6 +++--- src/main/parser.js | 2 +- website/playground/storage.js | 4 ++-- website/playground/urlHash.js | 2 +- website/static/worker.js | 2 +- 14 files changed, 27 insertions(+), 23 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 6d95d39be..c10450533 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -73,6 +73,7 @@ rules: unicorn/no-useless-undefined: error unicorn/prefer-includes: error unicorn/prefer-number-properties: error + unicorn/prefer-optional-catch-binding: error unicorn/prefer-regexp-test: error unicorn/prefer-spread: error unicorn/prefer-string-slice: error diff --git a/scripts/build/build.js b/scripts/build/build.js index ccd0e6517..886a1049e 100644 --- a/scripts/build/build.js +++ b/scripts/build/build.js @@ -127,7 +127,7 @@ async function cacheFiles(cache) { path.join(".cache", file.replace("dist", "files")), ]); } - } catch (err) { + } catch { // Don't fail the build } } diff --git a/scripts/build/bundler.js b/scripts/build/bundler.js index 08d66c1e1..e6095245d 100644 --- a/scripts/build/bundler.js +++ b/scripts/build/bundler.js @@ -110,10 +110,13 @@ function getBabelConfig(bundle) { }, ], ]; - config.plugins.push([ - require.resolve("@babel/plugin-proposal-object-rest-spread"), - { loose: true, useBuiltIns: true }, - ]); + config.plugins.push( + [ + require.resolve("@babel/plugin-proposal-object-rest-spread"), + { loose: true, useBuiltIns: true }, + ], + require.resolve("@babel/plugin-proposal-optional-catch-binding") + ); return config; } diff --git a/scripts/build/cache.js b/scripts/build/cache.js index 8f7b5c961..ac37701ea 100644 --- a/scripts/build/cache.js +++ b/scripts/build/cache.js @@ -48,7 +48,7 @@ class Cache { for (const files of Object.values(this.files)) { assert.ok(Array.isArray(files)); } - } catch (err) { + } catch { this.checksums = {}; this.files = {}; } @@ -97,7 +97,7 @@ class Cache { async save() { try { await fs.writeFile(this.manifest, JSON.stringify(this.updated, null, 2)); - } catch (err) { + } catch { // Don't fail the build } } diff --git a/src/cli/format.js b/src/cli/format.js index e611408cb..e9aef8ebb 100644 --- a/src/cli/format.js +++ b/src/cli/format.js @@ -165,7 +165,7 @@ function format(context, input, opt) { let benchmark; try { benchmark = eval("require")("benchmark"); - } catch (err) { + } catch { context.logger.debug( "'--debug-benchmark' requires the 'benchmark' package to be installed." ); diff --git a/src/common/load-plugins.js b/src/common/load-plugins.js index e94b1884b..6f0f3a527 100644 --- a/src/common/load-plugins.js +++ b/src/common/load-plugins.js @@ -45,7 +45,7 @@ function load(plugins, pluginSearchDirs) { try { // try local files requirePath = resolve(path.resolve(process.cwd(), pluginName)); - } catch (_) { + } catch { // try node modules requirePath = resolve(pluginName, { paths: [process.cwd()] }); } @@ -120,7 +120,7 @@ function findPluginsInNodeModules(nodeModulesDir) { function isDirectory(dir) { try { return fs.statSync(dir).isDirectory(); - } catch (e) { + } catch { return false; } } diff --git a/src/language-css/parser-postcss.js b/src/language-css/parser-postcss.js index 0a97be293..0858e7f3e 100644 --- a/src/language-css/parser-postcss.js +++ b/src/language-css/parser-postcss.js @@ -213,7 +213,7 @@ function parseValue(value, options) { try { result = valueParser(value, { loose: true }).parse(); - } catch (e) { + } catch { return { type: "value-unknown", value, @@ -247,7 +247,7 @@ function parseSelector(selector) { selectorParser((result_) => { result = result_; }).process(selector); - } catch (e) { + } catch { // Fail silently. It's better to print it as is than to try and parse it // Note: A common failure is for SCSS nested properties. `background: // none { color: red; }` is parsed as a NestedDeclaration by @@ -270,7 +270,7 @@ function parseMediaQuery(params) { try { result = mediaParser(params); - } catch (e) { + } catch { // Ignore bad media queries /* istanbul ignore next */ return { diff --git a/src/language-html/conditional-comment.js b/src/language-html/conditional-comment.js index 228076446..0bb2ce47a 100644 --- a/src/language-html/conditional-comment.js +++ b/src/language-html/conditional-comment.js @@ -39,7 +39,7 @@ function parseIeConditionalStartEndComment(node, parseHtml, match) { const [complete, children] = (() => { try { return [true, parseHtml(data, contentStartSpan).children]; - } catch (e) { + } catch { const text = { type: "text", value: data, diff --git a/src/language-html/printer-html.js b/src/language-html/printer-html.js index 2f1ac6cf7..f6efb5b72 100644 --- a/src/language-html/printer-html.js +++ b/src/language-html/printer-html.js @@ -1172,7 +1172,7 @@ function printEmbeddedAttributeValue(node, originalTextToDoc, options) { "}}", ]) ); - } catch (e) { + } catch { parts.push("{{", replaceEndOfLineWith(part, literalline), "}}"); } } diff --git a/src/main/options.js b/src/main/options.js index afb89be8d..43a1d228e 100644 --- a/src/main/options.js +++ b/src/main/options.js @@ -128,7 +128,7 @@ function getInterpreter(filepath) { let fd; try { fd = fs.openSync(filepath, "r"); - } catch (err) { + } catch { // istanbul ignore next return ""; } @@ -149,7 +149,7 @@ function getInterpreter(filepath) { return m2[1]; } return ""; - } catch (err) { + } catch { // There are some weird cases where paths are missing, causing Jest // failures. It's unclear what these correspond to in the real world. /* istanbul ignore next */ @@ -159,7 +159,7 @@ function getInterpreter(filepath) { // There are some weird cases where paths are missing, causing Jest // failures. It's unclear what these correspond to in the real world. fs.closeSync(fd); - } catch (err) { + } catch { // nop } } diff --git a/src/main/parser.js b/src/main/parser.js index 65b31bc39..499e1c4ad 100644 --- a/src/main/parser.js +++ b/src/main/parser.js @@ -57,7 +57,7 @@ function resolveParser(opts, parsers = getParsers(opts)) { locStart, locEnd, }; - } catch (err) { + } catch { /* istanbul ignore next */ throw new ConfigError(`Couldn't resolve parser "${opts.parser}"`); } diff --git a/website/playground/storage.js b/website/playground/storage.js index df4c96c73..a56957389 100644 --- a/website/playground/storage.js +++ b/website/playground/storage.js @@ -1,7 +1,7 @@ export function get(key) { try { return JSON.parse(window.localStorage.getItem(key)); - } catch (_) { + } catch { // noop } } @@ -9,7 +9,7 @@ export function get(key) { export function set(key, value) { try { window.localStorage.setItem(key, JSON.stringify(value)); - } catch (_) { + } catch { // noop } } diff --git a/website/playground/urlHash.js b/website/playground/urlHash.js index b37cd4e6b..892f9a9cc 100644 --- a/website/playground/urlHash.js +++ b/website/playground/urlHash.js @@ -13,7 +13,7 @@ export function read() { try { return JSON.parse(decode(hash)); - } catch (_) { + } catch { return {}; } } diff --git a/website/static/worker.js b/website/static/worker.js index 8b06bd8ef..5af519105 100644 --- a/website/static/worker.js +++ b/website/static/worker.js @@ -96,7 +96,7 @@ function handleMessage(message) { if (!errored) { try { ast = formatCode(ast, { parser: "json", plugins }).formatted; - } catch (e) { + } catch { ast = serializeAst(ast); } }