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 first arrow function in default parameter #11305

Closed
nstepien opened this issue Jan 22, 2019 · 1 comment · Fixed by #11307, WealthWizardsEngineering/hpropagate#8 or DavidKindler/burger-builder#46 · May be fixed by jakeherp/burger-builder#11 or jakeherp/burger-builder#15
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@nstepien
Copy link

nstepien commented Jan 22, 2019

Tell us about your environment

  • ESLint Version: 5.12.1
  • Node Version: 11.7.0
  • npm Version: 6.5.0

What parser (default, Babel-ESLint, etc.) are you using?
babel-eslint, @typescript-eslint/parser, and the default parser.
Please show your full configuration:

Configuration
module.exports = {
  parserOptions: {
    "ecmaVersion": 6
  },
  rules: {
    'object-shorthand': [1, "always", { avoidExplicitReturnArrows: true }]
  }
};

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

var test = {
  key0: function(arg) {},
  key1: (arg) => {},
  key2: (arg = () => {}) => {},
  key3: (arg1 = () => {}, arg2 = () => {}) => {}
}
eslint --fix --ext js,jsx,ts,tsx --max-warnings 0 -f codeframe --cache --color src

What did you expect to happen?
Autofix should have been:

var test = {
  key0(arg) {},
  key1(arg) {},
  key2(arg = () => {}) {},
  key3(arg1 = () => {}, arg2 = () => {}) {}
}

What actually happened? Please include the actual, raw output from ESLint.
Autofix gives invalid syntax:

var test = {
  key0(arg) {},
  key1(arg) {},
  key2(arg = () {}) => {},
  key3(arg1 = () {}, arg2 = () => {}) => {}
}

Command output:

error: Parsing error: Unexpected token ) at src\file.js:4:15:
  2 |   key0(arg) {},
  3 |   key1(arg) {},
> 4 |   key2(arg = () {}) => {},
    |               ^
  5 |   key3(arg1 = () {}, arg2 = () => {}) => {}
  6 | }
  7 |


1 error found.

Are you willing to submit a pull request to fix this bug?
No, but @platinumazure might have some thoughts about this.

@nstepien nstepien added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Jan 22, 2019
@platinumazure
Copy link
Member

platinumazure commented Jan 22, 2019

It looks like the issue is we find and remove the first arrow token in the object value, which could find an arrow token in a default parameter:

const arrowToken = sourceCode.getTokens(node.value).find(token => token.value === "=>");

Instead, I think we should be looking for the last arrow token before the body node. Something like:

// Note: We know we're in an ArrowFunctionExpression already
const arrowToken = sourceCode.getTokenBefore(node.value.body, { filter: token => token.value === "=>" });

There might be a bit more to it, but I'm hoping that will do the job for most cases.

@platinumazure platinumazure added rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Jan 22, 2019
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Aug 1, 2019
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Aug 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.