-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(eslint-plugin): support BigInt in restrict-plus-operands rule #344
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
Conversation
@@ -283,5 +285,25 @@ var foo = pair + pair; | |||
}, | |||
], | |||
}, | |||
{ | |||
code: `var foo = 1n; foo + 1`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already an error thrown by TypeScript. Why you need a lint rule for it?
Operator '+' cannot be applied to types 'bigint' and '1'.
https://www.typescriptlang.org/play/index.html#src=var%20foo%20%3D%201n%3B%20foo%20%2B%201
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mohsen1 , good point. Maybe it's really an overhead and this rule makes sense only for strings and numbers.
I've implemented this to fix issue #309, but you're right that TypeScript does the same check already.
@novemberborn , @j-f1 , what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, it would do that. However the rule then needs to ensure either operand is actually a string or number before complaining (or any
/ unknown
). The problem in #309 was that it complained about bigint
even though bigints can be summed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, I'd like to propose the next behaviour for this rule:
(any or unknown) + (string or number or bigint)
- rule triggers an error, as it does now
all other cases
- let TS decide what error to show
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems reasonable. You may want to pull that suggestion into its own issue for wider discussion, if you haven't already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a table to show the cases this rule should cover in my opinion:
+ | number | string | BigInt | any | unknown |
---|---|---|---|---|---|
number | rpo |
TS | nua |
TS | |
string | rpo |
rpo |
nua |
TS | |
BigInt | TS | rpo |
nua |
TS | |
any | nua |
nua |
nua |
nua |
TS |
unknown | TS | TS | TS | TS | TS |
Legend:
rpo
: The behavior is invalid and should be caught byrestrict-plus-operands
.- TS: The behavior is invalid and is already caught by Typescript.
nua
: The behavior coercesany
into a stricter type and should thus be caught byno-unsafe-any
.- An empty cell means valid behavior.
Codecov Report
@@ Coverage Diff @@
## master #344 +/- ##
=======================================
Coverage 97.14% 97.14%
=======================================
Files 74 74
Lines 2875 2875
Branches 473 473
=======================================
Hits 2793 2793
Misses 49 49
Partials 33 33
|
Fixes #309