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

no-new-object: warns on non-global Object instantiation #12809

Closed
ljharb opened this issue Jan 19, 2020 · 7 comments · Fixed by #13115
Closed

no-new-object: warns on non-global Object instantiation #12809

ljharb opened this issue Jan 19, 2020 · 7 comments · Fixed by #13115
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

Comments

@ljharb
Copy link
Sponsor Contributor

ljharb commented Jan 19, 2020

Tell us about your environment

https://eslint.org/demo#eyJ0ZXh0IjoiLyogZXNsaW50IG5vLW5ldy1vYmplY3Q6IDIsIG5vLXJlZGVjbGFyZTogMCAqL1xudmFyIE9iamVjdCA9IGZ1bmN0aW9uIE9iamVjdCgpIHt9O1xubmV3IE9iamVjdCgpOyIsIm9wdGlvbnMiOnsicGFyc2VyT3B0aW9ucyI6eyJlY21hVmVyc2lvbiI6NSwic291cmNlVHlwZSI6InNjcmlwdCIsImVjbWFGZWF0dXJlcyI6e319LCJydWxlcyI6eyJjb25zdHJ1Y3Rvci1zdXBlciI6MiwiZm9yLWRpcmVjdGlvbiI6MiwiZ2V0dGVyLXJldHVybiI6Miwibm8tYXN5bmMtcHJvbWlzZS1leGVjdXRvciI6Miwibm8tY2FzZS1kZWNsYXJhdGlvbnMiOjIsIm5vLWNsYXNzLWFzc2lnbiI6Miwibm8tY29tcGFyZS1uZWctemVybyI6Miwibm8tY29uZC1hc3NpZ24iOjIsIm5vLWNvbnN0LWFzc2lnbiI6Miwibm8tY29uc3RhbnQtY29uZGl0aW9uIjoyLCJuby1jb250cm9sLXJlZ2V4IjoyLCJuby1kZWJ1Z2dlciI6Miwibm8tZGVsZXRlLXZhciI6Miwibm8tZHVwZS1hcmdzIjoyLCJuby1kdXBlLWNsYXNzLW1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I6MiwidmFsaWQtdHlwZW9mIjoyfSwiZW52Ijp7fX19

What did you expect to happen?
No warning.

What actually happened? Please include the actual, raw output from ESLint.
The object literal notation {} is preferrable. eslint(no-new-object)

Are you willing to submit a pull request to fix this bug?
Sure.

@ljharb ljharb added bug ESLint is working incorrectly triage An ESLint team member will look at this issue soon labels Jan 19, 2020
@mdjermanovic
Copy link
Member

There is an inconsistency between rules that target particular use of some globals:

  • Some rules check just identifier names (e.g., no-new-object).
  • Some rules additionally check if the variable is shadowed (e.g., radix).
  • Some rules use eslint-utils.ReferenceTracker (e.g., no-obj-calls).
/* eslint no-new-object: "error" */
/* eslint radix: "error" */
/* eslint no-obj-calls: "error" */

/* eslint-env browser*/

new Object(); // error
parseInt(a); // error
JSON(); // error

new window.Object(); // ok
window.parseInt(a); // ok
window.JSON(); // error

function foo(Object, parseInt, JSON) {
    new Object(); // error
    parseInt(a); // ok
    JSON(); // ok
}

Online Demo Link

Bug fix for no-new-object could be to do something like radix, and that shouldn't produce any new warnings.

On the other hand, it might be nice to align the rules of this kind and use ReferenceTracker in all of them.

But, it would also start reporting code such as new window.Object() or var x = Object; new x();, which is an enhancement I guess (maybe even breaking?).

Thoughts on how to proceed?

@mdjermanovic mdjermanovic added enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Jan 21, 2020
@ljharb
Copy link
Sponsor Contributor Author

ljharb commented Jan 22, 2020

Since v7 is coming, I'd definitely want to get all those ReferenceTracker changes in.

In general, I feel like anyone who enables this rule wants those things reported, so i'd consider it a patch.

@mdjermanovic
Copy link
Member

I have concerns about cases such as the following, which would be reported if this rule uses ReferenceTracker:

var x = something ? MyClass : Object;

var y = new x();

In this case, user can't just replace something with {}. The code has to be refactored to avoid warning. There could be a more complex example that makes sense but wouldn't satisfy the new version of this rule.

I'm wondering is ReferenceTracker suitable for some rules that are already using it, because it tracks all potential references to the targeted global.

It seems useful in no-obj-calls as it can warn about possible calls to Math, JSON and other non-callable globals in some code paths, which is indeed a runtime error.

On the other hand, this looks like a false positive in the prefer-object-spread rule and an autofix that changes the behavior:

/* eslint prefer-object-spread: ["error"] */

var doSomething = foo ? myFunc : Object.assign;

var x = doSomething({}, a, b); // gets fixed to: var x = { ...a, ...b};

Online Demo

We should probably revisit those rules.

@ljharb
Copy link
Sponsor Contributor Author

ljharb commented Jan 23, 2020

In that case, that code should be rewritten, as new should never be used with Object :-)

@ljharb
Copy link
Sponsor Contributor Author

ljharb commented Jan 23, 2020

I certainly do agree, though, that if a reference does not always result in being a thing, it shouldn't be warned/autofixed as being that thing.

@kaicataldo
Copy link
Member

@ljharb Agreed that this case is a bug.

@mdjermanovic Thanks for digging into this! Could we create a new meta issue with all the differences in how we handle globals at the moment?

@kaicataldo kaicataldo added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint labels Jan 27, 2020
@mdjermanovic
Copy link
Member

I'll open an issue for globals, it would be nice to unify that.

As for this issue with no-new-objects, it looks safe for now to just fix the shadowing bug, without ReferenceTracker?

anikethsaha added a commit to anikethsaha/eslint that referenced this issue Mar 28, 2020
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Sep 29, 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 29, 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 a pull request may close this issue.

3 participants