Update eslint-config-ts-prefixer@v0.23.3

Further update async/await/Promise rules.

ยท

2 min read

Here is original post

I've published eslint-config-ts-prefixer@v0.23.3.

eslint-config-ts-prefixer is ESLint rule set that integrated prettier as one of ESLint rule and specialized fixable rule set. And

  • ๐Ÿ“ฆ Zero extend for explicit rules.

  • ๐Ÿ’… Prettier integration, specialized fixable import rules.

  • โœ… Meamingful rules code behavior.

I added these new rules this update. One of my favorite rule is @typescript-eslint/promise-function-async.
This is available --fix so auto adding async keyword property position.

1. @typescript-eslint/no-misused-new

This rule enforces that constructors within classes are marked with the new keyword.

// Bad
interface Foo {
  new (): Foo;
}

// Good
class Foo {
  constructor() {}
}

2. @typescript-eslint/no-misused-promises

This rule avoids using promises in places not designed to handle them.

// Bad
addEventListener('click', async () => {
  await doSomethingAsync();
});

// Good
addEventListener('click', () => {
  doSomethingAsync();
});

3. @typescript-eslint/promise-function-async

This rule requires any function or method that returns a Promise to be marked async.

// Bad
function foo(): Promise<void> {
  return Promise.resolve();
}

// Good
async function foo(): Promise<void> {
  return;
}

4. no-await-in-loop

This rule disallows await inside of loops.

// Bad
for (let i = 0; i < 10; i++) {
  await asyncFunction()
}

// Good
const promises = []
for (let i = 0; i < 10; i++) {
  promises.push(asyncFunction())
}
await Promise.all(promises)

5. no-return-await

This rule disallows unnecessary return await.

// Bad
async function foo() {
  return await bar()
}

// Good
async function foo() {
  return bar()
}

6. prefer-promise-reject-errors

This rule requires using Error objects as Promise rejection reasons.

// Bad
Promise.reject('something went wrong')

// Good
Promise.reject(new Error('something went wrong'))

7. require-atomic-updates

This rule disallows assignments that can lead to race conditions due to usage of await or yield.

// Bad
async function foo() {
  c = a + b
  await somethingAsync()
  c = c + 1 // This might be an outdated value of c
}

// Good
async function foo() {
  const temp = a + b
  await somethingAsync()
  c = temp + 1
}

Thank you for all yours. Really welcome ESLint & TS setting improvement feedback!