From 68a64af5b3706b08ccbb32b563d6ab2210d7310f Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Fri, 17 Jul 2020 20:19:52 -0230 Subject: [PATCH] Fix callback-return issues (#8996) See [`callback-return`](https://eslint.org/docs/rules/callback-return) for more information. This change enables `callback-return` and fixes the resulting issues. --- .eslintrc.js | 1 + .../permissions/methodMiddleware.js | 1 + app/scripts/controllers/preferences.js | 8 ++++--- app/scripts/metamask-controller.js | 23 +++++++++++++++++++ app/scripts/platforms/extension.js | 1 + test/e2e/helpers.js | 1 + .../controllers/metamask-controller-test.js | 12 +++++----- 7 files changed, 38 insertions(+), 9 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 4baffd5d1372..e1b8818029e6 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -43,6 +43,7 @@ module.exports = { rules: { /* TODO: Remove these when upgrading to `@metamask/eslint-config@2` */ 'array-callback-return': 'error', + 'callback-return': 'error', /* End v2 rules */ 'arrow-parens': 'error', 'no-tabs': 'error', diff --git a/app/scripts/controllers/permissions/methodMiddleware.js b/app/scripts/controllers/permissions/methodMiddleware.js index b57585acd8d6..613ea3876df2 100644 --- a/app/scripts/controllers/permissions/methodMiddleware.js +++ b/app/scripts/controllers/permissions/methodMiddleware.js @@ -107,6 +107,7 @@ export default function createMethodMiddleware ({ } // when this promise resolves, the response is on its way back + // eslint-disable-next-line callback-return await next() if (responseHandler) { diff --git a/app/scripts/controllers/preferences.js b/app/scripts/controllers/preferences.js index 0a2c0bbe3fdf..855a07c0c30c 100644 --- a/app/scripts/controllers/preferences.js +++ b/app/scripts/controllers/preferences.js @@ -206,13 +206,15 @@ export default class PreferencesController { res.result = result end() } - break + return default: end(new Error(`Asset of type ${type} not supported`)) + return } - } else { - next() } + + next() + return } /** diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 396873d3618e..738cd5c9a684 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1119,6 +1119,7 @@ export default class MetamaskController extends EventEmitter { messageManager.rejectMsg(msgId) if (cb && typeof cb === 'function') { cb(null, this.getState()) + return } } @@ -1177,6 +1178,7 @@ export default class MetamaskController extends EventEmitter { messageManager.rejectMsg(msgId) if (cb && typeof cb === 'function') { cb(null, this.getState()) + return } } @@ -1261,6 +1263,7 @@ export default class MetamaskController extends EventEmitter { messageManager.rejectMsg(msgId) if (cb && typeof cb === 'function') { cb(null, this.getState()) + return } } @@ -1318,6 +1321,7 @@ export default class MetamaskController extends EventEmitter { messageManager.rejectMsg(msgId) if (cb && typeof cb === 'function') { cb(null, this.getState()) + return } } @@ -1377,6 +1381,7 @@ export default class MetamaskController extends EventEmitter { messageManager.rejectMsg(msgId) if (cb && typeof cb === 'function') { cb(null, this.getState()) + return } } @@ -1843,8 +1848,10 @@ export default class MetamaskController extends EventEmitter { this.currencyRateController.update(currencyState) this.currencyRateController.configure(currencyState) cb(null, this.currencyRateController.state) + return } catch (err) { cb(err) + return } } @@ -1925,8 +1932,10 @@ export default class MetamaskController extends EventEmitter { try { this.preferencesController.setUseBlockie(val) cb(null) + return } catch (err) { cb(err) + return } } @@ -1939,8 +1948,10 @@ export default class MetamaskController extends EventEmitter { try { this.preferencesController.setUseNonceField(val) cb(null) + return } catch (err) { cb(err) + return } } @@ -1953,8 +1964,10 @@ export default class MetamaskController extends EventEmitter { try { this.preferencesController.setUsePhishDetect(val) cb(null) + return } catch (err) { cb(err) + return } } @@ -1967,8 +1980,10 @@ export default class MetamaskController extends EventEmitter { try { this.preferencesController.setIpfsGateway(val) cb(null) + return } catch (err) { cb(err) + return } } @@ -1981,8 +1996,10 @@ export default class MetamaskController extends EventEmitter { try { const metaMetricsId = this.preferencesController.setParticipateInMetaMetrics(bool) cb(null, metaMetricsId) + return } catch (err) { cb(err) + return } } @@ -1990,8 +2007,10 @@ export default class MetamaskController extends EventEmitter { try { this.preferencesController.setMetaMetricsSendCount(val) cb(null) + return } catch (err) { cb(err) + return } } @@ -2004,8 +2023,10 @@ export default class MetamaskController extends EventEmitter { try { this.preferencesController.setFirstTimeFlowType(type) cb(null) + return } catch (err) { cb(err) + return } } @@ -2019,8 +2040,10 @@ export default class MetamaskController extends EventEmitter { try { const direction = this.preferencesController.setCurrentLocale(key) cb(null, direction) + return } catch (err) { cb(err) + return } } diff --git a/app/scripts/platforms/extension.js b/app/scripts/platforms/extension.js index 0bf94dac06db..f62ef98e4fda 100644 --- a/app/scripts/platforms/extension.js +++ b/app/scripts/platforms/extension.js @@ -105,6 +105,7 @@ export default class ExtensionPlatform { }) } catch (e) { cb(e) + return } } diff --git a/test/e2e/helpers.js b/test/e2e/helpers.js index f7954ffd7044..c9bc3642eaa2 100644 --- a/test/e2e/helpers.js +++ b/test/e2e/helpers.js @@ -33,6 +33,7 @@ async function withFixtures (options, callback) { const { driver } = await buildWebDriver(driverOptions) webDriver = driver + // eslint-disable-next-line callback-return await callback({ driver, }) diff --git a/test/unit/app/controllers/metamask-controller-test.js b/test/unit/app/controllers/metamask-controller-test.js index e3c4d2618582..746b098d35f2 100644 --- a/test/unit/app/controllers/metamask-controller-test.js +++ b/test/unit/app/controllers/metamask-controller-test.js @@ -53,9 +53,9 @@ const createLoggerMiddlewareMock = () => (req, res, next) => { loggerMiddlewareMock.responses.push(res) cb() }) - } else { - next() + return } + next() } const MetaMaskController = proxyquire('../../../../app/scripts/metamask-controller', { @@ -839,9 +839,9 @@ describe('MetaMaskController', function () { const streamTest = createThoughStream((chunk, _, cb) => { if (chunk.data && chunk.data.method) { cb(null, chunk) - } else { - cb() + return } + cb() }) metamaskController.setupUntrustedCommunication(streamTest, messageSender) @@ -877,9 +877,9 @@ describe('MetaMaskController', function () { const streamTest = createThoughStream((chunk, _, cb) => { if (chunk.data && chunk.data.method) { cb(null, chunk) - } else { - cb() + return } + cb() }) metamaskController.setupUntrustedCommunication(streamTest, messageSender)