TristonJ / eslint-plugin-prefer-arrow

ESLint plugin to prefer arrow functions
MIT License
54 stars 10 forks source link

handle async functions with void return type #23

Open ahmedkotb opened 4 years ago

ahmedkotb commented 4 years ago

trying to lint async functions in typescript that has a return type of Promise<void> will cause the plugin to crash.

image

image

functions that returns something seems to be ok.

this is an example that repros the case

tester.run('lib/rules/prefer-arrow-functions', rule, {
  parserOptions: {ecmaVersion: 6},
  valid: [ ],
  invalid: [
    ...[
      // Support fixes with typescript typings
      ...[
        [
          `const good = {
            async myFunc(a: number, b: number): Promise<number> {
              return Promise.resolve(1);
            }
          }`,
          `const good = {
            async myFunc: (a: number, b: number): Promise<number> => Promise.resolve(1)
          }`,
        ],
        [
        `const bad = {
          async myFunc(a: number, b: number): Promise<void> {
            return;
          }
        }`,
        `const bad = {
          async myFunc: (a: number, b: number): Promise<void> => {}
        }`,
        ],
      ].map(test => [...test, null, { parser: require.resolve('@typescript-eslint/parser') }])
    ].map(inputOutput => Object.assign(
      {
        errors: ['Prefer using arrow functions over plain functions which only return a value'],
        output: inputOutput[1],
        ...(inputOutput[3] || {}),
      },
      {
        ...singleReturnOnly(inputOutput[0], inputOutput[2]),
        parserOptions: {
          ...singleReturnOnly(inputOutput[0], inputOutput[2]).parserOptions,
          ...((inputOutput[3] || {}).parserOptions || {})
        },
      }
    )),
  ]
});