Skip to main content

No Ternary in Template Literal

Keep template literals readable by disallowing ternary expressions inside interpolation slots.

Summary

The no-ternary-in-template-literal rule reports any ConditionalExpression (ternary) found inside a template literal's ${} interpolation. It is recommended to extract the ternary into a named variable before embedding it in the template.

Why Use This Rule?

  1. Prevents dense, hard-to-scan template literals that bury logic inside interpolation.
  2. Encourages descriptive variable names that explain the branching intent.
  3. Makes debugging easier because the intermediate value is visible in a debugger.

Examples

const label = (isActive === true) ? 'enabled' : 'disabled';

Logger.info(`Status: ${label}`);

Configuration

Enable the rule in your ESLint flat config:

import { noTernaryInTemplateLiteral } from '@cbnventures/nova/rules/eslint';

export default [
{
plugins: {
'@cbnventures/nova': {
rules: {
'no-ternary-in-template-literal': noTernaryInTemplateLiteral,
},
},
},
rules: {
'@cbnventures/nova/no-ternary-in-template-literal': ['warn'],
},
},
];

This rule has no additional options.

Troubleshooting

  • Want to keep the ternary inline for very short expressions. — It is recommended to extract even short ternaries. The variable name documents the branching intent and the template stays clean.
  • Nested template literals are not flagged. — The rule only checks the direct expressions inside ${}. A ternary inside a nested template literal within an expression would be flagged separately.