Skip to content

Commit

Permalink
Bug 1790354 - warn about using browser/ code from toolkit, r=Standard8
Browse files Browse the repository at this point in the history
  • Loading branch information
gijsk committed Oct 30, 2023
1 parent 96c9acb commit ffeedb3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1987,5 +1987,12 @@ module.exports = {
"mozilla/reject-chromeutils-import": "error",
},
},
{
files: ["toolkit/**"],
excludedFiles: ["toolkit/**/test/**", "toolkit/**/tests/**"],
rules: {
"mozilla/no-browser-refs-in-toolkit": "warn",
},
},
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
no-browser-refs-in-toolkit
==========================

Rejects for using any code from ``browser/`` (Desktop Firefox) inside
``toolkit``, as ``toolkit`` is for code shared with other Gecko consumers
like Firefox on Android, Thunderbird, etc.

Examples of incorrect code for this rule:
-----------------------------------------

.. code-block:: js
"chrome://browser/content/browser.xhtml"
"resource:///modules/BrowserWindowTracker.sys.mjs"
"browser/browser.ftl"
Examples of correct code for this rule:
---------------------------------------

.. code-block:: js
"chrome://global/content/aboutAbout.html"
"resource://gre/modules/AppConstants.sys.mjs"
"toolkit/global/aboutFoo.ftl"
1 change: 1 addition & 0 deletions tools/lint/eslint/eslint-plugin-mozilla/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module.exports = {
"no-aArgs": require("../lib/rules/no-aArgs"),
"no-addtask-setup": require("../lib/rules/no-addtask-setup"),
"no-arbitrary-setTimeout": require("../lib/rules/no-arbitrary-setTimeout"),
"no-browser-refs-in-toolkit": require("../lib/rules/no-browser-refs-in-toolkit"),
"no-compare-against-boolean-literals": require("../lib/rules/no-compare-against-boolean-literals"),
"no-cu-reportError": require("../lib/rules/no-cu-reportError"),
"no-define-cc-etc": require("../lib/rules/no-define-cc-etc"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @fileoverview Reject use of browser/-based references from code in
* directories like toolkit/ that ought not to depend on
* running inside desktop Firefox.
*
* 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: {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/no-browser-refs-in-toolkit.html",
},
messages: {
noBrowserChrome:
"> {{url}} is part of Desktop Firefox and cannot be unconditionally " +
"used by this code (which has to also work elsewhere).",
},
schema: [],
type: "suggestion",
},

create(context) {
return {
Literal(node) {
if (typeof node.value != "string") {
return;
}
if (
node.value.startsWith("chrome://browser") ||
node.value.startsWith("resource:///") ||
node.value.startsWith("resource://app/") ||
(node.value.startsWith("browser/") && node.value.endsWith(".ftl"))
) {
context.report({
node,
messageId: "noBrowserChrome",
data: { url: node.value },
});
}
},
};
},
};

0 comments on commit ffeedb3

Please sign in to comment.