跳转到内容

noUselessStringConcat (since v1.8.0)

Diagnostic Category: lint/nursery/noUselessStringConcat

Sources:

Disallow unnecessary concatenation of string or template literals.

This rule aims to flag the concatenation of 2 literals when they could be combined into a single literal. Literals can be strings or template literals. Concatenation of multiple strings is allowed when the strings are spread over multiple lines in order to prevent exceeding the maximum line width.

const a = "a" + "b";
code-block.js:1:11 lint/nursery/noUselessStringConcat  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Useless string concatenation.
  
  > 1 │ const a = "a" + "b";
             ^^^^^^^^^
    2 │ 
  
   Consider turning the expression into a single string to improve readability and runtime performance.
  
   Unsafe fix: Remove the useless concatenation
  
    1  - const·a·=·"a"·+·"b";
      1+ const·a·=·"ab";
    2 2  
  
const a = "a" + "b" + "c";
code-block.js:1:11 lint/nursery/noUselessStringConcat  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Useless string concatenation.
  
  > 1 │ const a = "a" + "b" + "c";
             ^^^^^^^^^^^^^^^
    2 │ 
  
   Consider turning the expression into a single string to improve readability and runtime performance.
  
   Unsafe fix: Remove the useless concatenation
  
    1  - const·a·=·"a"·+·"b"·+·"c";
      1+ const·a·=·"abc";
    2 2  
  
const a = (foo + "a") + ("b" + "c");
code-block.js:1:26 lint/nursery/noUselessStringConcat  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Useless string concatenation.
  
  > 1 │ const a = (foo + "a") + ("b" + "c");
                            ^^^^^^^^^
    2 │ 
  
   Consider turning the expression into a single string to improve readability and runtime performance.
  
   Unsafe fix: Remove the useless concatenation
  
    1  - const·a·=·(foo·+·"a")·+·("b"·+·"c");
      1+ const·a·=·(foo·+·"a")·+·("bc");
    2 2  
  
const a = 1 + 1;
const a = 1 * '2';
const a = 1 - 2;
const a = foo + bar;
const a = 'foo' + bar;
const a = 'foo' +
'bar'