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: allowFunctionParams option in no-underscore-dangle (fixes 12579) #13545

Merged
merged 20 commits into from Aug 14, 2020

Conversation

sunghyunjo
Copy link
Contributor

@sunghyunjo sunghyunjo commented Aug 2, 2020

Prerequisites checklist

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

[X] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[X] 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)

(fixes #12579 )
If there is an underscore at the beginning of the function parameter name, it will give a warning.

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

None

@jsf-clabot
Copy link

jsf-clabot commented Aug 2, 2020

CLA assistant check
All committers have signed the CLA.

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Aug 2, 2020
@sunghyunjo sunghyunjo changed the title Update: added the allowFunctionParam option in no-underscore-dangle (fixes 12579) Update: add allowFunctionParam option in no-underscore-dangle (fixes 12579) Aug 2, 2020
@sunghyunjo sunghyunjo changed the title Update: add allowFunctionParam option in no-underscore-dangle (fixes 12579) Update: allowFunctionParam option in no-underscore-dangle (fixes 12579) Aug 2, 2020
@yeonjuan yeonjuan added accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Aug 2, 2020
Copy link
Member

@yeonjuan yeonjuan left a comment

Choose a reason for hiding this comment

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

@sunghyunjo
Thanks for working on this 👍 . I left some comments

docs/rules/no-underscore-dangle.md Outdated Show resolved Hide resolved
lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
@sunghyunjo
Copy link
Contributor Author

sunghyunjo commented Aug 2, 2020

@sunghyunjo
Thanks for working on this 👍 . I left some comments

@yeonjuan
Thanks for the review. Everything that was reviewed has been corrected.

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.

Thanks for working on this!

docs/rules/no-underscore-dangle.md Outdated Show resolved Hide resolved
docs/rules/no-underscore-dangle.md Outdated Show resolved Hide resolved
lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
@sunghyunjo
Copy link
Contributor Author

Thanks for working on this!

@kaicataldo
Corrected what you mentioned. Please tell me if there is any part I misunderstood.
Thanks for the review. 👍

Copy link
Member

@yeonjuan yeonjuan left a comment

Choose a reason for hiding this comment

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

Thanks for the change 👍 I left some reviews.

docs/rules/no-underscore-dangle.md Outdated Show resolved Hide resolved
lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
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, this looks great!

@mdjermanovic
Copy link
Member

Should we also check this parameter: (_a = 0) => {}

@mdjermanovic
Copy link
Member

Also not sure about this: (..._a) => {}

It might also make sense to check some identifiers in destructuring, like ([_a]) => {}, but since we're not checking those identifiers in variable declarations, then we probably shouldn't check them in parameters.

@mdjermanovic
Copy link
Member

Not checking function expression names might have been an oversight, but checking them now could introduce some conflicts:

/* eslint no-underscore-dangle: "error" */
/* eslint func-name-matching: "error" */

var foo = {
  _x: function _x() {}
}
/* eslint no-underscore-dangle: ["error", { "allowAfterThis": true }] */
/* eslint func-name-matching: "error" */

this._x = function _x() {};

Maybe we shouldn't change this behavior now?

lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
@yeonjuan
Copy link
Member

yeonjuan commented Aug 6, 2020

@mdjermanovic

Not checking function expression names might have been an oversight, but checking them now could introduce some conflicts:

I agree. 👍 I think this rule should check function expression names also.

Maybe we shouldn't change this behavior now?

IMO, it seems good to keep current behavior(non-checking) in this PR, because I think checking function expression's name seems not deeply related with adding allowFunctionParams option. :)

@mdjermanovic
Copy link
Member

IMO, it seems good to keep current behavior(non-checking) in this PR, because I think checking function expression's name seems not deeply related with adding allowFunctionParams option. :)

I support this, we can add node.type === "FunctionDeclaration" condition to keep the current behavior when checking node.id.

@kaicataldo thoughts?

@kaicataldo
Copy link
Member

Good catch! We definitely want to avoid situations where rules conflict with each other without some kind of escape hatch.

I support this, we can add node.type === "FunctionDeclaration" condition to keep the current behavior when checking node.id.

This seems like a good solution to me 👍

lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
@sunghyunjo
Copy link
Contributor Author

Should we also check this parameter: (_a = 0) => {}

@mdjermanovic
I have a question! I added a test case for this,
I couldn't pass the invalid case.

The invalid case is written as follows.

{ code: "function foo(_bar = 0) {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "Identifier" }] },
{ code: "const foo = { onClick(_bar = 0) { } }", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "Identifier" }] },
{ code: "const foo = (_bar = 0) => {}", options: [{ allowFunctionParams: false }], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "unexpectedUnderscore", data: { identifier: "_bar" }, type: "Identifier" }] }

IMO, The reason the invalid case is not passed
I think this is because the current code does not check the AssignmentPattern such as _bar = 0.

function printTips(_bar = 0, _let) {
  //
}

Using the code above as an example,
When AST is extracted from AST Explorer, it is determined that the AssignmentPattern needs additional logic as shown below.
image

Please answer if you want additional implementation or if my opinion is wrong.

@mdjermanovic
Copy link
Member

I think that with allowFunctionParams: false this rule should report _bar = 0 and ..._bar.

So, if a param is AssignmentPattern, then the rule should check its left (if it is an Identifier). If a param is RestElement, then the rule should check its argument (if it is an Identifier).

Given how the rule already works with variable declarations, I think it shouldn't go further into destructuring patterns.

@kaicataldo, @yeonjuan thoughts?

@yeonjuan
Copy link
Member

yeonjuan commented Aug 7, 2020

I think that with allowFunctionParams: false this rule should report _bar = 0 and ..._bar.

I think so. :). I think the option should check both(AssignmentPattern, RestElement).

Given how the rule already works with variable declarations, I think it shouldn't go further into destructuring patterns.

Agree 👍 Non-checking about the destructuring pattern seems a bug to me. But I think we can evaluate the issue separately.

@mdjermanovic mdjermanovic changed the title Update: allowFunctionParam option in no-underscore-dangle (fixes 12579) Update: allowFunctionParams option in no-underscore-dangle (fixes 12579) Aug 7, 2020
@sunghyunjo
Copy link
Contributor Author

sunghyunjo commented Aug 9, 2020

@mdjermanovic @yeonjuan
Thank you for answer. One answer has not arrived, But I agree with you.
Added additional logic for RestElement and AssignmentPattern. 😃

lib/rules/no-underscore-dangle.js Outdated Show resolved Hide resolved
Copy link
Member

@yeonjuan yeonjuan 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 👍

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.

Looks great, I left just a couple of notes about examples in the docs, and the tests.

docs/rules/no-underscore-dangle.md Show resolved Hide resolved
tests/lib/rules/no-underscore-dangle.js Show resolved Hide resolved
tests/lib/rules/no-underscore-dangle.js Show resolved Hide resolved
- `"allowAfterSuper": false` (default) disallows dangling underscores in members of the `super` object
- `"allowAfterThisConstructor": false` (default) disallows dangling underscores in members of the `this.constructor` object
- `"enforceInMethodNames": false` (default) allows dangling underscores in method names
- `"allowFunctionParams": true` (default) allows dangling underscores in function parameter names
Copy link
Member

Choose a reason for hiding this comment

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

To make more sense, it should defaults to false. we can change it in next major release( as it's a breaking change). 😄

@sunghyunjo
Copy link
Contributor Author

@mdjermanovic
Thanks for the review. I added all the test cases you mentioned.
Please make sure I understand and update it correctly.

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.

Looks great, thanks!

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.

This LGTM. Thanks for contributing to ESLint!

@kaicataldo kaicataldo merged commit b46f3ee into eslint:master Aug 14, 2020
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 11, 2021
@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 Feb 11, 2021
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 enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

no-underscore-dangle to check function params
7 participants