From 4c7ff13fc575e14585518e8fb2cf9940dd41fdb4 Mon Sep 17 00:00:00 2001 From: Lucas Hill Date: Tue, 25 Jun 2024 09:26:58 -0400 Subject: [PATCH 1/4] Fix API deprecations blocking eslint v9 compatibility --- lib/rules/require-return-from-computed.js | 40 +++++++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/rules/require-return-from-computed.js b/lib/rules/require-return-from-computed.js index a1c0516c2b..50bf8548e6 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'; @@ -37,19 +43,20 @@ module.exports = { let funcInfo = { upper: null, codePath: null, + hasReturn: false, 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 +68,41 @@ module.exports = { } }, - onCodePathStart(codePath) { + onCodePathStart(codePath, node) { funcInfo = { upper: funcInfo, codePath, + hasReturn: false, shouldCheck: - context - .getAncestors() + context.sourceCode + .getAncestors(node) .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) { From 157e331bd8eb5e2e9b54ccfc7517fb8ace0cb606 Mon Sep 17 00:00:00 2001 From: Lucas Hill Date: Tue, 25 Jun 2024 09:28:12 -0400 Subject: [PATCH 2/4] Remove unused property --- lib/rules/require-return-from-computed.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/rules/require-return-from-computed.js b/lib/rules/require-return-from-computed.js index 50bf8548e6..846af581d7 100644 --- a/lib/rules/require-return-from-computed.js +++ b/lib/rules/require-return-from-computed.js @@ -43,7 +43,6 @@ module.exports = { let funcInfo = { upper: null, codePath: null, - hasReturn: false, shouldCheck: false, node: null, currentSegments: [], @@ -72,7 +71,6 @@ module.exports = { funcInfo = { upper: funcInfo, codePath, - hasReturn: false, shouldCheck: context.sourceCode .getAncestors(node) From 15ee60a05ea471739e6e244a5e819f29a3d93d09 Mon Sep 17 00:00:00 2001 From: Lucas Hill Date: Tue, 25 Jun 2024 09:47:44 -0400 Subject: [PATCH 3/4] Make accessing ancestors compatible with older eslint versions. --- lib/rules/require-return-from-computed.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/rules/require-return-from-computed.js b/lib/rules/require-return-from-computed.js index 846af581d7..e47e184ce6 100644 --- a/lib/rules/require-return-from-computed.js +++ b/lib/rules/require-return-from-computed.js @@ -68,15 +68,18 @@ module.exports = { }, onCodePathStart(codePath, node) { + const sourceCode = context.sourceCode ?? context.getSourceCode(); + const ancestors = sourceCode.getAncestors + ? sourceCode.getAncestors(node) + : context.getAncestors(); + funcInfo = { upper: funcInfo, codePath, shouldCheck: - context.sourceCode - .getAncestors(node) - .findIndex((node) => - ember.isComputedProp(node, importedEmberName, importedComputedName) - ) > -1, + ancestors.findIndex((node) => + ember.isComputedProp(node, importedEmberName, importedComputedName) + ) > -1, node, currentSegments: new Set(), }; From 7f4c06b8e4aceed1c565cb31f7b4911c7331a01d Mon Sep 17 00:00:00 2001 From: Lucas Hill Date: Tue, 25 Jun 2024 10:16:03 -0400 Subject: [PATCH 4/4] 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/use-ember-get-and-set.js | 4 +++- 7 files changed, 30 insertions(+), 12 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/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) {