3

I understand that () => {} does not need return, however if it is not there then Eslint complains about unused expressions.

export const isInInterval = (from, to, target) => {
  if (isNaN(Date.parse(to)) && isNaN(Date.parse(from)) === true) {
    return
  }
  const toUnixTimestamp = time => new Date(time).getTime()
  toUnixTimestamp(to) - target > toUnixTimestamp(from) ? true : false
}

Here is the function: it tries to find out whether some specified date(to) minus specified period(target) is later in time than from. It should return true if so and false in the opposite case. I keep bumping into eslint error expected assignment to a function call and instead saw expression.

I tried to rewrite it several times, but in most iterations I got `arrow function expects no return error, ex.:

return (toUnixTimestamp(to) - target > toUnixTimestamp(from)) ? true : false

2
  • 1
    It is difficult to see what is happening because your code is too compact! I would add more intermediate variables to better see what is happening. Short compact one liners do not improve code readability. Dumb plain obvious code improves readability. Commented Aug 15, 2018 at 6:24
  • 1
    Per @givanse's comment, you might consider a function like isDateValid and then your first line can be if (!isDateValid(to) || !isDateValid(from)) return; Commented Aug 15, 2018 at 6:28

2 Answers 2

5

I understand that () => {} does not need return

That's not the case. Arrow functions only implicitly return when what followed the => is a single expression. If you use => {, the opening bracket { indicates the start of a function block, and you do indeed need to explicitly return at the end of the block (or wherever else you want to return something).

At the moment, your code isn't returning anything at all - that's what the linting error is trying to tell you - the true : false is going unused at the moment, it's just an orphaned expression.

So, just add the return statement to the beginning of your conditional:

export const isInInterval = (from, to, target) => {
  if (isNaN(Date.parse(to)) && isNaN(Date.parse(from)) === true) {
    return
  }
  const toUnixTimestamp = time => new Date(time).getTime()
  return toUnixTimestamp(to) - target > toUnixTimestamp(from)
    ? true
    : false
}

Or, because > evaluates to a boolean already, you might omit the conditional operator entirely:

return toUnixTimestamp(to) - target > toUnixTimestamp(from)

Here's an example of how you would write an arrow function that does use implicit return:

export const isEarlyTimestamp = (timestamp) => (
  timestamp < 4000000
  ? true
  : false
);
Sign up to request clarification or add additional context in comments.

2 Comments

thank you CertainPerformance, eslint screams at me again with arrow function expected no return value, I guess I will just ignore the line with eslint-disable-line.
The rule that's tripping you up is probably consistent-return: eslint.org/docs/rules/consistent-return If you like implicit return when possible (which I think is just fine - it significantly reduces syntax noise, especially with array methods), just disable that rule.
1

try this:

export const isInInterval = (from, to, target) => {
    if (isNaN(Date.parse(to)) && isNaN(Date.parse(from)) === true) {
    return false
    }
    const toUnixTimestamp = time => new Date(time).getTime()
    return toUnixTimestamp(to) - target > toUnixTimestamp(from);
}

7 Comments

Thanks, Tilak, I thought of it as well, unfortunately I still got [eslint] Unnecessary use of boolean literals in conditional expression. (no-unneeded-ternary).
Okay, do one thing. write a if condition statement instead of ternary and cheeck once!!
It did help! Thank you! Though it looks a little bit inelegant, I am wondering whether it can be done better: if (toUnixTimestamp(to) - target > toUnixTimestamp(from)) { return true } return false However it is the only way that works with eslint from all the proposed solutions. Thank you
Same, error on true: [eslint] Unnecessary use of boolean literals in conditional expression. (no-unneeded-ternary), rewrite to if - else helped though.
Try again, this will be better way!!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.