Skip to content

Commit

Permalink
feat: eslint 9 support
Browse files Browse the repository at this point in the history
Eslint 9 has removed a couple of methods from the `context` object. Adapt the code to work with both the new and old eslint runtime.

More info:
https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/
  • Loading branch information
ej612 authored Jun 13, 2024
1 parent 3ce1766 commit f9e0798
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/rules/missing-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ function create (context) {
if (allowed.indexOf(buildName(node)) === -1) {
return
}
context.getAncestors().some(function (ancestor) {
const ancestors = context.sourceCode
? context.sourceCode.getAncestors(node)
: context.getAncestors()

ancestors.some(function (ancestor) {
var index = unchecked.indexOf(ancestor)

if (index !== -1) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/new-line-before-expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
return
}
lastExpectNode = node
var sourceCode = context.getSourceCode()
var sourceCode = context.sourceCode || context.getSourceCode()
let prevToken = sourceCode.getTokenBefore(node)
if (prevToken.value === 'await' || prevToken.value === 'return') {
node = prevToken
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/no-promise-without-done-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ function genReport (node, context) {
node: node,
message: 'An "it" that uses an async method should handle failure (use "done.fail")',
fix: function (fixer) {
const lastToken = context.getLastToken(node.parent)
const lastToken = context.sourceCode
? context.sourceCode.getLastToken(node.parent)
: context.getLastToken(node.parent)

if (!isSemicolon(lastToken)) {
return fixer.insertTextAfter(node.parent, '.catch(done.fail)')
} else {
Expand Down
6 changes: 5 additions & 1 deletion lib/rules/no-unsafe-spy.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ function create (context) {
)
) { return }

if (blocksWhitelistRegexp.test(getParentJasmineBlock(context.getAncestors()))) { return }
const ancestors = context.sourceCode
? context.sourceCode.getAncestors(node)
: context.getAncestors()

if (blocksWhitelistRegexp.test(getParentJasmineBlock(ancestors))) { return }

context.report({
message: 'Spy declared outside of before/after/it block',
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/prefer-promise-strategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = {
},

fix (fixer) {
const code = context.getSourceCode()
const code = context.sourceCode || context.getSourceCode()
return [
// Replace Promise constructor call with its arguments
fixer.remove(promiseCall.callee),
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/prefer-toHaveBeenCalledWith.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ function create (context) {
return {
Identifier: function (node) {
if (node.name === 'toHaveBeenCalled') {
const tokensBefore = context.getTokensBefore(node, 2)
const tokensBefore = context.sourceCode
? context.sourceCode.getTokensBefore(node, 2)
: context.getTokensBefore(node, 2)

if (tokensBefore[1] && tokensBefore[1].type === 'Punctuator' && tokensBefore[1].value === '.' &&
tokensBefore[0] && tokensBefore[0].type === 'Identifier' && tokensBefore[0].value === 'not') {
return
Expand Down

0 comments on commit f9e0798

Please sign in to comment.