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: allow references to external globals in id-blacklist (fixes #12567) #12987

Merged
merged 2 commits into from Mar 23, 2020

Conversation

mdjermanovic
Copy link
Member

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 an item)

[X] Bug fix

Fixes #12567

What changes did you make? (Give an overview)

id-blacklist will now ignore identifiers that represent a reference to an external global variable:

/* eslint id-blacklist: ["error", "Number", "undefined"] */

const foo = Number.parseInt(bar); // ok

let baz = undefined; // ok

Global variables declared in the same source code will be still invalid:

/* eslint id-blacklist: ["error", "foo"] */

var foo = 1; // error

foo = 2; // error

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

This will be still invalid although it is a reference to a global variable, because it also represents a property name:

/* eslint id-blacklist: ["error", "undefined"] */

var foo = { undefined };

@mdjermanovic mdjermanovic added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion labels Mar 1, 2020
* Write access isn't allowed, because it potentially creates a new property with a blacklisted name.
*/
if (
parent.type === "MemberExpression" &&
Copy link
Member

@yeonjuan yeonjuan Mar 11, 2020

Choose a reason for hiding this comment

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

maybe, Is this PR intended to ignoring check an identifier in an assignment that has no declaration?

I tested it and recognized it pass the blacklist identifier with assignment in destructuring.

This is the case which I tested.

({a: parseInt} = a); // no warning in this pr version.

// but it warns with the declaration.
var paseInt; ({a: parseInt} = a);  // warning

And it is warned in v6.8.0 (no declaraion)

/*eslint id-blacklist: ["error", "parseInt"] */
({a: parseInt} = foo); // warning

Copy link
Member Author

Choose a reason for hiding this comment

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

maybe, Is this PR intended to ignoring check an identifier in an assignment that has no declaration?

Only if it is a global variable that isn't declared in the same source code. The logic is: if it is declared, then we can assume it's a user-controlled name. Otherwise, it's a built-in/env- specific/third-party global so user can't change the name.

It could be also indeed a user-controlled name declared elsewhere, but in that case id-blacklist will warn in the file where it is declared.

This shouldn't be a warning because parseInt is a global variable that isn't declared in this code:

/*eslint id-blacklist: ["error", "parseInt"] */

({a: parseInt} = a);

This should be warning (2 warnings, one for each Identifier node) because parseInt is declared:

/*eslint id-blacklist: ["error", "parseInt"] */

var parseInt; ({a: parseInt} = a);

This should be also a warning because foo isn't defined in the configuration as a global variable:

/*eslint id-blacklist: ["error", "foo"] */

({a: foo} = a);

Copy link
Member

Choose a reason for hiding this comment

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

@mdjermanovic

Only if it is a global variable that isn't declared in the same source code. The logic is: if it is declared, then we can assume it's a user-controlled name. Otherwise, it's a built-in/env- specific/third-party global so user can't change the name.

Thanks for the explanation. 👍 it makes sense to me.

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

@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! Appreciate all the clear comments in the code and tests 👍

]
},
{
code: "var foo = [Map];", // this actually isn't a disabled global: it was never enabled because es6 environment isn't enabled
Copy link
Member

Choose a reason for hiding this comment

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

👍

@kaicataldo kaicataldo merged commit aef9488 into master Mar 23, 2020
@kaicataldo kaicataldo deleted the idblacklist-globals branch March 23, 2020 23:16
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Sep 21, 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 Sep 21, 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-blacklist catches calls to Number
3 participants