Skip to content

Commit

Permalink
prefer-top-level-await: Ignore expressions in class (#1976)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Nov 18, 2022
1 parent 046cf90 commit b90a3aa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions 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';
Expand All @@ -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([
Expand Down
24 changes: 24 additions & 0 deletions test/prefer-top-level-await.mjs
Expand Up @@ -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)',
Expand Down

0 comments on commit b90a3aa

Please sign in to comment.