question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: Don't pass default values to functions

See original GitHub issue

Please describe what the rule should do:

Warn about using function parameters that are already set by the function’s defaults.

What category of rule is this? (place an “X” next to just one item)

[ ] Warns about a potential error (problem) [ ] Suggests an alternate way of doing something (suggestion) [ ] Enforces code style (layout) [x] Other (please specify:) Reduces code

Provide 2-3 code examples that this rule will warn about:

function run(always = true) {}
run(true); // Error
function run({always} = {always: true}) {}
run({always: true}); // Error
function run({always = true} = {}) {}
run({always: true}); // Error
function run(options) {
	options = {
		always: true,
		...options
	}
}
run({always: true}); // Error
function run(options) {
	options = Object.assign({
		always: true
	}, options);
}
run({always: true}); // Error

And any other common defaults pattern (unless already blocked by other rules like prefer-object-spread)

Why should this rule be included in ESLint (instead of a plugin)?

I think this is something you should tell me, I don’t know about your reasoning about what belongs to ESLint and what doesn’t. Could you include that in GitHub’s Issue template?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
fregantecommented, Apr 9, 2020

Actually, you’re wrong - I prefer the second version, strongly

You forgot to specify the HTTP status here, then: https://github.com/ljharb/tc39-ipr-check/blob/cc15bb5e28e8d7cbb8ee2f6f6d244c72920f90db/src/http/get-index/index.js#L32-L40

Why didn’t you set statusCode: 200 if you prefer it? That’s a default.

2reactions
mdjermanoviccommented, Apr 9, 2020

I agree with this comment from @ljharb, it looks like this rule wouldn’t provide much value since it would be limited to function calls in the same file where the function is defined.

Users could reasonably expect that it also works on imported/global functions and might see a lack of warnings as false negatives.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rule proposal: Don't pass default values to functions #2304
TypeScript deals with and tracks types of things, not values. To TypeScript, your function run has parameter {always} , which has type {always: ......
Read more >
[proposal] Allow function argument type to be omitted when passing ...
I propose that function argument types could be omitted in the same way as variable and property argument types are omitted when they...
Read more >
php - Using Default Arguments in a Function - Stack Overflow
I am confused about default values for PHP functions. Say I have a function like this: function foo($blah, $x = "some value", $y...
Read more >
Default parameters - JavaScript - MDN Web Docs
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
Read more >
Using Python Optional Arguments When Defining Functions
In this tutorial, you'll learn about Python optional arguments and how to define functions with default values. You'll also learn how to create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found