Skip to content

Commit

Permalink
Bug 1890330 - Support new ESLint APIs in eslint-plugin-mozilla. r=fro…
Browse files Browse the repository at this point in the history
…ntend-codestyle-reviewers,Gijs

This gets the ESLint plugin code in a state where it can work with the old APIs or the newer ESLint v9 ones.

The testing system will be updated to ESLint v9 when we do the main upgrade. However, in the meantime, this allows
consumers to start testing out v9 implementations.

Differential Revision: https://phabricator.services.mozilla.com/D206914
  • Loading branch information
Standard8 committed Apr 9, 2024
1 parent 339c073 commit 4618d67
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 16 deletions.
12 changes: 8 additions & 4 deletions tools/lint/eslint/eslint-plugin-mozilla/lib/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,8 @@ module.exports = {
let globalScope;

let parser = {
Program() {
globalScope = context.getScope();
Program(node) {
globalScope = helpers.getScope(context, node);
},
};
let filename = context.getFilename();
Expand All @@ -651,10 +651,14 @@ module.exports = {
for (let type of Object.keys(GlobalsForNode.prototype)) {
parser[type] = function (node) {
if (type === "Program") {
globalScope = context.getScope();
globalScope = helpers.getScope(context, node);
helpers.addGlobals(extraHTMLGlobals, globalScope);
}
let globals = handler[type](node, context.getAncestors(), globalScope);
let globals = handler[type](
node,
helpers.getAncestors(context, node),
globalScope
);
helpers.addGlobals(
globals,
globalScope,
Expand Down
34 changes: 34 additions & 0 deletions tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,4 +794,38 @@ module.exports = {
}
return null;
},

/**
* Gets the scope for a node taking account of where the scope function
* is available (supports node versions earlier than 8.37.0).
*
* @param {object} context
* The context passed from ESLint.
* @param {object} node
* The node to get the scope for.
* returns {function}
* The getScope function object.
*/
getScope(context, node) {
return context.sourceCode?.getScope
? context.sourceCode.getScope(node)
: context.getScope();
},

/**
* Gets the ancestors for a node taking account of where the ancestors function
* is available (supports node versions earlier than 8.38.0).
*
* @param {object} context
* The context passed from ESLint.
* @param {object} node
* The node to get the scope for.
* returns {function}
* The getScope function object.
*/
getAncestors(context, node) {
return context.sourceCode?.getAncestors
? context.sourceCode.getAncestors(node)
: context.getAncestors();
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,26 @@ function markArrayElementsAsUsed(context, node, expression) {
}

for (let element of expression.elements) {
context.markVariableAsUsed(element.value);
context.markVariableAsUsed
? context.markVariableAsUsed(element.value)
: context.sourceCode.markVariableAsUsed(element.value);
}
// Also mark EXPORTED_SYMBOLS as used.
context.markVariableAsUsed("EXPORTED_SYMBOLS");
context.markVariableAsUsed
? context.markVariableAsUsed("EXPORTED_SYMBOLS")
: context.sourceCode.markVariableAsUsed("EXPORTED_SYMBOLS");
}

// 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(context) {
function isGlobalScope(context, node) {
if (context.sourceCode?.getScope) {
let upper = context.sourceCode.getScope(node).upper;
// ESLint v9 uses a global scope object with type = "global". Earlier
// versions use a null upper scope.
return !upper || upper.type == "global";
}
return !context.getScope().upper;
}

Expand All @@ -55,14 +65,14 @@ module.exports = {
node.left.type === "MemberExpression" &&
node.left.object.type === "ThisExpression" &&
node.left.property.name === "EXPORTED_SYMBOLS" &&
isGlobalScope(context)
isGlobalScope(context, node)
) {
markArrayElementsAsUsed(context, node, node.right);
}
},

VariableDeclaration(node) {
if (!isGlobalScope(context)) {
if (!isGlobalScope(context, node)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
create(context) {
return {
ThisExpression(node) {
if (!helpers.getIsGlobalThis(context.getAncestors())) {
if (!helpers.getIsGlobalThis(helpers.getAncestors(context, node))) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = {
(callerSource === "ChromeUtils.import" ||
callerSource === "ChromeUtils.importESModule") &&
helpers.getIsTopLevelAndUnconditionallyExecuted(
context.getAncestors()
helpers.getAncestors(context, node)
)
) {
if (node.arguments.length < 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ module.exports = {
create(context) {
return {
AwaitExpression(node) {
if (!helpers.getIsTopLevelScript(context.getAncestors())) {
if (!helpers.getIsTopLevelScript(helpers.getAncestors(context, node))) {
return;
}
context.report({ node, messageId: "rejectTopLevelAwait" });
},
ForOfStatement(node) {
if (
!node.await ||
!helpers.getIsTopLevelScript(context.getAncestors())
!helpers.getIsTopLevelScript(helpers.getAncestors(context, node))
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
const fs = require("fs");

const { maybeGetMemberPropertyName } = require("../helpers");
const helpers = require("../helpers");

const privilegedGlobals = Object.keys(
require("../environments/privileged.js").globals
Expand Down Expand Up @@ -133,7 +134,7 @@ module.exports = {
const { operator, right } = node;
if (
operator === "instanceof" &&
pointsToDOMInterface(context.getScope(), right)
pointsToDOMInterface(helpers.getScope(context, node), right)
) {
context.report({
node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
node.init?.type != "CallExpression" ||
node.init?.callee?.type != "MemberExpression" ||
!context.getFilename().endsWith(".sys.mjs") ||
!helpers.isTopLevel(context.getAncestors())
!helpers.isTopLevel(helpers.getAncestors(context, node))
) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ module.exports = {
}
if (
helpers.getIsTopLevelAndUnconditionallyExecuted(
context.getAncestors()
helpers.getAncestors(context, node)
)
) {
context.report({
Expand Down

0 comments on commit 4618d67

Please sign in to comment.