From c0c716b1c5e513ece52443c0f0848e5aee5e2ef7 Mon Sep 17 00:00:00 2001 From: Sebastian Good <2230835+scagood@users.noreply.github.com> Date: Mon, 27 May 2024 10:19:13 +0100 Subject: [PATCH] feat: Resolve `getAncestors` and `getScope` calls in eslint v9 (#466) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 唯然 --- __tests__/always-return.js | 2 +- __tests__/avoid-new.js | 2 +- __tests__/catch-or-return.js | 13 +++-- __tests__/no-callback-in-promise.js | 2 +- __tests__/no-multiple-resolved.js | 2 +- __tests__/no-native.js | 2 +- __tests__/no-nesting.js | 2 +- __tests__/no-new-statics.js | 2 +- __tests__/no-promise-in-callback.js | 2 +- __tests__/no-return-in-finally.js | 2 +- __tests__/no-return-wrap.js | 2 +- __tests__/param-names.js | 2 +- __tests__/prefer-await-to-callbacks.js | 2 +- __tests__/prefer-await-to-then.js | 2 +- __tests__/rule-tester.js | 68 +++++++++++++++++++++++ __tests__/valid-params.js | 2 +- package-lock.json | 77 +++++++++++++++++++++----- package.json | 13 ++++- rules/lib/eslint-compat.js | 33 +++++++++++ rules/no-callback-in-promise.js | 3 +- rules/no-multiple-resolved.js | 3 +- rules/no-native.js | 5 +- rules/no-nesting.js | 3 +- rules/no-promise-in-callback.js | 3 +- rules/no-return-wrap.js | 8 +-- rules/prefer-await-to-callbacks.js | 7 ++- rules/prefer-await-to-then.js | 11 ++-- 27 files changed, 220 insertions(+), 55 deletions(-) create mode 100644 __tests__/rule-tester.js create mode 100644 rules/lib/eslint-compat.js diff --git a/__tests__/always-return.js b/__tests__/always-return.js index df319dc3..f9411f19 100644 --- a/__tests__/always-return.js +++ b/__tests__/always-return.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/always-return') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 11, diff --git a/__tests__/avoid-new.js b/__tests__/avoid-new.js index 55b63af3..dded79a8 100644 --- a/__tests__/avoid-new.js +++ b/__tests__/avoid-new.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/avoid-new') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, diff --git a/__tests__/catch-or-return.js b/__tests__/catch-or-return.js index 68d4135f..c4426d5b 100644 --- a/__tests__/catch-or-return.js +++ b/__tests__/catch-or-return.js @@ -1,8 +1,12 @@ 'use strict' const rule = require('../rules/catch-or-return') -const RuleTester = require('eslint').RuleTester -const ruleTester = new RuleTester() +const { RuleTester } = require('./rule-tester') +const ruleTester = new RuleTester({ + parserOptions: { + ecmaVersion: 6, + }, +}) const catchMessage = 'Expected catch() or return' const doneMessage = 'Expected done() or return' @@ -19,10 +23,7 @@ ruleTester.run('catch-or-return', rule, { 'frank.then(to).finally(fn).catch(jail)', // arrow function use case - { - code: 'postJSON("/smajobber/api/reportJob.json")\n\t.then(()=>this.setState())\n\t.catch(()=>this.setState())', - parserOptions: { ecmaVersion: 6 }, - }, + 'postJSON("/smajobber/api/reportJob.json")\n\t.then(()=>this.setState())\n\t.catch(()=>this.setState())', // return 'function a() { return frank().then(go) }', diff --git a/__tests__/no-callback-in-promise.js b/__tests__/no-callback-in-promise.js index 606a6d23..8dcbf916 100644 --- a/__tests__/no-callback-in-promise.js +++ b/__tests__/no-callback-in-promise.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/no-callback-in-promise') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, diff --git a/__tests__/no-multiple-resolved.js b/__tests__/no-multiple-resolved.js index 1792025b..4adf614b 100644 --- a/__tests__/no-multiple-resolved.js +++ b/__tests__/no-multiple-resolved.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/no-multiple-resolved') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2020, diff --git a/__tests__/no-native.js b/__tests__/no-native.js index 77edce3a..cd8d15cd 100644 --- a/__tests__/no-native.js +++ b/__tests__/no-native.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/no-native') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const parserOptions = { ecmaVersion: 6, sourceType: 'module', diff --git a/__tests__/no-nesting.js b/__tests__/no-nesting.js index 1943efa9..722d1041 100644 --- a/__tests__/no-nesting.js +++ b/__tests__/no-nesting.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/no-nesting') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, diff --git a/__tests__/no-new-statics.js b/__tests__/no-new-statics.js index e0fbe214..96abe1ec 100644 --- a/__tests__/no-new-statics.js +++ b/__tests__/no-new-statics.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/no-new-statics') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester() ruleTester.run('no-new-statics', rule, { diff --git a/__tests__/no-promise-in-callback.js b/__tests__/no-promise-in-callback.js index 5f766640..a11142b9 100644 --- a/__tests__/no-promise-in-callback.js +++ b/__tests__/no-promise-in-callback.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/no-promise-in-callback') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, diff --git a/__tests__/no-return-in-finally.js b/__tests__/no-return-in-finally.js index 1e3ca83d..77636868 100644 --- a/__tests__/no-return-in-finally.js +++ b/__tests__/no-return-in-finally.js @@ -1,6 +1,6 @@ 'use strict' -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const rule = require('../rules/no-return-in-finally') const ruleTester = new RuleTester({ parserOptions: { diff --git a/__tests__/no-return-wrap.js b/__tests__/no-return-wrap.js index 601dcd5f..eb7f082f 100644 --- a/__tests__/no-return-wrap.js +++ b/__tests__/no-return-wrap.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/no-return-wrap') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, diff --git a/__tests__/param-names.js b/__tests__/param-names.js index f4a80599..54416fc2 100644 --- a/__tests__/param-names.js +++ b/__tests__/param-names.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/param-names') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, diff --git a/__tests__/prefer-await-to-callbacks.js b/__tests__/prefer-await-to-callbacks.js index 4c391d7e..3ea4d748 100644 --- a/__tests__/prefer-await-to-callbacks.js +++ b/__tests__/prefer-await-to-callbacks.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/prefer-await-to-callbacks') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 8, diff --git a/__tests__/prefer-await-to-then.js b/__tests__/prefer-await-to-then.js index a01d33af..3bdf5bd8 100644 --- a/__tests__/prefer-await-to-then.js +++ b/__tests__/prefer-await-to-then.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/prefer-await-to-then') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 8, diff --git a/__tests__/rule-tester.js b/__tests__/rule-tester.js new file mode 100644 index 00000000..057a31ed --- /dev/null +++ b/__tests__/rule-tester.js @@ -0,0 +1,68 @@ +/** + * @fileoverview Helpers for tests. + * @author 唯然 + */ +'use strict' +const { version } = require('eslint/package.json') +const { RuleTester } = require('eslint') +const globals = require('globals') + +const majorVersion = Number.parseInt(version.split('.')[0], 10) + +function convertConfig(config) { + if (config instanceof Object === false) { + return config + } + + if (config.languageOptions == null) { + config.languageOptions = {} + } + + if (config.parserOptions) { + Object.assign(config.languageOptions, config.parserOptions) + delete config.parserOptions + } + + if (typeof config.parser === 'string') { + config.languageOptions.parser = require(config.parser) + delete config.parser + } + + if (config.globals instanceof Object) { + config.languageOptions.globals = config.globals + delete config.globals + } + + if (config.env instanceof Object) { + if (config.languageOptions.globals == null) { + config.languageOptions.globals = {} + } + + for (const key in config.env) { + Object.assign(config.languageOptions.globals, globals[key]) + } + + delete config.env + } + + delete config.parserOptions + delete config.parser + + return config +} + +exports.RuleTester = function (config = {}) { + if (majorVersion <= 8) { + return new RuleTester(config) + } + + const ruleTester = new RuleTester(convertConfig(config)) + const $run = ruleTester.run.bind(ruleTester) + ruleTester.run = function (name, rule, tests) { + tests.valid = tests.valid.map(convertConfig) + tests.invalid = tests.invalid.map(convertConfig) + + $run(name, rule, tests) + } + return ruleTester +} diff --git a/__tests__/valid-params.js b/__tests__/valid-params.js index ca671b95..968135c3 100644 --- a/__tests__/valid-params.js +++ b/__tests__/valid-params.js @@ -1,7 +1,7 @@ 'use strict' const rule = require('../rules/valid-params') -const RuleTester = require('eslint').RuleTester +const { RuleTester } = require('./rule-tester') const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6, diff --git a/package-lock.json b/package-lock.json index 768a358a..6fc178fb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "eslint-plugin-jest": "^26.9.0", "eslint-plugin-node": "^11.1.0", "eslint-plugin-prettier": "^4.2.1", + "globals": "^14.0.0", "husky": "^7.0.4", "jest": "^28.1.3", "lint-staged": "^12.5.0", @@ -32,7 +33,7 @@ "url": "https://opencollective.com/eslint" }, "peerDependencies": { - "eslint": "^7.0.0 || ^8.0.0" + "eslint": "^7.0.0 || ^8.0.0 || ^9.0.0" } }, "node_modules/@ampproject/remapping": { @@ -651,6 +652,21 @@ "url": "https://opencollective.com/eslint" } }, + "node_modules/@eslint/eslintrc/node_modules/globals": { + "version": "13.24.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-13.24.0.tgz", + "integrity": "sha512-AhO5QUcj8llrbG09iWhPU2B204J1xnPeL8kQmVorSsy+Sjj1sk8gIyh6cUocGmH4L0UuhAJy+hJMRA4mgA4mFQ==", + "dev": true, + "dependencies": { + "type-fest": "^0.20.2" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/@humanwhocodes/config-array": { "version": "0.11.6", "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.6.tgz", @@ -2908,6 +2924,21 @@ "node": "^12.22.0 || ^14.17.0 || >=16.0.0" } }, + "node_modules/eslint/node_modules/globals": { + "version": "13.24.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-13.24.0.tgz", + "integrity": "sha512-AhO5QUcj8llrbG09iWhPU2B204J1xnPeL8kQmVorSsy+Sjj1sk8gIyh6cUocGmH4L0UuhAJy+hJMRA4mgA4mFQ==", + "dev": true, + "dependencies": { + "type-fest": "^0.20.2" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/espree": { "version": "9.4.0", "resolved": "https://registry.npmjs.org/espree/-/espree-9.4.0.tgz", @@ -3340,15 +3371,12 @@ } }, "node_modules/globals": { - "version": "13.17.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-13.17.0.tgz", - "integrity": "sha512-1C+6nQRb1GwGMKm2dH/E7enFAMxGTmGI7/dEdhy/DNelv85w9B72t3uc5frtMNXIbzrarJJ/lTCjcaZwbLJmyw==", + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-14.0.0.tgz", + "integrity": "sha512-oahGvuMGQlPw/ivIYBjVSrWAfWLBeku5tpPE2fOPLi+WHffIWbuh2tCjhyQhTBPMf5E9jDEH4FOmTYgYwbKwtQ==", "dev": true, - "dependencies": { - "type-fest": "^0.20.2" - }, "engines": { - "node": ">=8" + "node": ">=18" }, "funding": { "url": "https://github.com/sponsors/sindresorhus" @@ -7732,6 +7760,17 @@ "js-yaml": "^4.1.0", "minimatch": "^3.1.2", "strip-json-comments": "^3.1.1" + }, + "dependencies": { + "globals": { + "version": "13.24.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-13.24.0.tgz", + "integrity": "sha512-AhO5QUcj8llrbG09iWhPU2B204J1xnPeL8kQmVorSsy+Sjj1sk8gIyh6cUocGmH4L0UuhAJy+hJMRA4mgA4mFQ==", + "dev": true, + "requires": { + "type-fest": "^0.20.2" + } + } } }, "@humanwhocodes/config-array": { @@ -9158,6 +9197,17 @@ "strip-ansi": "^6.0.1", "strip-json-comments": "^3.1.0", "text-table": "^0.2.0" + }, + "dependencies": { + "globals": { + "version": "13.24.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-13.24.0.tgz", + "integrity": "sha512-AhO5QUcj8llrbG09iWhPU2B204J1xnPeL8kQmVorSsy+Sjj1sk8gIyh6cUocGmH4L0UuhAJy+hJMRA4mgA4mFQ==", + "dev": true, + "requires": { + "type-fest": "^0.20.2" + } + } } }, "eslint-config-prettier": { @@ -9717,13 +9767,10 @@ } }, "globals": { - "version": "13.17.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-13.17.0.tgz", - "integrity": "sha512-1C+6nQRb1GwGMKm2dH/E7enFAMxGTmGI7/dEdhy/DNelv85w9B72t3uc5frtMNXIbzrarJJ/lTCjcaZwbLJmyw==", - "dev": true, - "requires": { - "type-fest": "^0.20.2" - } + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-14.0.0.tgz", + "integrity": "sha512-oahGvuMGQlPw/ivIYBjVSrWAfWLBeku5tpPE2fOPLi+WHffIWbuh2tCjhyQhTBPMf5E9jDEH4FOmTYgYwbKwtQ==", + "dev": true }, "globby": { "version": "11.1.0", diff --git a/package.json b/package.json index daf58742..c55945e4 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,15 @@ "lines": 100, "statements": 100 } - } + }, + "collectCoverageFrom": [ + "rules/*.js", + "rules/*/*.js", + "!rules/lib/eslint-compat.js" + ], + "testPathIgnorePatterns": [ + "__tests__/rule-tester.js" + ] }, "devDependencies": { "@typescript-eslint/parser": "^5.45.0", @@ -67,6 +75,7 @@ "eslint-plugin-jest": "^26.9.0", "eslint-plugin-node": "^11.1.0", "eslint-plugin-prettier": "^4.2.1", + "globals": "^14.0.0", "husky": "^7.0.4", "jest": "^28.1.3", "lint-staged": "^12.5.0", @@ -75,7 +84,7 @@ "typescript": "^4.9.3" }, "peerDependencies": { - "eslint": "^7.0.0 || ^8.0.0" + "eslint": "^7.0.0 || ^8.0.0 || ^9.0.0" }, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" diff --git a/rules/lib/eslint-compat.js b/rules/lib/eslint-compat.js new file mode 100644 index 00000000..bf969d25 --- /dev/null +++ b/rules/lib/eslint-compat.js @@ -0,0 +1,33 @@ +'use strict' + +function getSourceCode(context) { + if (context.sourceCode != null) { + return context.sourceCode + } + + return context.getSourceCode() +} + +function getAncestors(context, node) { + const sourceCode = getSourceCode(context) + if (typeof sourceCode.getAncestors === 'function') { + return sourceCode.getAncestors(node) + } + + return context.getAncestors(node) +} + +function getScope(context, node) { + const sourceCode = getSourceCode(context) + if (typeof sourceCode.getScope === 'function') { + return sourceCode.getScope(node) + } + + return context.getScope(node) +} + +module.exports = { + getSourceCode, + getAncestors, + getScope, +} diff --git a/rules/no-callback-in-promise.js b/rules/no-callback-in-promise.js index 45d0bf0a..429621cf 100644 --- a/rules/no-callback-in-promise.js +++ b/rules/no-callback-in-promise.js @@ -5,6 +5,7 @@ 'use strict' +const { getAncestors } = require('./lib/eslint-compat') const getDocsUrl = require('./lib/get-docs-url') const hasPromiseCallback = require('./lib/has-promise-callback') const isInsidePromise = require('./lib/is-inside-promise') @@ -61,7 +62,7 @@ module.exports = { } return } - if (context.getAncestors().some(isInsidePromise)) { + if (getAncestors(context, node).some(isInsidePromise)) { context.report({ node, messageId: 'callback', diff --git a/rules/no-multiple-resolved.js b/rules/no-multiple-resolved.js index 4d454051..86de6907 100644 --- a/rules/no-multiple-resolved.js +++ b/rules/no-multiple-resolved.js @@ -5,6 +5,7 @@ 'use strict' +const { getScope } = require('./lib/eslint-compat') const getDocsUrl = require('./lib/get-docs-url') const { isPromiseConstructorWithInlineExecutor, @@ -400,7 +401,7 @@ module.exports = { (node) => node && node.type === 'Identifier' ) for (const resolver of resolvers) { - const variable = context.getScope().set.get(resolver.name) + const variable = getScope(context, node).set.get(resolver.name) // istanbul ignore next -- Usually always present. if (!variable) continue for (const reference of variable.references) { diff --git a/rules/no-native.js b/rules/no-native.js index c92d60ae..373c7f91 100644 --- a/rules/no-native.js +++ b/rules/no-native.js @@ -3,6 +3,7 @@ 'use strict' +const { getScope } = require('./lib/eslint-compat') const getDocsUrl = require('./lib/get-docs-url') function isDeclared(scope, ref) { @@ -45,8 +46,8 @@ module.exports = { * @private */ return { - 'Program:exit'() { - const scope = context.getScope() + 'Program:exit'(node) { + const scope = getScope(context, node) const leftToBeResolved = scope.implicit.left || /** diff --git a/rules/no-nesting.js b/rules/no-nesting.js index 128b3912..4f5a8a32 100644 --- a/rules/no-nesting.js +++ b/rules/no-nesting.js @@ -5,6 +5,7 @@ 'use strict' +const { getScope } = require('./lib/eslint-compat') const getDocsUrl = require('./lib/get-docs-url') const hasPromiseCallback = require('./lib/has-promise-callback') const isInsidePromise = require('./lib/is-inside-promise') @@ -41,7 +42,7 @@ module.exports = { return { ':function'(node) { if (isInsidePromise(node)) { - callbackScopes.unshift(context.getScope()) + callbackScopes.unshift(getScope(context, node)) } }, ':function:exit'(node) { diff --git a/rules/no-promise-in-callback.js b/rules/no-promise-in-callback.js index 05b23d7c..fcc039aa 100644 --- a/rules/no-promise-in-callback.js +++ b/rules/no-promise-in-callback.js @@ -5,6 +5,7 @@ 'use strict' +const { getAncestors } = require('./lib/eslint-compat') const getDocsUrl = require('./lib/get-docs-url') const isPromise = require('./lib/is-promise') const isInsideCallback = require('./lib/is-inside-callback') @@ -30,7 +31,7 @@ module.exports = { // what about if the parent is an ArrowFunctionExpression // would that imply an implicit return? - if (context.getAncestors().some(isInsideCallback)) { + if (getAncestors(context, node).some(isInsideCallback)) { context.report({ node: node.callee, message: 'Avoid using promises inside of callbacks.', diff --git a/rules/no-return-wrap.js b/rules/no-return-wrap.js index 334f55da..5ab2ccb9 100644 --- a/rules/no-return-wrap.js +++ b/rules/no-return-wrap.js @@ -6,12 +6,12 @@ 'use strict' +const { getAncestors } = require('./lib/eslint-compat') const getDocsUrl = require('./lib/get-docs-url') const isPromise = require('./lib/is-promise') -function isInPromise(context) { - let functionNode = context - .getAncestors() +function isInPromise(context, node) { + let functionNode = getAncestors(context, node) .filter((node) => { return ( node.type === 'ArrowFunctionExpression' || @@ -70,7 +70,7 @@ module.exports = { */ function checkCallExpression({ callee }, node) { if ( - isInPromise(context) && + isInPromise(context, node) && callee.type === 'MemberExpression' && callee.object.name === 'Promise' ) { diff --git a/rules/prefer-await-to-callbacks.js b/rules/prefer-await-to-callbacks.js index a0070fa6..ca5f9ff9 100644 --- a/rules/prefer-await-to-callbacks.js +++ b/rules/prefer-await-to-callbacks.js @@ -1,5 +1,6 @@ 'use strict' +const { getAncestors } = require('./lib/eslint-compat') const getDocsUrl = require('./lib/get-docs-url') module.exports = { @@ -21,8 +22,8 @@ module.exports = { context.report({ node: lastParam, messageId: 'error' }) } } - function isInsideYieldOrAwait() { - return context.getAncestors().some((parent) => { + function isInsideYieldOrAwait(node) { + return getAncestors(context, node).some((parent) => { return ( parent.type === 'AwaitExpression' || parent.type === 'YieldExpression' ) @@ -82,7 +83,7 @@ module.exports = { arg.params[0] && (arg.params[0].name === 'err' || arg.params[0].name === 'error') ) { - if (!isInsideYieldOrAwait()) { + if (!isInsideYieldOrAwait(node)) { context.report({ node: arg, messageId: 'error' }) } } diff --git a/rules/prefer-await-to-then.js b/rules/prefer-await-to-then.js index 2faba9f2..43629439 100644 --- a/rules/prefer-await-to-then.js +++ b/rules/prefer-await-to-then.js @@ -5,6 +5,7 @@ 'use strict' +const { getAncestors, getScope } = require('./lib/eslint-compat') const getDocsUrl = require('./lib/get-docs-url') module.exports = { @@ -19,8 +20,8 @@ module.exports = { }, create(context) { /** Returns true if node is inside yield or await expression. */ - function isInsideYieldOrAwait() { - return context.getAncestors().some((parent) => { + function isInsideYieldOrAwait(node) { + return getAncestors(context, node).some((parent) => { return ( parent.type === 'AwaitExpression' || parent.type === 'YieldExpression' ) @@ -32,13 +33,13 @@ module.exports = { * Await statements are not allowed at the top level, * only within function declarations. */ - function isTopLevelScoped() { - return context.getScope().block.type === 'Program' + function isTopLevelScoped(node) { + return getScope(context, node).block.type === 'Program' } return { 'CallExpression > MemberExpression.callee'(node) { - if (isTopLevelScoped() || isInsideYieldOrAwait()) { + if (isTopLevelScoped(node) || isInsideYieldOrAwait(node)) { return }