Typescript-eslint: [promise-function-async] problem with exported functions

Created on 7 Feb 2019  路  3Comments  路  Source: typescript-eslint/typescript-eslint

Repro

{
  "rules": {
    "@typescript-eslint/promise-function-async": [
      "error",
      {
        "allowedPromiseNames": [],
        "checkArrowFunctions": false,
        "checkFunctionDeclarations": true,
        "checkFunctionExpressions": false,
        "checkMethodDeclarations": false
      }
    ]
  }
}

code 1:

export function valid(n: number) { return n; }

code 2

export default function invalid(n: number) { return n; }

Expected Result
code 1 - no error
code 2 - lint error

Actual Result

TypeError: Cannot read property 'resolvedReturnType' of undefined
    at Object.getReturnTypeOfSignature (/path/to/repo/node_modules/typescript/lib/typescript.js:38106:28)
    at validateNode (/path/to/repo/node_modules/@typescript-eslint/eslint-plugin/lib/rules/promise-function-async.js:91:34)
    at FunctionDeclaration (/path/to/repo/node_modules/@typescript-eslint/eslint-plugin/lib/rules/promise-function-async.js:111:11)
    at listeners.(anonymous function).forEach.listener (/path/to/repo/node_modules/eslint/lib/util/safe-emitter.js:45:58)
    at Array.forEach (<anonymous>)
    at Object.emit (/path/to/repo/node_modules/eslint/lib/util/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/path/to/repo/node_modules/eslint/lib/util/node-event-generator.js:251:26)
    at NodeEventGenerator.applySelectors (/path/to/repo/node_modules/eslint/lib/util/node-event-generator.js:280:22)
    at NodeEventGenerator.enterNode (/path/to/repo/node_modules/eslint/lib/util/node-event-generator.js:294:14)
    at CodePathAnalyzer.enterNode (/path/to/repo/node_modules/eslint/lib/code-path-analysis/code-path-analyzer.js:632:23)

Additional Info

No problem with non-exported function declarations:

function valid(n: number) { return n; }
function invalid(p: Promise<void>) { return p; } // lint error

and also with arrow functions ( checkArrowFunctions: true ):

export const valid = (n: number) => n;
export const invalid = (p: Promise<void>) => p; // lint error

Versions

| package | version |
| ---------------------------------- | ------- |
| @typescript-eslint/eslint-plugin | 1.2.0 |
| @typescript-eslint/parser | 1.2.0 |
| TypeScript | 3.2.4, 3.3.0 |
| ESLint | 5.13.0 |
| node | 11.8.0 |
| npm | 6.7.0 |

bug eslint-plugin

All 3 comments

Bug is specifically with the line

https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/lib/rules/promise-function-async.js#L87

const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node);

This is returning undefined.

It happens because of how we create the ExportDefaultDeclaration (https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/typescript-estree/src/node-utils.ts#L458-L497).

Because the FunctionDeclaration node is not created as a standalone node, it never gets added to the ts -> es node map (https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/typescript-estree/src/convert.ts#L120-L123).

Hitting the same problem in my code base

I'm the TypeError: Cannot read property 'resolvedReturnType' of undefined with the following code:

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

    bar(): string {
        return 'baz';
    }
}

I believe the issue with me is coming from the constructor()

Versions

| Package | Version |
|--|--|
| @typescript-eslint/eslint-plugin | ^1.3.0 |
| eslint-config-xo-typescript | ^0.8.0 |
| typescript | ^3.3.3 |
| xo | 0.24.0 |

Was this page helpful?
0 / 5 - 0 ratings