Skip to content

add eslint rules for promises #3796

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

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins:
- node
- import
- simple-import-sort
- promise
settings:
node:
tryExtensions: ['.js', '.ts', '.jsx', '.json', '.node', '.d.ts']
Expand Down Expand Up @@ -177,6 +178,27 @@ rules:
- ["^\\./"]
simple-import-sort/exports: off # TODO

##############################################################################
# `eslint-plugin-promise` rule list based on `v6.1.x`
# https://github.com/eslint-community/eslint-plugin-promise/blob/main/README.md
##############################################################################

promise/always-return: error
promise/avoid-new: off
promise/catch-or-return: error
promise/no-callback-in-promise: error
promise/no-multiple-resolved: error
promise/no-native: off
promise/no-nesting: error
promise/no-new-statics: error
promise/no-promise-in-callback: off
promise/no-return-in-finally: error
promise/no-return-wrap: error
promise/param-names: error
promise/prefer-await-to-callbacks: off
promise/prefer-await-to-then: error
promise/valid-params: error

##############################################################################
# ESLint builtin rules list based on `v8.27.x`
##############################################################################
Expand Down Expand Up @@ -629,7 +651,7 @@ overrides:
]
'@typescript-eslint/no-useless-constructor': error
'@typescript-eslint/require-await': error
'@typescript-eslint/return-await': error
'@typescript-eslint/return-await': [error, 'always']

# Disable for JS and TS
'@typescript-eslint/init-declarations': off
Expand Down
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"eslint-plugin-import": "2.26.0",
"eslint-plugin-internal-rules": "file:./resources/eslint-internal-rules",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "6.1.1",
"eslint-plugin-react": "7.31.10",
"eslint-plugin-react-hooks": "4.6.0",
"eslint-plugin-simple-import-sort": "8.0.0",
Expand Down
2 changes: 1 addition & 1 deletion resources/gen-changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ async function getPRsInfo(
let prNumbers = await splitBatches(commits, batchCommitToPR);
prNumbers = Array.from(new Set(prNumbers)); // Remove duplicates

return splitBatches(prNumbers, batchPRInfo);
return await splitBatches(prNumbers, batchPRInfo);
}

// Split commits into batches of 50 to prevent timeouts
Expand Down
13 changes: 7 additions & 6 deletions src/__testUtils__/__tests__/resolveOnNextTick-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ describe('resolveOnNextTick', () => {
it('resolves promise on the next tick', async () => {
const output = [];

const promise1 = resolveOnNextTick().then(() => {
output.push('second');
});
const promise2 = resolveOnNextTick().then(() => {
output.push('third');
});
async function outputOnNextTick(message: string) {
await resolveOnNextTick();
output.push(message);
}

const promise1 = outputOnNextTick('second');
const promise2 = outputOnNextTick('third');
output.push('first');

await Promise.all([promise1, promise2]);
Expand Down
7 changes: 6 additions & 1 deletion src/__testUtils__/expectEqualPromisesOrValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import type { PromiseOrValue } from '../jsutils/PromiseOrValue.js';

import { expectMatchingValues } from './expectMatchingValues.js';

async function expectMatchingPromises<T>(items: Promise<ReadonlyArray<T>>) {
const values = await items;
return expectMatchingValues(values);
}

export function expectEqualPromisesOrValues<T>(
items: ReadonlyArray<PromiseOrValue<T>>,
): PromiseOrValue<T> {
const [firstItem, ...remainingItems] = items;
if (isPromise(firstItem)) {
if (remainingItems.every(isPromise)) {
return Promise.all(items).then(expectMatchingValues);
return expectMatchingPromises(Promise.all(items));
}
} else if (remainingItems.every((item) => !isPromise(item))) {
return expectMatchingValues(items);
Expand Down
2 changes: 1 addition & 1 deletion src/execution/__tests__/executor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ describe('Execute: Handles basic execution tasks', () => {
return new Promise((resolve) => resolve('async'));
},
asyncReject() {
return new Promise((_, reject) =>
return new Promise((_resolve, reject) =>
reject(new Error('Error getting asyncReject')),
);
},
Expand Down
10 changes: 7 additions & 3 deletions src/execution/__tests__/simplePubSub-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ describe('SimplePubSub', () => {
value: 'Banana',
});

async function getNextItem(i: AsyncIterator<unknown>) {
return await i.next();
}

// Read ahead
const i3 = iterator.next().then((x) => x);
const i4 = iterator.next().then((x) => x);
const i3 = getNextItem(iterator);
const i4 = getNextItem(iterator);

// Publish
expect(pubsub.emit('Coconut')).to.equal(true);
Expand All @@ -35,7 +39,7 @@ describe('SimplePubSub', () => {
expect(await i3).to.deep.equal({ done: false, value: 'Coconut' });

// Read ahead
const i5 = iterator.next().then((x) => x);
const i5 = getNextItem(iterator);

// Terminate queue
await iterator.return();
Expand Down
12 changes: 1 addition & 11 deletions src/execution/__tests__/stream-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ async function completeAsync(
for (let i = 0; i < numCalls; i++) {
promises.push(iterator.next());
}
return Promise.all(promises);
return await Promise.all(promises);
}

function createResolvablePromise<T>(): [Promise<T>, (value?: T) => void] {
Expand Down Expand Up @@ -531,11 +531,6 @@ describe('Execute: stream directive', () => {
},
],
},
],
hasNext: true,
},
{
incremental: [
{
items: [{ name: 'Leia', id: '3' }],
path: ['friendList', 2],
Expand Down Expand Up @@ -984,11 +979,6 @@ describe('Execute: stream directive', () => {
},
],
},
],
hasNext: true,
},
{
incremental: [
{
items: [{ nonNullName: 'Han' }],
path: ['friendList', 2],
Expand Down
Loading