From 7bffb7a666ed74a876ba3a6c482c36ea6f9d6d07 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Wed, 24 Jul 2024 19:22:21 +0800 Subject: [PATCH] feat(`prefer-await-to-then`): ignore constructor scope unless with `strict` option (#496) Fixes #436 **What is the purpose of this pull request?** - [x] Changes an existing rule **What changes did you make? (Give an overview)** Ignores constructor scope in `prefer-await-to-then` when reporting on use of `then` (unless the `strict` option is enabled). Co-authored-by: Yosuke Ota --- __tests__/prefer-await-to-then.js | 18 ++++++++++++++++++ rules/lib/is-inside-callback.js | 4 ++-- rules/prefer-await-to-then.js | 10 ++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/__tests__/prefer-await-to-then.js b/__tests__/prefer-await-to-then.js index 897002a1..917f6410 100644 --- a/__tests__/prefer-await-to-then.js +++ b/__tests__/prefer-await-to-then.js @@ -34,6 +34,11 @@ ruleTester.run('prefer-await-to-then', rule, { `function isThenable(obj) { return obj && typeof obj.then === 'function'; }`, + `class Foo { + constructor () { + doSomething.then(abc); + } + }`, ], invalid: [ @@ -70,6 +75,19 @@ ruleTester.run('prefer-await-to-then', rule, { }, ], }, + { + code: `class Foo { + constructor () { + doSomething.then(abc); + } + }`, + errors: [{ message }], + options: [ + { + strict: true, + }, + ], + }, { code: 'async function hi() { await thing().catch() }', errors: [{ message }], diff --git a/rules/lib/is-inside-callback.js b/rules/lib/is-inside-callback.js index fd915952..b1bb80e3 100644 --- a/rules/lib/is-inside-callback.js +++ b/rules/lib/is-inside-callback.js @@ -3,7 +3,7 @@ const isInsidePromise = require('./is-inside-promise') function isInsideCallback(node) { - const isCallExpression = + const isFunction = node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression' || node.type === 'FunctionDeclaration' // this may be controversial @@ -13,7 +13,7 @@ function isInsideCallback(node) { const name = node.params && node.params[0] && node.params[0].name const firstArgIsError = name === 'err' || name === 'error' - const isInACallback = isCallExpression && firstArgIsError + const isInACallback = isFunction && firstArgIsError return isInACallback } diff --git a/rules/prefer-await-to-then.js b/rules/prefer-await-to-then.js index 6fafa8dc..09532477 100644 --- a/rules/prefer-await-to-then.js +++ b/rules/prefer-await-to-then.js @@ -41,6 +41,15 @@ module.exports = { }) } + /** Returns true if node is inside a constructor */ + function isInsideConstructor(node) { + return getAncestors(context, node).some((parent) => { + return ( + parent.type === 'MethodDefinition' && parent.kind === 'constructor' + ) + }) + } + /** * Returns true if node is created at the top-level scope. * Await statements are not allowed at the top level, @@ -57,6 +66,7 @@ module.exports = { if ( isTopLevelScoped(node) || (!strict && isInsideYieldOrAwait(node)) || + (!strict && isInsideConstructor(node)) || isMemberCallWithObjectName('cy', node.parent) ) { return