8000 [promise-function-async] Error line offset when using decorators · Issue #751 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

[promise-function-async] Error line offset when using decorators #751

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
RecuencoJones opened this issue Jul 24, 2019 · 3 comments
Closed
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended

Comments

@RecuencoJones
Copy link
RecuencoJones commented Jul 24, 2019

Repro

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "tsconfig.json"
  },
  "plugins": [
    "@typescript-eslint"
  ],
  "rules": {
    "@typescript-eslint/promise-function-async": "error"
  }
}
{
  "compilerOptions": {
    "target": "es2017",
    "experimentalDecorators": true
  },
  "files": [
    "index.ts"
  ]
}
const SomeFancyDecorator = (): MethodDecorator => (target) => target;

export class Foo {
  @SomeFancyDecorator()
  method() {
    return Promise.resolve('foo');
  }
}

Expected Result

/Users/recuedav/Workspace/GitHub/RecuencoJones/typescript-eslint-issue/index.ts
  5:3  error  Functions that return promises must be async  @typescript-eslint/promise-function-async

✖ 1 problem (1 error, 0 warnings)

Actual Result

/Users/recuedav/Workspace/GitHub/RecuencoJones/typescript-eslint-issue/index.ts
  4:3  error  Functions that return promises must be async  @typescript-eslint/promise-function-async

✖ 1 problem (1 error, 0 warnings)

Additional Info

Repository for reproduction: https://github.com/RecuencoJones/typescript-eslint-issue-751

Versions

package version
@typescript-eslint/eslint-plugin 1.11.0
@typescript-eslint/parser 1.11.0
TypeScript 3.4.3
ESLint 5.16.0
node 10.15.0
npm 6.7.0
@RecuencoJones RecuencoJones added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jul 24, 2019
@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed triage Waiting for team members to take a look labels Jul 24, 2019
@bradzacher
Copy link
Member

This is working as expected.

As far as the AST is concerned (and typescript), the decorator is considered to be owned by the method, and as such it is included within its bounds.

AST Example

Note in the following example that the method definition has the decorator property.

repl

{
    "type": "MethodDefinition",
    "key": {
        "type": "Identifier",
        "name": "method",
        "range": [116, 122],
        "loc": {
            "start": {
                "line": 5,
                "column": 2
            },
            "end": {
                "line": 5,
                "column": 8
            }
        }
    },
    "value": {
        "type": "FunctionExpression",
        "id": null,
        "generator": false,
        "expression": false,
        "async": false,
        "body": {
            "type": "BlockStatement",
            "body": [
                {
                    "type": "ReturnStatement",
                    "argument": {
                        "type": "CallExpression",
                        "callee": {
                            "type": "MemberExpression",
                            "object": {
                                "type": "Identifier",
                                "name": "Promise",
                                "range": [138, 145],
                                "loc": {
                                    "start": {
                                        "line": 6,
                                        "column": 11
                                    },
                                    "end": {
                                        "line": 6,
                                        "column": 18
                                    }
                                }
                            },
                            "property": {
                                "type": "Identifier",
                                "name": "resolve",
                                "range": [146, 153],
                                "loc": {
                                    "start": {
                                        "line": 6,
                                        "column": 19
                                    },
                                    "end": {
                                        "line": 6,
                                        "column": 26
                                    }
                                }
                            },
                            "computed": false,
                            "range": [138, 153],
                            "loc": {
                                "start": {
                                    "line": 6,
                                    "column": 11
                                },
                                "end": {
                                    "line": 6,
                                    "column": 26
                                }
                            }
                        },
                        "arguments": [
                            {
                                "type": "Literal",
                                "raw": "'foo'",
                                "value": "foo",
                                "range": [154, 159],
                                "loc": {
                                    "start": {
                                        "line": 6,
                                        "column": 27
                                    },
                                    "end": {
                                        "line": 6,
                                        "column": 32
                                    }
                                }
                            }
                        ],
                        "range": [138, 160],
                        "loc": {
                            "start": {
                                "line": 6,
                                "column": 11
                            },
                            "end": {
                                "line": 6,
                                "column": 33
                            }
                        }
                    },
                    "range": [131, 161],
                    "loc": {
                        "start": {
                            "line": 6,
                            "column": 4
                        },
                        "end": {
                            "line": 6,
                            "column": 34
                        }
                    }
                }
            ],
            "range": [125, 165],
            "loc": {
                "start": {
                    "line": 5,
                    "column": 11
                },
                "end": {
                    "line": 7,
                    "column": 3
                }
            }
        },
        "range": [122, 165],
        "params": [],
        "loc": {
            "start": {
                "line": 5,
                "column": 8
            },
            "end": {
                "line": 7,
                "column": 3
            }
        }
    },
    "computed": false,
    "static": false,
    "kind": "method",
    "range": [92, 165],
    "loc": {
        "start": {
            "line": 4,
            "column": 2
        },
        "end": {
            "line": 7,
            "column": 3
        }
    },
    "decorators": [
        {
            "type": "Decorator",
            "expression": {
                "type": "CallExpression",
                "callee": {
                    "type": "Identifier",
                    "name": "SomeFancyDecorator",
                    "range": [93, 111],
                    "loc": {
                        "start": {
                            "line": 4,
                            "column": 3
                        },
                        "end": {
                            "line": 4,
                            "column": 21
                        }
                    }
                },
                "arguments": [],
                "range": [93, 113],
                "loc": {
                    "start": {
                        "line": 4,
                        "column": 3
                    },
                    "end": {
                        "line": 4,
                        "column": 23
                    }
                }
            },
            "range": [92, 113],
            "loc": {
                "start": {
                    "line": 4,
                    "column": 2
                },
                "end": {
                    "line": 4,
                    "column": 23
                }
            }
        }
    ]
}

@SuperOleg39
Copy link

Hello!

Behaviour, when Node range include decorators range - default Typescript AST behaviour, is inconsistent with babel-parser behaviour, for example you can see this issue babel/babel#6676

Maybe when converting we need to reset ranges?

@bradzacher
Copy link
Member

please file a new issue instead of commenting on an old, closed issue.
https://github.com/typescript-eslint/typescript-eslint/blob/master/CONTRIBUTING.md#commenting

@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Apr 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin working as intended Issues that are closed as they are working as intended
Projects
None yet
Development

No branches or pull requests

3 participants
0