From 55cd0649066f666d72cd2a3efdc9ba3b803a3933 Mon Sep 17 00:00:00 2001 From: Lucas Hill Date: Tue, 25 Jun 2024 13:21:41 -0400 Subject: [PATCH] Fix API deprecations blocking eslint v9 compatibility (#2153) * Fix API deprecations blocking eslint v9 compatibility * Remove unused property * Make accessing ancestors compatible with older eslint versions. * Fix getScope eslint v9 deprecation --- lib/rules/jquery-ember-run.js | 6 ++- lib/rules/no-ember-testing-in-module-scope.js | 5 +- lib/rules/no-global-jquery.js | 7 ++- lib/rules/no-jquery.js | 6 ++- lib/rules/prefer-ember-test-helpers.js | 7 ++- lib/rules/require-fetch-import.js | 7 ++- lib/rules/require-return-from-computed.js | 47 +++++++++++++++---- lib/rules/use-ember-get-and-set.js | 4 +- 8 files changed, 67 insertions(+), 22 deletions(-) diff --git a/lib/rules/jquery-ember-run.js b/lib/rules/jquery-ember-run.js index 35aab5fce3..8b07eebc0e 100644 --- a/lib/rules/jquery-ember-run.js +++ b/lib/rules/jquery-ember-run.js @@ -119,8 +119,10 @@ module.exports = { } }, - 'Program:exit'() { - const scope = context.getScope(); + 'Program:exit'(node) { + const sourceCode = context.sourceCode ?? context.getSourceCode(); + const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope(); + const tracker = new ReferenceTracker(scope); /** diff --git a/lib/rules/no-ember-testing-in-module-scope.js b/lib/rules/no-ember-testing-in-module-scope.js index 309d401a38..b93b254a16 100644 --- a/lib/rules/no-ember-testing-in-module-scope.js +++ b/lib/rules/no-ember-testing-in-module-scope.js @@ -45,7 +45,10 @@ module.exports = { node.parent.type === 'MemberExpression' && node.parent.object.name === emberImportAliasName ) { - if (context.getScope().variableScope.type === 'module') { + const sourceCode = context.sourceCode ?? context.getSourceCode(); + const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope(); + + if (scope.variableScope.type === 'module') { context.report({ node: node.parent, message: ERROR_MESSAGES[0] }); } const nodeGrandParent = utils.getPropertyValue(node, 'parent.parent.type'); diff --git a/lib/rules/no-global-jquery.js b/lib/rules/no-global-jquery.js index 7eaf6410af..673236f16a 100644 --- a/lib/rules/no-global-jquery.js +++ b/lib/rules/no-global-jquery.js @@ -27,8 +27,11 @@ module.exports = { create(context) { return { - Program() { - const tracker = new ReferenceTracker(context.getScope()); + Program(node) { + const sourceCode = context.sourceCode ?? context.getSourceCode(); + const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope(); + + const tracker = new ReferenceTracker(scope); for (const { node } of tracker.iterateGlobalReferences(globalMap)) { context.report({ node, message: ERROR_MESSAGE }); diff --git a/lib/rules/no-jquery.js b/lib/rules/no-jquery.js index b7011257ed..65a3737aa9 100644 --- a/lib/rules/no-jquery.js +++ b/lib/rules/no-jquery.js @@ -48,8 +48,10 @@ module.exports = { /** * Report references of jQuery */ - Program() { - const scope = context.getScope(); + Program(node) { + const sourceCode = context.sourceCode ?? context.getSourceCode(); + const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope(); + const tracker = new ReferenceTracker(scope); /** diff --git a/lib/rules/prefer-ember-test-helpers.js b/lib/rules/prefer-ember-test-helpers.js index d2298f2177..853dce3908 100644 --- a/lib/rules/prefer-ember-test-helpers.js +++ b/lib/rules/prefer-ember-test-helpers.js @@ -34,8 +34,11 @@ module.exports = { }; return { - Program() { - const tracker = new ReferenceTracker(context.getScope()); + Program(node) { + const sourceCode = context.sourceCode ?? context.getSourceCode(); + const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope(); + + const tracker = new ReferenceTracker(scope); const traceMap = { blur: { [ReferenceTracker.CALL]: true }, find: { [ReferenceTracker.CALL]: true }, diff --git a/lib/rules/require-fetch-import.js b/lib/rules/require-fetch-import.js index 4dddb95c01..e18e15de71 100644 --- a/lib/rules/require-fetch-import.js +++ b/lib/rules/require-fetch-import.js @@ -22,8 +22,11 @@ module.exports = { create(context) { return { - 'Program:exit'() { - const tracker = new ReferenceTracker(context.getScope()); + 'Program:exit'(node) { + const sourceCode = context.sourceCode ?? context.getSourceCode(); + const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope(); + + const tracker = new ReferenceTracker(scope); const traceMap = { fetch: { [ReferenceTracker.CALL]: true }, diff --git a/lib/rules/require-return-from-computed.js b/lib/rules/require-return-from-computed.js index a1c0516c2b..e47e184ce6 100644 --- a/lib/rules/require-return-from-computed.js +++ b/lib/rules/require-return-from-computed.js @@ -7,8 +7,14 @@ const { getImportIdentifier } = require('../utils/import'); // General rule - Always return a value from computed properties //------------------------------------------------------------------------------ -function isReachable(segment) { - return segment.reachable; +function isAnySegmentReachable(segments) { + for (const segment of segments) { + if (segment.reachable) { + return true; + } + } + + return false; } const ERROR_MESSAGE = 'Always return a value from computed properties'; @@ -39,17 +45,17 @@ module.exports = { codePath: null, shouldCheck: false, node: null, + currentSegments: [], }; function checkLastSegment(node) { - if (funcInfo.shouldCheck && funcInfo.codePath.currentSegments.some(isReachable)) { + if (funcInfo.shouldCheck && isAnySegmentReachable(funcInfo.currentSegments)) { report(node); } } let importedEmberName; let importedComputedName; - return { ImportDeclaration(node) { if (node.source.value === 'ember') { @@ -61,22 +67,43 @@ module.exports = { } }, - onCodePathStart(codePath) { + onCodePathStart(codePath, node) { + const sourceCode = context.sourceCode ?? context.getSourceCode(); + const ancestors = sourceCode.getAncestors + ? sourceCode.getAncestors(node) + : context.getAncestors(); + funcInfo = { upper: funcInfo, codePath, shouldCheck: - context - .getAncestors() - .findIndex((node) => - ember.isComputedProp(node, importedEmberName, importedComputedName) - ) > -1, + ancestors.findIndex((node) => + ember.isComputedProp(node, importedEmberName, importedComputedName) + ) > -1, + node, + currentSegments: new Set(), }; }, + // Pops this function's information. onCodePathEnd() { funcInfo = funcInfo.upper; }, + onUnreachableCodePathSegmentStart(segment) { + funcInfo.currentSegments.add(segment); + }, + + onUnreachableCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, + + onCodePathSegmentStart(segment) { + funcInfo.currentSegments.add(segment); + }, + + onCodePathSegmentEnd(segment) { + funcInfo.currentSegments.delete(segment); + }, 'FunctionExpression:exit'(node) { if (node.parent.parent.parent === null) { diff --git a/lib/rules/use-ember-get-and-set.js b/lib/rules/use-ember-get-and-set.js index 030affdb09..00da9f7a23 100644 --- a/lib/rules/use-ember-get-and-set.js +++ b/lib/rules/use-ember-get-and-set.js @@ -98,7 +98,9 @@ module.exports = { VariableDeclarator(node) { const isEmberImported = Boolean(emberImportAliasName); - const isModuleScope = context.getScope().type === 'module'; + const sourceCode = context.sourceCode ?? context.getSourceCode(); + const scope = sourceCode.getScope ? sourceCode.getScope(node) : context.getScope(); + const isModuleScope = scope.type === 'module'; if (isEmberImported && isModuleScope) { // Populate localModulesPresent as a mapping of (avoided method -> local module alias) for (const methodName of avoidedMethods) {