Skip to content

Commit

Permalink
Bug 1858148 - Change ESLint rule reject-importGlobalProperties to rej…
Browse files Browse the repository at this point in the history
…ect all imports in sjs files. r=mossop

Differential Revision: https://phabricator.services.mozilla.com/D190760
  • Loading branch information
Standard8 committed Nov 4, 2023
1 parent a033642 commit 922f753
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ In system modules all the required properties should already be available. In
non-module code or non-system modules, webidl defined interfaces should already
be available and hence do not need importing.

For sjs test files, if the relevant global is not already available, then consider
extending the `list of globals available from the httpd server <https://searchfox.org/mozilla-central/rev/e9b338c2d597067f99e96d5f20769f41f312fa8f/netwerk/test/httpserver/httpd.sys.mjs#2875-2889>`_.

Options
-------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ module.exports = {
},
files: ["**/*.sjs"],
rules: {
// TODO Bug 1501127: sjs files have their own sandbox, and do not inherit
// the Window backstage pass directly. Turn this rule off for sjs files for
// now until we develop a solution.
"mozilla/reject-importGlobalProperties": "off",
// For sjs files, reject everything as we should update the sandbox
// to include the globals we need, as these are test-only files.
"mozilla/reject-importGlobalProperties": ["error", "everything"],
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const privilegedGlobals = Object.keys(
require("../environments/privileged.js").globals
);

function getMessageId(context) {
return path.extname(context.getFilename()) == ".sjs"
? "unexpectedCallSjs"
: "unexpectedCall";
}

module.exports = {
meta: {
docs: {
Expand All @@ -23,6 +29,8 @@ module.exports = {
unexpectedCall: "Unexpected call to Cu.importGlobalProperties",
unexpectedCallCuWebIdl:
"Unnecessary call to Cu.importGlobalProperties for {{name}} (webidl names are automatically imported)",
unexpectedCallSjs:
"Do not call Cu.importGlobalProperties in sjs files, expand the global instead (see rule docs).",
unexpectedCallXPCOMWebIdl:
"Unnecessary call to XPCOMUtils.defineLazyGlobalGetters for {{name}} (webidl names are automatically imported)",
},
Expand All @@ -37,12 +45,7 @@ module.exports = {
create(context) {
return {
CallExpression(node) {
if (
node.callee.type !== "MemberExpression" ||
// TODO Bug 1501127: sjs files have their own sandbox, and do not inherit
// the Window backstage pass directly.
path.extname(context.getFilename()) == ".sjs"
) {
if (node.callee.type !== "MemberExpression") {
return;
}
let memexp = node.callee;
Expand All @@ -64,7 +67,7 @@ module.exports = {
}
}
} else {
context.report({ node, messageId: "unexpectedCall" });
context.report({ node, messageId: getMessageId(context) });
}
}
if (
Expand All @@ -85,7 +88,7 @@ module.exports = {
}
}
} else {
context.report({ node, messageId: "unexpectedCall" });
context.report({ node, messageId: getMessageId(context) });
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ ruleTester.run("reject-importGlobalProperties", rule, {
options: ["allownonwebidl"],
code: "XPCOMUtils.defineLazyGlobalGetters(this, ['fetch'])",
},
{
options: ["allownonwebidl"],
code: "Cu.importGlobalProperties(['TextEncoder'])",
filename: "foo.sjs",
},
{
options: ["allownonwebidl"],
code: "XPCOMUtils.defineLazyGlobalGetters(this, ['TextEncoder'])",
filename: "foo.sjs",
},
],
invalid: [
{
Expand All @@ -56,6 +46,12 @@ ruleTester.run("reject-importGlobalProperties", rule, {
options: ["everything"],
errors: [{ messageId: "unexpectedCall" }],
},
{
options: ["everything"],
code: "XPCOMUtils.defineLazyGlobalGetters(this, ['TextEncoder'])",
errors: [{ messageId: "unexpectedCallSjs" }],
filename: "foo.sjs",
},
{
code: "XPCOMUtils.defineLazyGlobalGetters(this, ['TextEncoder'])",
options: ["everything"],
Expand Down Expand Up @@ -83,5 +79,11 @@ ruleTester.run("reject-importGlobalProperties", rule, {
errors: [{ messageId: "unexpectedCallXPCOMWebIdl" }],
filename: "foo.js",
},
{
options: ["allownonwebidl"],
code: "Cu.importGlobalProperties(['TextEncoder'])",
errors: [{ messageId: "unexpectedCallCuWebIdl" }],
filename: "foo.sjs",
},
],
});

0 comments on commit 922f753

Please sign in to comment.