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

Update: check identifier in array pattern in id-length (fixes #12832) #12839

Merged
merged 4 commits into from Feb 28, 2020

Conversation

yeonjuan
Copy link
Member

@yeonjuan yeonjuan commented Jan 28, 2020

Prerequisites checklist

  • I have read the contributing guidelines.
  • The team has reached consensus on the changes proposed in this pull request. If not, I understand that the evaluation process will begin with this pull request and won't be merged until the team has reached consensus.

What is the purpose of this pull request? (put an "X" next to item)

[ ] Documentation update
[x] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

Change the id-length rule to check an identifier in the array pattern.

const [a] = arr;

Is there anything you'd like reviewers to focus on?

This PR will fix #12832.
It will generate more errors so I labeled this pr as Update.

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Jan 28, 2020
@yeonjuan yeonjuan added accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Jan 28, 2020
Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

tests/lib/rules/id-length.js Outdated Show resolved Hide resolved
@mdjermanovic
Copy link
Member

By the code it seems that this rule aims to report only variable creations (declarations, params etc.), not all references, maybe we should do that for array patterns as well?

[a] = foo; // shouldn't be error?

Though, RestElement and AssignmentPattern would be already false positives in a similar way (and there are other false positives/false negatives).

@yeonjuan
Copy link
Member Author

yeonjuan commented Feb 4, 2020

@mdjermanovic
Thanks for the review :)

By the code it seems that this rule aims to report only variable creations

Could you please explain it more?

Online Demo

/*eslint id-length: "error"*/

var a = 4; // Error
a = 5; // No error

// Object Pattern
var {c} = foo; // Error
({c} = foo); // Error

// AssignmentPattern
var [d = b] = foo; // Error
[d = b] = foo; // Error

// RestElement
var [...d] = foo; // Error
[...d] = foo; // Error

// No Error in v6.8.0 but both Errors in the PR version.
var [a] = foo;
[a] = foo; 

In my understanding. this rule seems to report the syntax which can make a new identifier if there isn't. (maybe LHS lookup?)

// without declaration
[a] = [3];   // it creates an identifier 'a`
({b} = { b: 3 });   // it creates an identifier 'b`

From this point of view, the below cases also need to be warned.

a = 5;  // No error in the current version.
[a] = 5;
({a} = {});

I would like to hear your opinion :)

@kaicataldo
Copy link
Member

Given that other ExpressionStatement nodes generate errors, I think it makes sense for this option to be consistent with the current behavior. Could we land this now and create a follow-up issue to discuss this futher?

@mdjermanovic
Copy link
Member

A problem is that the current behavior is already inconsistent.

I'm not sure if someone may complain about why [a] = foo; becomes an error now, since a = foo[0]; isn't. Also, a simple a = foo; isn't an error.

By looking at the actual and the initial version of the code, I think that a = foo; is intentionally no error, while the fact that [a = foo] = bar;, [...a] = foo;, and ({ a } = foo); are all errors could be an oversight. The last one is same as a = foo.a;, which isn't an error. Unfortunately, there are no tests for either of these.

@mdjermanovic
Copy link
Member

In the docs, Examples of correct code for this rule with a minimum of 4: var { prop: [x] } = {}; should be also invalid now?

Also, Examples of correct code for this rule with the { "min": 4 } option: var { prop: [x] } = {};

Hm, these two sections look pretty much same? The first one has text "This rule has a shorthand integer option for the "min" object property". It could be for an option that wasn't implemented?

@yeonjuan
Copy link
Member Author

yeonjuan commented Feb 8, 2020

@mdjermanovic

In the docs, Examples of correct code for this rule with a minimum of 4: var { prop: [x] } = {}; should be also invalid now?
Also, Examples of correct code for this rule with the { "min": 4 } option: var { prop: [x] } = {};

Yes, I agree. I'll update the documentation.

Hm, these two sections look pretty much same? The first one has text "This rule has a shorthand integer option for the "min" object property". It could be for an option that wasn't implemented?

It seems so. I think it should be removed because it is not supported actually.

Thanks :)

@kaicataldo
Copy link
Member

I think merging #12881 created merge conflicts here - sorry!

# Conflicts:
#	docs/rules/id-length.md
#	tests/lib/rules/id-length.js
@yeonjuan
Copy link
Member Author

@kaicataldo That's no problem 😄 I knew it

Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Non-blockers:

  • Some examples in correct { "min": 4 } are incorrect, but that's another bug.
  • I still think the intention was to not report left side of assignments unless it's an assignment to a property with invalid length.

@kaicataldo kaicataldo merged commit 051567a into master Feb 28, 2020
@kaicataldo kaicataldo deleted the idlength-arraypattern branch February 28, 2020 20:50
montmanu pushed a commit to montmanu/eslint that referenced this pull request Mar 4, 2020
…12832) (eslint#12839)

* Update: check identifier in array pattern in id-length (fixes eslint#12832)

* fix wrong test case

* Update documentation
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Aug 28, 2020
@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 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

id-length does not check identifier in array pattern
3 participants