Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-top-level-await: Ignore expressions in class #1976

Merged
merged 2 commits into from Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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