Skip to content

Commit

Permalink
Fix API deprecations blocking eslint v9 compatibility (#2153)
Browse files Browse the repository at this point in the history
* Fix API deprecations blocking eslint v9 compatibility

* Remove unused property

* Make accessing ancestors compatible with older eslint versions.

* Fix getScope eslint v9 deprecation
  • Loading branch information
LucasHill authored Jun 25, 2024
1 parent 91657d4 commit 55cd064
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 22 deletions.
6 changes: 4 additions & 2 deletions lib/rules/jquery-ember-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/no-ember-testing-in-module-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 5 additions & 2 deletions lib/rules/no-global-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
6 changes: 4 additions & 2 deletions lib/rules/no-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down
7 changes: 5 additions & 2 deletions lib/rules/prefer-ember-test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
7 changes: 5 additions & 2 deletions lib/rules/require-fetch-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
47 changes: 37 additions & 10 deletions lib/rules/require-return-from-computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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') {
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/use-ember-get-and-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 55cd064

Please sign in to comment.