From d837c7a087c815f24359531445b394f450423cd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Qu=C3=A8ze?= Date: Fri, 12 May 2017 14:54:42 +0200 Subject: [PATCH] Bug 1353542 - Add an eslint rule deprecating usage of Task.jsm in browser/ and toolkit/, r=Mossop. --- browser/.eslintrc.js | 2 ++ .../general/browser_e10s_chrome_process.js | 3 +- .../extension/configurations/Buttons.jsm | 16 +++++----- toolkit/.eslintrc.js | 2 ++ .../test/browser_thumbnails_storage.js | 32 +++++++++---------- toolkit/modules/Sqlite.jsm | 2 +- toolkit/modules/Task.jsm | 2 ++ .../modules/tests/xpcshell/test_Promise.js | 2 +- .../eslint/eslint-plugin-mozilla/lib/index.js | 2 ++ .../lib/rules/no-task.js | 31 ++++++++++++++++++ 10 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-task.js diff --git a/browser/.eslintrc.js b/browser/.eslintrc.js index 4df38ac4f9c5..856a17e0cf97 100644 --- a/browser/.eslintrc.js +++ b/browser/.eslintrc.js @@ -25,5 +25,7 @@ module.exports = { "no-mixed-spaces-and-tabs": "error", "no-shadow": "error", + + "mozilla/no-task": "error", } }; diff --git a/browser/base/content/test/general/browser_e10s_chrome_process.js b/browser/base/content/test/general/browser_e10s_chrome_process.js index 9ef5e8c1277b..4cde29298104 100644 --- a/browser/base/content/test/general/browser_e10s_chrome_process.js +++ b/browser/base/content/test/general/browser_e10s_chrome_process.js @@ -21,8 +21,7 @@ function makeTest(name, startURL, startProcessIsRemote, endURL, endProcessIsRemo is(browser.isRemoteBrowser, startProcessIsRemote, "Should be displayed in the right process"); let docLoadedPromise = waitForDocLoadComplete(); - let asyncTask = Task.async(transitionTask); - let expectSyncChange = await asyncTask(browser, endURL); + let expectSyncChange = await transitionTask(browser, endURL); if (expectSyncChange) { is(browser.isRemoteBrowser, endProcessIsRemote, "Should have switched to the right process synchronously"); } diff --git a/browser/tools/mozscreenshots/mozscreenshots/extension/configurations/Buttons.jsm b/browser/tools/mozscreenshots/mozscreenshots/extension/configurations/Buttons.jsm index 6b5f90bf6fae..d438de2c6ec1 100644 --- a/browser/tools/mozscreenshots/mozscreenshots/extension/configurations/Buttons.jsm +++ b/browser/tools/mozscreenshots/mozscreenshots/extension/configurations/Buttons.jsm @@ -19,21 +19,21 @@ this.Buttons = { configurations: { navBarButtons: { - applyConfig: Task.async(() => { + applyConfig: async () => { CustomizableUI.addWidgetToArea("screenshot-widget", CustomizableUI.AREA_NAVBAR); - }), + }, }, tabsToolbarButtons: { - applyConfig: Task.async(() => { + applyConfig: async () => { CustomizableUI.addWidgetToArea("screenshot-widget", CustomizableUI.AREA_TABSTRIP); - }), + }, }, menuPanelButtons: { - applyConfig: Task.async(() => { + applyConfig: async () => { CustomizableUI.addWidgetToArea("screenshot-widget", CustomizableUI.AREA_PANEL); - }), + }, verifyConfig() { let browserWindow = Services.wm.getMostRecentWindow("navigator:browser"); @@ -45,9 +45,9 @@ this.Buttons = { }, custPaletteButtons: { - applyConfig: Task.async(() => { + applyConfig: async () => { CustomizableUI.removeWidgetFromArea("screenshot-widget"); - }), + }, verifyConfig() { let browserWindow = Services.wm.getMostRecentWindow("navigator:browser"); diff --git a/toolkit/.eslintrc.js b/toolkit/.eslintrc.js index 5d2284b92c67..e668160ce114 100644 --- a/toolkit/.eslintrc.js +++ b/toolkit/.eslintrc.js @@ -9,5 +9,7 @@ module.exports = { // XXX Bug 1326071 - This should be reduced down - probably to 20 or to // be removed & synced with the mozilla/recommended value. "complexity": ["error", 41], + + "mozilla/no-task": "error", } }; diff --git a/toolkit/components/thumbnails/test/browser_thumbnails_storage.js b/toolkit/components/thumbnails/test/browser_thumbnails_storage.js index f62ce1258f2f..3d357fb5ed45 100644 --- a/toolkit/components/thumbnails/test/browser_thumbnails_storage.js +++ b/toolkit/components/thumbnails/test/browser_thumbnails_storage.js @@ -18,19 +18,19 @@ XPCOMUtils.defineLazyGetter(this, "Sanitizer", function() { * be removed when the user sanitizes their history. */ function* runTests() { - yield Task.spawn(function*() { + yield (async function() { dontExpireThumbnailURLs([URL, URL_COPY]); - yield promiseClearHistory(); - yield promiseAddVisitsAndRepopulateNewTabLinks(URL); - yield promiseCreateThumbnail(); + await promiseClearHistory(); + await promiseAddVisitsAndRepopulateNewTabLinks(URL); + await promiseCreateThumbnail(); // Make sure Storage.copy() updates an existing file. - yield PageThumbsStorage.copy(URL, URL_COPY); + await PageThumbsStorage.copy(URL, URL_COPY); let copy = new FileUtils.File(PageThumbsStorage.getFilePathForURL(URL_COPY)); let mtime = copy.lastModifiedTime -= 60; - yield PageThumbsStorage.copy(URL, URL_COPY); + await PageThumbsStorage.copy(URL, URL_COPY); isnot(new FileUtils.File(PageThumbsStorage.getFilePathForURL(URL_COPY)).lastModifiedTime, mtime, "thumbnail file was updated"); @@ -41,37 +41,37 @@ function* runTests() { // locks them sometimes. info("Clearing history"); while (file.exists() || fileCopy.exists()) { - yield promiseClearHistory(); + await promiseClearHistory(); } info("History is clear"); info("Repopulating"); - yield promiseAddVisitsAndRepopulateNewTabLinks(URL); - yield promiseCreateThumbnail(); + await promiseAddVisitsAndRepopulateNewTabLinks(URL); + await promiseCreateThumbnail(); info("Clearing the last 10 minutes of browsing history"); // Clear the last 10 minutes of browsing history. - yield promiseClearHistory(true); + await promiseClearHistory(true); info("Attempt to clear file"); // Retry until the file is gone because Windows locks it sometimes. - yield promiseClearFile(file, URL); + await promiseClearFile(file, URL); info("Done"); - }); + })(); } -var promiseClearFile = Task.async(function*(aFile, aURL) { +async function promiseClearFile(aFile, aURL) { if (!aFile.exists()) { return undefined; } // Re-add our URL to the history so that history observer's onDeleteURI() // is called again. - yield PlacesTestUtils.addVisits(makeURI(aURL)); - yield promiseClearHistory(true); + await PlacesTestUtils.addVisits(makeURI(aURL)); + await promiseClearHistory(true); // Then retry. return promiseClearFile(aFile, aURL); -}); +} function promiseClearHistory(aUseRange) { let s = new Sanitizer(); diff --git a/toolkit/modules/Sqlite.jsm b/toolkit/modules/Sqlite.jsm index 114f253235ba..30014e8a2b14 100644 --- a/toolkit/modules/Sqlite.jsm +++ b/toolkit/modules/Sqlite.jsm @@ -592,7 +592,7 @@ ConnectionData.prototype = Object.freeze({ try { // Keep Task.spawn here to preserve API compat; unfortunately // func was a generator rather than a task here. - result = await Task.spawn(func); + result = await Task.spawn(func); // eslint-disable-line mozilla/no-task } catch (ex) { // It's possible that the exception has been caused by trying to // close the connection in the middle of a transaction. diff --git a/toolkit/modules/Task.jsm b/toolkit/modules/Task.jsm index 6ecffc46e930..5c7f433f2366 100644 --- a/toolkit/modules/Task.jsm +++ b/toolkit/modules/Task.jsm @@ -6,6 +6,8 @@ "use strict"; +/* eslint-disable mozilla/no-task */ + this.EXPORTED_SYMBOLS = [ "Task" ]; diff --git a/toolkit/modules/tests/xpcshell/test_Promise.js b/toolkit/modules/tests/xpcshell/test_Promise.js index 1e70f6738979..3a4ee0c38258 100644 --- a/toolkit/modules/tests/xpcshell/test_Promise.js +++ b/toolkit/modules/tests/xpcshell/test_Promise.js @@ -550,7 +550,7 @@ tests.push( // Test that Promise.resolve throws when its argument is an async function. tests.push( make_promise_test(function test_promise_resolve_throws_with_async_function(test) { - Assert.throws(() => Promise.resolve(Task.async(function* () {})), + Assert.throws(() => Promise.resolve(Task.async(function* () {})), // eslint-disable-line mozilla/no-task /Cannot resolve a promise with an async function/); return Promise.resolve(); })); diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/index.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/index.js index 64c9e114bc64..9d333bcef922 100644 --- a/tools/lint/eslint/eslint-plugin-mozilla/lib/index.js +++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/index.js @@ -46,6 +46,7 @@ module.exports = { "no-single-arg-cu-import": require("../lib/rules/no-single-arg-cu-import"), "no-import-into-var-and-global": require("../lib/rules/no-import-into-var-and-global.js"), + "no-task": require("../lib/rules/no-task"), "no-useless-parameters": require("../lib/rules/no-useless-parameters"), "no-useless-removeEventListener": require("../lib/rules/no-useless-removeEventListener"), @@ -70,6 +71,7 @@ module.exports = { "no-cpows-in-tests": "off", "no-single-arg-cu-import": "off", "no-import-into-var-and-global": "off", + "no-task": "off", "no-useless-parameters": "off", "no-useless-removeEventListener": "off", "reject-importGlobalProperties": "off", diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-task.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-task.js new file mode 100644 index 000000000000..1eafbaf4f066 --- /dev/null +++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-task.js @@ -0,0 +1,31 @@ +/** + * @fileoverview Reject common XPCOM methods called with useless optional + * parameters, or non-existent parameters. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +"use strict"; + +// ----------------------------------------------------------------------------- +// Rule Definition +// ----------------------------------------------------------------------------- + +module.exports = function(context) { + // --------------------------------------------------------------------------- + // Public + // -------------------------------------------------------------------------- + + return { + "CallExpression": function(node) { + let callee = node.callee; + if (callee.type === "MemberExpression" && + callee.object.type === "Identifier" && + callee.object.name === "Task") { + context.report({node, message: "Task.jsm is deprecated."}); + } + } + }; +};