-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Enhancement: [restrict-plus-operands] Bring in options from restrict-template-expressions #6110
8000New 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
Comments
@bradzacher bringing in #6161 (comment) on
...and #6161 (comment) on
I've anecdotally seen developers intentionally do both those string concatenations, such as for console logs when debugging. If we want to enable this in the |
IMO lint rules exist to ensure your code is ready for production. If the default/recommended config ignored that then you could very easily do like
|
Yes, I've been annoyed by it. It's happened a few times that I've wanted to print out something that includes, say, a boolean or regular expression in a message in a CLI and been bit by this rule. But I can't remember where those are now. You know, in my mind I had real world evidence from other users supporting this issue. But I can't find it anywhere. I suppose it's fine if we just close it until a real user requests the need. Thanks for the discussion! 😄 |
Aha @bradzacher I just found some examples. I tried enabling on the TypeScript repo and found about 200 lint complaints. A few examples of common code styles there are ...
The unfortunate predicament many repos are in, I think, is that they intentionally want to violate this rule if and only if the types are known to safely create developer-readable strings. Without the more permissive opt-in options, their choices are to either disable the rule, use very many inline comments, or modify the code to be more verbose. |
I wonder if there is a better way to do this sort of thing instead of using concatenation at the callsite? For example instead of APIs like log('Foo bar' + baz + ' ' + bam);
log('Foo bar', baz, bam); Eg an implementation like function log(...args: (string | number | boolean)[]) {
debugger(args.join(' '));
} There's opportunities to improve the codebase and have more safety in general instead of doing a shotgun, super unsafe option like I definitely understand why you would want to turn on such an option - because it's easier to ignore the cases instead of changing the codebase - but IMO "fix the API and disable the existing cases to be fixed later" is a better approach. IDK. Options live forever, but migrations are temporary. I'm always very averse to adding options instead of guiding teams to just do better. |
Sure - I think we're in agreement that the concatenation pattern isn't great. I like the |
I'm unhappy with, but won't block the addition of an option - but I think that we need to clearly document the caveat that it severely weakens the rule to the point that all it bans is I think we should document alternatives and better patterns to use to help guide users towards better code because better code means you can have a stricter rule and a better codebase. |
Uh oh!
There was an error while loading. Please reload this page.
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Link to the rule's documentation
https://typescript-eslint.io/rules/restrict-plus-operands
Description
restrict-plus-operands
andrestrict-template-expressions
don't tackle the exact same JavaScript behaviors (+
operands and template expressions don't concatenate/toString()
/toValue()
quite the same). But they both generally tackle the specific common case of code trying to join a string with a non-string:As a user, I've been inconvenienced by
restrict-plus-operands
complaining on concatenating two primitive values: e.g.number
andstring
. Sometimes I want to be able to join primitives together in my logs!Proposal: can we add the following options that already exist in
restrict-template-expressions
?: already exists!allowAny
allowNumber
allowBoolean
allowNullish
allowRegExp
Fail
Pass
Additional Info
I think we've seen a bigger demand for them in
restrict-template-expressions
because of https://eslint.org/docs/latest/rules/prefer-template. Linted projects don't concatenate strings with+
as much as they used to.The text was updated successfully, but these errors were encountered: