summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/lib/rules
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /tools/lint/eslint/eslint-plugin-mozilla/lib/rules
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/lint/eslint/eslint-plugin-mozilla/lib/rules')
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/avoid-Date-timing.js67
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/avoid-removeChild.js64
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/balanced-listeners.js147
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/balanced-observers.js119
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/consistent-if-bracing.js49
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-browser-window-globals.js49
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-content-task-globals.js84
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-globals.js15
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js47
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/mark-exported-symbols-as-used.js83
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/mark-test-function-used.js37
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-aArgs.js56
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-arbitrary-setTimeout.js70
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-compare-against-boolean-literals.js34
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-define-cc-etc.js49
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-task.js33
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-throw-cr-literal.js100
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-parameters.js147
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-removeEventListener.js64
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-run-test.js72
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/prefer-boolean-length-check.js130
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/prefer-formatValues.js96
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-chromeutils-import-null.js44
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-importGlobalProperties.js62
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-relative-requires.js34
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-some-requires.js35
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/rejects-requires-await.js42
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-cc-etc.js44
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-chromeutils-generateqi.js103
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-chromeutils-import.js89
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-default-preference-values.js45
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-includes-instead-of-indexOf.js45
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-ownerGlobal.js38
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-returnValue.js42
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-services.js45
-rw-r--r--tools/lint/eslint/eslint-plugin-mozilla/lib/rules/var-only-at-top-level.js34
36 files changed, 2314 insertions, 0 deletions
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/avoid-Date-timing.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/avoid-Date-timing.js
new file mode 100644
index 0000000000..4f06390189
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/avoid-Date-timing.js
@@ -0,0 +1,67 @@
+/**
+ * @fileoverview Disallow using Date for timing in performance sensitive code
+ *
+ * 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 = {
+ meta: {
+ docs: {
+ description: "disallow use of Date for timing measurements",
+ category: "Best Practices",
+ },
+ schema: [],
+ },
+
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ create(context) {
+ return {
+ CallExpression(node) {
+ let callee = node.callee;
+ if (
+ callee.type !== "MemberExpression" ||
+ callee.object.type !== "Identifier" ||
+ callee.object.name !== "Date" ||
+ callee.property.type !== "Identifier" ||
+ callee.property.name !== "now"
+ ) {
+ return;
+ }
+
+ context.report(
+ node,
+ "use performance.now() instead of Date.now() for timing " +
+ "measurements"
+ );
+ },
+
+ NewExpression(node) {
+ let callee = node.callee;
+ if (
+ callee.type !== "Identifier" ||
+ callee.name !== "Date" ||
+ node.arguments.length > 0
+ ) {
+ return;
+ }
+
+ context.report(
+ node,
+ "use performance.now() instead of new Date() for timing " +
+ "measurements"
+ );
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/avoid-removeChild.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/avoid-removeChild.js
new file mode 100644
index 0000000000..99d0ff5027
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/avoid-removeChild.js
@@ -0,0 +1,64 @@
+/**
+ * @fileoverview Reject using element.parentNode.removeChild(element) when
+ * element.remove() can be used instead.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+var helpers = require("../helpers");
+
+module.exports = function(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ return {
+ CallExpression(node) {
+ let callee = node.callee;
+ if (
+ callee.type !== "MemberExpression" ||
+ callee.property.type !== "Identifier" ||
+ callee.property.name != "removeChild" ||
+ node.arguments.length != 1
+ ) {
+ return;
+ }
+
+ if (
+ callee.object.type == "MemberExpression" &&
+ callee.object.property.type == "Identifier" &&
+ callee.object.property.name == "parentNode" &&
+ helpers.getASTSource(callee.object.object, context) ==
+ helpers.getASTSource(node.arguments[0])
+ ) {
+ context.report(
+ node,
+ "use element.remove() instead of " +
+ "element.parentNode.removeChild(element)"
+ );
+ }
+
+ if (
+ node.arguments[0].type == "MemberExpression" &&
+ node.arguments[0].property.type == "Identifier" &&
+ node.arguments[0].property.name == "firstChild" &&
+ helpers.getASTSource(callee.object, context) ==
+ helpers.getASTSource(node.arguments[0].object)
+ ) {
+ context.report(
+ node,
+ "use element.firstChild.remove() instead of " +
+ "element.removeChild(element.firstChild)"
+ );
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/balanced-listeners.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/balanced-listeners.js
new file mode 100644
index 0000000000..b6fb25139a
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/balanced-listeners.js
@@ -0,0 +1,147 @@
+/**
+ * @fileoverview Check that there's a removeEventListener for each
+ * addEventListener and an off for each on.
+ * Note that for now, this rule is rather simple in that it only checks that
+ * for each event name there is both an add and remove listener. It doesn't
+ * check that these are called on the right objects or with the same callback.
+ *
+ * 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) {
+ // ---------------------------------------------------------------------------
+ // Helpers
+ // ---------------------------------------------------------------------------
+
+ var DICTIONARY = {
+ addEventListener: "removeEventListener",
+ on: "off",
+ };
+ // Invert this dictionary to make it easy later.
+ var INVERTED_DICTIONARY = {};
+ for (var i in DICTIONARY) {
+ INVERTED_DICTIONARY[DICTIONARY[i]] = i;
+ }
+
+ // Collect the add/remove listeners in these 2 arrays.
+ var addedListeners = [];
+ var removedListeners = [];
+
+ function addAddedListener(node) {
+ var capture = false;
+ let options = node.arguments[2];
+ if (options) {
+ if (options.type == "ObjectExpression") {
+ if (
+ options.properties.some(
+ p => p.key.name == "once" && p.value.value === true
+ )
+ ) {
+ // No point in adding listeners using the 'once' option.
+ return;
+ }
+ capture = options.properties.some(
+ p => p.key.name == "capture" && p.value.value === true
+ );
+ } else {
+ capture = options.value;
+ }
+ }
+ addedListeners.push({
+ functionName: node.callee.property.name,
+ type: node.arguments[0].value,
+ node: node.callee.property,
+ useCapture: capture,
+ });
+ }
+
+ function addRemovedListener(node) {
+ var capture = false;
+ let options = node.arguments[2];
+ if (options) {
+ if (options.type == "ObjectExpression") {
+ capture = options.properties.some(
+ p => p.key.name == "capture" && p.value.value === true
+ );
+ } else {
+ capture = options.value;
+ }
+ }
+ removedListeners.push({
+ functionName: node.callee.property.name,
+ type: node.arguments[0].value,
+ useCapture: capture,
+ });
+ }
+
+ function getUnbalancedListeners() {
+ var unbalanced = [];
+
+ for (var j = 0; j < addedListeners.length; j++) {
+ if (!hasRemovedListener(addedListeners[j])) {
+ unbalanced.push(addedListeners[j]);
+ }
+ }
+ addedListeners = removedListeners = [];
+
+ return unbalanced;
+ }
+
+ function hasRemovedListener(addedListener) {
+ for (var k = 0; k < removedListeners.length; k++) {
+ var listener = removedListeners[k];
+ if (
+ DICTIONARY[addedListener.functionName] === listener.functionName &&
+ addedListener.type === listener.type &&
+ addedListener.useCapture === listener.useCapture
+ ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ // ---------------------------------------------------------------------------
+ // Public
+ // ---------------------------------------------------------------------------
+
+ return {
+ CallExpression(node) {
+ if (node.arguments.length === 0) {
+ return;
+ }
+
+ if (node.callee.type === "MemberExpression") {
+ var listenerMethodName = node.callee.property.name;
+
+ if (DICTIONARY.hasOwnProperty(listenerMethodName)) {
+ addAddedListener(node);
+ } else if (INVERTED_DICTIONARY.hasOwnProperty(listenerMethodName)) {
+ addRemovedListener(node);
+ }
+ }
+ },
+
+ "Program:exit": function() {
+ getUnbalancedListeners().forEach(function(listener) {
+ context.report(
+ listener.node,
+ "No corresponding '{{functionName}}({{type}})' was found.",
+ {
+ functionName: DICTIONARY[listener.functionName],
+ type: listener.type,
+ }
+ );
+ });
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/balanced-observers.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/balanced-observers.js
new file mode 100644
index 0000000000..f714b31cda
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/balanced-observers.js
@@ -0,0 +1,119 @@
+/**
+ * @fileoverview Check that there's a Services.(prefs|obs).removeObserver for
+ * each addObserver.
+ *
+ * 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) {
+ // ---------------------------------------------------------------------------
+ // Helpers
+ // ---------------------------------------------------------------------------
+
+ var addedObservers = [];
+ var removedObservers = [];
+
+ function getObserverAPI(node) {
+ const object = node.callee.object;
+ if (
+ object.type == "MemberExpression" &&
+ object.property.type == "Identifier"
+ ) {
+ return object.property.name;
+ }
+ return null;
+ }
+
+ function isServicesObserver(api) {
+ return api == "obs" || api == "prefs";
+ }
+
+ function getObservableName(node, api) {
+ if (api === "obs") {
+ return node.arguments[1].value;
+ }
+ return node.arguments[0].value;
+ }
+
+ function addAddedObserver(node) {
+ const api = getObserverAPI(node);
+ if (!isServicesObserver(api)) {
+ return;
+ }
+
+ addedObservers.push({
+ functionName: node.callee.property.name,
+ observable: getObservableName(node, api),
+ node: node.callee.property,
+ });
+ }
+
+ function addRemovedObserver(node) {
+ const api = getObserverAPI(node);
+ if (!isServicesObserver(api)) {
+ return;
+ }
+
+ removedObservers.push({
+ functionName: node.callee.property.name,
+ observable: getObservableName(node, api),
+ });
+ }
+
+ function getUnbalancedObservers() {
+ const unbalanced = addedObservers.filter(
+ observer => !hasRemovedObserver(observer)
+ );
+ addedObservers = removedObservers = [];
+
+ return unbalanced;
+ }
+
+ function hasRemovedObserver(addedObserver) {
+ return removedObservers.some(
+ observer => addedObserver.observable === observer.observable
+ );
+ }
+
+ // ---------------------------------------------------------------------------
+ // Public
+ // ---------------------------------------------------------------------------
+
+ return {
+ CallExpression(node) {
+ if (node.arguments.length === 0) {
+ return;
+ }
+
+ if (node.callee.type === "MemberExpression") {
+ var methodName = node.callee.property.name;
+
+ if (methodName === "addObserver") {
+ addAddedObserver(node);
+ } else if (methodName === "removeObserver") {
+ addRemovedObserver(node);
+ }
+ }
+ },
+
+ "Program:exit": function() {
+ getUnbalancedObservers().forEach(function(observer) {
+ context.report(
+ observer.node,
+ "No corresponding 'removeObserver(\"{{observable}}\")' was found.",
+ {
+ observable: observer.observable,
+ }
+ );
+ });
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/consistent-if-bracing.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/consistent-if-bracing.js
new file mode 100644
index 0000000000..af51dcea1f
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/consistent-if-bracing.js
@@ -0,0 +1,49 @@
+/**
+ * @fileoverview checks if/else if/else bracing is consistent
+ *
+ * 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";
+
+module.exports = {
+ meta: {
+ messages: {
+ consistentIfBracing: "Bracing of if..else bodies should be consistent.",
+ },
+ },
+
+ create(context) {
+ return {
+ IfStatement(node) {
+ if (node.parent.type !== "IfStatement") {
+ let types = new Set();
+ for (
+ let currentNode = node;
+ currentNode;
+ currentNode = currentNode.alternate
+ ) {
+ let type = currentNode.consequent.type;
+ types.add(type == "BlockStatement" ? "Block" : "NotBlock");
+ if (
+ currentNode.alternate &&
+ currentNode.alternate.type !== "IfStatement"
+ ) {
+ type = currentNode.alternate.type;
+ types.add(type == "BlockStatement" ? "Block" : "NotBlock");
+ break;
+ }
+ }
+ if (types.size > 1) {
+ context.report({
+ node,
+ messageId: "consistentIfBracing",
+ });
+ }
+ }
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-browser-window-globals.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-browser-window-globals.js
new file mode 100644
index 0000000000..bcbdb3ec54
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-browser-window-globals.js
@@ -0,0 +1,49 @@
+/**
+ * @fileoverview For scripts included in browser-window, this will automatically
+ * inject the browser-window global scopes into the file.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+var path = require("path");
+var helpers = require("../helpers");
+var browserWindowEnv = require("../environments/browser-window");
+
+module.exports = function(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // ---------------------------------------------------------------------------
+
+ return {
+ Program(node) {
+ let filePath = helpers.getAbsoluteFilePath(context);
+ let relativePath = path.relative(helpers.rootDir, filePath);
+ // We need to translate the path on Windows, due to the change
+ // from \ to /, and browserjsScripts assumes Posix.
+ if (path.win32) {
+ relativePath = relativePath.split(path.sep).join("/");
+ }
+
+ if (
+ browserWindowEnv.browserjsScripts &&
+ browserWindowEnv.browserjsScripts.includes(relativePath)
+ ) {
+ for (let global in browserWindowEnv.globals) {
+ helpers.addVarToScope(
+ global,
+ context.getScope(),
+ browserWindowEnv.globals[global]
+ );
+ }
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-content-task-globals.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-content-task-globals.js
new file mode 100644
index 0000000000..b8545b45f8
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-content-task-globals.js
@@ -0,0 +1,84 @@
+/**
+ * @fileoverview For ContentTask.spawn, this will automatically declare the
+ * frame script variables in the global scope.
+ * Note: due to the way ESLint works, it appears it is only
+ * easy to declare these variables on a file-global scope, rather
+ * than function global.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+var helpers = require("../helpers");
+var frameScriptEnv = require("../environments/frame-script");
+
+// The global environment of SpecialPowers.spawn tasks is
+// controlled by the Sandbox environment created by
+// SpecialPowersSandbox.jsm. This list should be kept in sync with
+// that module.
+var sandboxGlobals = [
+ "Assert",
+ "Blob",
+ "BrowsingContext",
+ "ChromeUtils",
+ "ContentTaskUtils",
+ "EventUtils",
+ "Services",
+ "TextDecoder",
+ "TextEncoder",
+ "URL",
+ "assert",
+ "info",
+ "is",
+ "isnot",
+ "ok",
+ "todo",
+ "todo_is",
+];
+
+module.exports = function(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // ---------------------------------------------------------------------------
+
+ return {
+ "CallExpression[callee.object.name='ContentTask'][callee.property.name='spawn']": function(
+ node
+ ) {
+ for (let global in frameScriptEnv.globals) {
+ helpers.addVarToScope(
+ global,
+ context.getScope(),
+ frameScriptEnv.globals[global]
+ );
+ }
+ },
+ "CallExpression[callee.object.name='SpecialPowers'][callee.property.name='spawn']": function(
+ node
+ ) {
+ let globals = [...sandboxGlobals, "SpecialPowers", "content", "docShell"];
+ for (let global of globals) {
+ helpers.addVarToScope(global, context.getScope(), false);
+ }
+ },
+ "CallExpression[callee.object.name='SpecialPowers'][callee.property.name='spawnChrome']": function(
+ node
+ ) {
+ let globals = [
+ ...sandboxGlobals,
+ "browsingContext",
+ "windowGlobalParent",
+ ];
+ for (let global of globals) {
+ helpers.addVarToScope(global, context.getScope(), false);
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-globals.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-globals.js
new file mode 100644
index 0000000000..053a9e702f
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-globals.js
@@ -0,0 +1,15 @@
+/**
+ * @fileoverview Discovers all globals for the current file.
+ *
+ * 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 = require("../globals").getESLintGlobalParser;
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js
new file mode 100644
index 0000000000..cffd7a4c8e
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/import-headjs-globals.js
@@ -0,0 +1,47 @@
+/**
+ * @fileoverview Import globals from head.js and from any files that were
+ * imported by head.js (as far as we can correctly resolve the path).
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+var fs = require("fs");
+var helpers = require("../helpers");
+var globals = require("../globals");
+
+module.exports = function(context) {
+ function importHead(path, node) {
+ try {
+ let stats = fs.statSync(path);
+ if (!stats.isFile()) {
+ return;
+ }
+ } catch (e) {
+ return;
+ }
+
+ let newGlobals = globals.getGlobalsForFile(path);
+ helpers.addGlobals(newGlobals, context.getScope());
+ }
+
+ // ---------------------------------------------------------------------------
+ // Public
+ // ---------------------------------------------------------------------------
+
+ return {
+ Program(node) {
+ let heads = helpers.getTestHeadFiles(context);
+ for (let head of heads) {
+ importHead(head, node);
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/mark-exported-symbols-as-used.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/mark-exported-symbols-as-used.js
new file mode 100644
index 0000000000..c535a54a16
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/mark-exported-symbols-as-used.js
@@ -0,0 +1,83 @@
+/**
+ * @fileoverview Simply marks exported symbols as used. Designed for use in
+ * .jsm files only.
+ *
+ * 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";
+
+function markArrayElementsAsUsed(context, node, expression) {
+ if (expression.type != "ArrayExpression") {
+ context.report({
+ node,
+ message: "Unexpected assignment of non-Array to EXPORTED_SYMBOLS",
+ });
+ return;
+ }
+
+ for (let element of expression.elements) {
+ context.markVariableAsUsed(element.value);
+ }
+ // Also mark EXPORTED_SYMBOLS as used.
+ context.markVariableAsUsed("EXPORTED_SYMBOLS");
+}
+
+// -----------------------------------------------------------------------------
+// Rule Definition
+// -----------------------------------------------------------------------------
+
+module.exports = function(context) {
+ // Ignore assignments not in the global scope, e.g. where special module
+ // definitions are required due to having different ways of importing files,
+ // e.g. osfile.
+ function isGlobalScope() {
+ return !context.getScope().upper;
+ }
+
+ // ---------------------------------------------------------------------------
+ // Public
+ // ---------------------------------------------------------------------------
+
+ return {
+ AssignmentExpression(node, parents) {
+ if (
+ node.operator === "=" &&
+ node.left.type === "MemberExpression" &&
+ node.left.object.type === "ThisExpression" &&
+ node.left.property.name === "EXPORTED_SYMBOLS" &&
+ isGlobalScope()
+ ) {
+ markArrayElementsAsUsed(context, node, node.right);
+ }
+ },
+
+ VariableDeclaration(node, parents) {
+ if (!isGlobalScope()) {
+ return;
+ }
+
+ for (let item of node.declarations) {
+ if (
+ item.id &&
+ item.id.type == "Identifier" &&
+ item.id.name === "EXPORTED_SYMBOLS"
+ ) {
+ if (node.kind === "let") {
+ // The use of 'let' isn't allowed as the lexical scope may die after
+ // the script executes.
+ context.report({
+ node,
+ message:
+ "EXPORTED_SYMBOLS cannot be declared via `let`. Use `var` or `this.EXPORTED_SYMBOLS =`",
+ });
+ }
+
+ markArrayElementsAsUsed(context, node, item.init);
+ }
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/mark-test-function-used.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/mark-test-function-used.js
new file mode 100644
index 0000000000..0c0dcfdbe5
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/mark-test-function-used.js
@@ -0,0 +1,37 @@
+/**
+ * @fileoverview Simply marks `test` (the test method) or `run_test` as used
+ * when in mochitests or xpcshell tests respectively. This avoids ESLint telling
+ * us that the function is never called.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+var helpers = require("../helpers");
+
+module.exports = function(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // ---------------------------------------------------------------------------
+
+ return {
+ Program() {
+ let testType = helpers.getTestType(context);
+ if (testType == "browser") {
+ context.markVariableAsUsed("test");
+ return;
+ }
+
+ if (testType == "xpcshell") {
+ context.markVariableAsUsed("run_test");
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-aArgs.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-aArgs.js
new file mode 100644
index 0000000000..af7fe06336
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-aArgs.js
@@ -0,0 +1,56 @@
+/**
+ * @fileoverview warns against using hungarian notation in function arguments
+ * (i.e. aArg).
+ *
+ * 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) {
+ // ---------------------------------------------------------------------------
+ // Helpers
+ // ---------------------------------------------------------------------------
+
+ function isPrefixed(name) {
+ return name.length >= 2 && /^a[A-Z]/.test(name);
+ }
+
+ function deHungarianize(name) {
+ return name.substring(1, 2).toLowerCase() + name.substring(2, name.length);
+ }
+
+ function checkFunction(node) {
+ for (var i = 0; i < node.params.length; i++) {
+ var param = node.params[i];
+ if (param.name && isPrefixed(param.name)) {
+ var errorObj = {
+ name: param.name,
+ suggestion: deHungarianize(param.name),
+ };
+ context.report(
+ param,
+ "Parameter '{{name}}' uses Hungarian Notation, " +
+ "consider using '{{suggestion}}' instead.",
+ errorObj
+ );
+ }
+ }
+ }
+
+ // ---------------------------------------------------------------------------
+ // Public
+ // ---------------------------------------------------------------------------
+
+ return {
+ FunctionDeclaration: checkFunction,
+ ArrowFunctionExpression: checkFunction,
+ FunctionExpression: checkFunction,
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-arbitrary-setTimeout.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-arbitrary-setTimeout.js
new file mode 100644
index 0000000000..be0b5c5ffb
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-arbitrary-setTimeout.js
@@ -0,0 +1,70 @@
+/**
+ * @fileoverview Reject use of non-zero values in setTimeout
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+var helpers = require("../helpers");
+var testTypes = new Set(["browser", "xpcshell"]);
+
+module.exports = {
+ meta: {
+ docs: {
+ description: "disallow setTimeout with non-zero values in tests",
+ category: "Best Practices",
+ },
+ schema: [],
+ },
+
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ create(context) {
+ return {
+ CallExpression(node) {
+ // We don't want to run this on mochitest plain as it already
+ // prevents flaky setTimeout at runtime. This check is built-in
+ // to the rule itself as sometimes other tests can live alongside
+ // plain mochitests and so it can't be configured via eslintrc.
+ if (!testTypes.has(helpers.getTestType(context))) {
+ return;
+ }
+
+ let callee = node.callee;
+ if (callee.type === "MemberExpression") {
+ if (
+ callee.property.name !== "setTimeout" ||
+ callee.object.name !== "window" ||
+ node.arguments.length < 2
+ ) {
+ return;
+ }
+ } else if (callee.type === "Identifier") {
+ if (callee.name !== "setTimeout" || node.arguments.length < 2) {
+ return;
+ }
+ } else {
+ return;
+ }
+
+ let timeout = node.arguments[1];
+ if (timeout.type !== "Literal" || timeout.value > 0) {
+ context.report(
+ node,
+ "listen for events instead of setTimeout() " +
+ "with arbitrary delay"
+ );
+ }
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-compare-against-boolean-literals.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-compare-against-boolean-literals.js
new file mode 100644
index 0000000000..bb48563fff
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-compare-against-boolean-literals.js
@@ -0,0 +1,34 @@
+/**
+ * @fileoverview Restrict comparing against `true` or `false`.
+ *
+ * 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 {
+ BinaryExpression(node) {
+ if (
+ ["==", "!="].includes(node.operator) &&
+ (["true", "false"].includes(node.left.raw) ||
+ ["true", "false"].includes(node.right.raw))
+ ) {
+ context.report(
+ node,
+ "Don't compare for inexact equality against boolean literals"
+ );
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-define-cc-etc.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-define-cc-etc.js
new file mode 100644
index 0000000000..0b441e4712
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-define-cc-etc.js
@@ -0,0 +1,49 @@
+/**
+ * @fileoverview Reject defining Cc/Ci/Cr/Cu.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+const componentsBlacklist = ["Cc", "Ci", "Cr", "Cu"];
+
+module.exports = function(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ return {
+ VariableDeclarator(node) {
+ if (
+ node.id.type == "Identifier" &&
+ componentsBlacklist.includes(node.id.name)
+ ) {
+ context.report(
+ node,
+ `${node.id.name} is now defined in global scope, a separate definition is no longer necessary.`
+ );
+ }
+
+ if (node.id.type == "ObjectPattern") {
+ for (let property of node.id.properties) {
+ if (
+ property.type == "Property" &&
+ componentsBlacklist.includes(property.value.name)
+ ) {
+ context.report(
+ node,
+ `${property.value.name} is now defined in global scope, a separate definition is no longer necessary.`
+ );
+ }
+ }
+ }
+ },
+ };
+};
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 0000000000..2bab6e8742
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-task.js
@@ -0,0 +1,33 @@
+/**
+ * @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(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." });
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-throw-cr-literal.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-throw-cr-literal.js
new file mode 100644
index 0000000000..21f27ff130
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-throw-cr-literal.js
@@ -0,0 +1,100 @@
+/**
+ * @fileoverview Rule to prevent throwing bare Cr.ERRORs.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+function isCr(object) {
+ return object.type === "Identifier" && object.name === "Cr";
+}
+
+function isComponentsResults(object) {
+ return (
+ object.type === "MemberExpression" &&
+ object.object.type === "Identifier" &&
+ object.object.name === "Components" &&
+ object.property.type === "Identifier" &&
+ object.property.name === "results"
+ );
+}
+
+function isNewError(argument) {
+ return (
+ argument.type === "NewExpression" &&
+ argument.callee.type === "Identifier" &&
+ argument.callee.name === "Error" &&
+ argument.arguments.length === 1
+ );
+}
+
+function fixT(context, node, argument, fixer) {
+ const sourceText = context.getSourceCode().getText(argument);
+ return fixer.replaceText(node, `Components.Exception("", ${sourceText})`);
+}
+
+module.exports = {
+ meta: {
+ fixable: "code",
+ messages: {
+ bareCR: "Do not throw bare Cr.ERRORs, use Components.Exception instead",
+ bareComponentsResults:
+ "Do not throw bare Components.results.ERRORs, use Components.Exception instead",
+ newErrorCR:
+ "Do not pass Cr.ERRORs to new Error(), use Components.Exception instead",
+ newErrorComponentsResults:
+ "Do not pass Components.results.ERRORs to new Error(), use Components.Exception instead",
+ },
+ },
+
+ create(context) {
+ return {
+ ThrowStatement(node) {
+ if (node.argument.type === "MemberExpression") {
+ const fix = fixT.bind(null, context, node.argument, node.argument);
+
+ if (isCr(node.argument.object)) {
+ context.report({
+ node,
+ messageId: "bareCR",
+ fix,
+ });
+ } else if (isComponentsResults(node.argument.object)) {
+ context.report({
+ node,
+ messageId: "bareComponentsResults",
+ fix,
+ });
+ }
+ } else if (isNewError(node.argument)) {
+ const argument = node.argument.arguments[0];
+
+ if (argument.type === "MemberExpression") {
+ const fix = fixT.bind(null, context, node.argument, argument);
+
+ if (isCr(argument.object)) {
+ context.report({
+ node,
+ messageId: "newErrorCR",
+ fix,
+ });
+ } else if (isComponentsResults(argument.object)) {
+ context.report({
+ node,
+ messageId: "newErrorComponentsResults",
+ fix,
+ });
+ }
+ }
+ }
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-parameters.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-parameters.js
new file mode 100644
index 0000000000..4fd0734465
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-parameters.js
@@ -0,0 +1,147 @@
+/**
+ * @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 = {
+ meta: {
+ type: "suggestion",
+ fixable: "code",
+ },
+ create(context) {
+ function getRangeAfterArgToEnd(argNumber, args) {
+ let sourceCode = context.getSourceCode();
+ return [
+ sourceCode.getTokenAfter(args[argNumber]).range[0],
+ args[args.length - 1].range[1],
+ ];
+ }
+
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ return {
+ CallExpression(node) {
+ let callee = node.callee;
+ if (
+ callee.type !== "MemberExpression" ||
+ callee.property.type !== "Identifier"
+ ) {
+ return;
+ }
+
+ let isFalse = arg => arg.type === "Literal" && arg.value === false;
+ let isFalsy = arg => arg.type === "Literal" && !arg.value;
+ let isBool = arg =>
+ arg.type === "Literal" && (arg.value === false || arg.value === true);
+ let name = callee.property.name;
+ let args = node.arguments;
+
+ if (
+ ["addEventListener", "removeEventListener", "addObserver"].includes(
+ name
+ ) &&
+ args.length === 3 &&
+ isFalse(args[2])
+ ) {
+ context.report({
+ node,
+ fix: fixer => {
+ return fixer.removeRange(getRangeAfterArgToEnd(1, args));
+ },
+ message: `${name}'s third parameter can be omitted when it's false.`,
+ });
+ }
+
+ if (name === "clearUserPref" && args.length > 1) {
+ context.report({
+ node,
+ fix: fixer => {
+ return fixer.removeRange(getRangeAfterArgToEnd(0, args));
+ },
+ message: `${name} takes only 1 parameter.`,
+ });
+ }
+
+ if (name === "removeObserver" && args.length === 3 && isBool(args[2])) {
+ context.report({
+ node,
+ fix: fixer => {
+ return fixer.removeRange(getRangeAfterArgToEnd(1, args));
+ },
+ message: "removeObserver only takes 2 parameters.",
+ });
+ }
+
+ if (name === "appendElement" && args.length === 2 && isFalse(args[1])) {
+ context.report({
+ node,
+ fix: fixer => {
+ return fixer.removeRange(getRangeAfterArgToEnd(0, args));
+ },
+ message: `${name}'s second parameter can be omitted when it's false.`,
+ });
+ }
+
+ if (
+ name === "notifyObservers" &&
+ args.length === 3 &&
+ isFalsy(args[2])
+ ) {
+ context.report({
+ node,
+ fix: fixer => {
+ return fixer.removeRange(getRangeAfterArgToEnd(1, args));
+ },
+ message: `${name}'s third parameter can be omitted.`,
+ });
+ }
+
+ if (
+ name === "getComputedStyle" &&
+ args.length === 2 &&
+ isFalsy(args[1])
+ ) {
+ context.report({
+ node,
+ fix: fixer => {
+ return fixer.removeRange(getRangeAfterArgToEnd(0, args));
+ },
+ message: "getComputedStyle's second parameter can be omitted.",
+ });
+ }
+
+ if (
+ name === "newURI" &&
+ args.length > 1 &&
+ isFalsy(args[args.length - 1])
+ ) {
+ context.report({
+ node,
+ fix: fixer => {
+ if (args.length > 2 && isFalsy(args[args.length - 2])) {
+ return fixer.removeRange(getRangeAfterArgToEnd(0, args));
+ }
+
+ return fixer.removeRange(
+ getRangeAfterArgToEnd(args.length - 2, args)
+ );
+ },
+ message: "newURI's last parameters are optional.",
+ });
+ }
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-removeEventListener.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-removeEventListener.js
new file mode 100644
index 0000000000..e58edfcb98
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-removeEventListener.js
@@ -0,0 +1,64 @@
+/**
+ * @fileoverview Reject calls to removeEventListenter where {once: true} could
+ * be used instead.
+ *
+ * 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(node) {
+ let callee = node.callee;
+ if (
+ callee.type !== "MemberExpression" ||
+ callee.property.type !== "Identifier" ||
+ callee.property.name !== "addEventListener" ||
+ node.arguments.length == 4
+ ) {
+ return;
+ }
+
+ let listener = node.arguments[1];
+ if (
+ !listener ||
+ listener.type != "FunctionExpression" ||
+ !listener.body ||
+ listener.body.type != "BlockStatement" ||
+ !listener.body.body.length ||
+ listener.body.body[0].type != "ExpressionStatement" ||
+ listener.body.body[0].expression.type != "CallExpression"
+ ) {
+ return;
+ }
+
+ let call = listener.body.body[0].expression;
+ if (
+ call.callee.type == "MemberExpression" &&
+ call.callee.property.type == "Identifier" &&
+ call.callee.property.name == "removeEventListener" &&
+ ((call.arguments[0].type == "Literal" &&
+ call.arguments[0].value == node.arguments[0].value) ||
+ (call.arguments[0].type == "Identifier" &&
+ call.arguments[0].name == node.arguments[0].name))
+ ) {
+ context.report(
+ call,
+ "use {once: true} instead of removeEventListener as " +
+ "the first instruction of the listener"
+ );
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-run-test.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-run-test.js
new file mode 100644
index 0000000000..d16757c347
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/no-useless-run-test.js
@@ -0,0 +1,72 @@
+/**
+ * @fileoverview Reject run_test() definitions where they aren't necessary.
+ *
+ * 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 = {
+ meta: {
+ type: "suggestion",
+ fixable: "code",
+ },
+ create(context) {
+ return {
+ "Program > FunctionDeclaration": function(node) {
+ if (
+ node.id.name === "run_test" &&
+ node.body.type === "BlockStatement" &&
+ node.body.body.length === 1 &&
+ node.body.body[0].type === "ExpressionStatement" &&
+ node.body.body[0].expression.type === "CallExpression" &&
+ node.body.body[0].expression.callee.name === "run_next_test"
+ ) {
+ context.report({
+ node,
+ fix: fixer => {
+ let sourceCode = context.getSourceCode();
+ let startNode;
+ if (sourceCode.getCommentsBefore) {
+ // ESLint 4 has getCommentsBefore.
+ startNode = sourceCode.getCommentsBefore(node);
+ } else if (node && node.body && node.leadingComments) {
+ // This is for ESLint 3.
+ startNode = node.leadingComments;
+ }
+
+ // If we have comments, we want the start node to be the comments,
+ // rather than the token before the comments, so that we don't
+ // remove the comments - for run_test, these are likely to be useful
+ // information about the test.
+ if (startNode && startNode.length) {
+ startNode = startNode[startNode.length - 1];
+ } else {
+ startNode = sourceCode.getTokenBefore(node);
+ }
+
+ return fixer.removeRange([
+ // If there's no startNode, we fall back to zero, i.e. start of
+ // file.
+ startNode ? startNode.range[1] + 1 : 0,
+ // We know the function is a block and it'll end with }. Normally
+ // there's a new line after that, so just advance past it. This
+ // may be slightly not dodgy in some cases, but covers the existing
+ // cases.
+ node.range[1] + 1,
+ ]);
+ },
+ message:
+ "Useless run_test function - only contains run_next_test; whole function can be removed",
+ });
+ }
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/prefer-boolean-length-check.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/prefer-boolean-length-check.js
new file mode 100644
index 0000000000..543d07b765
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/prefer-boolean-length-check.js
@@ -0,0 +1,130 @@
+/**
+ * @fileoverview Prefer boolean length check
+ *
+ * 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
+// -----------------------------------------------------------------------------
+function funcForBooleanLength(context, node, conditionCheck) {
+ let newText = "";
+ const sourceCode = context.getSourceCode();
+ switch (node.operator) {
+ case ">":
+ if (node.right.value == 0) {
+ if (conditionCheck) {
+ newText = sourceCode.getText(node.left);
+ } else {
+ newText = "!!" + sourceCode.getText(node.left);
+ }
+ } else {
+ newText = "!" + sourceCode.getText(node.right);
+ }
+ break;
+ case "<":
+ if (node.right.value == 0) {
+ newText = "!" + sourceCode.getText(node.left);
+ } else if (conditionCheck) {
+ newText = sourceCode.getText(node.right);
+ } else {
+ newText = "!!" + sourceCode.getText(node.right);
+ }
+ break;
+ case "==":
+ if (node.right.value == 0) {
+ newText = "!" + sourceCode.getText(node.left);
+ } else {
+ newText = "!" + sourceCode.getText(node.right);
+ }
+ break;
+ case "!=":
+ if (node.right.value == 0) {
+ if (conditionCheck) {
+ newText = sourceCode.getText(node.left);
+ } else {
+ newText = "!!" + sourceCode.getText(node.left);
+ }
+ } else if (conditionCheck) {
+ newText = sourceCode.getText(node.right);
+ } else {
+ newText = "!!" + sourceCode.getText(node.right);
+ }
+ break;
+ }
+ return newText;
+}
+
+module.exports = {
+ meta: {
+ type: "suggestion",
+ fixable: "code",
+ },
+ create(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ const conditionStatement = [
+ "IfStatement",
+ "WhileStatement",
+ "DoWhileStatement",
+ "ForStatement",
+ "ForInStatement",
+ "ConditionalExpression",
+ ];
+
+ return {
+ BinaryExpression(node) {
+ if (
+ ["==", "!=", ">", "<"].includes(node.operator) &&
+ ((node.right.type == "Literal" &&
+ node.right.value == 0 &&
+ node.left.property &&
+ node.left.property.name == "length") ||
+ (node.left.type == "Literal" &&
+ node.left.value == 0 &&
+ node.right.property &&
+ node.right.property.name == "length"))
+ ) {
+ if (
+ conditionStatement.includes(node.parent.type) ||
+ (node.parent.type == "LogicalExpression" &&
+ conditionStatement.includes(node.parent.parent.type))
+ ) {
+ context.report({
+ node,
+ fix: fixer => {
+ let generateExpression = funcForBooleanLength(
+ context,
+ node,
+ true
+ );
+
+ return fixer.replaceText(node, generateExpression);
+ },
+ message: "Prefer boolean length check",
+ });
+ } else {
+ context.report({
+ node,
+ fix: fixer => {
+ let generateExpression = funcForBooleanLength(
+ context,
+ node,
+ false
+ );
+ return fixer.replaceText(node, generateExpression);
+ },
+ message: "Prefer boolean length check",
+ });
+ }
+ }
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/prefer-formatValues.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/prefer-formatValues.js
new file mode 100644
index 0000000000..45b8c40901
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/prefer-formatValues.js
@@ -0,0 +1,96 @@
+/**
+ * @fileoverview Reject multiple calls to document.l10n.formatValue in the same
+ * code block.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+function isIdentifier(node, id) {
+ return node && node.type === "Identifier" && node.name === id;
+}
+
+/**
+ * As we enter blocks new sets are pushed onto this stack and then popped when
+ * we exit the block.
+ */
+const BlockStack = [];
+
+module.exports = {
+ meta: {
+ docs: {
+ description: "disallow multiple document.l10n.formatValue calls",
+ category: "Best Practices",
+ },
+ schema: [],
+ },
+
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ create(context) {
+ function enterBlock() {
+ BlockStack.push(new Set());
+ }
+
+ function exitBlock() {
+ let calls = BlockStack.pop();
+ if (calls.size > 1) {
+ for (let callNode of calls) {
+ context.report(
+ callNode,
+ "prefer to use a single document.l10n.formatValues call instead " +
+ "of multiple calls to document.l10n.formatValue or document.l10n.formatValues"
+ );
+ }
+ }
+ }
+
+ return {
+ Program: enterBlock,
+ "Program:exit": exitBlock,
+ BlockStatement: enterBlock,
+ "BlockStatement:exit": exitBlock,
+
+ CallExpression(node) {
+ if (!BlockStack.length) {
+ context.report(node, "call expression found outside of known block");
+ }
+
+ let callee = node.callee;
+ if (callee.type !== "MemberExpression") {
+ return;
+ }
+
+ if (
+ !isIdentifier(callee.property, "formatValue") &&
+ !isIdentifier(callee.property, "formatValues")
+ ) {
+ return;
+ }
+
+ if (callee.object.type !== "MemberExpression") {
+ return;
+ }
+
+ if (
+ !isIdentifier(callee.object.object, "document") ||
+ !isIdentifier(callee.object.property, "l10n")
+ ) {
+ return;
+ }
+
+ let calls = BlockStack[BlockStack.length - 1];
+ calls.add(node);
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-chromeutils-import-null.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-chromeutils-import-null.js
new file mode 100644
index 0000000000..b7fd1593b8
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-chromeutils-import-null.js
@@ -0,0 +1,44 @@
+/**
+ * @fileoverview Reject calls to ChromeUtils.import(..., null). This allows to
+ * retrieve the global object for the JSM, instead we should rely on explicitly
+ * exported symbols.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+function isIdentifier(node, id) {
+ return node && node.type === "Identifier" && node.name === id;
+}
+
+module.exports = function(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ return {
+ CallExpression(node) {
+ let { callee } = node;
+ if (
+ isIdentifier(callee.object, "ChromeUtils") &&
+ isIdentifier(callee.property, "import") &&
+ node.arguments.length >= 2 &&
+ node.arguments[1].type == "Literal" &&
+ node.arguments[1].raw == "null"
+ ) {
+ context.report(
+ node,
+ "ChromeUtils.import should not be called with (..., null) to " +
+ "retrieve the JSM global object. Rely on explicit exports instead."
+ );
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-importGlobalProperties.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-importGlobalProperties.js
new file mode 100644
index 0000000000..a2f495ddb7
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-importGlobalProperties.js
@@ -0,0 +1,62 @@
+/**
+ * @fileoverview Reject use of Cu.importGlobalProperties
+ *
+ * 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";
+
+const privilegedGlobals = Object.keys(
+ require("../environments/privileged.js").globals
+);
+
+// -----------------------------------------------------------------------------
+// Rule Definition
+// -----------------------------------------------------------------------------
+
+module.exports = {
+ meta: {
+ messages: {
+ unexpectedCall: "Unexpected call to Cu.importGlobalProperties",
+ unexpectedCallWebIdl:
+ "Unnecessary call to Cu.importGlobalProperties (webidl names are automatically imported)",
+ },
+ schema: [
+ {
+ // XXX Better name?
+ enum: ["everything", "allownonwebidl"],
+ },
+ ],
+ type: "problem",
+ },
+
+ create(context) {
+ return {
+ CallExpression(node) {
+ if (node.callee.type !== "MemberExpression") {
+ return;
+ }
+ let memexp = node.callee;
+ if (
+ memexp.object.type === "Identifier" &&
+ // Only Cu, not Components.utils; see bug 1230369.
+ memexp.object.name === "Cu" &&
+ memexp.property.type === "Identifier" &&
+ memexp.property.name === "importGlobalProperties"
+ ) {
+ if (context.options.includes("allownonwebidl")) {
+ for (let element of node.arguments[0].elements) {
+ if (privilegedGlobals.includes(element.value)) {
+ context.report({ node, messageId: "unexpectedCallWebIdl" });
+ }
+ }
+ } else {
+ context.report({ node, messageId: "unexpectedCall" });
+ }
+ }
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-relative-requires.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-relative-requires.js
new file mode 100644
index 0000000000..eb100b13fa
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-relative-requires.js
@@ -0,0 +1,34 @@
+/**
+ * @fileoverview Reject some uses of require.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+var helpers = require("../helpers");
+
+module.exports = function(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ const isRelativePath = function(path) {
+ return path.startsWith("./") || path.startsWith("../");
+ };
+
+ return {
+ CallExpression(node) {
+ const path = helpers.getDevToolsRequirePath(node);
+ if (path && isRelativePath(path)) {
+ context.report(node, "relative paths are not allowed with require()");
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-some-requires.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-some-requires.js
new file mode 100644
index 0000000000..ab31069293
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/reject-some-requires.js
@@ -0,0 +1,35 @@
+/**
+ * @fileoverview Reject some uses of require.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+var helpers = require("../helpers");
+
+module.exports = function(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ if (typeof context.options[0] !== "string") {
+ throw new Error("reject-some-requires expects a regexp");
+ }
+ const RX = new RegExp(context.options[0]);
+
+ return {
+ CallExpression(node) {
+ const path = helpers.getDevToolsRequirePath(node);
+ if (path && RX.test(path)) {
+ context.report(node, `require(${path}) is not allowed`);
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/rejects-requires-await.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/rejects-requires-await.js
new file mode 100644
index 0000000000..1bc0bcd58d
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/rejects-requires-await.js
@@ -0,0 +1,42 @@
+/**
+ * @fileoverview Reject use of Cu.importGlobalProperties
+ *
+ * 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";
+
+module.exports = {
+ meta: {
+ messages: {
+ rejectRequiresAwait: "Assert.rejects needs to be preceded by await.",
+ },
+ },
+
+ create(context) {
+ return {
+ CallExpression(node) {
+ if (node.callee.type === "MemberExpression") {
+ let memexp = node.callee;
+ if (
+ memexp.object.type === "Identifier" &&
+ memexp.object.name === "Assert" &&
+ memexp.property.type === "Identifier" &&
+ memexp.property.name === "rejects"
+ ) {
+ // We have ourselves an Assert.rejects.
+
+ if (node.parent.type !== "AwaitExpression") {
+ context.report({
+ node,
+ messageId: "rejectRequiresAwait",
+ });
+ }
+ }
+ }
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-cc-etc.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-cc-etc.js
new file mode 100644
index 0000000000..053f379adb
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-cc-etc.js
@@ -0,0 +1,44 @@
+/**
+ * @fileoverview Reject use of Components.classes etc, prefer the shorthand instead.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+const componentsMap = {
+ classes: "Cc",
+ interfaces: "Ci",
+ results: "Cr",
+ utils: "Cu",
+};
+
+module.exports = function(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ return {
+ MemberExpression(node) {
+ if (
+ node.object.type === "Identifier" &&
+ node.object.name === "Components" &&
+ node.property.type === "Identifier" &&
+ Object.getOwnPropertyNames(componentsMap).includes(node.property.name)
+ ) {
+ context.report(
+ node,
+ `Use ${componentsMap[node.property.name]} rather than Components.${
+ node.property.name
+ }`
+ );
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-chromeutils-generateqi.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-chromeutils-generateqi.js
new file mode 100644
index 0000000000..6d72f785de
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-chromeutils-generateqi.js
@@ -0,0 +1,103 @@
+/**
+ * @fileoverview Reject use of XPCOMUtils.generateQI and JS-implemented
+ * QueryInterface methods in favor of ChromeUtils.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+function isIdentifier(node, id) {
+ return node && node.type === "Identifier" && node.name === id;
+}
+
+function isMemberExpression(node, object, member) {
+ return (
+ node.type === "MemberExpression" &&
+ isIdentifier(node.object, object) &&
+ isIdentifier(node.property, member)
+ );
+}
+
+const MSG_NO_JS_QUERY_INTERFACE =
+ "Please use ChromeUtils.generateQI rather than manually creating " +
+ "JavaScript QueryInterface functions";
+
+const MSG_NO_XPCOMUTILS_GENERATEQI =
+ "Please use ChromeUtils.generateQI instead of XPCOMUtils.generateQI";
+
+function funcToGenerateQI(context, node) {
+ const sourceCode = context.getSourceCode();
+ const text = sourceCode.getText(node);
+
+ let interfaces = [];
+ let match;
+ let re = /\bCi\.([a-zA-Z0-9]+)\b|\b(nsI[A-Z][a-zA-Z0-9]+)\b/g;
+ while ((match = re.exec(text))) {
+ interfaces.push(match[1] || match[2]);
+ }
+
+ let ifaces = interfaces
+ .filter(iface => iface != "nsISupports")
+ .map(iface => JSON.stringify(iface))
+ .join(", ");
+
+ return `ChromeUtils.generateQI([${ifaces}])`;
+}
+
+module.exports = {
+ meta: {
+ fixable: "code",
+ },
+
+ create(context) {
+ return {
+ CallExpression(node) {
+ let { callee } = node;
+ if (isMemberExpression(callee, "XPCOMUtils", "generateQI")) {
+ context.report({
+ node,
+ message: MSG_NO_XPCOMUTILS_GENERATEQI,
+ fix(fixer) {
+ return fixer.replaceText(callee, "ChromeUtils.generateQI");
+ },
+ });
+ }
+ },
+
+ "AssignmentExpression > MemberExpression[property.name='QueryInterface']": function(
+ node
+ ) {
+ const { right } = node.parent;
+ if (right.type === "FunctionExpression") {
+ context.report({
+ node: node.parent,
+ message: MSG_NO_JS_QUERY_INTERFACE,
+ fix(fixer) {
+ return fixer.replaceText(right, funcToGenerateQI(context, right));
+ },
+ });
+ }
+ },
+
+ "Property[key.name='QueryInterface'][value.type='FunctionExpression']": function(
+ node
+ ) {
+ context.report({
+ node,
+ message: MSG_NO_JS_QUERY_INTERFACE,
+ fix(fixer) {
+ let generateQI = funcToGenerateQI(context, node.value);
+ return fixer.replaceText(node, `QueryInterface: ${generateQI}`);
+ },
+ });
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-chromeutils-import.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-chromeutils-import.js
new file mode 100644
index 0000000000..eefff06e11
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-chromeutils-import.js
@@ -0,0 +1,89 @@
+/**
+ * @fileoverview Reject use of Cu.import and XPCOMUtils.defineLazyModuleGetter
+ * in favor of ChromeUtils.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+function isIdentifier(node, id) {
+ return node && node.type === "Identifier" && node.name === id;
+}
+
+function isMemberExpression(node, object, member) {
+ return (
+ node.type === "MemberExpression" &&
+ isIdentifier(node.object, object) &&
+ isIdentifier(node.property, member)
+ );
+}
+
+module.exports = {
+ meta: {
+ schema: [
+ {
+ type: "object",
+ properties: {
+ allowCu: {
+ type: "boolean",
+ },
+ },
+ additionalProperties: false,
+ },
+ ],
+ fixable: "code",
+ },
+
+ create(context) {
+ return {
+ CallExpression(node) {
+ if (node.callee.type !== "MemberExpression") {
+ return;
+ }
+
+ let { allowCu } = context.options[0] || {};
+ let { callee } = node;
+
+ // Is the expression starting with `Cu` or `Components.utils`?
+ if (
+ ((!allowCu && isIdentifier(callee.object, "Cu")) ||
+ isMemberExpression(callee.object, "Components", "utils")) &&
+ isIdentifier(callee.property, "import")
+ ) {
+ context.report({
+ node,
+ message: "Please use ChromeUtils.import instead of Cu.import",
+ fix(fixer) {
+ return fixer.replaceText(callee, "ChromeUtils.import");
+ },
+ });
+ }
+
+ if (
+ isMemberExpression(callee, "XPCOMUtils", "defineLazyModuleGetter") &&
+ node.arguments.length < 4
+ ) {
+ context.report({
+ node,
+ message:
+ "Please use ChromeUtils.defineModuleGetter instead of " +
+ "XPCOMUtils.defineLazyModuleGetter",
+ fix(fixer) {
+ return fixer.replaceText(
+ callee,
+ "ChromeUtils.defineModuleGetter"
+ );
+ },
+ });
+ }
+ },
+ };
+ },
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-default-preference-values.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-default-preference-values.js
new file mode 100644
index 0000000000..7c2ffaf933
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-default-preference-values.js
@@ -0,0 +1,45 @@
+/**
+ * @fileoverview Require providing a second parameter to get*Pref
+ * methods instead of using a try/catch block.
+ *
+ * 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 {
+ TryStatement(node) {
+ let types = ["Bool", "Char", "Float", "Int"];
+ let methods = types.map(type => "get" + type + "Pref");
+ if (node.block.type != "BlockStatement" || node.block.body.length != 1) {
+ return;
+ }
+
+ let firstStm = node.block.body[0];
+ if (
+ firstStm.type != "ExpressionStatement" ||
+ firstStm.expression.type != "AssignmentExpression" ||
+ firstStm.expression.right.type != "CallExpression" ||
+ firstStm.expression.right.callee.type != "MemberExpression" ||
+ firstStm.expression.right.callee.property.type != "Identifier" ||
+ !methods.includes(firstStm.expression.right.callee.property.name)
+ ) {
+ return;
+ }
+
+ let msg = "provide a default value instead of using a try/catch block";
+ context.report(node, msg);
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-includes-instead-of-indexOf.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-includes-instead-of-indexOf.js
new file mode 100644
index 0000000000..d4b3da5f1e
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-includes-instead-of-indexOf.js
@@ -0,0 +1,45 @@
+/**
+ * @fileoverview Use .includes instead of .indexOf
+ *
+ * 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 {
+ BinaryExpression(node) {
+ if (
+ node.left.type != "CallExpression" ||
+ node.left.callee.type != "MemberExpression" ||
+ node.left.callee.property.type != "Identifier" ||
+ node.left.callee.property.name != "indexOf"
+ ) {
+ return;
+ }
+
+ if (
+ (["!=", "!==", "==", "==="].includes(node.operator) &&
+ node.right.type == "UnaryExpression" &&
+ node.right.operator == "-" &&
+ node.right.argument.type == "Literal" &&
+ node.right.argument.value == 1) ||
+ ([">=", "<"].includes(node.operator) &&
+ node.right.type == "Literal" &&
+ node.right.value == 0)
+ ) {
+ context.report(node, "use .includes instead of .indexOf");
+ }
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-ownerGlobal.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-ownerGlobal.js
new file mode 100644
index 0000000000..34b22a1269
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-ownerGlobal.js
@@ -0,0 +1,38 @@
+/**
+ * @fileoverview Require .ownerGlobal instead of .ownerDocument.defaultView.
+ *
+ * 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 {
+ MemberExpression(node) {
+ if (
+ node.property.type != "Identifier" ||
+ node.property.name != "defaultView" ||
+ node.object.type != "MemberExpression" ||
+ node.object.property.type != "Identifier" ||
+ node.object.property.name != "ownerDocument"
+ ) {
+ return;
+ }
+
+ context.report(
+ node,
+ "use .ownerGlobal instead of .ownerDocument.defaultView"
+ );
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-returnValue.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-returnValue.js
new file mode 100644
index 0000000000..eb693d0014
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-returnValue.js
@@ -0,0 +1,42 @@
+/**
+ * @fileoverview Warn when idempotent methods are called and their return value is unused.
+ *
+ * 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 {
+ ExpressionStatement(node) {
+ if (
+ !node.expression ||
+ node.expression.type != "CallExpression" ||
+ !node.expression.callee ||
+ node.expression.callee.type != "MemberExpression" ||
+ !node.expression.callee.property ||
+ node.expression.callee.property.type != "Identifier" ||
+ (node.expression.callee.property.name != "concat" &&
+ node.expression.callee.property.name != "join" &&
+ node.expression.callee.property.name != "slice")
+ ) {
+ return;
+ }
+
+ context.report(
+ node,
+ `{Array/String}.${node.expression.callee.property.name} doesn't modify the instance in-place`
+ );
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-services.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-services.js
new file mode 100644
index 0000000000..1235cd0f99
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/use-services.js
@@ -0,0 +1,45 @@
+/**
+ * @fileoverview Require use of Services.* rather than getService.
+ *
+ * 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";
+const helpers = require("../helpers");
+
+let servicesInterfaceMap = helpers.servicesData;
+
+// -----------------------------------------------------------------------------
+// Rule Definition
+// -----------------------------------------------------------------------------
+module.exports = function(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+ return {
+ CallExpression(node) {
+ if (
+ !node.callee ||
+ !node.callee.property ||
+ node.callee.property.type != "Identifier" ||
+ node.callee.property.name != "getService" ||
+ node.arguments.length != 1 ||
+ !node.arguments[0].property ||
+ node.arguments[0].property.type != "Identifier" ||
+ !node.arguments[0].property.name ||
+ !(node.arguments[0].property.name in servicesInterfaceMap)
+ ) {
+ return;
+ }
+
+ let serviceName = servicesInterfaceMap[node.arguments[0].property.name];
+
+ context.report(
+ node,
+ `Use Services.${serviceName} rather than getService().`
+ );
+ },
+ };
+};
diff --git a/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/var-only-at-top-level.js b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/var-only-at-top-level.js
new file mode 100644
index 0000000000..9365982b0c
--- /dev/null
+++ b/tools/lint/eslint/eslint-plugin-mozilla/lib/rules/var-only-at-top-level.js
@@ -0,0 +1,34 @@
+/**
+ * @fileoverview Marks all var declarations that are not at the top level
+ * invalid.
+ *
+ * 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
+// -----------------------------------------------------------------------------
+
+var helpers = require("../helpers");
+
+module.exports = function(context) {
+ // ---------------------------------------------------------------------------
+ // Public
+ // --------------------------------------------------------------------------
+
+ return {
+ VariableDeclaration(node) {
+ if (node.kind === "var") {
+ if (helpers.getIsGlobalScope(context.getAncestors())) {
+ return;
+ }
+
+ context.report(node, "Unexpected var, use let or const instead.");
+ }
+ },
+ };
+};