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
MayhemYDG opened this Issue Jan 22, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@MayhemYDG
Copy link

MayhemYDG 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.

@platinumazure

This comment has been minimized.

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 accepted and removed triage labels Jan 22, 2019

g-plane added a commit that referenced this issue Jan 23, 2019

PlayMa256 added a commit to PlayMa256/GrCartuchos that referenced this issue Feb 1, 2019

Update eslint in group default to the latest version 🚀 (#125)
## The devDependency [eslint](https://github.com/eslint/eslint) was updated from `5.12.1` to `5.13.0`.
This version is **not covered** by your **current version range**.

If you don’t accept this pull request, your project will work just like it did before. However, you might be missing out on a bunch of new features, fixes and/or performance improvements from the dependency update.

---

<details>
<summary>Release Notes for v5.13.0</summary>

<ul>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/91c8884971f5e57f5f7490d8daf92c4a9a489836"><code>91c8884</code></a> Chore: use local function to append "s" instead of a package (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="400953921" data-permission-text="Issue title is private" data-url="eslint/eslint#11293" data-hovercard-type="pull_request" data-hovercard-url="/eslint/eslint/pull/11293/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/pull/11293">#11293</a>) (Timo Tijhof)</li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/b5143bfc09e53d8da8f63421ade093b7593f4f51"><code>b5143bf</code></a> Update: for-direction detection false positives/negatives (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="397135133" data-permission-text="Issue title is private" data-url="eslint/eslint#11254" data-hovercard-type="pull_request" data-hovercard-url="/eslint/eslint/pull/11254/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/pull/11254">#11254</a>) (Ruben Bridgewater)</li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/9005e632d13476880c55f7e3c8a6e450762a5171"><code>9005e63</code></a> Chore: increase camelcase test coverage (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="401174018" data-permission-text="Issue title is private" data-url="eslint/eslint#11299" data-hovercard-type="pull_request" data-hovercard-url="/eslint/eslint/pull/11299/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/pull/11299">#11299</a>) (Redmond Tran)</li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/5b14ad1003c7df9a37621dea55c6d6d0484adc05"><code>5b14ad1</code></a> Fix: false positive in no-constant-condition (fixes <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="401916289" data-permission-text="Issue title is private" data-url="eslint/eslint#11306" data-hovercard-type="issue" data-hovercard-url="/eslint/eslint/issues/11306/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/issues/11306">#11306</a>) (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="402047796" data-permission-text="Issue title is private" data-url="eslint/eslint#11308" data-hovercard-type="pull_request" data-hovercard-url="/eslint/eslint/pull/11308/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/pull/11308">#11308</a>) (Pig Fang)</li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/6567c4f6665df85c3347388b29d8193cc8208d63"><code>6567c4f</code></a> Fix: only remove arrow before body in object-shorthand (fixes <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="401807378" data-permission-text="Issue title is private" data-url="eslint/eslint#11305" data-hovercard-type="issue" data-hovercard-url="/eslint/eslint/issues/11305/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/issues/11305">#11305</a>) (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="402033639" data-permission-text="Issue title is private" data-url="eslint/eslint#11307" data-hovercard-type="pull_request" data-hovercard-url="/eslint/eslint/pull/11307/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/pull/11307">#11307</a>) (Pig Fang)</li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/fa2f370affa4814dbdda278f9859d0172d4b7aa2"><code>fa2f370</code></a> Docs: update rule configuration values in examples (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="404018485" data-permission-text="Issue title is private" data-url="eslint/eslint#11323" data-hovercard-type="pull_request" data-hovercard-url="/eslint/eslint/pull/11323/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/pull/11323">#11323</a>) (Kai Cataldo)</li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/0a3c3ff1d91e8f39943efc4a7d2bf6927d68d37e"><code>0a3c3ff</code></a> New: Allow globals to be disabled/configured with strings (fixes <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="294234548" data-permission-text="Issue title is private" data-url="eslint/eslint#9940" data-hovercard-type="issue" data-hovercard-url="/eslint/eslint/issues/9940/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/issues/9940">#9940</a>) (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="405027784" data-permission-text="Issue title is private" data-url="eslint/eslint#11338" data-hovercard-type="pull_request" data-hovercard-url="/eslint/eslint/pull/11338/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/pull/11338">#11338</a>) (Teddy Katz)</li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/dccee63cf41234180c71bf0fe01b165c9078fc69"><code>dccee63</code></a> Chore: avoid hard-coding the list of core rules in eslint:recommended (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="404605578" data-permission-text="Issue title is private" data-url="eslint/eslint#11336" data-hovercard-type="pull_request" data-hovercard-url="/eslint/eslint/pull/11336/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/pull/11336">#11336</a>) (Teddy Katz)</li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/c1fd6f54d92efe615bcae529006221e122dbe9e6"><code>c1fd6f5</code></a> Chore: remove undocumented <code>Linter#rules</code> property (refs <a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="253051999" data-permission-text="Issue title is private" data-url="eslint/eslint#9161" data-hovercard-type="issue" data-hovercard-url="/eslint/eslint/issues/9161/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/issues/9161">#9161</a>) (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="404599049" data-permission-text="Issue title is private" data-url="eslint/eslint#11335" data-hovercard-type="pull_request" data-hovercard-url="/eslint/eslint/pull/11335/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/pull/11335">#11335</a>) (Teddy Katz)</li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/36e335681d61cbe3c83b653b7cc5f95730f1d86e"><code>36e3356</code></a> Chore: remove dead code for loading rules (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="404588598" data-permission-text="Issue title is private" data-url="eslint/eslint#11334" data-hovercard-type="pull_request" data-hovercard-url="/eslint/eslint/pull/11334/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/pull/11334">#11334</a>) (Teddy Katz)</li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/c464e2744ec76e7e9c6c5af0f6162c92187f1ece"><code>c464e27</code></a> Docs: Rename <code>result</code> -&gt; <code>foo</code> (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="394168276" data-permission-text="Issue title is private" data-url="eslint/eslint#11210" data-hovercard-type="pull_request" data-hovercard-url="/eslint/eslint/pull/11210/hovercard" href="https://urls.greenkeeper.io/eslint/eslint/pull/11210">#11210</a>) (Alexis Tyler)</li>
</ul>
</details>

<details>
<summary>Commits</summary>
<p>The new version differs by 13 commits.</p>
<ul>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/4b267a5c8a42477bb2384f33b20083ff17ad578c"><code>4b267a5</code></a> <code>5.13.0</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/720dce11ed89ce17ce3600044fd8e45145d353eb"><code>720dce1</code></a> <code>Build: changelog update for 5.13.0</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/91c8884971f5e57f5f7490d8daf92c4a9a489836"><code>91c8884</code></a> <code>Chore: use local function to append "s" instead of a package (#11293)</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/b5143bfc09e53d8da8f63421ade093b7593f4f51"><code>b5143bf</code></a> <code>Update: for-direction detection false positives/negatives (#11254)</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/9005e632d13476880c55f7e3c8a6e450762a5171"><code>9005e63</code></a> <code>Chore: increase camelcase test coverage (#11299)</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/5b14ad1003c7df9a37621dea55c6d6d0484adc05"><code>5b14ad1</code></a> <code>Fix: false positive in no-constant-condition (fixes #11306) (#11308)</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/6567c4f6665df85c3347388b29d8193cc8208d63"><code>6567c4f</code></a> <code>Fix: only remove arrow before body in object-shorthand (fixes #11305) (#11307)</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/fa2f370affa4814dbdda278f9859d0172d4b7aa2"><code>fa2f370</code></a> <code>Docs: update rule configuration values in examples (#11323)</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/0a3c3ff1d91e8f39943efc4a7d2bf6927d68d37e"><code>0a3c3ff</code></a> <code>New: Allow globals to be disabled/configured with strings (fixes #9940) (#11338)</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/dccee63cf41234180c71bf0fe01b165c9078fc69"><code>dccee63</code></a> <code>Chore: avoid hard-coding the list of core rules in eslint:recommended (#11336)</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/c1fd6f54d92efe615bcae529006221e122dbe9e6"><code>c1fd6f5</code></a> <code>Chore: remove undocumented <code>Linter#rules</code> property (refs #9161) (#11335)</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/36e335681d61cbe3c83b653b7cc5f95730f1d86e"><code>36e3356</code></a> <code>Chore: remove dead code for loading rules (#11334)</code></li>
<li><a href="https://urls.greenkeeper.io/eslint/eslint/commit/c464e2744ec76e7e9c6c5af0f6162c92187f1ece"><code>c464e27</code></a> <code>Docs: Rename <code>result</code> -&gt; <code>foo</code> (#11210)</code></li>
</ul>
<p>See the <a href="https://urls.greenkeeper.io/eslint/eslint/compare/faf3c4eda0d27323630d0bc103a99dd0ecffe842...4b267a5c8a42477bb2384f33b20083ff17ad578c">full diff</a></p>
</details>

<details>
  <summary>FAQ and help</summary>

  There is a collection of [frequently asked questions](https://greenkeeper.io/faq.html). If those don’t help, you can always [ask the humans behind Greenkeeper](https://github.com/greenkeeperio/greenkeeper/issues/new).
</details>

---


Your [Greenkeeper](https://greenkeeper.io) bot 🌴
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment