Skip to main content

useTemplate (since v0.7.0)

This rule is recommended by Rome.

Template literals are preferred over string concatenation.

Examples

Invalid

console.log(foo + "baz");
error[js/useTemplate]: Template literals are preferred over string concatenation.
   js/useTemplate.js:1:13
  
1  console.log(foo + "baz");
               ^^^^^^^^^^^

Suggested fix: Use a TemplateLiteral.
    | @@ -1 +1 @@
0   | - console.log(foo + "baz");
  0 | + console.log(`${foo}baz`);

console.log(1 * 2 + "foo");
error[js/useTemplate]: Template literals are preferred over string concatenation.
   js/useTemplate.js:1:13
  
1  console.log(1 * 2 + "foo");
               ^^^^^^^^^^^^^

Suggested fix: Use a TemplateLiteral.
    | @@ -1 +1 @@
0   | - console.log(1 * 2 + "foo");
  0 | + console.log(`${1 * 2}foo`);

console.log(1 + "foo" + 2 + "bar" + "baz" + 3);
error[js/useTemplate]: Template literals are preferred over string concatenation.
   js/useTemplate.js:1:13
  
1  console.log(1 + "foo" + 2 + "bar" + "baz" + 3);
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Suggested fix: Use a TemplateLiteral.
    | @@ -1 +1 @@
0   | - console.log(1 + "foo" + 2 + "bar" + "baz" + 3);
  0 | + console.log(`${1}foo${2}barbaz${3}`);

console.log((1 + "foo") * 2);
error[js/useTemplate]: Template literals are preferred over string concatenation.
   js/useTemplate.js:1:14
  
1  console.log((1 + "foo") * 2);
                ^^^^^^^^^

Suggested fix: Use a TemplateLiteral.
    | @@ -1 +1 @@
0   | - console.log((1 + "foo") * 2);
  0 | + console.log((`${1}foo`) * 2);

console.log("foo" + 1);
error[js/useTemplate]: Template literals are preferred over string concatenation.
   js/useTemplate.js:1:13
  
1  console.log("foo" + 1);
               ^^^^^^^^^

Suggested fix: Use a TemplateLiteral.
    | @@ -1 +1 @@
0   | - console.log("foo" + 1);
  0 | + console.log(`foo${1}`);

Valid

console.log("foo" + "bar");
console.log(foo() + "\n");