From 535d27b501b8cb0591ebc0b9e133f2291b74cd59 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 8 Apr 2024 14:09:25 +0200 Subject: [PATCH 1/3] trying to support eslint 9 --- lib/util/Components.js | 46 +++++++++++++++++++++--------------------- lib/util/variable.js | 16 +++++++++++---- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/lib/util/Components.js b/lib/util/Components.js index 5e47f3e..70740c9 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 (_node) { return ( - utils.getParentES6Component() - || utils.getParentES5Component() - || utils.getParentStatelessComponent() + utils.getParentES6Component(_node) + || utils.getParentES5Component(_node) + || utils.getParentStatelessComponent(_node) ); }, @@ -178,9 +178,9 @@ function componentRule(rule, context) { * * @returns {ASTNode} component node, null if we are not in a component */ - getParentES5Component: function () { + getParentES5Component: function (_node) { // eslint-disable-next-line react/destructuring-assignment - let scope = context.getScope(); + let scope = (context.sourceCode || context).getScope(_node); 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 (_node) { + let scope = (context.sourceCode || context).getScope(_node); 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 (_node) { // eslint-disable-next-line react/destructuring-assignment - let scope = context.getScope(); + let scope = (context.sourceCode || context).getScope(_node); 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 (_node) { + const node = utils.getParentComponent(_node); if (!node) { return; } @@ -338,24 +338,24 @@ function componentRule(rule, context) { components.add(node, 2); }, - FunctionExpression: function () { - const node = utils.getParentComponent(); + FunctionExpression: function (_node) { + const node = utils.getParentComponent(_node); if (!node) { return; } components.add(node, 1); }, - FunctionDeclaration: function () { - const node = utils.getParentComponent(); + FunctionDeclaration: function (_node) { + const node = utils.getParentComponent(_node); if (!node) { return; } components.add(node, 1); }, - ArrowFunctionExpression: function () { - const node = utils.getParentComponent(); + ArrowFunctionExpression: function (_node) { + const node = utils.getParentComponent(_node); if (!node) { return; } @@ -366,8 +366,8 @@ function componentRule(rule, context) { } }, - ThisExpression: function () { - const node = utils.getParentComponent(); + ThisExpression: function (_node) { + const node = utils.getParentComponent(_node); 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..592b0e2 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,14 @@ 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 +75,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, _node) { + let scope = getScope(context, node); let { variables } = scope; while (scope.type !== 'global') { From f374f75af2d3e6aede979aafd9b55d215d49dec8 Mon Sep 17 00:00:00 2001 From: Hugo Rune Date: Wed, 14 Aug 2024 10:52:49 +0200 Subject: [PATCH 2/3] Fix lint --- lib/util/Components.js | 40 ++++++++++++++++++++-------------------- lib/util/variable.js | 5 ++--- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/lib/util/Components.js b/lib/util/Components.js index 70740c9..f744d52 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -165,11 +165,11 @@ function componentRule(rule, context, _node) { * * @returns {ASTNode} component node, null if we are not in a component */ - getParentComponent: function (_node) { + getParentComponent: function (_n) { return ( - utils.getParentES6Component(_node) - || utils.getParentES5Component(_node) - || utils.getParentStatelessComponent(_node) + utils.getParentES6Component(_n) + || utils.getParentES5Component(_n) + || utils.getParentStatelessComponent(_n) ); }, @@ -178,9 +178,9 @@ function componentRule(rule, context, _node) { * * @returns {ASTNode} component node, null if we are not in a component */ - getParentES5Component: function (_node) { + getParentES5Component: function (_n) { // eslint-disable-next-line react/destructuring-assignment - let scope = (context.sourceCode || context).getScope(_node); + 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, _node) { * * @returns {ASTNode} component node, null if we are not in a component */ - getParentES6Component: function (_node) { - let scope = (context.sourceCode || context).getScope(_node); + 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, _node) { * * @returns {ASTNode} component node, null if we are not in a component */ - getParentStatelessComponent: function (_node) { + getParentStatelessComponent: function (_n) { // eslint-disable-next-line react/destructuring-assignment - let scope = (context.sourceCode || context).getScope(_node); + let scope = (context.sourceCode || context).getScope(_n); while (scope) { const node = scope.block; // Ignore non functions @@ -323,8 +323,8 @@ function componentRule(rule, context, _node) { components.add(node, 2); }, - ClassProperty: function (_node) { - const node = utils.getParentComponent(_node); + ClassProperty: function (_n) { + const node = utils.getParentComponent(_n); if (!node) { return; } @@ -338,24 +338,24 @@ function componentRule(rule, context, _node) { components.add(node, 2); }, - FunctionExpression: function (_node) { - const node = utils.getParentComponent(_node); + FunctionExpression: function (_n) { + const node = utils.getParentComponent(_n); if (!node) { return; } components.add(node, 1); }, - FunctionDeclaration: function (_node) { - const node = utils.getParentComponent(_node); + FunctionDeclaration: function (_n) { + const node = utils.getParentComponent(_n); if (!node) { return; } components.add(node, 1); }, - ArrowFunctionExpression: function (_node) { - const node = utils.getParentComponent(_node); + ArrowFunctionExpression: function (_n) { + const node = utils.getParentComponent(_n); if (!node) { return; } @@ -366,8 +366,8 @@ function componentRule(rule, context, _node) { } }, - ThisExpression: function (_node) { - const node = utils.getParentComponent(_node); + ThisExpression: function (_n) { + const node = utils.getParentComponent(_n); if (!node || !/Function/.test(node.type)) { return; } diff --git a/lib/util/variable.js b/lib/util/variable.js index 592b0e2..ddb02da 100644 --- a/lib/util/variable.js +++ b/lib/util/variable.js @@ -65,7 +65,6 @@ function getScope(context, node) { return context.getScope(); } - /** * List all variable in a given scope * @@ -75,8 +74,8 @@ function getScope(context, node) { * @param {Array} name The name of the variable to search. * @returns {Boolean} True if the variable was found, false if not. */ -function variablesInScope(context, _node) { - let scope = getScope(context, node); +function variablesInScope(context, _n) { + let scope = getScope(context, _n); let { variables } = scope; while (scope.type !== 'global') { From a5d6c1568d8d1712d3112ffc274c426b7522b603 Mon Sep 17 00:00:00 2001 From: Hugo Rune Date: Wed, 14 Aug 2024 10:53:33 +0200 Subject: [PATCH 3/3] Add eslint9 to peerDeps --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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",