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

Avoid naming collision with default array element variable in autofix for no-for-loop rule #749

Merged
merged 6 commits into from May 27, 2020

Conversation

bmish
Copy link
Sponsor Collaborator

@bmish bmish commented May 25, 2020

Fixes #747. Will also help prepare us for #745.

@bmish bmish force-pushed the no-for-loop-avoidCapture branch 2 times, most recently from 165ecad to ae3e0bb Compare May 25, 2020 16:29
@bmish
Copy link
Sponsor Collaborator Author

bmish commented May 25, 2020

@fisker I added one more test case.

Copy link
Collaborator

@fisker fisker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix tests

@bmish bmish force-pushed the no-for-loop-avoidCapture branch from 231eed9 to a1c5b30 Compare May 26, 2020 03:02
@bmish
Copy link
Sponsor Collaborator Author

bmish commented May 26, 2020

@fisker thanks a lot for adding additional test cases. I fixed the failure.

@@ -335,7 +343,7 @@ const create = context => {
const shouldGenerateIndex = isIndexVariableUsedElsewhereInTheLoopBody(indexVariable, bodyScope, arrayIdentifierName);

const index = indexIdentifierName;
const element = elementIdentifierName || defaultElementName;
const element = elementIdentifierName || avoidCapture(defaultElementName, getChildScopesRecursive(bodyScope), context.parserOptions.ecmaVersion);
Copy link
Collaborator

@fisker fisker May 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not right.

			for (let i = 0; i < arr.length; i += 1) {
				console.log(arr[1]);
				function foo(element) {
					console.log(element);
				}
			}

It should allow use variable element.

this foo function scope should not included, because it didn't use arr[1], this is why avoidCapture didn't recursive automaticlly.

We need collect scopes from references.


BTW: don't squash commits, this makes review harder.

Copy link
Sponsor Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few questions for you.

  1. By arr[1], do you mean arr[i]? The rule doesn't report a violation with arr[1].
  2. In your example, I see that we technically can use element as our new array variable name, but wouldn't that be unnecessarily confusing? The no-shadow rule suggests this is bad practice.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yes, arr[1], sorry.
  2. Yes no-shadow suggest no variable shadowing, it's default to function, do you use all option? My example is about function scope, but there are other scopes.

This example

for (let i = 0; i < errors.length; i++) {
  console.log(errors[i]);

  try {
    doSomethingThrowError();
  } catch(error) {}
}

After #745 we will fix it to

for (const error_ of errors) {
  console.log(error_);

  try {
    doSomethingThrowError();
  } catch(error) {}
}

I don't want use error_, I can use error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, this is not very important, let's keep it this way now.

I'm going to check this PR again.

Copy link
Sponsor Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. I will follow-up with your recommendation if I have a chance.

@fisker fisker merged commit 48bd5c8 into sindresorhus:master May 27, 2020
@fisker
Copy link
Collaborator

fisker commented May 27, 2020

Thanks for fixing this.

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.

no-for-loop fix to invalid code
2 participants