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: Report constructor calls in no-obj-calls #12909

Merged
merged 3 commits into from Mar 24, 2020
Merged

Conversation

mdjermanovic
Copy link
Member

@mdjermanovic mdjermanovic commented Feb 13, 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 an item)

[X] Changes an existing rule

Note: this PR also removes Math and JSON from the no-new-wrappers rule.

What rule do you want to change?

no-obj-calls

Does this change cause the rule to produce more or fewer warnings?

more

How will the change be implemented? (New option, new default behavior, etc.)?

new default behavior

Please provide some example code that this change will affect:

/*eslint no-obj-calls: "error"*/
/*eslint-env es2017*/

var newMath = new Math();

var newJSON = new JSON();

var newReflect = new Reflect();

var newAtomics = new Atomics();

What does the rule currently do for this code?

no errors

What will the rule do after it's changed?

4 errors

What changes did you make? (Give an overview)

Updated no-obj-calls to warn on NewExpression as well.

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

As suggested in #11984 (comment), no-obj-calls instead of no-new-wrappers should report new Math() and new JSON(). I guess it makes sense to report all 4 objects when used with new, it's always a runtime error.

I think there is no need for a new messageId.

@mdjermanovic mdjermanovic added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Feb 13, 2020
@ljharb
Copy link
Sponsor Contributor

ljharb commented Feb 14, 2020

Would it also be worth throwing on new $BuiltIn.prototype for every builtin that's not a function?

@mdjermanovic
Copy link
Member Author

Would it also be worth throwing on new $BuiltIn.prototype for every builtin that's not a function?

Like new Math.prototype? Wouldn't that always throw for all functions as well, e.g., new RegExp.prototype

@ljharb
Copy link
Sponsor Contributor

ljharb commented Feb 14, 2020

RegExp.prototype isn't a function; Function.prototype is :-) so i mean like, new Boolean.prototype(), new Object.prototype(), etc.

@mdjermanovic
Copy link
Member Author

RegExp.prototype isn't a function; Function.prototype is :-) so i mean like, new Boolean.prototype(), new Object.prototype(), etc.

I think this isn't related to globals, new foo.prototype() is always an error (assuming the common meaning of the prototype property).

@ljharb
Copy link
Sponsor Contributor

ljharb commented Feb 14, 2020

Yes, so is new Math().

I’m suggesting that the “no object calls” rule should warn on builtin objects that aren’t functions, which includes both Math and Boolean.prototype.

@mdjermanovic
Copy link
Member Author

Yes, so is new Math().

I’m suggesting that the “no object calls” rule should warn on builtin objects that aren’t functions, which includes both Math and Boolean.prototype.

I understand the logic now - for every builtin function BuiltIn, BuiltIn.prototype is also a builtin object :)

Makes sense (just not sure how often people write new anything.prototype()), though I think that should be a separate proposal?

@ljharb
Copy link
Sponsor Contributor

ljharb commented Feb 14, 2020

Perhaps, just seems like an easy addition and in the same spirit of the RFC.

@kaicataldo
Copy link
Member

I support this change, though I think we should mark it as breaking since the rule is catching a whole new class of error.

@ljharb
Copy link
Sponsor Contributor

ljharb commented Feb 17, 2020

@kaicataldo it seems like the same class to me - using new on something that can’t possibly do anything but throw.

@nzakas
Copy link
Member

nzakas commented Feb 25, 2020

I’m not a fan of checking for prototypes as I just don’t think that’s a common enough mistake. However, if others disagree please open a separate issue to discuss. We shouldn’t add anything more to this PR.

@ljharb
Copy link
Sponsor Contributor

ljharb commented Feb 25, 2020

I mean, fair, but is new JSON or new Math common?

@nzakas
Copy link
Member

nzakas commented Mar 21, 2020

@mdjermanovic it looks like there’s a merge conflict, can you rebase and fix the conflicts?

@mdjermanovic
Copy link
Member Author

Rebased, fixed conflicts, and added some tests that combine changes from this PR with changes from #12774.

@mdjermanovic
Copy link
Member Author

If it would be easier, I can add changes from #11984 to this PR.

This PR and #11984 should be, ideally, merged at the same time anyway.

@nzakas
Copy link
Member

nzakas commented Mar 23, 2020

That seems like a good idea to get #11984 unblocked. 👍 to adding those commits to this PR

@mdjermanovic
Copy link
Member Author

Done!

(tests are failing only because of #13076).

@nzakas
Copy link
Member

nzakas commented Mar 24, 2020

Merged #13076. Can you try rebasing to see if that makes the CI happy?

@mdjermanovic
Copy link
Member Author

It works well now 🎉

@nzakas
Copy link
Member

nzakas commented Mar 24, 2020

Awesome, thanks!

@nzakas nzakas merged commit a1370ab into master Mar 24, 2020
@nzakas nzakas deleted the noobjcalls-newcalls branch March 24, 2020 19:37
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Sep 22, 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 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
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 evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants