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

Fix invalid autofix with destructuring assignment in no-for-loops rule #476

Merged
merged 5 commits into from Dec 17, 2019

Conversation

bmish
Copy link
Sponsor Collaborator

@bmish bmish commented Dec 17, 2019

Example (original):

for (let i = 0; i < arr.length; i++) {
  const { a, b } = arr[i];
  console.log(a, b);
}

Autofix (before this fix):

for (const element of arr) {
  console.log(a, b);
}

Autofix (after this fix):

for (const element of arr) {
  const { a, b } = element;
  console.log(a, b);
}

When arr[i] is used in a destructuring assignment, the entire assignment should not be removed (previous behavior), only arr[i] should be replaced with element (new behavior).

Example (original):
```js
for (let i = 0; i < arr.length; i++) {
  const { a, b } = arr[i];
  console.log(a, b);
}
```

Autofix (before this fix):
```js
for (const element of arr) {
  console.log(a, b);
}
```

Autofix (after this fix):
```js
for (const element of arr) {
  const { a, b } = element;
  console.log(a, b);
}
```

When `arr[i]` is used in a destructuring assignment, the entire assignment should not be removed (previous behavior), only `arr[i]` should be replaced with `element` (new behavior).
@fisker
Copy link
Collaborator

fisker commented Dec 17, 2019

LGTM, but can we fix this to

for (const { a, b } of arr) {
  console.log(a, b);
}

if element is not used?

maybe we can keep removeRange, but do something with replacement

@bmish
Copy link
Sponsor Collaborator Author

bmish commented Dec 17, 2019

@fisker that's a good idea. However, since that is a much more involved fix, it's probably worth considering as a follow-up improvement.

@fisker
Copy link
Collaborator

fisker commented Dec 17, 2019

sending a PR to your fork

@fisker
Copy link
Collaborator

fisker commented Dec 17, 2019

@bmish

bmish#1

what do you think? can you merge it?

@bmish
Copy link
Sponsor Collaborator Author

bmish commented Dec 17, 2019

@fisker your improvement looks great, thank you! But I'm not sure how to merge it in my fork due to the conflicts. Perhaps you could just add the commit to this PR?

…g-fix' into no-for-loops-destructuring-fix

# Conflicts:
#	rules/no-for-loop.js
#	test/no-for-loop.js
@fisker

This comment has been minimized.

@fisker fisker requested a review from futpib December 17, 2019 07:11
@fisker fisker changed the title fix: invalid autofix with destructuring assignment in no-for-loops rule Fix invalid autofix with destructuring assignment in no-for-loops rule Dec 17, 2019
@fisker fisker merged commit 44a67f1 into sindresorhus:master Dec 17, 2019
@fisker
Copy link
Collaborator

fisker commented Dec 17, 2019

Good job, thank you! @bmish

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

Successfully merging this pull request may close these issues.

None yet

3 participants