Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-4624

Template literals should not be nested

    XMLWordPrintable

    Details

    • Message:
      Refactor this code to not use nested template literals.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way, Sonar way recommended
    • Covered Languages:
      JavaScript, TypeScript
    • Irrelevant for Languages:
      ABAP, C#, C, C++, Cobol, CSS, Flex, Go, HTML, Java, Objective-C, PHP, PL/I, PL/SQL, Python, RPG, Swift, T-SQL, VB.Net, VB6, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • TSLint-SonarTS:
      no-nested-template-literals

      Description

      Template literals (previously named "template strings") are an elegant way to build a string without using the + operator to make strings concatenation more readable.
      However, it's possible to build complex string literals by nesting together multiple template literals, and therefore lose readability and maintainability.

      In such situations, it's preferable to move the nested template into a separate statement.

      Noncompliant Code Example

      let color = "red";
      let count = 3;
      let message = `I have ${color ? `${count} ${color}` : count} apples`; // Noncompliant; nested template strings not easy to read
      

      Compliant Solution

      let color = "red";
      let count = 3;
      let apples = color ? `${count} ${color}` : count;
      let message = `I have ${apples} apples`;
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            alexandre.gigleux Alexandre Gigleux
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: