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
Comments
There is an inconsistency between rules that target particular use of some globals:
/* 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
} Bug fix for On the other hand, it might be nice to align the rules of this kind and use But, it would also start reporting code such as Thoughts on how to proceed? |
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. |
I have concerns about cases such as the following, which would be reported if this rule uses var x = something ? MyClass : Object;
var y = new x(); In this case, user can't just replace something with I'm wondering is It seems useful in On the other hand, this looks like a false positive in the /* eslint prefer-object-spread: ["error"] */
var doSomething = foo ? myFunc : Object.assign;
var x = doSomething({}, a, b); // gets fixed to: var x = { ...a, ...b}; We should probably revisit those rules. |
In that case, that code should be rewritten, as |
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. |
@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? |
I'll open an issue for globals, it would be nice to unify that. As for this issue with |
Tell us about your environment
https://eslint.org/demo#eyJ0ZXh0IjoiLyogZXNsaW50IG5vLW5ldy1vYmplY3Q6IDIsIG5vLXJlZGVjbGFyZTogMCAqL1xudmFyIE9iamVjdCA9IGZ1bmN0aW9uIE9iamVjdCgpIHt9O1xubmV3IE9iamVjdCgpOyIsIm9wdGlvbnMiOnsicGFyc2VyT3B0aW9ucyI6eyJlY21hVmVyc2lvbiI6NSwic291cmNlVHlwZSI6InNjcmlwdCIsImVjbW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3BhY2VzIjoyLCJuby1zZWxmLWFzc2lnbiI6Miwibm8tc2hhZG93LXJlc3RyaWN0ZWQtbmFtZXMiOjIsIm5vLXNwYXJzZS1hcnJheXMiOjIsIm5vLXRoaXMtYmVmb3JlLXN1cGVyIjoyLCJuby11bmRlZiI6Miwibm8tdW5leHBlY3RlZC1tdWx0aWxpbmUiOjIsIm5vLXVucmVhY2hhYmxlIjoyLCJuby11bnNhZmUtZmluYWxseSI6Miwibm8tdW5zYWZlLW5lZ2F0aW9uIjoyLCJuby11bnVzZWQtbGFiZWxzIjoyLCJuby11bnVzZWQtdmFycyI6Miwibm8tdXNlbGVzcy1jYXRjaCI6Miwibm8tdXNlbGVzcy1lc2NhcGUiOjIsIm5vLXdpdGgiOjIsInJlcXVpcmUteWllbGQiOjIsInVzZS1pc25hbiI6MiwidmFsaWQtdHlwZW9mIjoyfSwiZW52Ijp7fX19
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.
The text was updated successfully, but these errors were encountered: