Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ESLint: Why is no-unused-expressions failing?

In my code I have a series of ternary operators using the addition/subtraction assignment operator to modify the value for a variable corresponding to the pixel position of an element in an SVG.

A few examples of the expressions:

target === 'L' ? (endX -= edgeShift) : (endX += edgeShift);
source === 'L' ? (startX += halfSize) : (startX -= halfSize);
target === 'T' ? (endY -= edgeShift) : (endY += (edgeShift + 12));

My linter is throwing the error @typescript-eslint/no-unused-expressions. From how I am interpreting the documentation for the rule it seems that each side of the ternary operator modifies the state of the program so I’d think it wouldn’t fail.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am unable to see what "allowTernary" is set too and the examples make it unclear of the expected behavior when its false.

Since I’d assume this is an issue on my end, 1) What is causing this? 2) Is there a way I can do this in one line without modifying the configuration? I’d prefer to not have to use two lines with an if/else since it hurts readability.

>Solution :

I think allowTernary is false in your case, so the expectation is you would use an if/else block rather than a ternary expression that goes unassigned.

You might try something like this instead though.

endX += target === 'L' ? -edgeShift : edgeShift;
startX += source === 'L' ? halfSize : -halfSize;
endY += target === 'T' ? -edgeShift : (edgeShift + 12);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading