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

valid-typeof: recommend "requireStringLiterals" true by default #14074

Closed
wmertens opened this issue Feb 6, 2021 · 10 comments
Closed

valid-typeof: recommend "requireStringLiterals" true by default #14074

wmertens opened this issue Feb 6, 2021 · 10 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion breaking This change is backwards-incompatible rule Relates to ESLint's core rules
Projects

Comments

@wmertens
Copy link

wmertens commented Feb 6, 2021

The version of ESLint you are using.
7.18

The problem you want to solve.
I typed typeof v !== undefined instead of typeof v !== 'undefined' and eslint with the recommended settings didn't warn me. This was one of those hard to find stupid bugs.

Your take on the correct solution to problem.
Since typeof nowadays only returns strings, I propose that the option requireStringLiterals be true by default in the eslint:recommended settings.

Are you willing to submit a pull request to implement this change?
Sure

@wmertens wmertens added core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint triage An ESLint team member will look at this issue soon labels Feb 6, 2021
@ljharb
Copy link
Sponsor Contributor

ljharb commented Feb 6, 2021

not just nowadays - typeof has never returned a non-string value. The reason for the option is so you can do typeof x === y and have that be allowed.

That said, it's clearer to hardcode the string literal, so I'm in favor of this change :-)

@mdjermanovic mdjermanovic added breaking This change is backwards-incompatible rule Relates to ESLint's core rules and removed core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint triage An ESLint team member will look at this issue soon labels Feb 6, 2021
@mdjermanovic mdjermanovic added this to Evaluating in Triage Feb 6, 2021
@mdjermanovic
Copy link
Member

Hi @wmertens, thanks for the issue!

Since typeof nowadays only returns strings, I propose that the option requireStringLiterals be true by default in the eslint:recommended settings.

I'm not in favor of this change, because requireStringLiterals: true can report valid code like typeof x === y and thus is a stylistic option (at least partially, though it was indeed added for cases like this, ref #6698), while the rule by default reports only a certainly invalid code and in eslint:recommended we would like to have only such errors.

I typed typeof v !== undefined instead of typeof v !== 'undefined' and eslint with the recommended settings didn't warn me. This was one of those hard to find stupid bugs.

Maybe we could use getStaticValue to check more than just string literals by default.

@ljharb
Copy link
Sponsor Contributor

ljharb commented Feb 6, 2021

if typeof x === y is code you want to be considered valid, then typeof x === undefined must be too, since undefined and y are both identifiers.

It's worth noting that many engines optimize typeof with a string literal, but historically do not do this for typeof x === y, so while it's arguably stylistic it's also arguably an avoidable performance cliff.

@mdjermanovic
Copy link
Member

if typeof x === y is code you want to be considered valid, then typeof x === undefined must be too, since undefined and y are both identifiers.

I think we can assume that the global undefined has the undefined value, and report typeof x === undefined if it's a reference to the global undefined.

It's worth noting that many engines optimize typeof with a string literal, but historically do not do this for typeof x === y, so while it's arguably stylistic it's also arguably an avoidable performance cliff.

That's a good point, but I'm still not sure would it qualify for eslint:recommended, where we have only rules that catch possible errors or some legacy syntax.

@ljharb
Copy link
Sponsor Contributor

ljharb commented Feb 7, 2021

That is very much not a safe assumption unless you’re statically looking for all undefined assignments in the file, and unless eslint can know for certain it’s either an ES Module, or a node module - otherwise it could be concatenated with another file that defined undefined. I believe the strict rule has the same concerns.

@wmertens
Copy link
Author

wmertens commented Feb 7, 2021

But wouldn't it be ok to show an error in this case, so that only crazy people that redefine undefined have to explicitly disable the setting?

Are people like that even running eslint?

@mdjermanovic
Copy link
Member

If anyone wants to submit that rule change proposal, I'd be willing to champion reporting typeof v !== undefined regardless of the requireStringLiterals option.

@btmills
Copy link
Member

btmills commented Mar 18, 2021

Do we need a special case in valid-typeof when no-undefined already reports typeof v !== undefined? Or is the goal to say, sure, go ahead and use undefined except adjacent to a typeof where it's probably a mistake?

@btmills btmills moved this from Evaluating to Feedback Needed in Triage Jun 10, 2021
@btmills btmills moved this from Feedback Needed to Evaluating in Triage Jun 10, 2021
@wmertens
Copy link
Author

@btmills not everybody uses no-undefined, I personally find it annoying

@nzakas
Copy link
Member

nzakas commented Aug 28, 2021

We aren’t moving forward with this, so closing.

@nzakas nzakas closed this as completed Aug 28, 2021
Triage automation moved this from Evaluating to Complete Aug 28, 2021
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Feb 25, 2022
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 25, 2022
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 breaking This change is backwards-incompatible rule Relates to ESLint's core rules
Projects
Archived in project
Triage
Complete
Development

No branches or pull requests

5 participants