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

Rule proposal: no-useless-undefined #683

Closed
fisker opened this issue Apr 14, 2020 · 9 comments · Fixed by #718
Closed

Rule proposal: no-useless-undefined #683

fisker opened this issue Apr 14, 2020 · 9 comments · Fixed by #718

Comments

@fisker
Copy link
Collaborator

fisker commented Apr 14, 2020

Fail

function foo() {
	return undefind;
}
let a = undefined;
call(undefined);
call(foo, bar, undefined);
const {foo = undefined} = {};
function foo({bar = undefined} = {}) {}
function foo(bar = undefined) {}

Pass

function foo() {
	return;
}
let a;
call();
call(foo, bar);
call(foo, bar,);
const {foo} = {};
function foo({bar} = {}) {}
function foo(bar) {}

Discussion #636 (comment)

Not sure about this,

const noop = () => undefined;

// prefer this?
const noop = () => {};
@sindresorhus
Copy link
Owner

// prefer this?
const noop = () => {};

Yes

@sindresorhus
Copy link
Owner

This is accepted.

@snowteamer
Copy link

I think this rule is a great idea, but how are we supposed to mark a formal parameter as optional if the following form is disallowed ?

function foo(bar = undefined) {
  if (typeof bar !== "undefined") {
    // Do something with 'bar'
  }
}
  • Omitting the parameter declaration altogether does not seem to be an option since the function body will probably use it ;
  • It is unclear to me wheather replacing = undefined with = void 0 is a good thing.

This information can of course be expressed in annotations, but I would prefer to be able to also keep it in the code.

@fisker
Copy link
Collaborator Author

fisker commented May 6, 2020

We are not removing parameter bar

function foo(bar = undefined) {
  if (typeof bar !== "undefined") {
    // Do something with 'bar'
  }
}

To

function foo(bar) {
  if (typeof bar !== "undefined") {
    // Do something with 'bar'
  }
}

Is this breaking something?

@snowteamer
Copy link

snowteamer commented May 6, 2020

It does not break anything per se, but it still removes the visual indication that the bar parameter is optional, which is the main reason why I sometimes use that form.

@fisker
Copy link
Collaborator Author

fisker commented May 6, 2020

How about

function foo(bar /* optional */) {}

@snowteamer
Copy link

snowteamer commented May 6, 2020

Yes this is an interesting solution, I might give it a try if nothing else changes - thank you for your time.

Although I am not sure about how to consistently enforce the presence and the exact formatting of this inline comment, as I currently do not know of any linter rule that does this.

@fisker
Copy link
Collaborator Author

fisker commented May 6, 2020

Not sure which one better, but all seems clear

function foo(bar, baz /* optional */) {}
function foo(bar, /* optional */ baz) {}

EvgenyOrekhov added a commit to EvgenyOrekhov/eslint-config-hardcore that referenced this issue May 17, 2020
I see several valid usecases for using "useless" `undefined`:

- declaring parameters as optional (this also affects code behavior
  and IDE hints)
- declaring properties as optional when using parameter destructuring
  (affects IDE hints)
- passing `undefined` to functions (affects code behavior when it
  depends on `arguments.length`)
- passing `undefined` to functions in tests for explicitness

See discussions:

- sindresorhus/eslint-plugin-unicorn#731
- sindresorhus/eslint-plugin-unicorn#683
@fisker fisker reopened this Dec 29, 2020
@fisker
Copy link
Collaborator Author

fisker commented Dec 29, 2020

reopen by accident.

@fisker fisker closed this as completed Dec 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants