Skip to content
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

[object-shorthand] autofix breaks arrow functions with function types #124

Closed
nstepien opened this issue Jan 22, 2019 · 9 comments
Closed
Labels
bug Something isn't working external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@nstepien
Copy link
Contributor

What code were you trying to parse?

/* eslint object-shorthand: [1, "always", { avoidExplicitReturnArrows: true }] */
const test = {
  key: (arg: () => any) => {}
}

What did you expect to happen?
Autofix result should be

/* eslint object-shorthand: [1, "always", { avoidExplicitReturnArrows: true }] */
const test = {
  key(arg: () => any) {}
}

What actually happened?
Autofix gives

/* eslint object-shorthand: [1, "always", { avoidExplicitReturnArrows: true }] */
const test = {
  key(arg: () any) => {}
}

Versions

package version
@typescript-eslint/parser 1.0.0
TypeScript 3.2.4
ESLint 5.12.1
node 11.7.0
npm 6.5.0
@nstepien nstepien added package: parser Issues related to @typescript-eslint/parser triage Waiting for maintainers to take a look labels Jan 22, 2019
@platinumazure
Copy link
Contributor

Seems this could be fixed upstream (i.e., in the core ESLint rule) if we are careful to look for and remove arrow tokens after the full range of the arguments section of the node, instead of just getting the first arrow token.

What happens if you have multiple arguments that are function types? Does it only remove the arrow for the first argument? If so, that supports my hypothesis.

All of this assumes the parser is returning the correct range info-- we should check that first. 😄

@nstepien
Copy link
Contributor Author

/* eslint object-shorthand: [1, "always", { avoidExplicitReturnArrows: true }] */
const test = {
  key: (arg: () => any, arg2: () => any) => {}
}

becomes

/* eslint object-shorthand: [1, "always", { avoidExplicitReturnArrows: true }] */
const test = {
  key(arg: () any, arg2: () => any) => {}
}

Following your logic, I tried with a default parameter in a js file and it bugs out as well:

/* eslint object-shorthand: [1, "always", { avoidExplicitReturnArrows: true }] */
const test = {
  key: (arg = () => {}) => {}
}

becomes

/* eslint object-shorthand: [1, "always", { avoidExplicitReturnArrows: true }] */
const test = {
  key(arg = () {}) => {}
};

I'll open up an issue in the ESLint repo. I don't know if we should close this one yet, maybe once it's fixed in ESLint, it might still be broken with TS types.

@platinumazure
Copy link
Contributor

Thanks @MayhemYDG for your tests, this all seems to point to my hypothesis as you noted.

@JamesHenry We don't have any labels related to the issue being blocked, or that this might be solvable in ESLint core. How do you want to handle the labeling of this issue? I agree with @MayhemYDG that it could be worthwhile to leave this open.

@j-f1 j-f1 added external This issue is with another package, not typescript-eslint itself and removed triage Waiting for maintainers to take a look labels Jan 22, 2019
@j-f1
Copy link
Contributor

j-f1 commented Jan 22, 2019

Let’s close this for now since the issue is with ESLint. If the upstream fix doesn’t fix our case, please comment and we’ll reopen this.

@j-f1 j-f1 closed this as completed Jan 22, 2019
@platinumazure
Copy link
Contributor

@MayhemYDG Once you've opened the ESLint issue, please CC me-- I have some thoughts on how to fix. Thanks!

@nstepien
Copy link
Contributor Author

Upstream issue: eslint/eslint#11305

@platinumazure
Copy link
Contributor

For what it's worth: I used AST Explorer to check the parser output (although this was with typescript-eslint-parser rather than @typescript-eslint/parser) and the range info looks correct, so there's nothing we need to change there.

armanio123 pushed a commit to armanio123/typescript-eslint that referenced this issue Jan 24, 2019
@nstepien
Copy link
Contributor Author

nstepien commented Feb 4, 2019

@platinumazure @j-f1
I've updated to ESLint 5.13.0, which fixes most bugs with the rule, but unfortunately the rule's autofix is still broken if the function has a return type defined:

/* eslint object-shorthand: [1, "always", { avoidExplicitReturnArrows: true }] */
const test = {
  key: (arg: any): any => {}
}

becomes

const test = {
  key((arg: any): any) {}
}

instead of

const test = {
  key(arg: any): any {}
}

@j-f1 j-f1 reopened this Feb 4, 2019
@bradzacher bradzacher added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin and removed package: parser Issues related to @typescript-eslint/parser external This issue is with another package, not typescript-eslint itself labels Feb 4, 2019
@armano2 armano2 added external This issue is with another package, not typescript-eslint itself and removed external This issue is with another package, not typescript-eslint itself labels Feb 10, 2019
@bradzacher bradzacher added enhancement: new plugin rule New rule request for eslint-plugin enhancement: new base rule extension New base rule extension required to handle a TS specific case and removed enhancement: new plugin rule New rule request for eslint-plugin labels Apr 15, 2019
@bradzacher
Copy link
Member

I put up a fix for this in the eslint repo:
eslint/eslint#12260

I'll close this, feel free to track the fix via the above PR.

If that PR gets rejected, I'll reopen this issue and we can bring the rule into the plugin.

@bradzacher bradzacher added external This issue is with another package, not typescript-eslint itself and removed enhancement: new base rule extension New base rule extension required to handle a TS specific case labels Sep 11, 2019
@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Feb 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working external This issue is with another package, not typescript-eslint itself package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

5 participants