Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected an assignment or function call and instead saw an expression.eslintno-unused-expressions

Hi I am using a ternary operator inside a function. But eslint throws error on this. Pls help me to fix this.

  const Test = (showBtn, bubbleId, latitude, longitude, zoom, drillLevel) => {
    setShowBtn(showBtn);
    drillLevel === 'area' ? getCitiesData(bubbleId) : getStatesData();
    setViewport({
      ...viewport,
      latitude,
      longitude,
      zoom,
    });
  };
like image 251
Dhanush Kumar Sivaji Avatar asked Apr 18 '20 10:04

Dhanush Kumar Sivaji


People also ask

What is no unused expressions?

This rule aims to eliminate unused expressions which have no effect on the state of the program. This rule does not apply to function calls or constructor calls with the new operator, because they could have side effects on the state of the program.


3 Answers

If your current code works, it would probably be more appropriate to avoid the conditional operator entirely and use if/else instead:

if (drillLevel === 'area') getCitiesData(bubbleId)
else getStatesData();

This way, you don't actually have any unused expressions, which is the right way to fix this sort of linter warning.

like image 111
CertainPerformance Avatar answered Oct 19 '22 23:10

CertainPerformance


You can simply add this allowTernary comment above the ternary operator expression, to disable eslint error for that line:

/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/
drillLevel === 'area' ? getCitiesData(bubbleId) : getStatesData();
like image 27
palaѕн Avatar answered Oct 19 '22 21:10

palaѕн


May be you forget to assign get* result to some variable, waht about viewport?

const data = drillLevel === 'area' ?
  getCitiesData(bubbleId) :
  getStatesData();
like image 43
Kyr Avatar answered Oct 19 '22 23:10

Kyr