Is there an eslint rule warning about operator precedence when || ?? chaining is mixed with ternary operator? #18201
-
I had at least two bugs in the last two days that were caused by a wrong assumption about operator precedence with regard to Is there already a rule I can activate to warn me if I use anything like this in my code base without using parentheses? The concrete bugs I had: First Example const errorMessage =
error_data?.error?.upstream?.error_text ||
error_data?.message ||
typeof error_data === 'string'
? error_data
: 'unknown error' Expected outcome if const errorMessage =
error_data?.error?.upstream?.error_text ||
error_data?.message ||
(typeof error_data === 'string'
? error_data
: 'unknown error') Second Example const buttonAction =
!config.button?.href ? () => state.loadMore() : undefined
const buttonText = config.button?.text ?? buttonAction ? 'Load more items' : 'Show all' Expected outcome if const buttonAction =
!config.button?.href ? () => state.loadMore() : undefined
const buttonText = config.button?.text ?? (buttonAction ? 'Load more items' : 'Show all') |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think this is covered by the |
Beta Was this translation helpful? Give feedback.
I think this is covered by the
@stylistic/js/no-mixed-operators
rule:https://eslint.style/rules/js/no-mixed-operators