From 032e96aad23b265d838068a1c89ec12fb05d23ea Mon Sep 17 00:00:00 2001 From: Martin <8627696+MartinInAction@users.noreply.github.com> Date: Mon, 30 Dec 2024 21:11:46 +0100 Subject: [PATCH] flat config eslint 9.x.x working (#336) * trying to support eslint 9 * Fix lint * Add eslint9 to peerDeps --------- Co-authored-by: Hugo Rune --- lib/util/Components.js | 46 +++++++++++++++++++++--------------------- lib/util/variable.js | 15 ++++++++++---- package-lock.json | 2 +- package.json | 2 +- 4 files changed, 36 insertions(+), 29 deletions(-) diff --git a/lib/util/Components.js b/lib/util/Components.js index 5e47f3e..f744d52 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -99,7 +99,7 @@ Components.prototype.length = function () { return length; }; -function componentRule(rule, context) { +function componentRule(rule, context, _node) { const sourceCode = context.getSourceCode(); const components = new Components(); @@ -165,11 +165,11 @@ function componentRule(rule, context) { * * @returns {ASTNode} component node, null if we are not in a component */ - getParentComponent: function () { + getParentComponent: function (_n) { return ( - utils.getParentES6Component() - || utils.getParentES5Component() - || utils.getParentStatelessComponent() + utils.getParentES6Component(_n) + || utils.getParentES5Component(_n) + || utils.getParentStatelessComponent(_n) ); }, @@ -178,9 +178,9 @@ function componentRule(rule, context) { * * @returns {ASTNode} component node, null if we are not in a component */ - getParentES5Component: function () { + getParentES5Component: function (_n) { // eslint-disable-next-line react/destructuring-assignment - let scope = context.getScope(); + let scope = (context.sourceCode || context).getScope(_n); while (scope) { const node = scope.block && scope.block.parent && scope.block.parent.parent; if (node && utils.isES5Component(node)) { @@ -196,8 +196,8 @@ function componentRule(rule, context) { * * @returns {ASTNode} component node, null if we are not in a component */ - getParentES6Component: function () { - let scope = context.getScope(); + getParentES6Component: function (_n) { + let scope = (context.sourceCode || context).getScope(_n); while (scope && scope.type !== 'class') { scope = scope.upper; } @@ -213,9 +213,9 @@ function componentRule(rule, context) { * * @returns {ASTNode} component node, null if we are not in a component */ - getParentStatelessComponent: function () { + getParentStatelessComponent: function (_n) { // eslint-disable-next-line react/destructuring-assignment - let scope = context.getScope(); + let scope = (context.sourceCode || context).getScope(_n); while (scope) { const node = scope.block; // Ignore non functions @@ -263,7 +263,7 @@ function componentRule(rule, context) { return null; } let variableInScope; - const { variables } = context.getScope(); + const { variables } = (context.sourceCode || context).getScope(_node); for (i = 0, j = variables.length; i < j; i++) { // eslint-disable-line no-plusplus if (variables[i].name === variableName) { variableInScope = variables[i]; @@ -323,8 +323,8 @@ function componentRule(rule, context) { components.add(node, 2); }, - ClassProperty: function () { - const node = utils.getParentComponent(); + ClassProperty: function (_n) { + const node = utils.getParentComponent(_n); if (!node) { return; } @@ -338,24 +338,24 @@ function componentRule(rule, context) { components.add(node, 2); }, - FunctionExpression: function () { - const node = utils.getParentComponent(); + FunctionExpression: function (_n) { + const node = utils.getParentComponent(_n); if (!node) { return; } components.add(node, 1); }, - FunctionDeclaration: function () { - const node = utils.getParentComponent(); + FunctionDeclaration: function (_n) { + const node = utils.getParentComponent(_n); if (!node) { return; } components.add(node, 1); }, - ArrowFunctionExpression: function () { - const node = utils.getParentComponent(); + ArrowFunctionExpression: function (_n) { + const node = utils.getParentComponent(_n); if (!node) { return; } @@ -366,8 +366,8 @@ function componentRule(rule, context) { } }, - ThisExpression: function () { - const node = utils.getParentComponent(); + ThisExpression: function (_n) { + const node = utils.getParentComponent(_n); if (!node || !/Function/.test(node.type)) { return; } @@ -379,7 +379,7 @@ function componentRule(rule, context) { if (!utils.isReturningJSX(node)) { return; } - const parentNode = utils.getParentComponent(); + const parentNode = utils.getParentComponent(node); if (!parentNode) { return; } diff --git a/lib/util/variable.js b/lib/util/variable.js index 38dc59d..ddb02da 100644 --- a/lib/util/variable.js +++ b/lib/util/variable.js @@ -11,8 +11,8 @@ * @param {String} name The name of the variable to mark as used. * @returns {Boolean} True if the variable was found and marked as used, false if not. */ -function markVariableAsUsed(context, name) { - let scope = context.getScope(); +function markVariableAsUsed(context, name, _node) { + let scope = (context.sourceCode || context).getScope(_node); let variables; let i; let len; @@ -58,6 +58,13 @@ function findVariable(variables, name) { return false; } +function getScope(context, node) { + if (context.sourceCode && context.sourceCode.getScope) { + return context.sourceCode.getScope(node); + } + return context.getScope(); +} + /** * List all variable in a given scope * @@ -67,8 +74,8 @@ function findVariable(variables, name) { * @param {Array} name The name of the variable to search. * @returns {Boolean} True if the variable was found, false if not. */ -function variablesInScope(context) { - let scope = context.getScope(); +function variablesInScope(context, _n) { + let scope = getScope(context, _n); let { variables } = scope; while (scope.type !== 'global') { diff --git a/package-lock.json b/package-lock.json index c8fc42b..e03b5d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,7 +25,7 @@ "typescript": "^5.2.2" }, "peerDependencies": { - "eslint": "^3.17.0 || ^4 || ^5 || ^6 || ^7 || ^8" + "eslint": "^3.17.0 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9" } }, "node_modules/@aashutoshrathi/word-wrap": { diff --git a/package.json b/package.json index 6901ee9..a908f88 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "typescript": "^5.2.2" }, "peerDependencies": { - "eslint": "^3.17.0 || ^4 || ^5 || ^6 || ^7 || ^8" + "eslint": "^3.17.0 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9" }, "keywords": [ "eslint",