Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

[Error] Decorated getter error #102

Closed
oreqizer opened this issue Oct 9, 2016 · 6 comments
Closed

[Error] Decorated getter error #102

oreqizer opened this issue Oct 9, 2016 · 6 comments
Labels

Comments

@oreqizer
Copy link

oreqizer commented Oct 9, 2016

What version of TypeScript are you using?
^2.0.3

What version of typescript-eslint-parser are you using?
^0.4.0

What code were you trying to parse?

I'm getting an error if I'm using a decorated getter (using Mobx). Information is in the attached npm debug. This is the code sample:

screen shot 2016-10-09 at 19 35 14

If I remove the @computed thingie, or the get thingie, it works. It breaks when both are used simultaneously.

Thanks 👍

What did you expect to happen?
expected it to lint my code correctly

What happened?
npm-debug.txt

@JamesHenry
Copy link
Member

Note: We do not yet officially support TypeScript 2 (although that does not mean that it does not work in all cases).

It is not clear from your debug output that this parser, or even ESLint, is the cause of your issue. Please provide more information as to what is going wrong.

What is your ESLint config?

@oreqizer
Copy link
Author

{
  "parser": "typescript-eslint-parser",
  "extends": ["airbnb"],
  "env": {
    "node": true,
    "jasmine": true
  },
  "globals": {
    "jest": true
  },
  "rules": {
    // conflicts with 'arrow body style'
    "no-confusing-arrow": "off",
    // due to TypeScript
    "no-undef": "off",
    "import/no-unresolved": "off",
    "import/extensions": "off",
    "react/prop-types": "off"
  }
}

@JamesHenry
Copy link
Member

In future, please provide more detailed source code, and in a form which can be copied. Naturally if you give me a screenshot, I have to manually type it out.

There is an error being thrown inside the core keyword-spacing rule in this case.

An immediate workaround for the error is to disable the rule in your eslint config:

"rules": {
  "keyword-spacing": "off"
}

@nzakas I have investigated this a bit:

  • The error is thrown when attempting to use the result of checkMethodMap[token.value] within the keyword-spacing rule's checkSpacingAround() logic.
  • checkMethodMap['@'] is currently undefined.
  • Adding @ as a keyword does solve the problem, but I am curious as to why it is only occurring for the accessor decorator i.e. why there is no issue if you remove get from the method...

@soda0289
Copy link
Member

soda0289 commented Jan 15, 2017

I took a look into this issue. It is caused by the keyword-spacing rule as @JamesHenry pointed out. When the get, set, or async keywords are applied to a method the rule will get the first token, in this case the @, and check spacing around it. Since this is not a keyword found in the KEYS array an error occurs.

If we remove the decorator then the first token found is get which is a keyword and works as expected. If we remove the get the code takes a different path and the first token is not checked.

I looked into how babel-eslint parser handles this situation, since they do not throw an error. They change the node.kind to 'method' instead of 'get' or 'set' which skips over checking the first token. For async methods however it seems they have made sourceCode.getFirstToken() skip over the decorator. Not sure how they did that but I guess they just remove it during parsing.

@JamesHenry @nzakas
Do you believe we should support decorators in eslint? This would mean updating the rules to skip over decorator tokens but would allow us to overcome issues with no-undef and other rules.
Or should we copy the technique babel-eslint used and remove the decorator tokens when getFirstToken() is called?

@soda0289
Copy link
Member

I have opened an issue on eslint to hopefully gain some more attention.
eslint/eslint#8110

@soda0289
Copy link
Member

soda0289 commented Mar 4, 2017

Should be fixed in eslint 3.17

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants