From b90a3aad16bff231235db481318505a912620149 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 18 Nov 2022 22:12:51 +0800 Subject: [PATCH] `prefer-top-level-await`: Ignore expressions in class (#1976) --- rules/prefer-top-level-await.js | 7 +++++-- test/prefer-top-level-await.mjs | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/rules/prefer-top-level-await.js b/rules/prefer-top-level-await.js index ea279c9241..615fd858cb 100644 --- a/rules/prefer-top-level-await.js +++ b/rules/prefer-top-level-await.js @@ -1,6 +1,6 @@ 'use strict'; const {findVariable, getFunctionHeadLocation} = require('eslint-utils'); -const {matches, memberExpressionSelector} = require('./selectors/index.js'); +const {matches, not, memberExpressionSelector} = require('./selectors/index.js'); const ERROR_PROMISE = 'promise'; const ERROR_IIFE = 'iife'; @@ -15,7 +15,10 @@ const messages = { const promiseMethods = ['then', 'catch', 'finally']; -const topLevelCallExpression = 'CallExpression:not(:function *)'; +const topLevelCallExpression = [ + 'CallExpression', + not([':function *', 'ClassDeclaration *', 'ClassExpression *']), +].join(''); const iife = [ topLevelCallExpression, matches([ diff --git a/test/prefer-top-level-await.mjs b/test/prefer-top-level-await.mjs index 8d7ef97be8..b4bdd083d7 100644 --- a/test/prefer-top-level-await.mjs +++ b/test/prefer-top-level-await.mjs @@ -47,6 +47,30 @@ test.snapshot({ 'await foo.then?.(bar)', 'await foo.then(bar)?.catch(bar)', 'await foo.then(bar)?.catch?.(bar)', + outdent` + class Example { + property = promise.then(bar) + } + `, + outdent` + const Example = class Example { + property = promise.then(bar) + } + `, + outdent` + class Example { + static { + promise.then(bar) + } + } + `, + outdent` + const Example = class Example { + static { + promise.then(bar) + } + } + `, ], invalid: [ 'foo.then(bar)',