-
Notifications
You must be signed in to change notification settings - Fork 173
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
feature: add no-boolean-literal-args lint rule #976
base: main
Are you sure you want to change the base?
Conversation
How can I track when this lands in |
There should be a speaker icon or button on this page that says subscribe. Then you'll get notifications about this thread in github. |
@MierenManz : That let's me get notifications about this issue. But I want to know when this lands in |
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 rule seems quite annoying for when calling external code outside the user's control. Perhaps this rule would be more useful if it were no-boolean-literal-params
where it checks function, constructor, and method declarations for use of a boolean parameter instead of the call expressions? cc @bartlomieju
src/rules/no_boolean_literal_args.rs
Outdated
struct NoBooleanLiteralArgsHandler; | ||
|
||
impl NoBooleanLiteralArgsHandler { | ||
fn check_args<'a>(&'a mut self, params: &[&ExprOrSpread], ctx: &mut Context) { |
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.
It says params
here, but it seems these are actually arguments.
impl Handler for NoBooleanLiteralArgsHandler { | ||
fn call_expr(&mut self, expr: &deno_ast::view::CallExpr, ctx: &mut Context) { | ||
self.check_args(&expr.args, ctx); | ||
} |
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.
Should we add this for new expressions as well? new SomeClass(true)
I agree with @dsherret, this rule should only consider declarations, not invocations of functions. |
I agree this is a superfluous and overly specific rule (is this a real problem with code and why not other kinds of literals?), but applying it to declarations doesn't make sense either. Hitting it when passing args to third party functions aligns with the intent fwiw. It's supposed to make you name the argument on the calling side. |
Oh, I didn't realize that. |
The rule is intended to require that the calling code name boolean arguments and not simply pass true/false. E.g.
Then don't enable it in your ruleset.
Yes it's a real and well-known problem with code maintenance, and this simple rule can greatly improve the use of APIs that utilize boolean parameters (which is not always a bad API in and of itself).
Because boolean arguments are unique in that they always benefit from naming; other literals may or may not, depending on the context and API, and it's impossible to apply a blanket rule. For example,
That's 100% correct. |
It's not an analogy, it's an example, specifically one in which a literal integer value would be used because there's no useful abstract name for the value.
Yes they can. But booleans are unique in that, again, they always benefit from being named; as the above example shows, that's not true of other literals. Sure it might be true of an integer being used as an enumeration, but one can't practically say all integers shall have names. But I don't get the push-back. It's an optional rule that will have real, practical benefit for our team. If you don't like it, don't use it. |
I'm not sure I understand this correctly, you want the rule to forbid the following ?
|
7f039cc
to
fda0a6f
Compare
I understood @lawrence-dol opinion (which I personally agree with), I want to understand others' too |
I'm neither a deno user nor a js/ts expert. I find this discussion interesting nonetheless, because it applies to all languages without keyword arguments. IDE inlays for positional arguments, as shown by @nayeemrmn, solve the issue this lint rule is addressing, if the reader has an IDE and it the IDE understands the code. There are valid uses of unnamed boolean arguments, and it's too hard for a linter to tell them apart from invalid uses
More often than not, an unnamed boolean argument makes code harder to understand. Uses should be flagged by a linter, but without causing false positives. foxglove, an eslint plugin, has this same rule, but with an important option:
I've never used this rule, I suppose it acts like this
I can't think of an example where a function with multiple parameters, one of them being a Here's a good read about boolean traps |
Hi, I've read the blog from @Cre3per's comment and I must say that IMO, it justifies this rule as an opt-in rule. Although as @dsherret said, it should also warn on function declaration (and constructors), and suggest using an This : class Widget {
function repaint(immediate: boolean) {
// ...
}
} Should be replaced by this : class Widget {
function repaint({immediate: boolean}) {
// ...
}
} So in the end, the rule will warn users if they use boolean literals in function and constructor calls, and warn the use of booleans in function and constructor declaration. What do you think ? Should I start making these modifications ? |
@CRB169
No, I don't think you should. Whether to use an object or a boolean argument is a design choice and there a good arguments for both. The requested linting rule is to encourage labeling boolean arguments at the call point so that the calling code is self-documenting. It has nothing to do with trying to force the designer of the API to use or not use boolean arguments. It is, in effect, a warning upon finding the literals What you are now describing is an entirely separate rule which affects API design, whereas this one is intended for API use. I do think @Cre3per's earlier suggestion to exempt lone boolean calls may have merit, but even there I think the code is better with a constant than simply |
@CRBl69 : Incidentally, your idea could be implemented as |
To be clear, what I am saying is that the two rules call-point and declaration-point should be separate, as they address separate developers. The one I requested in this feature has been implemented as I've suggested. An additional rule could be also added to address the need at the declaration-point. |
This PR contains the lint rule asked for in issue #975. It will not allow you to use boolean literals (
true
andfalse
) when you make a function call.It is an opt-in (as requested) lint rule.
Previously, this would be allowed :
But with this lint rule, you have to do the following or else you will get a warning :